Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
def create_vlan_pool(infra_infra, vlan, allocation_mode, vlan_range_from,
                     vlan_range_to):
    """Create VLAN Pool, the policy definition for ID ranges used for VLAN encapsulation. """
    fvns_vlaninstp = VlanInstP(infra_infra, vlan, allocation_mode)
    # Set up the VLAN range.
    fvns_encapblk = EncapBlk(fvns_vlaninstp, 'vlan-' + str(vlan_range_from),
                             'vlan-' + str(vlan_range_to))
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
def create_vlan_pool(modir, vlan_name, allocation_mode, vlan_range_from, vlan_range_to):

    # Query to the vlan pool collections.
    infra_infra = modir.lookupByDn('uni/infra')
    # Create a VLAN.
    fvns_vlaninstp = VlanInstP(infra_infra, vlan_name, allocation_mode)
    # Set up the VLAN range.
    fvns_encapblk = EncapBlk(fvns_vlaninstp, 'vlan-'+vlan_range_from, 'vlan-'+vlan_range_to)
    print toXMLStr(infra_infra, prettyPrint=True)
    commit_change(modir, infra_infra)
Ejemplo n.º 6
0
def main(host, username, password, pool_name, from_vlan, to_vlan):
    apic = "https://%s" % host
    print("Connecting to APIC : %s" % apic)
    moDir = MoDirectory(LoginSession(apic, username, password))
    moDir.login()
    topMO = moDir.lookupByDn('uni')
    moDir.lookupByDn
    infraInfra = cobra.model.infra.Infra(topMO)
    fvnsVlanInstP = VlanInstP(infraInfra, name=pool_name, allocMode="static")
    temp_from_vlan = "vlan-" + from_vlan
    temp_to_vlan = "vlan-" + to_vlan
    fvnsEncapBlk = EncapBlk(fvnsVlanInstP, temp_from_vlan, temp_to_vlan)

    print(toXMLStr(topMO))
    c = ConfigRequest()
    c.addMo(infraInfra)
    moDir.commit(c)
Ejemplo n.º 7
0
def vlan_pool(host, user, password):
    print('[BEG] VLAN Pool Creation')
    moDir = apic_login(host, user, password)

    # Get Top Level Objects
    polUni = Uni('')
    infraInfra = Infra(polUni)

    vlanPName = 'ASA-L3-OUT-STATIC-Cobra'
    vlanMode = 'static'
    vlanTo = 'vlan-182'
    vlanFrom = 'vlan-181'

    print('--- Creating VLAN Pool: ' + vlanPName)
    fvnsVlanInstP = VlanInstP(infraInfra, name=vlanPName, allocMode=vlanMode)
    print('	Adding ' + vlanMode + ' pool from ' + vlanFrom + ' to ' + vlanTo)
    fvnsEncapBlk = EncapBlk(fvnsVlanInstP,
                            to=vlanTo,
                            from_=vlanFrom,
                            allocMode=vlanMode)

    cfg_commit(moDir, infraInfra)
    print('[END] VLAN Pool Creation \n')
    return vlanPName
