def test_ConfigRequest_addMo_raises_not_allowed_context(self): fvTenant = Tenant('uni', 'testing') fvnsVlanInstP = VlanInstP('uni/infra', 'namespace1', 'dynamic') cr = ConfigRequest() cr.addMo(fvTenant) with pytest.raises(ValueError): cr.addMo(fvnsVlanInstP)
def createTenant(md, tn, desc): uniMo = md.lookupByDn('uni') fvTenantMo = Tenant(uniMo, name=tn, descr=desc) cfgRequest = ConfigRequest() cfgRequest.addMo(fvTenantMo) md.commit(cfgRequest)
def config_tenant(moDir, name): # Build Configuration Object uniMo = moDir.lookupByDn('uni') new_mo = Tenant(uniMo, name=name) config_obj(moDir, new_mo)
def create_tenant(logon_session, tenant_name="Example_TN", description="Description"): from cobra.internal.codec.jsoncodec import toJSONStr from cobra.model.fv import Tenant, RsTenantMonPol from cobra.mit.request import ConfigRequest import cobra.model.pol import cobra.model.vns c = ConfigRequest() #apic_logon(apic_url, user, password) polUni = cobra.model.pol.Uni('') fvTenant = Tenant(polUni, ownerKey=u'', name=tenant_name, descr=description, nameAlias=tenant_name, ownerTag=u'', annotation=u'') vnsSvcCont = cobra.model.vns.SvcCont(fvTenant, annotation=u'') fvRsTenantMonPol = RsTenantMonPol(fvTenant, annotation=u'', tnMonEPGPolName=u'') tenant_data = toJSONStr(polUni) c.addMo(polUni) logon_session.commit(c) return tenant_data
def test_ConfigRequest_requestargs(self): expected1 = { 'data': '<?xml version="1.0" encoding="UTF-8"?>\n' + '<fvTenant name=\'testing\' ' + 'status=\'created,modified\'></fvTenant>', 'headers': { 'Cookie': 'APIC-cookie=None' }, 'timeout': 90, 'verify': False } expected2 = { 'data': '<?xml version="1.0" encoding="UTF-8"?>\n' + '<fvTenant status=\'created,modified\' ' + 'name=\'testing\'></fvTenant>', 'headers': { 'Cookie': 'APIC-cookie=None' }, 'timeout': 90, 'verify': False } polUni = Uni('') fvTenant = Tenant(polUni, 'testing') session = LoginSession('http://1.1.1.1', 'admin', 'password') cr = ConfigRequest() cr.addMo(fvTenant) assert (cr.requestargs(session) == expected1 or cr.requestargs(session) == expected2)
def create_contracts(modir, tenant_name): policy_universe = modir.lookupByDn('uni') fv_tenant = Tenant(policy_universe, tenant_name) # create Contract for web vz_ct_web = BrCP(fv_tenant, CONTRACT_WEB_CT) vz_subj_web = Subj(vz_ct_web, 'Web') vz_rs_subj_filt_att_web = RsSubjFiltAtt(vz_subj_web, 'http') #create contract for App vz_ct_app = BrCP(fv_tenant, CONTRACT_APP_CT) vz_subj_rmi = Subj(vz_ct_app, 'RMI') vz_rs_subj_filt_att_rmi = RsSubjFiltAtt(vz_subj_rmi, 'rmi') # create filter for sql vz_ct_db = BrCP(fv_tenant, CONTRACT_DB_CT) vz_subj_db = Subj(vz_ct_db, 'DbCt') vz_rs_subj_filt_att_db = RsSubjFiltAtt(vz_subj_db, 'sql') # print the query in XML format print toXMLStr(policy_universe, prettyPrint=True) # Commit the change using a ConfigRequest object configReq = ConfigRequest() configReq.addMo(policy_universe) modir.commit(configReq)
def create_bridge_domains(delete=''): bd_df = pd.read_excel("input-data/ACI_DD_Workbook.xlsx", sheet_name='Bridge_Domains') file = open("BD_Configuration.log", "w") logon = apic_logon() uniMo = logon.lookupByDn('uni') for index, row in bd_df.iterrows(): fvTenant = Tenant(uniMo, row['Tenant']) if delete == 'yes': fvBD = BD(fvTenant, name=row['Name'], status='deleted') else: fvBD = BD(fvTenant, name=row['Name'], arpFlood=row['ARP Flood'], ipLearning=row['EP_learn'], description=row['Description'], multiDstPktAct=row['MultiDest_Flood'], mcastAllow=row['mcastAllow'], unkMcastAct=row['L3Unk_Mcast'], limitIpLearnToSubnets=row['Limit_IP_Learn']) fvRsCtx = RsCtx(fvBD, tnFvCtxName=row['VRF']) if pd.isnull(row['L3O']) == False: fvRsBDToOut = RsBDToOut(fvBD, tnL3extOutName=row['L3O']) cfgRequest = ConfigRequest() cfgRequest.addMo(fvBD) logon.commit(cfgRequest) json_data = toJSONStr(fvBD, prettyPrint=True) file.write( '\n-------------------------------------------------------------------\n' ) file.write(json_data) file.close()
def create_apn(logon_session, tenant_name="Example_TN", description="Description", ap_name=''): from cobra.internal.codec.jsoncodec import toJSONStr from cobra.model.fv import Tenant, RsTenantMonPol, Ap from cobra.mit.request import ConfigRequest import cobra.model.pol import cobra.model.vns c = ConfigRequest() #apic_logon(apic_url, user, password) polUni = cobra.model.pol.Uni('') fvTenant = Tenant(polUni, tenant_name) # build the request using cobra syntax fvAp = Ap(fvTenant, ownerKey=u'', name=ap_name, descr=description, nameAlias=u'', ownerTag=u'', prio=u'unspecified', annotation=u'') apn_data = toJSONStr(polUni) c.addMo(polUni) logon_session.commit(c) return apn_data
def test_post_tn(self, apics, certobject, userobject): apic = apics[0] secure = False if apics[1] == 'False' else True userobject.pkey = certobject.readFile( fileName=certobject.pkeyfile) session = CertSession(apic, userobject.certDn, userobject.pkey, secure=secure, requestFormat='xml') moDir = MoDirectory(session) uni = Uni('') fvTenant = Tenant(uni, name='t') fvBD = BD(fvTenant, 't-bd') fvAp = Ap(fvTenant, 't-app') cr = ConfigRequest() #cr.subtree = 'full' cr.addMo(fvTenant) if userobject.user == 'rouser': with pytest.raises(RestError) as excinfo: r = moDir.commit(cr) assert excinfo.value.reason == ('user rouser does not have ' + 'domain access to config Mo, ' + 'class fvTenant') elif userobject.user == 'rwuser': r = moDir.commit(cr) else: raise NotImplementedError
def create_vrfs(delete=''): vrf_df = pd.read_excel("input-data/ACI_DD_Workbook.xlsx", sheet_name='VRFs') file = open("VRF_Configuration.log", "w") logon = apic_logon() uniMo = logon.lookupByDn('uni') for index, row in vrf_df.iterrows(): fvTenant = Tenant(uniMo, row['Tenant']) if delete == 'yes': fvCtx = Ctx(fvTenant, name=row['Name'], status='deleted') else: fvCtx = Ctx(fvTenant, name=row['Name'], pcEnfDir=row['Enforcement Direction'], pcEndPref=row['Enforcement'], description=row['Description']) cfgRequest = ConfigRequest() cfgRequest.addMo(fvCtx) logon.commit(cfgRequest) json_data = toJSONStr(fvCtx, prettyPrint=True) file.write( '\n-------------------------------------------------------------------\n' ) file.write(json_data) file.close()
def test_ConfigRequest_removeMo_no_configMos_left(self): fvTenant = Tenant('uni', 'testing') fvnsVlanInstP = VlanInstP('uni/infra', 'namespace1', 'dynamic') cr = ConfigRequest() cr.addMo(fvTenant) cr.removeMo(fvTenant) assert not cr.hasMo(fvTenant.dn)
def test_ConfigRequest_data(self): expected = ('{"fvTenant": {"attributes": {"name": "test", "status": ' + '"created,modified"}}}') polUni = Uni('') fvTenant = Tenant(polUni, 'test') cr = ConfigRequest() cr.addMo(fvTenant) assert cr.data == expected
def create_tenant(self, tenantStr): """Create a tenant""" # Get the top level policy universe directory # uniMo = self.modir.lookupByDn('uni') # create the tenant object return Tenant(self.mo, tenantStr)
def test_ConfigRequest_removeMo_and_hasMo_positive(self): fvTenant = Tenant('uni', 'testing') fvnsVlanInstP = VlanInstP('uni/infra', 'namespace1', 'dynamic') cr = ConfigRequest() cr.addMo(fvTenant) cr.removeMo(fvTenant) cr.addMo(fvnsVlanInstP) assert cr.hasMo(fvnsVlanInstP.dn)
def create_tenant(self, tenant_name): """ Creates a tenant and commit changes to controller :param tenant_name: :return: """ fv_tenant_mo = Tenant(self.uniMo, tenant_name) self.commit(fv_tenant_mo) return fv_tenant_mo
def create_tenant(modir, tenant_name): """Create a tenant""" policy_universe = modir.lookupByDn('uni') fvTenant = Tenant(policy_universe, tenant_name) # print the query in XML format print_query_xml(policy_universe) # Commit the change using a ConfigRequest object commit_change(modir, policy_universe)
def get_or_create_tenant(self, tenant_name): tenant_mo = Tenant(self.uni_mo(), tenant_name) if not self.mo_exists(tenant_mo.dn): LOG.info("Configured tenant {} is missing, creating it now".format(tenant_mo.dn)) self.commit(tenant_mo) else: LOG.info("Using existing ACI tenant {}".format(tenant_mo.dn)) return tenant_mo
def create_tenant(modir, tenant_name): policy_universe = modir.lookupByDn('uni') fvTenant = Tenant(policy_universe, tenant_name) print toXMLStr(policy_universe, prettyPrint=True) configReq = ConfigRequest() configReq.addMo(policy_universe) modir.commit(configReq)
def create_bd(logon_session, tenant_name="Example_TN", description="Description", bd_name='', vrf_name=''): from cobra.internal.codec.jsoncodec import toJSONStr from cobra.model.fv import Tenant, RsTenantMonPol, BD, RsIgmpsn, RsCtx from cobra.model.fv import RsBdToEpRet, RsBDToNdP from cobra.mit.request import ConfigRequest import cobra.model.pol import cobra.model.vns c = ConfigRequest() #apic_logon(apic_url, user, password) polUni = cobra.model.pol.Uni('') fvTenant = Tenant(polUni, tenant_name) fvBD = BD(fvTenant, multiDstPktAct=u'bd-flood', mcastAllow=u'no', limitIpLearnToSubnets=u'yes', unicastRoute=u'no', unkMcastAct=u'flood', descr=u'', llAddr=u'::', nameAlias=u'', type=u'regular', ipLearning=u'no', vmac=u'not-applicable', mac=u'00:22:BD:F8:19:FF', epMoveDetectMode=u'', ownerTag=u'', intersiteBumTrafficAllow=u'no', annotation=u'', ownerKey=u'', name=bd_name, epClear=u'no', unkMacUcastAct=u'flood', arpFlood=u'yes', intersiteL2Stretch=u'no', OptimizeWanBandwidth=u'no') fvRsIgmpsn = RsIgmpsn(fvBD, tnIgmpSnoopPolName=u'', annotation=u'') fvRsCtx = RsCtx(fvBD, annotation=u'', tnFvCtxName=vrf_name) fvRsBdToEpRet = RsBdToEpRet(fvBD, resolveAct=u'resolve', annotation=u'', tnFvEpRetPolName=u'') fvRsBDToNdP = RsBDToNdP(fvBD, annotation=u'', tnNdIfPolName=u'') bd_data = toJSONStr(polUni) c.addMo(polUni) logon_session.commit(c) return bd_data
def test_tn_cleanup(self, apics, certobject, userobject): if userobject.user == 'rouser': return apic = apics[0] user = apics[2] password = apics[3] secure = False if apics[1] == 'False' else True uni = Uni('') fvTenant = Tenant(uni, name='t') fvTenant.delete() fvBD = BD(fvTenant, 't-bd') fvBD.delete() fvAp = Ap(fvTenant, 't-app') fvAp.delete() session = LoginSession(apic, user, password, secure=secure) moDir = MoDirectory(session) moDir.login() cr = ConfigRequest() cr.addMo(fvTenant) r = moDir.commit(cr) assert r.status_code == 200
def test_tn_cleanup(self, apics, certobject, userobject): if userobject.user == 'rouser': return apic = apics[0] user = apics[2] password = apics[3] secure = False if apics[1] == 'False' else True uni = Uni('') fvTenant = Tenant(uni, name='t') fvTenant.delete() fvBD = BD(fvTenant, 't-bd') fvBD.delete() fvAp = Ap(fvTenant, 't-app') fvAp.delete() session = LoginSession(apic, user, password, secure=secure) moDir = MoDirectory(session) moDir.login() cr = ConfigRequest() cr.addMo(fvTenant) r = moDir.commit(cr) assert r == []
def test_ConfigRequest_xmldata(self): expected1 = ('<?xml version="1.0" encoding="UTF-8"?>\n' + '<fvTenant name=\'test\' status=\'created,modified\'>' + '</fvTenant>') expected2 = ('<?xml version="1.0" encoding="UTF-8"?>\n' + '<fvTenant status=\'created,modified\' name=\'test\'>' + '</fvTenant>') polUni = Uni('') fvTenant = Tenant(polUni, 'test') cr = ConfigRequest() cr.addMo(fvTenant) assert (cr.xmldata == expected1 or cr.xmldata == expected2)
def create_vrf(logon_session, tenant_name="Example_TN", description="Description", vrf_name=''): from cobra.internal.codec.jsoncodec import toJSONStr from cobra.model.fv import Tenant, RsTenantMonPol, Ctx, RsOspfCtxPol from cobra.model.fv import RsCtxToExtRouteTagPol, RsBgpCtxPol, RsCtxToEpRet from cobra.model.fv import RsBgpCtxPol, RsVrfValidationPol from cobra.mit.request import ConfigRequest import cobra.model.pol import cobra.model.vns from cobra.model.vz import Any c = ConfigRequest() #apic_logon(apic_url, user, password) polUni = cobra.model.pol.Uni('') fvTenant = Tenant(polUni, tenant_name) fvCtx = Ctx(fvTenant, ownerKey=u'', name=vrf_name, descr=u'', knwMcastAct=u'permit', pcEnfDir=u'ingress', nameAlias=u'', ownerTag=u'', annotation=u'', pcEnfPref=u'enforced', bdEnforcedEnable=u'no') fvRsVrfValidationPol = RsVrfValidationPol(fvCtx, tnL3extVrfValidationPolName=u'', annotation=u'') vzAny = Any(fvCtx, matchT=u'AtleastOne', name=u'', descr=u'', prefGrMemb=u'disabled', nameAlias=u'', annotation=u'') fvRsOspfCtxPol = RsOspfCtxPol(fvCtx, annotation=u'', tnOspfCtxPolName=u'') fvRsCtxToEpRet = RsCtxToEpRet(fvCtx, annotation=u'', tnFvEpRetPolName=u'') fvRsCtxToExtRouteTagPol = RsCtxToExtRouteTagPol(fvCtx, annotation=u'', tnL3extRouteTagPolName=u'') fvRsBgpCtxPol = RsBgpCtxPol(fvCtx, tnBgpCtxPolName=u'', annotation=u'') ctx_data = toJSONStr(polUni) c.addMo(polUni) logon_session.commit(c) return ctx_data
def create_tenant(modir, tenant_name): policy_universe = modir.lookupByDn('uni') # fvTenant = Tenant(policy_universe, tenant_name) # print the query in XML format print toXMLStr(policy_universe, prettyPrint=True) # Commit the change using a ConfigRequest object configReq = ConfigRequest() configReq.addMo(fvTenant) modir.commit(configReq)
def create_tenants(delete=''): tenant_df = pd.read_excel("input-data/ACI_DD_Workbook.xlsx", sheet_name='Tenants') file = open("Tenant_Configuration.log", "w") logon = apic_logon() uniMo = logon.lookupByDn('uni') for index, row in tenant_df.iterrows(): fvTenant = Tenant(uniMo, name=row["Tenant Name"], description=row["Tenant Description"]) cfgRequest = ConfigRequest() if delete == 'yes': fvTenant = Tenant(uniMo, name=row["Tenant Name"], status='deleted') cfgRequest.addMo(fvTenant) else: cfgRequest.addMo(fvTenant) logon.commit(cfgRequest) json_data = toJSONStr(fvTenant, prettyPrint=True) file.write( '\n-------------------------------------------------------------------\n' ) file.write(json_data) file.close()
def create_static_path(tenant_name, app_name, epg_name, path_name, encap_id, mode_name): vlan_id = 'vlan-' + encap_id uni_mo = moDir.lookupByDn('uni') tenant_mo = Tenant(uni_mo, tenant_name) app_mo = Ap(tenant_mo, app_name) epg_mo = AEPg(app_mo, epg_name) rspathatt_mo = RsPathAtt(epg_mo, tDn=path_name, instrImedcy=u'immediate', encap=vlan_id, mode=mode_name) config = ConfigRequest() config.addMo(tenant_mo) moDir.commit(config)
def create_3tier_application(modir, tenant_name): policy_universe = modir.lookupByDn('uni') fv_tenant = Tenant(policy_universe, tenant_name) # create context fv_ctx = Ctx(fv_tenant, CTX_NAME) # fv_bd = BD(fv_tenant, 'BD1') # fv_rs_ctx = RsCtx(fv_bd) fv_rs_ctx.__setattr__('tnFvCtxName', 'Apple-Router') fv_subnet_10 = Subnet(fv_bd,'10.0.0.1/24', scope='public') fv_subnet_20 = Subnet(fv_bd, '20.0.0.1/24', scope='public') fv_subnet_30 = Subnet(fv_bd, '30.0.0.1/24', scope='public') fv_subnet_40 = Subnet(fv_bd, '40.0.0.1/24', scope='public') # fv_ap = Ap(fv_tenant, '3-TierApp') fv_aepg_client = AEPg(fv_ap, 'Client') fv_rs_bd_client = RsBd(fv_aepg_client, tnFvBDName='BD1') # fv_rs_bd_client.__setattr__('tnFvBDName', 'BD1') fv_rs_cons_webct_client = RsCons(fv_aepg_client, 'WebCt') fv_aepg_web = AEPg(fv_ap, 'Web') fv_rs_bd_web = RsBd(fv_aepg_web, tnFvBDName='BD1') fv_rs_cons_webct_web = RsProv(fv_aepg_web, 'WebCt') fv_rs_cons_appct_web = RsCons(fv_aepg_web, 'AppCt') fv_aepg_app = AEPg(fv_ap, 'App') fv_rs_bd_app = RsBd(fv_aepg_app, tnFvBDName='DB1') fv_rs_cons_webct_app = RsProv(fv_aepg_app, 'WebCt') fv_rs_cons_appct_app = RsCons(fv_aepg_app, 'AppCt') fv_aepg_db = AEPg(fv_ap, 'DB') fv_rs_bd_db = RsBd(fv_aepg_db, tnFvBDName='BD1') fv_rs_prov_db = RsProv(fv_aepg_db, 'DbCt') print toXMLStr(policy_universe, prettyPrint=True) # Commit the change using a ConfigRequest object configReq = ConfigRequest() configReq.addMo(policy_universe) modir.commit(configReq)
def setupTenant(spec, apicMoDir): logging.debug('Inside setupTenant function') tenant = spec['tenant'] # Check if the APIC already knows about this tenant tenantDn = formTenantDn(tenant) exists, fvTenantMo = checkDnExists(apicMoDir, tenantDn) if exists: # The tenant already exists in the APIC. Stash what we got. logging.info('Tenant %s already exists.' % tenant) tenantDict[tenant] = fvTenantMo else: logging.info('Creating tenant %s' % tenant) uniMo = apicMoDir.lookupByDn('uni') fvTenantMo = Tenant(uniMo, tenant) # create a vrf for the tenant ctxMo = Ctx(fvTenantMo, tenant + '-Vrf') cR = ConfigRequest() cR.addMo(fvTenantMo) apicMoDir.commit(cR) tenantDict[tenant] = fvTenantMo return ['success', 'ok']
def create_filters(modir, tenant_name): policy_universe = modir.lookupByDn('uni') fv_tenant = Tenant(policy_universe, tenant_name) # create filter for http vz_filter_http = Filter(fv_tenant, FILTER_HTTP_NAME) vz_entry_http = Entry(vz_filter_http, 'DPort-80', dFromPort='80', dToPort='80', etherT='ip', prot='tcp') #create filter for rmi vz_filter_rmi = Filter(fv_tenant, FILTER_RMI_NAME) vz_entry_rmi = Entry(vz_filter_http, 'DPort-1514', dFromPort='1514', dToPort='1514', etherT='ip', prot='tcp') # create filter for sql vz_filter_sql = Filter(fv_tenant, FILTER_RMI_NAME) vz_entry_sql = Entry(vz_filter_http, 'DPort-1433', dFromPort='1433', dToPort='1433', etherT='ip', prot='tcp') # print the query in XML format print toXMLStr(policy_universe, prettyPrint=True) # Commit the change using a ConfigRequest object configReq = ConfigRequest() configReq.addMo(policy_universe) modir.commit(configReq)
def new_tenant(active_location, tenant_name): tenant = Tenant(active_location, tenant_name) return tenant
}, ] }] # CONNECT TO APIC print('Initializing connection to APIC.\n**') moDir = MoDirectory(LoginSession('https://apic', 'admin', 'cisco123')) moDir.login() # Get the top level Policy Universe Directory uniMo = moDir.lookupByDn('uni') print("Starting Tenant Creation.\n**") for tenant in TENANT_INFO: print("Creating tenant %s.." % (tenant['name'])) fvTenantMo = Tenant(uniMo, tenant['name']) # Create Private Network Ctx(fvTenantMo, tenant['ctx']) # Create Bridge Domain fvBDMo = BD(fvTenantMo, name=tenant['bd']) # Create association to private network RsCtx(fvBDMo, tnFvCtxName=tenant['ctx']) # Create Application Profile for app in tenant['ap']: print('Creating Application Profile: %s' % app['name']) fvApMo = Ap(fvTenantMo, app['name'])