### requires.py
from charms.reactive import hook
from charms.reactive import RelationBase
from charms.reactive import scopes
class HttpRequires(RelationBase):
scope = scopes.UNIT
@hook('{requires:http}-relation-{joined,changed}')
def changed(self):
conv = self.conversation()
if conv.get_remote('port'):
# this unit's conversation has a port, so
# it is part of the set of available units
conv.set_state('{relation_name}.available')
if conv.get_remote('httpsport'):
# this unit's conversation has a port, so
# it is part of the set of available units
conv.set_state('{relation_name}.available')
@hook('{requires:http}-relation-{departed,broken}')
def broken(self):
conv = self.conversation()
conv.remove_state('{relation_name}.available')
def services(self):
services = {}
for conv in self.conversations():
service_name = conv.scope.split('/')[0]
service = services.setdefault(service_name, {
'service_name': service_name,
'hosts': [],
})
host = conv.get_remote('hostname') or \
conv.get_remote('private-address')
port = conv.get_remote('port')
httpsport = conv.get_remote('httpsport')
if host and port:
service['hosts'].append({
'hostname': host,
'port': port,
'httpsport': httpsport,
})
return [s for s in services.values() if s['hosts']]