Ejemplo n.º 8
0
def main(host, port, user, password):

    # CONNECT TO APIC
    print('Initializing connection to APIC...')
    apicUrl = 'http://%s:%d' % (host, port)
    moDir = MoDirectory(LoginSession(apicUrl, user, password))
    moDir.login()

    # Get the top level Policy Universe Directory
    uniMo = moDir.lookupByDn('uni')
    uniInfraMo = moDir.lookupByDn('uni/infra')

    # Create Vlan Namespace
    nsInfo = VMM_DOMAIN_INFO['namespace']
    print("Creating namespace %s.." % (nsInfo['name']))
    fvnsVlanInstPMo = VlanInstP(uniInfraMo, nsInfo['name'], 'dynamic')
    #fvnsArgs = {'from': nsInfo['from'], 'to': nsInfo['to']}
    EncapBlk(fvnsVlanInstPMo, nsInfo['from'], nsInfo['to'], name=nsInfo['name'])
    
    nsCfg = ConfigRequest()
    nsCfg.addMo(fvnsVlanInstPMo)
    moDir.commit(nsCfg)

    # Create VMM Domain
    print('Creating VMM domain...')

    vmmpVMwareProvPMo = moDir.lookupByDn('uni/vmmp-VMware')
    vmmDomPMo = DomP(vmmpVMwareProvPMo, VMM_DOMAIN_INFO['name'])
    
    vmmUsrMo = []
    for usrp in VMM_DOMAIN_INFO['usrs']:
        usrMo = UsrAccP(vmmDomPMo, usrp['name'], usr=usrp['usr'],
                        pwd=usrp['pwd'])
        vmmUsrMo.append(usrMo)

    # Create Controllers under domain
    for ctrlr in VMM_DOMAIN_INFO['ctrlrs']:
        vmmCtrlrMo = CtrlrP(vmmDomPMo, ctrlr['name'], scope=ctrlr['scope'],
                            hostOrIp=ctrlr['ip'])
        # Associate Ctrlr to UserP
        RsAcc(vmmCtrlrMo, tDn=vmmUsrMo[0].dn)
    
    # Associate Domain to Namespace
    RsVlanNs(vmmDomPMo, tDn=fvnsVlanInstPMo.dn)
   
    vmmCfg = ConfigRequest()
    vmmCfg.addMo(vmmDomPMo)
    moDir.commit(vmmCfg)
    print("VMM Domain Creation Completed.")

    print("Starting Tenant Creation..")
    for tenant in TENANT_INFO:
        print("Creating tenant %s.." % (tenant['name']))
        fvTenantMo = Tenant(uniMo, tenant['name'])
        
        # Create Private Network
        Ctx(fvTenantMo, tenant['pvn'])
        
        # Create Bridge Domain
        fvBDMo = BD(fvTenantMo, name=tenant['bd'])
        
        # Create association to private network
        RsCtx(fvBDMo, tnFvCtxName=tenant['pvn'])
        
        # Create Application Profile
        for app in tenant['ap']:
            print('Creating Application Profile: %s' % app['name'])
            fvApMo = Ap(fvTenantMo, app['name'])
            
            # Create EPGs 
            for epg in app['epgs']:
                
                print("Creating EPG: %s..." % (epg['name'])) 
                fvAEPgMo = AEPg(fvApMo, epg['name'])
                
                # Associate EPG to Bridge Domain 
                RsBd(fvAEPgMo, tnFvBDName=tenant['bd'])
                # Associate EPG to VMM Domain
                RsDomAtt(fvAEPgMo, vmmDomPMo.dn)

        # Commit each tenant seperately
        tenantCfg = ConfigRequest()
        tenantCfg.addMo(fvTenantMo)
        moDir.commit(tenantCfg)
    print('All done!')
