def relation_factory(relation_name): """Get the RelationFactory for the given relation name. Looks for a RelationFactory in the first file matching: ``$CHARM_DIR/hooks/relations/{interface}/{provides,requires,peer}.py`` """ role, interface = hookenv.relation_to_role_and_interface(relation_name) if not (role and interface): hookenv.log('Unable to determine role and interface for relation ' '{}'.format(relation_name), hookenv.ERROR) return None return _find_relation_factory(_relation_module(role, interface))
def from_name(cls, relation_name, conversations=None): """ Find relation implementation in the current charm, based on the name of the relation. :return: A Relation instance, or None """ if relation_name is None: return None relation_class = cls._cache.get(relation_name) if relation_class: return relation_class(relation_name, conversations) role, interface = hookenv.relation_to_role_and_interface(relation_name) if role and interface: relation_class = cls._find_impl(role, interface) if relation_class: cls._cache[relation_name] = relation_class return relation_class(relation_name, conversations) return None
def from_name(cls, relation_name, conversations=None): """ Find relation implementation in the current charm, based on the name of the relation. :return: A Relation instance, or None """ if relation_name is None: return None relation_class = cls._cache.get(relation_name) if relation_class: return relation_class(relation_name, conversations) role, interface = hookenv.relation_to_role_and_interface(relation_name) if role and interface: relation_class = cls._find_impl(role, interface) if relation_class: cls._cache[relation_name] = relation_class return relation_class(relation_name, conversations) return None
#!/usr/bin/env python3 import json import os import sys from subprocess import check_call sys.path.append('lib') from charmhelpers.core import hookenv, unitdata if __name__ == '__main__': relname = hookenv.relation_type() role, _ = hookenv.relation_to_role_and_interface(relname) local_data = hookenv.relation_get() env = {} env.update(os.environ) env['ETCDCTL_ENDPOINT'] = hookenv.config().get('etcd') check_call([ 'etcdctl', 'set', '/{{ relay_name }}/{{ counterpart }}', json.dumps(local_data) ], env=env) kv = unitdata.kv() kv.set('relay.local.relation.name', relname) kv.set('relay.local.relation.role', role) kv.set('relay.remote.relation.role', '{{ counterpart }}') kv.flush(save=True)
#!/usr/bin/env python3 import json import os import sys from subprocess import check_call sys.path.append('lib') from charmhelpers.core import hookenv, unitdata if __name__ == '__main__': relname = hookenv.relation_type() role, _ = hookenv.relation_to_role_and_interface(relname) local_data = hookenv.relation_get() env = {} env.update(os.environ) env['ETCDCTL_ENDPOINT'] = hookenv.config().get('etcd') check_call(['etcdctl', 'set', '/{{ relay_name }}/{{ counterpart }}', json.dumps(local_data)], env=env) kv = unitdata.kv() kv.set('relay.local.relation.name', relname) kv.set('relay.local.relation.role', role) kv.set('relay.remote.relation.role', '{{ counterpart }}') kv.flush(save=True) # Invoke update-status immediately to trigger polling etcd os.execl(os.path.join(hookenv.charm_dir(), 'hooks/update-status'), 'update-status')