Initial openvim v0.4.6 upload
[osm/openvim.git] / charm / openvim / interface-openvim / requires.py
1 from charms.reactive import hook
2 from charms.reactive import RelationBase
3 from charms.reactive import scopes
4
5
6 class OpenVimRequires(RelationBase):
7 scope = scopes.UNIT
8
9 @hook('{requires:openvim}-relation-{joined,changed}')
10 def changed(self):
11 conv = self.conversation()
12 if conv.get_remote('port'):
13 # this unit's conversation has a port, so
14 # it is part of the set of available units
15 conv.set_state('{relation_name}.available')
16
17 @hook('{requires:openvim}-relation-{departed,broken}')
18 def broken(self):
19 conv = self.conversation()
20 conv.remove_state('{relation_name}.available')
21
22 def services(self):
23 """
24 Returns a list of available openvim services and their associated hosts
25 and ports.
26
27 The return value is a list of dicts of the following form::
28
29 [
30 {
31 'service_name': name_of_service,
32 'hosts': [
33 {
34 'hostname': address_of_host,
35 'port': port_for_host,
36 'user': user_for_host,
37 },
38 # ...
39 ],
40 },
41 # ...
42 ]
43 """
44 services = {}
45 for conv in self.conversations():
46 service_name = conv.scope.split('/')[0]
47 service = services.setdefault(service_name, {
48 'service_name': service_name,
49 'hosts': [],
50 })
51 host = conv.get_remote('hostname') or \
52 conv.get_remote('private-address')
53 port = conv.get_remote('port')
54 user = conv.get_remote('user')
55 if host and port:
56 service['hosts'].append({
57 'hostname': host,
58 'port': port,
59 'user': user,
60 })
61 return [s for s in services.values() if s['hosts']]