Ejemplo n.º 9
0
class Test_mit_request_ConfigRequest(object):

    def test_ConfigRequest_init(self):
        assert isinstance(ConfigRequest(), ConfigRequest)

    def test_ConfigRequest_options(self):
        cid = '1234567890'
        expectedOptions = ''
        cr =  ConfigRequest()
        cr.id = cid
        expectedOptions += '_dc=' + cid
        assert cr.options == expectedOptions

    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 test_ConfigRequest_data_raises(self):
        cr = ConfigRequest()
        with pytest.raises(CommitError):
            abc = cr.data

    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 test_ConfigRequest_xmldata_raises(self):
        cr = ConfigRequest()
        with pytest.raises(CommitError):
            abc = cr.xmldata

   # expected is None means you expect the same value returned that was set
    @pytest.mark.parametrize("value", [
        ('full'),
        ('modified'),
        ('no'),
    ])
    def test_ConfigRequest_subtree(self, value):
        cr = ConfigRequest()
        cr.subtree = value
        assert cr.subtree == value

    def test_ConfigRequest_subtree_raises(self):
        cr = ConfigRequest()
        with pytest.raises(ValueError):
            cr.subtree = 'children'

    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)

    # addMo is tested in test_ConfigRequest_requestargs but we still need
    # branch testing.  See test_ConfigRequest_addMo_raises_* methods
    #def test_ConfigRequest_addMo(self):

    def test_ConfigRequest_addMo_raises_no_context(self):
        polUni = Uni('')
        cr = ConfigRequest()
        with pytest.raises(ValueError):
            cr.addMo(polUni)

    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 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 test_ConfigRequest_removeMo_and_hasMo_negative(self):
        fvTenant = Tenant('uni', 'testing')
        fvnsVlanInstP = VlanInstP('uni/infra', 'namespace1', 'dynamic')
        cr = ConfigRequest()
        cr.addMo(fvTenant)
        cr.removeMo(fvTenant)
        cr.addMo(fvnsVlanInstP)
        assert not cr.hasMo(fvTenant.dn)

    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_removeMo_raises(self):
        fvTenant = Tenant('uni', 'testing')
        cr = ConfigRequest()
        with pytest.raises(KeyError):
            cr.removeMo(fvTenant)

    @pytest.mark.parametrize("mos, expected", [
        ([None], None),
        ([], LooseNode(u'topology', u"101")),
        ([BD(u'uni/tn-testing', u'test')], Tenant(u'uni', u'testing')),
    ])
    def test_ConfigRequest_getRootMo(self, mos, expected):
        cr = ConfigRequest()
        mos.append(expected)
        for mo in mos:
            if mo is not None:
                try:
                    cr.addMo(mo)
                except ValueError:
                    pass
        assert cr.getRootMo() == expected

    @pytest.mark.parametrize("sessionUrl,mo,dc,requestType", [
        ('http://1.1.1.1', Tenant('uni', 'testing'), None, 'xml'),
        ('http://1.1.1.1', Tenant('uni', 'testing'), None, 'json'),
        ('http://1.1.1.1', Tenant('uni', 'testing'), '1', 'xml'),
        ('http://1.1.1.1', Tenant('uni', 'testing'), '1', 'json'),
    ])
    def test_ConfigRequest_getUriPathAndOptions(self, sessionUrl, mo, dc,
                                                requestType):
        session = LoginSession(sessionUrl, 'admin', 'password',
                               requestFormat=requestType)

        cr = ConfigRequest()
        cr.addMo(mo)
        expected = '/api/mo/' + str(mo.dn) + '.' + requestType
        if dc is not None:
            cr.id =  dc
            expected += '?_dc=' + dc
        assert cr.getUriPathAndOptions(session) == expected


    @pytest.mark.parametrize("sessionUrl,mo,requestType", [
        ("http://1.1.1.1", Tenant('uni', 'testing'), 'xml'),
        ("http://1.1.1.1", Tenant('uni', 'testing'), 'json'),
        ("http://2.2.2.2", VlanInstP('uni/infra', 'namespace1', 'dynamic'),
            'xml'),
        ("http://2.2.2.2", VlanInstP('uni/infra', 'namespace1', 'dynamic'),
            'json'),
    ])
    def test_ConfigRequest_getUrl(self, sessionUrl, mo, requestType):
        session = LoginSession(sessionUrl, 'admin', 'password',
                               requestFormat=requestType)
        expected = sessionUrl + '/api/mo/' + str(mo.dn) + '.' + requestType
        cr = ConfigRequest()
        cr.addMo(mo)
        assert cr.getUrl(session) == expected
Ejemplo n.º 10
0
def remove_vlan_pool(infra_infra, vlan, allocation_mode, vlan_range_from, vlan_range_to):
    """Remove VLAN Pool, the policy definition for ID ranges used for VLAN encapsulation. """
    fvns_vlaninstp = VlanInstP(infra_infra, vlan, allocation_mode)
    fvns_vlaninstp.delete()