當前位置: 首頁>>代碼示例>>Python>>正文


Python hookenv.related_units方法代碼示例

本文整理匯總了Python中charmhelpers.core.hookenv.related_units方法的典型用法代碼示例。如果您正苦於以下問題:Python hookenv.related_units方法的具體用法?Python hookenv.related_units怎麽用?Python hookenv.related_units使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在charmhelpers.core.hookenv的用法示例。


在下文中一共展示了hookenv.related_units方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: single_mode_map

# 需要導入模塊: from charmhelpers.core import hookenv [as 別名]
# 或者: from charmhelpers.core.hookenv import related_units [as 別名]
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 
開發者ID:openstack,項目名稱:charms.openstack,代碼行數:27,代碼來源:adapters.py

示例2: test_gets_relations_for_id

# 需要導入模塊: from charmhelpers.core import hookenv [as 別名]
# 或者: from charmhelpers.core.hookenv import related_units [as 別名]
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),
        ]) 
開發者ID:juju,項目名稱:charm-helpers,代碼行數:25,代碼來源:test_hookenv.py

示例3: get_data

# 需要導入模塊: from charmhelpers.core import hookenv [as 別名]
# 或者: from charmhelpers.core.hookenv import related_units [as 別名]
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) 
開發者ID:openstack,項目名稱:charm-plumgrid-gateway,代碼行數:38,代碼來源:helpers.py

示例4: provide_data

# 需要導入模塊: from charmhelpers.core import hookenv [as 別名]
# 或者: from charmhelpers.core.hookenv import related_units [as 別名]
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) 
開發者ID:openstack,項目名稱:charm-plumgrid-gateway,代碼行數:39,代碼來源:base.py


注:本文中的charmhelpers.core.hookenv.related_units方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。