我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用charmhelpers.core.hookenv.relation_get()。
def test_relation_clear(self, local_unit, relation_get, relation_set): local_unit.return_value = 'local-unit' relation_get.return_value = { 'private-address': '10.5.0.1', 'foo': 'bar', 'public-address': '146.192.45.6' } hookenv.relation_clear('relation:1') relation_get.assert_called_with(rid='relation:1', unit='local-unit') relation_set.assert_called_with( relation_id='relation:1', **{'private-address': '10.5.0.1', 'foo': None, 'public-address': '146.192.45.6'})
def test_ingress_address(self, relation_get): """Ensure ingress_address returns the ingress-address when available and returns the private-address when not. """ _with_ingress = {'egress-subnets': '10.5.0.23/32', 'ingress-address': '10.5.0.23', 'private-address': '172.16.5.10'} _without_ingress = {'private-address': '172.16.5.10'} # Return the ingress-address relation_get.return_value = _with_ingress self.assertEqual(hookenv.ingress_address(rid='test:1', unit='unit/1'), '10.5.0.23') relation_get.assert_called_with(rid='test:1', unit='unit/1') # Return the private-address relation_get.return_value = _without_ingress self.assertEqual(hookenv.ingress_address(rid='test:1'), '172.16.5.10')
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)