def get_full_aci_config(apic, user, password, outfile): try: url = "https://" + apic ls = cobra.mit.session.LoginSession(url, "apic:ACS\\" + user, password, secure=False, timeout=30) md = cobra.mit.access.MoDirectory(ls) md.login() #Get tenant config cq = ClassQuery('fvTenant') cq.subtree = 'full' cq.propInclude= 'config-only' tenant_config = md.query(cq) #Infra config dq = DnQuery('uni/infra') dq.subtree = 'full' dq.propInclude = 'config-only' infra_config = md.query(dq) #Fabric config dq = DnQuery('uni/fabric') dq.subtree = 'full' dq.propInclude = 'config-only' fabric_config = md.query(dq) file = open(outfile, 'w+') file.write("# " + apic + " #") file.write("\n###########" + '\n') file.write("# TENANTS ") file.write("\n###########" + '\n') for tenant in tenant_config: file.write(toJSONStr(tenant, prettyPrint=True)) file.write("\n###########" + '\n') file.write("# Infra ") file.write("\n###########" + '\n') for infra in infra_config: file.write(toJSONStr(infra, prettyPrint=True)) file.write("\n###########" + '\n') file.write("# Fabric ") file.write("\n###########" + '\n') for fabric in fabric_config: file.write(toJSONStr(fabric, prettyPrint=True)) file.close() except: print "Error in get_full_aci_config() for %s: %s" % (apic, sys.exc_info()[0])
def get_faults(self, epg_dn): class_query = DnQuery(epg_dn) class_query.subtree = 'full' class_query.subtreeInclude = 'faults' epg_list = self.moDir.query(class_query) fault_list = self.get_faults_from_tree(epg_list[0], []) return fault_list
def add_servicegraph(): apicURL = os.getenv("CliqrCloud_AciApicEndpoint") apicUser = os.getenv("CliqrCloud_AciUsername") apicPwd = os.getenv("CliqrCloud_AciPassword") apicTenant = os.getenv("CliqrCloud_AciTenantName") apicServiceGraphTemplate = os.getenv("Cloud_Setting_serviceGraphTemplate") # Handle cases where APIC URL is configured without ssl (typically, in a lab). if apicURL.startswith("https"): loginSession = LoginSession(apicURL, apicUser, apicPwd) else: loginSession = LoginSession(apicURL, apicUser, apicPwd,secure=False) # CliqrTier_CentOS_1_Cloud_Setting_AciPortGroup_2 tmpString = "CliqrTier_" + os.getenv("CliqrDependencies") + "_Cloud_Setting_AciPortGroup_2" appProfileName = os.getenv(tmpString).split("|")[1] qTenant = "tn-" + apicTenant qString = "uni/" + qTenant + "/ap-" + appProfileName dnQuery = DnQuery(qString) dnQuery.queryTarget = 'subtree' dnQuery.classFilter = 'fvRsProv' dnQuery.subtree = 'children' #dnQuery.subtreePropFilter='eq(fvRsCons.tCl,"vzBrCP")' # Obtain Session from APIC. moDir = MoDirectory(loginSession) moDir.login() # Query to obtain data from Managed Object Directory. dmo = moDir.query(dnQuery) print str(dmo[0].tDn) # Debug String. Remove from running env. logging.debug(" Contract String Obtained :" + dmo[0].tDn) # Service Graph - Query String qStringAbsG = "uni/" + qTenant + "/AbsGraph-" + apicServiceGraphTemplate graphMO = moDir.lookupByDn(qStringAbsG) # Subject Query String qStringSubj = dmo[0].tDn + "/subj-cliqr-subject" subjMO = moDir.lookupByDn(qStringSubj) # Attach Graph to Contract. RsSubjGraphAtt(subjMO, tnVnsAbsGraphName=graphMO.name) # Create Commit Object. nsCfg = ConfigRequest() nsCfg.addMo(subjMO) moDir.commit(nsCfg) contractString = dmo[0].tDn tmpArr = contractString.split("/") apicContractName = tmpArr[len(tmpArr)-1].replace("brc-","") aviApicContractArg = apicContractName + ":" + apicServiceGraphTemplate aviApicEpgName = appProfileName + ":" + os.getenv(tmpString).split("|")[2] params = {} with open('params.json', 'r') as p: params = json.loads(p.read()) params['apic_contract_graph'] = aviApicContractArg params['apic_epg_name'] = aviApicEpgName logging.debug(" Dump Params :: " + json.dumps(params)) with open('params.json', 'w') as f: json.dump(params, f)
def get_full_tenant(self,tenant_name): dnQ = DnQuery('uni/tn-{}'.format(tenant_name)) dnQ.subtree = 'full' tenant = self.mo_dir.query(dnQ) if tenant: return tenant[0] return None
def findTenantVrfContexts(tenant, apicMoDir): logging.debug('Inside findTenantVrfContexts function') tenantDn = formTenantDn(tenant) dnQuery = DnQuery(tenantDn) dnQuery.subtree = 'children' tenantMo = apicMoDir.query(dnQuery) if len(tenantMo) > 0: # We expect only 1 tenant with that name return tenantMo[0].ctx else: return []
def main(host, username, password, tenant): apic = "https://%s" % host print("Connecting to APIC : %s" % apic) moDir = MoDirectory(LoginSession(apic, username, password)) moDir.login() dn_name = "uni/tn-" + tenant print(dn_name) dnq = DnQuery(dn_name) dnq.subtree = 'children' tenantMO = moDir.query(dnq) for bdMO in tenantMO.BD: print("BD NAME => {", bdMO.name, "}")
tenant, ap)) new_epg = AEPg(application_profile, "vmepg") # Committing the changes config_request = ConfigRequest() config_request.addMo(new_epg) moDir.commit(config_request) # Enter epg to use as template for vmepg epg_template = "default" dnQuery = DnQuery('uni/tn-{0}/ap-{1}/epg-{2}'.format( tenant, ap, epg_template)) dnQuery.subtree = 'children' epgMO = moDir.query(dnQuery) # Traversing every property within epg_template and copying it to vmepg for epg in epgMO: for epgChild in epg.children: for name, obj in inspect.getmembers(sys.modules[__name__]): if inspect.isclass(obj): copy_of_property = str(epgChild.rn) if (copy_of_property.lower().startswith( obj.__name__.lower())): exec("object_made = " + obj.__name__ + "(epg, \"" + copy_of_property + "\")") config_request.addMo(object_made) moDir.commit(config_request)
# Making a new epg and calling it "vmepg" application_profile = moDir.lookupByDn("uni/tn-{0}/ap-{1}".format(tenant, ap)) new_epg = AEPg(application_profile, "vmepg") # Committing the changes config_request = ConfigRequest() config_request.addMo(new_epg) moDir.commit(config_request) # Enter epg to use as template for vmepg epg_template= "default" dnQuery = DnQuery('uni/tn-{0}/ap-{1}/epg-{2}'.format(tenant, ap, epg_template)) dnQuery.subtree = 'children' epgMO = moDir.query(dnQuery) # Traversing every property within epg_template and copying it to vmepg for epg in epgMO: for epgChild in epg.children: for name, obj in inspect.getmembers(sys.modules[__name__]): if inspect.isclass(obj): copy_of_property = str(epgChild.rn) if (copy_of_property.lower().startswith(obj.__name__.lower())): exec("object_made = " + obj.__name__ + "(epg, \"" + copy_of_property + "\")") config_request.addMo(object_made) moDir.commit(config_request)