def __init__(self): super(Relations, self).__init__() for relname in sorted(hookenv.relation_types()): self[relname] = OrderedDict() relids = hookenv.relation_ids(relname) relids.sort(key=lambda x: int(x.split(':', 1)[-1])) for relid in relids: self[relname][relid] = Relation(relid)
def test_lists_relation_types(self): open_ = mock_open() open_.return_value = io.BytesIO(CHARM_METADATA) with patch('charmhelpers.core.hookenv.open', open_, create=True): with patch.dict('os.environ', {'CHARM_DIR': '/var/empty'}): reltypes = set(hookenv.relation_types()) open_.assert_called_once_with('/var/empty/metadata.yaml') self.assertEqual(set(('testreqs', 'testprov', 'testpeer')), reltypes)
def endpoint_from_flag(flag): """The object used for interacting with relations tied to a flag, or None. """ relation_name = None value = _get_flag_value(flag) if isinstance(value, dict) and 'relation' in value: # old-style RelationBase relation_name = value['relation'] elif flag.startswith('endpoint.'): # new-style Endpoint relation_name = flag.split('.')[1] elif '.' in flag: # might be an unprefixed new-style Endpoint relation_name = flag.split('.')[0] if relation_name not in hookenv.relation_types(): return None if relation_name: factory = relation_factory(relation_name) if factory: return factory.from_flag(flag) return None
def _startup(cls): """ Create Endpoint instances and manage automatic flags. """ for endpoint_name in sorted(hookenv.relation_types()): # populate context based on attached relations relf = relation_factory(endpoint_name) if not relf or not issubclass(relf, cls): continue rids = sorted(hookenv.relation_ids(endpoint_name)) # ensure that relation IDs have the endpoint name prefix, in case # juju decides to drop it at some point rids = ['{}:{}'.format(endpoint_name, rid) if ':' not in rid else rid for rid in rids] endpoint = relf(endpoint_name, rids) cls._endpoints[endpoint_name] = endpoint endpoint._manage_departed() endpoint._manage_flags() for relation in endpoint.relations: hookenv.atexit(relation._flush_data)