cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2331
Views
1
Helpful
2
Replies

API Script

Juraj Papic
Level 3
Level 3

Hi all,

I found this script and I have the issue to understand where I can change myvlanrange.

Any ideas?

Thanks.

# createNewStudent-* tenants

fromcobra.mit.access import MoDirectory

fromcobra.mit.session import LoginSession

fromcobra.mit.request importConfigRequest

fromcobra.model.fvnsimport VlanInstP, EncapBlk

fromcobra.model.infra import RsVlanNs, RsDomP, AttEntityP

from cobra.model.fv import Tenant, Ctx, BD, RsCtx, Ap, AEPg, RsBd, RsDomAtt, RsPathAtt

fromcobra.model.vmmimport DomP, UsrAccP, CtrlrP, RsAcc

fromcobra.model.physimportDomP

importjson

importrequests

fromrequests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

data= []

withopen('StudentTenants.json') as f:

   forline in f:

       data.append(json.loads(line))

physDomainObj= data[0]

print"Physical Domains"

printdata[0]

print"\nTenants:\n"

printdata[1:]

tenants= data[1:]

physDomainInst= data[0]['physDomainInst']

printphysDomainInst

printphysDomainInst['name']

printtenants

user= 'admin'

password= 'F4l4b3ll42017'

apicUrl= 'https://172.16.18.21:443'

moDir= MoDirectory(LoginSession(apicUrl, user, password))

moDir.login()

# Get the top level Policy Universe Directory

uniMo=moDir.lookupByDn('uni')

uniInfraMo=moDir.lookupByDn('uni/infra')

# Domain for All Student Tenants

# fromjsonfile and use it to create our Physical Domain

#physDomainInst['name'] => Physical Domain name

#physDomainObj['physDomainInst']

physDomPMo=DomP(uniMo,physDomainInst['name'])

domcfg=ConfigRequest()

domcfg.addMo(physDomPMo)

moDir.commit(domcfg)

# VLAN Pool for All Student Tenants

# VLAN Pool name to beexact sameas Physical Domain

vlanPhysDomPMo= VlanInstP(uniInfraMo,physDomainInst['name'], 'static')

EncapBlk(vlanPhysDomPMo, "vlan-3000", "vlan-3000", name="PhysVlanPoolCCIE")

physCfg=ConfigRequest()

physCfg.addMo(vlanPhysDomPMo)

moDir.commit(physCfg)

#relationship to Physical Domain

RsVlanNs(physDomPMo,tDn=vlanPhysDomPMo.dn)

domCfg=ConfigRequest()

domCfg.addMo(physDomPMo)

moDir.commit(domCfg)

# Physical Domain Relationship to Attached Entity Profile

AttEntityPmo= AttEntityP(uniInfraMo, "ATTENTITYP")

infra_rsdomp = RsDomP(AttEntityPmo,physDomPMo.dn)

attCfg=ConfigRequest()

attCfg.addMo(AttEntityPmo)

moDir.commit(attCfg)

# #Creation of Tenants and VLAN Ranges

print"Printing full tenant JSON, for debug"

fortenant in tenants:

print"Tenant name: " + tenant['name']

print"BD name: " + tenant['bd']

print"PVN name: " + tenant['ctx']

vlanRangeArgs= tenant['vlanRangeArgs']

print"VLAN Range name: " +vlanRangeArgs['name'] + ", from: " +vlanRangeArgs['from'] + ",to: " +vlanRangeArgs['to']

app= tenant['aps']

print"App Name: " + app['name']

forepgin app['epgs']:

printepg['name']

print"\nStarting tenant configuration...\n"

fortenant in tenants:

  # VLAN range for Student --->vlanRange['name'])

print"==================== Creating Tenant: " + tenant['name'] + " ===================="

vlanRangeArgs= tenant['vlanRangeArgs']

print"Creating VLAN range: " +vlanRangeArgs['name']

fvnsVlanInstPMo= VlanInstP(uniInfraMo,vlanRangeArgs['name'], 'dynamic')

  EncapBlk(fvnsVlanInstPMo, vlanRangeArgs['from'], vlanRangeArgs['to'], name=vlanRangeArgs['name'])

  # commit VLAN Range to APIC

nsCfg=ConfigRequest()

nsCfg.addMo(fvnsVlanInstPMo)

  moDir.commit(nsCfg) 

fvTenantMo= Tenant(uniMo, tenant['name'])

  # Create Private Network under Tenant

print"Creating Private Network: " + tenant['ctx']

Ctx(fvTenantMo, tenant['ctx'])

  # Create Bridge Domain under Tenant

print"Creating Bridge Domain: " + tenant['bd']

fvBDMo= BD(fvTenantMo, name=tenant['bd'])

  # Create association to private network under Tenant

print"Creating VRF to BD association"

fvRsCtx= RsCtx(fvBDMo,tnFvCtxName=tenant['ctx'])

app= tenant['aps']

  # creatingApplication Profile---> app['name']

print"Creating Application profile: " + app['name']

fvApMo=Ap(fvTenantMo, app['name'])

  # for all EPGS from JSON and create each EPG under Application

print"Creating EPGs: "

forepgin app['epgs']:

print"EPG: " +epg['name']

fvAEPgMo= AEPg(fvApMo,epg['name'])

  # Associate EPG to Bridge Domain

RsBd(fvAEPgMo,tnFvBDName=tenant['bd'])

  # Associate EPG to Phys Domain

RsDomAtt(fvAEPgMo,physDomPMo.dn)

encap=epg['encap']

print"Associating EPG to VLAN: " +str(encap)

  RsPathAtt(fvAEPgMo, tDn="topology/pod-1/", encap=('vlan-' + str(encap)))

RsPathAtt(fvAEPgMo,tDn="topology/pod-1/paths-102",encap=('vlan'))

RsPathAtt(fvAEPgMo,tDn="topology/pod-1/paths-103",encap=('vlan'))

  # Commit changes to the Configuration

tenantCfg=ConfigRequest()

tenantCfg.addMo(fvTenantMo)

  moDir.commit(tenantCfg)

print"\n"

  # Associate EPG to Static Path to node-103 and node-102

  # ch encap VLAN numbers from epg dictionary key encap, like this epg['encap]

  # mat EPG VLAN encap number by concatenating string "vlan-" and value of ep

  # ['encap'] => VLAN encap

  # fvRsPathAtt(fvAEPgMo, tDn=epg['path'], encap=('vlan-' + str(epg)))

  # fvRsPathAtt(fvAEPgMo,tDn=epg['path'].replace('paths-103','paths-102'),encap=('vlan'))

2 Replies 2

odysseus_hu163
Level 1
Level 1

what about the json file? you can change the vlan value within the json file.

You can send the json file to me, I will fix your problem.

simon_hu1983@hotmail.com

sandeepthumu
Level 1
Level 1

Hi Juraj,

It is at 'EncapBlk(vlanPhysDomPMo, "vlan-3000", "vlan-3000", name="PhysVlanPoolCCIE")', where you  can give your vlan range, the second and third parameters are nothing but 'start-vlan' and 'end-vlan'

-Thumu