def convert_dn_to_cobra(dn): # Taken from https://gist.github.com/mtimm/7c238d1c682a684867fd from cobra.mit.naming import Dn cobra_dn = Dn.fromString(dn) parentMoOrDn = "''" dn_dict = OrderedDict() for rn in cobra_dn.rns: rn_str = str(rn) dn_dict[rn_str] = {} dn_dict[rn_str]['namingVals'] = tuple(rn.namingVals) dn_dict[rn_str]['moClassName'] = rn.meta.moClassName dn_dict[rn_str]['className'] = rn.meta.className dn_dict[rn_str]['parentMoOrDn'] = parentMoOrDn parentMoOrDn = rn.meta.moClassName code = [] dn_dict.popitem() for arn in dn_dict.items(): if len(arn[1]['namingVals']) > 0: nvals_str = ", '" + ", ".join(map(str, arn[1]['namingVals'])) + "'" else: nvals_str = "" code.append("{0} = {1}({2}{3})".format(arn[1]['moClassName'], arn[1]['className'], arn[1]['parentMoOrDn'], nvals_str)) return arn[1]['moClassName'], '\n'.join(code)
def convert_dn_to_cobra(dn): # Taken from https://gist.github.com/mtimm/7c238d1c682a684867fd from cobra.mit.naming import Dn imports = [] cobra_dn = Dn.fromString(dn) parentMoOrDn = "''" dn_dict = OrderedDict() for rn in cobra_dn.rns: rn_str = str(rn) dn_dict[rn_str] = {} dn_dict[rn_str]['namingVals'] = tuple(rn.namingVals) dn_dict[rn_str]['moClassName'] = rn.meta.moClassName dn_dict[rn_str]['className'] = rn.meta.className dn_dict[rn_str]['parentMoOrDn'] = parentMoOrDn parentMoOrDn = rn.meta.moClassName code = [] dn_dict.popitem() for arn in dn_dict.items(): if len(arn[1]['namingVals']) > 0: nvals_str = ", " + ", ".join( map(lambda x: "'{0}'".format(str(x)), arn[1]['namingVals'])) else: nvals_str = "" code.append("{0} = {1}({2}{3})".format(arn[1]['moClassName'], arn[1]['className'], arn[1]['parentMoOrDn'], nvals_str)) package = '.'.join(arn[1]['className'].split('.')[0:-1]) imports.append(package) return arn[1]['moClassName'], imports, '\n'.join(code)
def convert_dn_to_cobra(dn_str): """Convert an ACI distinguished name to ACI Python SDK (cobra) code.""" cobra_dn = Dn.fromString(dn_str) parent_mo_or_dn = "''" dn_dict = OrderedDict() for rname in cobra_dn.rns: rn_str = str(rname) dn_dict[rn_str] = {} dn_dict[rn_str]['namingVals'] = tuple(rname.namingVals) dn_dict[rn_str]['moClassName'] = rname.meta.moClassName dn_dict[rn_str]['className'] = rname.meta.className dn_dict[rn_str]['parentMoOrDn'] = parent_mo_or_dn parent_mo_or_dn = rname.meta.moClassName cobra_str = "" for arn in dn_dict.items(): if len(arn[1]['namingVals']) > 0: nvals = [str(val) for val in arn[1]['namingVals']] nvals_str = ", '" + ", ".join(nvals) + "'" else: nvals_str = "" cobra_str += " # {0} = {1}({2}{3})\n".format(arn[1]['moClassName'], arn[1]['className'], arn[1]['parentMoOrDn'], nvals_str) return cobra_str
def get_leaf_prof_dn_by_nodeid(leaf_id): cq = ClassQuery('infraNodeBlk') result = moDir.query(cq) my_results = [] for key in result: if int(key.from_) <= int(leaf_id) <= int(key.to_): child = Dn.fromString(format(key.dn)) my_results.append(format(child.getAncestor(2))) return my_results
def get_int_prof_by_leaf_prof(parm): cq = ClassQuery('infraRsAccPortP') my_result = [] result = moDir.query(cq) for key in result: # temp = format(key.dn) # this is a string child = Dn.fromString(format(key.dn)) parent = format(child.getAncestor(1)) if parent == parm: my_result.append(key.tDn) return my_result
def get_policygroup_type(parm): pg_class = Dn.fromString(parm) if format(pg_class.moClass ) == "<class 'cobra.modelimpl.infra.accbndlgrp.AccBndlGrp'>": is_bundle = True return is_bundle elif format(pg_class.moClass ) == "<class 'cobra.modelimpl.infra.accportgrp.AccPortGrp'>": is_bundle = False return is_bundle
def getAncestorDnStrFromDnString(md, input, level): fvAEPg_dnStr = str(Dn.fromString(input).getAncestor(level)) tq = TraceQuery(fvAEPg_dnStr, 'fvBD') tq.subtree = 'children' fvBD = md.query(tq)[0] bd = tDnToBdCtx(str(fvBD.dn), 'BD') ctx = tDnToBdCtx((fvBD.rsctx._childObjects[None].tDn), 'ctx') if bd and ctx: return bd, ctx else: return 'na', 'na'
def getAncestorDnStrFromDnString(md, input, level): fvAEPg_dnStr = str(Dn.fromString(input).getAncestor(level)) tq = TraceQuery(fvAEPg_dnStr, 'fvBD') tq.subtree = 'children' fvBD = md.query(tq)[0] bd=tDnToBdCtx(str(fvBD.dn),'BD') ctx=tDnToBdCtx((fvBD.rsctx._childObjects[None].tDn),'ctx') if bd and ctx: return bd,ctx else: return 'na','na'
def _parentDn(self): """Get the Dn for the parent of this Mo. Initializes the parent Dn if it wasn't initilized yet. Returns: cobra.mit.naming.Dn: The Dn of the parent of this Mo. """ if self.__parentDn is None: # This attribute is defined in BaseMo # pylint:disable=attribute-defined-outside-init self.__parentDn = Dn.fromString(self.__parentDnStr) return self.__parentDn
def getRootMo(self): """Get the Root Mo for this configuration request Returns: None or cobra.mit.mo.Mo: The root Mo for the config request """ def addDescendantMo(rMo, descendantMo): rDn = rMo.dn descendantDn = descendantMo.dn parentDn = descendantDn.getParent() while rDn != parentDn: # This is a descendant. make the parent mo parentMo = ConfigRequest.__getMoForDnInFlatTree( parentDn, flatTreeDict) parentMo._attachChild(descendantMo) descendantMo = parentMo parentDn = parentDn.getParent() rMo._attachChild(descendantMo) if self.__rootMo: return self.__rootMo if not self.__configMos: return None # This dict stores all entries added to the tree. Fast lookups flatTreeDict = {} dns = list(self.__configMos.keys()) rootDn = Dn.findCommonParent(dns) configMos = dict(self.__configMos) # Check if the the root is in the list of MOs to be configure. # If it is there, remove it, else create a new MO, but in any case # add the MO to the flat tree dictionary for further lookups. rootMo = configMos.pop(rootDn) if rootDn in configMos else None rootMo = ConfigRequest.__getMoForDnInFlatTree(rootDn, flatTreeDict, rootMo) # Add the rest of the mos to the root. childMos = sorted(list(configMos.values()), key=lambda x: x.dn) for childMo in childMos: addDescendantMo(rootMo, childMo) self.__rootMo = rootMo return rootMo
def get_pg_and_portblock_by_int_prof_from_port_id(parm1, parm2): temp = parm2.split("/") port = temp[1] req = moDir.lookupByClass("infraPortBlk", parentDn=parm1) my_result = [] for key in req: if int(key.fromPort) <= int(port) <= int(key.toPort): # temp = format(key.dn) my_result.append(format(key.dn)) child = Dn.fromString(format(key.dn)) parent = format(child.getAncestor( 1)) # here I got the port selector DN as a string # print self.parent req2 = moDir.lookupByClass( "infraRsAccBaseGrp", parentDn=parent) # here I get the Policy group DN for pg in req2: my_result.append(format(pg.tDn)) return my_result
def _parentDn(self): if self.__parentDn is None: self.__parentDn = Dn.fromString(self.__parentDnStr) return self.__parentDn