我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用charmhelpers.core.hookenv.relation_ids()。
def single_mode_map(self): """Return map of local addresses only if this is a single node cluster @return dict of local address info e.g. {'cluster_hosts': {'this_unit_private_addr': { 'backends': { 'this_unit-1': 'this_unit_private_addr'}, 'network': 'this_unit_private_addr/private_netmask'}, 'internal_addresses': ['intaddr']} """ relation_info = {} try: cluster_relid = hookenv.relation_ids('cluster')[0] if not hookenv.related_units(relid=cluster_relid): relation_info = { 'cluster_hosts': self.local_default_addresses(), 'internal_addresses': self.internal_addresses, } net_split = self.local_network_split_addresses() for key in net_split.keys(): relation_info['cluster_hosts'][key] = net_split[key] except IndexError: pass return relation_info
def test_gets_relations_for_id(self, relation_for_unit, related_units, relation_ids): relid = 123 units = ['foo', 'bar'] unit_data = [ {'foo-item': 'bar-item'}, {'foo-item2': 'bar-item2'}, ] relation_ids.return_value = relid related_units.return_value = units relation_for_unit.side_effect = unit_data result = hookenv.relations_for_id() self.assertEqual(result[0]['__relid__'], relid) self.assertEqual(result[0]['foo-item'], 'bar-item') self.assertEqual(result[1]['__relid__'], relid) self.assertEqual(result[1]['foo-item2'], 'bar-item2') related_units.assert_called_with(relid) self.assertEqual(relation_for_unit.mock_calls, [ call('foo', relid), call('bar', relid), ])
def test_gets_the_relation_id(self, os_, relation_ids, remote_service_name): os_.environ = { 'JUJU_RELATION_ID': 'foo', } self.assertEqual(hookenv.relation_id(), 'foo') relation_ids.return_value = ['r:1', 'r:2'] remote_service_name.side_effect = ['other', 'service'] self.assertEqual(hookenv.relation_id('rel', 'service/0'), 'r:2') relation_ids.assert_called_once_with('rel') self.assertEqual(remote_service_name.call_args_list, [ call('r:1'), call('r:2'), ]) remote_service_name.side_effect = ['other', 'service'] self.assertEqual(hookenv.relation_id('rel', 'service'), 'r:2')
def get_data(self): """ Retrieve the relation data for each unit involved in a relation and, if complete, store it in a list under `self[self.name]`. This is automatically called when the RelationContext is instantiated. The units are sorted lexographically first by the service ID, then by the unit ID. Thus, if an interface has two other services, 'db:1' and 'db:2', with 'db:1' having two units, 'wordpress/0' and 'wordpress/1', and 'db:2' having one unit, 'mediawiki/0', all of which have a complete set of data, the relation data for the units will be stored in the order: 'wordpress/0', 'wordpress/1', 'mediawiki/0'. If you only care about a single unit on the relation, you can just access it as `{{ interface[0]['key'] }}`. However, if you can at all support multiple units on a relation, you should iterate over the list, like:: {% for unit in interface -%} {{ unit['key'] }}{% if not loop.last %},{% endif %} {%- endfor %} Note that since all sets of relation data from all related services and units are in a single list, if you need to know which service or unit a set of data came from, you'll need to extend this class to preserve that information. """ if not hookenv.relation_ids(self.name): return ns = self.setdefault(self.name, []) for rid in sorted(hookenv.relation_ids(self.name)): for unit in sorted(hookenv.related_units(rid)): reldata = hookenv.relation_get(rid=rid, unit=unit) if self._is_ready(reldata): ns.append(reldata)
def provide_data(self): """ Set the relation data for each provider in the ``provided_data`` list. A provider must have a `name` attribute, which indicates which relation to set data on, and a `provide_data()` method, which returns a dict of data to set. The `provide_data()` method can optionally accept two parameters: * ``remote_service`` The name of the remote service that the data will be provided to. The `provide_data()` method will be called once for each connected service (not unit). This allows the method to tailor its data to the given service. * ``service_ready`` Whether or not the service definition had all of its requirements met, and thus the ``data_ready`` callbacks run. Note that the ``provided_data`` methods are now called **after** the ``data_ready`` callbacks are run. This gives the ``data_ready`` callbacks a chance to generate any data necessary for the providing to the remote services. """ for service_name, service in self.services.items(): service_ready = self.is_ready(service_name) for provider in service.get('provided_data', []): for relid in hookenv.relation_ids(provider.name): units = hookenv.related_units(relid) if not units: continue remote_service = units[0].split('/')[0] argspec = getargspec(provider.provide_data) if len(argspec.args) > 1: data = provider.provide_data(remote_service, service_ready) else: data = provider.provide_data() if data: hookenv.relation_set(relid, data)