3 # This file is part of openvim
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
19 from charms
.reactive
import hook
20 from charms
.reactive
import RelationBase
21 from charms
.reactive
import scopes
24 class OpenVimRequires(RelationBase
):
27 @hook('{requires:openvim}-relation-{joined,changed}')
29 conv
= self
.conversation()
30 if conv
.get_remote('port'):
31 # this unit's conversation has a port, so
32 # it is part of the set of available units
33 conv
.set_state('{relation_name}.available')
35 @hook('{requires:openvim}-relation-{departed,broken}')
37 conv
= self
.conversation()
38 conv
.remove_state('{relation_name}.available')
42 Returns a list of available openvim services and their associated hosts
45 The return value is a list of dicts of the following form::
49 'service_name': name_of_service,
52 'hostname': address_of_host,
53 'port': port_for_host,
54 'user': user_for_host,
63 for conv
in self
.conversations():
64 service_name
= conv
.scope
.split('/')[0]
65 service
= services
.setdefault(service_name
, {
66 'service_name': service_name
,
69 host
= conv
.get_remote('hostname') or \
70 conv
.get_remote('private-address')
71 port
= conv
.get_remote('port')
72 user
= conv
.get_remote('user')
74 service
['hosts'].append({
79 return [s
for s
in services
.values() if s
['hosts']]