def root(self): mos_and_types = utils.decompose_dn(self._aci_mo_name, self.dn) mo = apic_client.ManagedObjectClass(mos_and_types[0][0]) if mo.rn_param_count > 0: return mo.rn(mos_and_types[0][1]) else: return mo.rn()
def root(self): mos_and_types = utils.decompose_dn(self._aci_mo_name, self.dn) if mos_and_types: # Faults associated with unrecognized MOs will not decompose mo = apic_client.ManagedObjectClass(mos_and_types[0][0]) return (mo.rn(mos_and_types[0][1]) if mo.rn_param_count else mo.rn())
def fv_rs_master_epg_converter(object_dict, otype, helper, source_identity_attributes, destination_identity_attributes, to_aim=True): result = [] if to_aim: res_dict = {} try: id = default_identity_converter(object_dict, otype, helper, to_aim=True) except apic_client.DNManager.InvalidNameFormat: return [] for index, attr in enumerate(destination_identity_attributes): res_dict[attr] = id[index] if object_dict.get('tDn'): master_id = apic_client.DNManager().aci_decompose_with_type( object_dict['tDn'], 'fvAEPg') res_dict['epg_contract_masters'] = [{ 'app_profile_name': master_id[1][1], 'name': master_id[2][1] }] result.append(default_to_resource(res_dict, helper, to_aim=True)) else: for p in object_dict['epg_contract_masters']: if p.get('app_profile_name') and p.get('name'): try: attr = [ object_dict.get('tenant_name'), p.get('app_profile_name'), p.get('name') ] path = apic_client.ManagedObjectClass('fvAEPg').dn(*attr) except Exception as e: LOG.error('Failed to make DN for %s with %s: %s', helper['resource'], attr, e) raise dn = default_identity_converter(object_dict, otype, helper, extra_attributes=[path], aci_mo_type=helper['resource'], to_aim=False)[0] result.append({ helper['resource']: { 'attributes': { 'dn': dn, 'tDn': path } } }) return result
def _mock_container_responses_for_create(self, obj): # Recursively generate responses for creating obj's containers. if obj: mo = apic.ManagedObjectClass(obj) if mo.can_create: if mo.container: self._mock_container_responses_for_create(mo.container) name = '-'.join([obj, 'name']) # useful for debugging self._stage_mocked_response('post', OK, obj, debug_name=name)
def get_children_mos(apic_session, root): root_type = 'uni' try: root_type = apic_client.DNManager().get_rn_base(root) except KeyError: pass global CHILDREN_MOS_UNI global CHILDREN_MOS_TOPOLOGY if root_type in ['uni']: if CHILDREN_MOS_UNI is None: CHILDREN_MOS_UNI = set() for mo in CHILDREN_LIST: if mo in apic_client.ManagedObjectClass.supported_mos: mo_name = apic_client.ManagedObjectClass(mo).klass_name else: mo_name = mo try: # Verify class support apic_session.GET('/mo/uni/tn-common.json?' 'target-subtree-class=%s' % mo_name) except apic_exc.ApicResponseNotOk as e: if int(e.err_status) == 400 and int(e.err_code) == 12: continue raise e CHILDREN_MOS_UNI.add(mo_name) return CHILDREN_MOS_UNI elif root_type in ['topology']: if CHILDREN_MOS_TOPOLOGY is None: CHILDREN_MOS_TOPOLOGY = set() for mo in TOPOLOGY_CHILDREN_LIST: if mo in apic_client.ManagedObjectClass.supported_mos: mo_name = apic_client.ManagedObjectClass(mo).klass_name else: mo_name = mo try: apic_session.GET('/node/class/%s.json?' % mo_name) except apic_exc.ApicResponseNotOk as e: if int(e.err_status) == 400 and int(e.err_code) == 12: continue raise e CHILDREN_MOS_TOPOLOGY.add(mo_name) return CHILDREN_MOS_TOPOLOGY
def _tdn_rs_converter(object_dict, otype, helper, source_identity_attributes, destination_identity_attributes, to_aim=True): result = [] if to_aim: # Converting a fvRsDomAtt into an EPG res_dict = {} try: id = default_identity_converter(object_dict, otype, helper, to_aim=True) except apic_client.DNManager.InvalidNameFormat: return [] for index, attr in enumerate(destination_identity_attributes): res_dict[attr] = id[index] tdn = object_dict.get('tDn') if tdn: dnm = apic_client.DNManager() mos_and_rns = dnm.aci_decompose_with_type(tdn, aci_mo) rns = dnm.filter_rns(mos_and_rns) res_dict.update(dict(zip(aim_attr_list, rns))) to_res = helper.get('to_resource', default_to_resource) result.append(to_res(res_dict, helper, to_aim=True)) else: dn = default_identity_converter(object_dict, otype, helper, to_aim=False)[0] dn_attrs = [ object_dict[a] for a in aim_attr_list if object_dict.get(a) ] if len(dn_attrs) == len(aim_attr_list): tdn = apic_client.ManagedObjectClass(aci_mo).dn(*dn_attrs) result.append({ helper['resource']: { 'attributes': { 'dn': dn, 'tDn': tdn } } }) return result
def func(object_dict, attribute, to_aim=True): if to_aim: dn = object_dict.get(attribute) if dn: dnm = apic_client.DNManager() mos_and_rns = dnm.aci_decompose_with_type(dn, aci_mo) rns = dnm.filter_rns(mos_and_rns) return dict(zip(aim_attr_list, rns)) else: return {} else: dn_attrs = [ object_dict[a] for a in aim_attr_list if object_dict.get(a) ] if len(dn_attrs) == len(aim_attr_list): dn = apic_client.ManagedObjectClass(aci_mo).dn(*dn_attrs) else: dn = '' return dn
def default_identity_converter(object_dict, otype, helper, extra_attributes=None, aci_mo_type=None, to_aim=True): """Default identity converter This converter uses the DN and splits it in its fundamental parts to retrieve the identity names. :param object_dict: dictionarty of the resource to be converted :param otype: Type of the object. Can be an AIM resource class or a APIC class name. :param extra_attributes: Ordered list of additional attribute values needed to create the identity attribute :param aci_mo_type: ACI ManagedObjectType to use when creating ACI identity attribute :param to_aim: Boolean indicating whether we are converting ACI/AIM (True) or AIM/ACI (False) :return: list with exactly all the attributes that need to be assigned to the resource class 'identity_attributes' """ if to_aim: dn_mgr = apic_client.DNManager() aci_type = aci_mo_type or otype mos_and_rns = dn_mgr.aci_decompose_with_type(object_dict['dn'], aci_type) return dn_mgr.filter_rns(mos_and_rns) else: attr = [object_dict[x] for x in otype.identity_attributes] if extra_attributes: attr.extend(extra_attributes) mo_type = aci_mo_type or helper['resource'] try: return [apic_client.ManagedObjectClass(mo_type).dn(*attr)] except Exception as e: LOG.error('Failed to make DN for %s with %s: %s', mo_type, attr, e) raise
def _dn(mo_type_name, *dn_attrs): mo = apic_client.ManagedObjectClass(mo_type_name) return mo.dn(*dn_attrs)
def rn(self): mo = apic_client.ManagedObjectClass(self._aci_mo_name) if mo.rn_param_count > 0: return mo.rn(*self.identity[-mo.rn_param_count:]) else: return mo.rn()
def dn(self): return apic_client.ManagedObjectClass(self._aci_mo_name).dn( *self.identity)
def get_top_container(self, mo): while mo.container: mo = apic.ManagedObjectClass(mo.container) return mo
def mock_responses_for_create(self, obj): self._mock_container_responses_for_create( apic.ManagedObjectClass(obj).container) name = '-'.join([obj, 'name']) # useful for debugging self._stage_mocked_response('post', OK, obj, name=name)