Esempio n. 1
0
def create_vcenter_controller(modir, vm_provider, vmm_domain_name,
                              controller_name, host_or_ip, data_center,
                              **args):
    vmm_domp = modir.lookupByDn('uni/vmmp-' + vm_provider + '/dom-' +
                                vmm_domain_name)
    args = args['args_from_CLI'] if 'args_from_CLI' in args.keys() else args
    if isinstance(vmm_domp, DomP):
        vmm_ctrlrp = CtrlrP(vmm_domp,
                            controller_name,
                            hostOrIp=host_or_ip,
                            rootContName=data_center,
                            statsMode=get_value(args, 'statsMode', 'disabled'))

        # define associated credential
        if 'associated_credential' in args.keys():
            vmm_usraccp_path = 'uni/vmmp-' + vm_provider + '/dom-' + vmm_domain_name + '/usracc-' + args[
                'associated_credential']
            vmm_usraccp = modir.lookupByDn(vmm_usraccp_path)
            if isinstance(vmm_usraccp, UsrAccP):
                vmm_rtctrlrp = RsAcc(vmm_ctrlrp, tDn=vmm_usraccp_path)
            else:
                print args['associated_credential'], 'has not been defined.'
                return
    else:
        print 'There is no VMM Domain', vmm_domain_name, 'in', vm_provider
        return

    print toXMLStr(vmm_domp, prettyPrint=True)
    commit_change(modir, vmm_domp)
Esempio n. 2
0
def disconnect_epg_contract(modir, tenant_name, application_name, epg_name, contract_type, contract_name):
    """Take out a consumed/provided contract from an EPG"""
    vz_brcp = modir.lookupByDn('uni/tn-' + tenant_name + '/brc-' + contract_name)
    if not isinstance(vz_brcp, BrCP):
        print 'There is no contract called', contract_name, 'in tenant', tenant_name, '.'
        return

    fv_aepg = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' + application_name + '/epg-' + epg_name)
    if isinstance(fv_aepg, AEPg):
        if contract_type.lower() == 'consumed':
            fv = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' + application_name + '/epg-' + epg_name + '/rscons-' + contract_name)
        elif contract_type.lower() == 'provided':
            fv = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' + application_name + '/epg-' + epg_name + '/rsprov-' + contract_name)
        else:
            print 'Contract_type is either \"consumed\" or \"provided\".'
            return
        if isinstance(fv, RsCons) or isinstance(fv, RsProv):
            fv.delete()
        else:
            print 'Contract', contract_name, 'is not connected to EPG', epg_name, '.'
            return
    else:
        print 'Wrong path! Please check if EPG', epg_name, 'is in application', application_name, 'in tenant', tenant_name, '.'
        return

    print toXMLStr(fv, prettyPrint=True)
    commit_change(modir, fv)
Esempio n. 3
0
def connect_epg_contract(modir, tenant_name, application_name, epg_name, contract_type, contract_name):
    """Assign a consumed/provided contract to an EPG"""

    # Check if the contract exist. If not, return.
    vz_brcp = modir.lookupByDn('uni/tn-' + tenant_name + '/brc-' + contract_name)
    if not isinstance(vz_brcp, BrCP):
        print 'There is no contract called', contract_name, 'in tenant', tenant_name, '.'
        return

    # Check if the EPG exist.
    fv_aepg = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' + application_name + '/epg-' + epg_name)
    if isinstance(fv_aepg, AEPg):

        # Check the contract type, consumed or provided.
        if contract_type.lower() == 'consumed':
            # Add a consumed contract to EPG
            fv_rscons = RsCons(fv_aepg, contract_name)
        elif contract_type.lower() == 'provided':
            # Add a provided contract to EPG
            fv_rsprov = RsProv(fv_aepg, contract_name)
        else:
            print 'Contract_type is either \"consumed\" or \"provided\".'
            return
    else:
        print 'Wrong path! Please check if EPG', epg_name, 'is in application', application_name, 'in tenant', tenant_name, '.'
        return

    print toXMLStr(fv_aepg, prettyPrint=True)
    commit_change(modir, fv_aepg)
Esempio n. 4
0
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)
Esempio n. 5
0
def disconnect_epg_contract(modir, tenant_name, application_name, epg_name,
                            contract_type, contract_name):
    """Take out a consumed/provided contract from an EPG"""
    vz_brcp = modir.lookupByDn('uni/tn-' + tenant_name + '/brc-' +
                               contract_name)
    if not isinstance(vz_brcp, BrCP):
        print 'There is no contract called', contract_name, 'in tenant', tenant_name, '.'
        return

    fv_aepg = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' +
                               application_name + '/epg-' + epg_name)
    if isinstance(fv_aepg, AEPg):
        if contract_type.lower() == 'consumed':
            fv = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' +
                                  application_name + '/epg-' + epg_name +
                                  '/rscons-' + contract_name)
        elif contract_type.lower() == 'provided':
            fv = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' +
                                  application_name + '/epg-' + epg_name +
                                  '/rsprov-' + contract_name)
        else:
            print 'Contract_type is either \"consumed\" or \"provided\".'
            return
        if isinstance(fv, RsCons) or isinstance(fv, RsProv):
            fv.delete()
        else:
            print 'Contract', contract_name, 'is not connected to EPG', epg_name, '.'
            return
    else:
        print 'Wrong path! Please check if EPG', epg_name, 'is in application', application_name, 'in tenant', tenant_name, '.'
        return

    print toXMLStr(fv, prettyPrint=True)
    commit_change(modir, fv)
Esempio n. 6
0
def connect_epg_contract(modir, tenant_name, application_name, epg_name,
                         contract_type, contract_name):
    """Assign a consumed/provided contract to an EPG"""

    # Check if the contract exist. If not, return.
    vz_brcp = modir.lookupByDn('uni/tn-' + tenant_name + '/brc-' +
                               contract_name)
    if not isinstance(vz_brcp, BrCP):
        print 'There is no contract called', contract_name, 'in tenant', tenant_name, '.'
        return

    # Check if the EPG exist.
    fv_aepg = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' +
                               application_name + '/epg-' + epg_name)
    if isinstance(fv_aepg, AEPg):

        # Check the contract type, consumed or provided.
        if contract_type.lower() == 'consumed':
            # Add a consumed contract to EPG
            fv_rscons = RsCons(fv_aepg, contract_name)
        elif contract_type.lower() == 'provided':
            # Add a provided contract to EPG
            fv_rsprov = RsProv(fv_aepg, contract_name)
        else:
            print 'Contract_type is either \"consumed\" or \"provided\".'
            return
    else:
        print 'Wrong path! Please check if EPG', epg_name, 'is in application', application_name, 'in tenant', tenant_name, '.'
        return

    print toXMLStr(fv_aepg, prettyPrint=True)
    commit_change(modir, fv_aepg)
Esempio n. 7
0
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)
Esempio n. 8
0
def int_rpi(cobra_md):
    # the top level object on which operations will be made
    # Confirm the dn below is for your top dn
    topDn = cobra.mit.naming.Dn.fromString('uni/infra/funcprof/accportgrp-RPIs')
    topParentDn = topDn.getParent()
    topMo = cobra_md.lookupByDn(topParentDn)

    # build the request using cobra syntax
    infraAccPortGrp = cobra.model.infra.AccPortGrp(topMo, ownerKey=u'', name=u'RPIs', descr=u'', ownerTag=u'')
    infraRsMonIfInfraPol = cobra.model.infra.RsMonIfInfraPol(infraAccPortGrp, tnMonInfraPolName=u'')
    infraRsLldpIfPol = cobra.model.infra.RsLldpIfPol(infraAccPortGrp, tnLldpIfPolName=u'LLDP_ON')
    infraRsStpIfPol = cobra.model.infra.RsStpIfPol(infraAccPortGrp, tnStpIfPolName=u'')
    infraRsL2IfPol = cobra.model.infra.RsL2IfPol(infraAccPortGrp, tnL2IfPolName=u'')
    infraRsCdpIfPol = cobra.model.infra.RsCdpIfPol(infraAccPortGrp, tnCdpIfPolName=u'On')
    infraRsMcpIfPol = cobra.model.infra.RsMcpIfPol(infraAccPortGrp, tnMcpIfPolName=u'')
    infraRsAttEntP = cobra.model.infra.RsAttEntP(infraAccPortGrp, tDn=u'uni/infra/attentp-RaspberryPIs')
    infraRsStormctrlIfPol = cobra.model.infra.RsStormctrlIfPol(infraAccPortGrp, tnStormctrlIfPolName=u'')
    infraRsHIfPol = cobra.model.infra.RsHIfPol(infraAccPortGrp, tnFabricHIfPolName=u'1g')


    # commit the generated code to APIC
    print toXMLStr(topMo)
    c = cobra.mit.request.ConfigRequest()
    c.addMo(topMo)
    cobra_md.commit(c)
Esempio n. 9
0
def VMM_Create(vswitch_name, vcenter_name, vdc_name, dvs_version, vlan_pool,
               vcenter_un, vcenter_pw):
    # the top level object on which operations will be made
    polUni = cobra.model.pol.Uni('')
    vmmProvP = cobra.model.vmm.ProvP(polUni, 'VMware')

    # build the request using cobra syntax
    vmmDomP = cobra.model.vmm.DomP(vmmProvP, name=vswitch_name)
    vmmCtrlrP = cobra.model.vmm.CtrlrP(vmmDomP,
                                       name=vcenter_name,
                                       rootContName=vdc_name,
                                       dvsVersion=dvs_version,
                                       hostOrIp=vcenter_name +
                                       '.datalinklabs.local')
    vmmRsAcc = cobra.model.vmm.RsAcc(vmmCtrlrP,
                                     tDn=u'uni/vmmp-VMware/dom-' +
                                     vswitch_name + '/usracc-' + vswitch_name +
                                     '_Credentials')
    infraRsVlanNs = cobra.model.infra.RsVlanNs(vmmDomP,
                                               tDn=u'uni/infra/vlanns-[' +
                                               vlan_pool + ']-dynamic')
    vmmUsrAccP = cobra.model.vmm.UsrAccP(vmmDomP,
                                         name=vswitch_name + '_Credentials',
                                         pwd=vcenter_pw,
                                         usr=vcenter_un)
    vmmVSwitchPolicyCont = cobra.model.vmm.VSwitchPolicyCont(vmmDomP)

    # commit the generated code to APIC
    print toXMLStr(vmmProvP)
    c = cobra.mit.request.ConfigRequest()
    c.addMo(vmmProvP)
    md.commit(c)
Esempio n. 10
0
def VMM_Create2():
    polUni = cobra.model.pol.Uni('')
    vmmProvP = cobra.model.vmm.ProvP(polUni, 'VMware')
    vmmDomP = cobra.model.vmm.DomP(vmmProvP, name=u'e7_VMW1_Guest')
    vmmCtrlrP = cobra.model.vmm.CtrlrP(
        vmmDomP,
        name=u'e7vmw1vic01',
        rootContName=u'e7dc1',
        dvsVersion=u'6.0',
        hostOrIp=u'e7vmw1vic01.datalinklabs.local')
    vmmRsAcc = cobra.model.vmm.RsAcc(
        vmmCtrlrP,
        tDn=
        u'uni/vmmp-VMware/dom-e7_VMW1_Guest/usracc-e7_VMW1_Guest_Credentials')
    infraRsVlanNs = cobra.model.infra.RsVlanNs(
        vmmDomP, tDn=u'uni/infra/vlanns-[VMW1_Data_Vlan_pool]-dynamic')
    vmmUsrAccP = cobra.model.vmm.UsrAccP(vmmDomP,
                                         name=u'e7_VMW1_Guest_Credentials',
                                         pwd=u'D@talink1',
                                         usr=u'*****@*****.**')
    vmmVSwitchPolicyCont = cobra.model.vmm.VSwitchPolicyCont(vmmDomP)

    # commit the generated code to APIC
    print toXMLStr(vmmProvP)
    c = cobra.mit.request.ConfigRequest()
    c.addMo(vmmProvP)
    md.commit(c)
Esempio n. 11
0
def conf_NTP(modir , ntp_list):
    '''
    Function that iterates through ntp_list and creates JSON call to configure NTP providers.
    The first NTP server in ntp_list will be the prefered NTP provider.
    '''
    topDn = cobra.mit.naming.Dn.fromString('uni/fabric/time-Best_NTP_Policy')
    topParentDn = topDn.getParent()
    topMo = modir.lookupByDn(topParentDn)
    datetimePol = cobra.model.datetime.Pol(topMo, ownerKey=u'', name=u'Best_NTP_Policy', descr=u'Scripted NTP_Config', adminSt=u'enabled', ownerTag=u'')
    ntp_servers_dict={}
    ntp_prov_dict={}

    for x in range(len(ntp_list)):
        if x == 1:
            ntp_servers_dict['ntp_server_%02d' % x] = cobra.model.datetime.NtpProv(datetimePol, name=ntp_list[x], preferred=u'true')
            ntp_prov_dict['ntp_prov_%02d' % x] = cobra.model.datetime.RsNtpProvToEpg(ntp_servers_dict['ntp_server_%02d' % x], tDn=u'uni/tn-mgmt/mgmtp-default/oob-default')
        else:
            ntp_servers_dict['ntp_server_%02d' % x] = cobra.model.datetime.NtpProv(datetimePol, name=ntp_list[x])
            ntp_prov_dict['ntp_prov_%02d' % x] = cobra.model.datetime.RsNtpProvToEpg(ntp_servers_dict['ntp_server_%02d' % x], tDn=u'uni/tn-mgmt/mgmtp-default/oob-default')


    print toXMLStr(topMo)
    c = ConfigRequest()
    c.addMo(topMo)
    modir.commit(c)
Esempio n. 12
0
def print_query_xml(xml_file, pretty_print=True):
    """
    :param xml_file: XML to be printed
    :param pretty_print: if set, uses pretty print
    :return XML to string
    """
    print toXMLStr(xml_file, prettyPrint=pretty_print)
    return toXMLStr(xml_file, prettyPrint=pretty_print)
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(fvTenant)
    modir.commit(configReq)
Esempio n. 14
0
def delete_vcenter_controller(modir, vm_provider, vmm_domain_name, controller_name):
    vmm_domp = modir.lookupByDn('uni/vmmp-' + vm_provider + '/dom-' + vmm_domain_name)
    if isinstance(vmm_domp, DomP):
        CtrlrP(vmm_domp, controller_name).delete()
    else:
        print 'There is no VMM Domain', vmm_domain_name, 'in', vm_provider
        return

    print toXMLStr(vmm_domp, prettyPrint=True)
    commit_change(modir, vmm_domp)
Esempio n. 15
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)
Esempio n. 16
0
def add_vmm_domain_association(modir, tenant_name, application, epg, vmm_domain):
    fv_rsdomatt = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' + application + '/epg-' + epg + '/rsdomAtt-[uni/vmmp-VMware/dom-' + vmm_domain + ']')
    if isinstance(fv_rsdomatt, RsDomAtt):
        fv_rsdomatt.delete()
    else:
        print 'VMM Domain', vmm_domain, ' has been added to EPG', epg
        return

    print toXMLStr(fv_rsdomatt, prettyPrint=True)
    commit_change(modir, fv_rsdomatt)
Esempio n. 17
0
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)
Esempio n. 18
0
def create_vmm_domain(modir, vm_provider, vmm_domain_name):

    vmm_domp = modir.lookupByDn('uni/vmmp-' + vm_provider + '/dom-' + vmm_domain_name)
    if isinstance(vmm_domp, DomP):
        vmm_domp.delete()
    else:
        print 'There is no VMM Domain', vmm_domain_name, 'in', vm_provider
        return
    print toXMLStr(vmm_domp, prettyPrint=True)
    commit_change(modir, vmm_domp)
Esempio n. 19
0
def create_vmm_domain(modir, vm_provider, vmm_domain_name, **args):
    vmm_provp = modir.lookupByDn('uni/vmmp-' + vm_provider)
    args = args['args_from_CLI'] if 'args_from_CLI' in args.keys() else args
    vmm_domp = DomP(vmm_provp, vmm_domain_name)
    if 'vlan_name' in args.keys() and 'vlan_mode' in args.keys():
        infra_revlanns = RsVlanNs(vmm_domp,tDn='uni/infra/vlanns-[' + args['vlan_name'] + ']-' + args['vlan_mode'])
    elif 'vlan_name' in args.keys() or 'vlan_mode' in args.keys():
        print 'Please specify both [vlan-name] and [vlan-mode]'
    print toXMLStr(vmm_provp, prettyPrint=True)
    commit_change(modir, vmm_provp)
Esempio n. 20
0
def create_vcenter_credential(modir, vm_provider, vmm_domain_name, profile_name, username, password):
    vmm_domp = modir.lookupByDn('uni/vmmp-' + vm_provider + '/dom-' + vmm_domain_name)
    if isinstance(vmm_domp, DomP):
        vmm_usraccp = UsrAccP(vmm_domp, profile_name, usr=username, pwd=password)

    else:
        print 'There is no VMM Domain', vmm_domain_name, 'in', vm_provider
        return

    print toXMLStr(vmm_domp, prettyPrint=True)
    commit_change(modir, vmm_domp)
Esempio n. 21
0
def delete_tenant(modir, tenant_name):
    fv_tenant = modir.lookupByDn('uni/tn-' + tenant_name)
    fv_tenant.delete()

    # print the query in XML format
    print toXMLStr(fv_tenant, prettyPrint=True)

    # Commit the change using a ConfigRequest object
    configReq = ConfigRequest()
    configReq.addMo(fv_tenant)
    modir.commit(configReq)
Esempio n. 22
0
def create_vmm_domain(modir, vm_provider, vmm_domain_name):

    vmm_domp = modir.lookupByDn('uni/vmmp-' + vm_provider + '/dom-' +
                                vmm_domain_name)
    if isinstance(vmm_domp, DomP):
        vmm_domp.delete()
    else:
        print 'There is no VMM Domain', vmm_domain_name, 'in', vm_provider
        return
    print toXMLStr(vmm_domp, prettyPrint=True)
    commit_change(modir, vmm_domp)
Esempio n. 23
0
def delete_tenant(modir, tenant_name):
    fv_tenant = modir.lookupByDn('uni/tn-' + tenant_name)
    fv_tenant.delete()

    # print the query in XML format
    print toXMLStr(fv_tenant, prettyPrint=True)

    # Commit the change using a ConfigRequest object
    configReq = ConfigRequest()
    configReq.addMo(fv_tenant)
    modir.commit(configReq)
Esempio n. 24
0
def setup_fabric_node(modir, pod_id, serial_num, node_id, node_name):
    fv_pod = modir.lookupByDn('topology/pod-' + pod_id)
    if isinstance(fv_pod, Pod):
        fv_node = Node(fv_pod, node_id, name=node_name, serial=serial_num)

    else:
        print 'Pod', pod_id, 'does not exist.'
        return

    print toXMLStr(fv_pod, prettyPrint=True)
    print 'Fabric configuration takes up to 5 minutes. Please wait.'
Esempio n. 25
0
def setup_fabric_node(modir, pod_id, serial_num, node_id, node_name):
    fv_pod = modir.lookupByDn('topology/pod-' + pod_id)
    if isinstance(fv_pod, Pod):
        fv_node = Node(fv_pod, node_id, name=node_name, serial=serial_num)

    else:
        print 'Pod', pod_id, 'does not exist.'
        return

    print toXMLStr(fv_pod, prettyPrint=True)
    print 'Fabric configuration takes up to 5 minutes. Please wait.'
Esempio n. 26
0
def delete_vcenter_controller(modir, vm_provider, vmm_domain_name,
                              controller_name):
    vmm_domp = modir.lookupByDn('uni/vmmp-' + vm_provider + '/dom-' +
                                vmm_domain_name)
    if isinstance(vmm_domp, DomP):
        CtrlrP(vmm_domp, controller_name).delete()
    else:
        print 'There is no VMM Domain', vmm_domain_name, 'in', vm_provider
        return

    print toXMLStr(vmm_domp, prettyPrint=True)
    commit_change(modir, vmm_domp)
Esempio n. 27
0
def delete_vcenter_credential(modir, vm_provider, vmm_domain_name,
                              profile_name):
    vmm_usraccp = modir.lookupByDn('uni/vmmp-' + vm_provider + '/dom-' +
                                   vmm_domain_name + '/usracc-' + profile_name)
    if isinstance(vmm_usraccp, UsrAccP):
        vmm_usraccp.delete()
    else:
        print 'There is no vCenter credential', profile_name, 'in', vmm_domain_name
        return

    print toXMLStr(vmm_usraccp, prettyPrint=True)
    commit_change(modir, vmm_usraccp)
Esempio n. 28
0
def create_vmm_domain(modir, vm_provider, vmm_domain_name, **args):
    vmm_provp = modir.lookupByDn('uni/vmmp-' + vm_provider)
    args = args['args_from_CLI'] if 'args_from_CLI' in args.keys() else args
    vmm_domp = DomP(vmm_provp, vmm_domain_name)
    if 'vlan_name' in args.keys() and 'vlan_mode' in args.keys():
        infra_revlanns = RsVlanNs(vmm_domp,
                                  tDn='uni/infra/vlanns-[' +
                                  args['vlan_name'] + ']-' + args['vlan_mode'])
    elif 'vlan_name' in args.keys() or 'vlan_mode' in args.keys():
        print 'Please specify both [vlan-name] and [vlan-mode]'
    print toXMLStr(vmm_provp, prettyPrint=True)
    commit_change(modir, vmm_provp)
Esempio n. 29
0
def delete_application(modir, tenant_name, application_name):
    """Delete an application profile"""
    fv_ap = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' + application_name)
    if isinstance(fv_ap, Ap):
        fv_ap.delete()
    else:
        print 'There is no application called', application_name, 'in tenant', tenant_name, '.'
        return

    print toXMLStr(fv_ap, prettyPrint=True)

    commit_change(modir, fv_ap)
Esempio n. 30
0
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)
Esempio n. 31
0
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(policy_universe)
    modir.commit(configReq)
Esempio n. 32
0
def add_vmm_domain_association(modir, tenant_name, application, epg, vmm_domain, **args):
    fv_epg = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' + application + '/epg-' + epg)
    args = args['args_from_CLI'] if 'args_from_CLI' in args.keys() else args
    if isinstance(fv_epg, AEPg):
        fv_rsdomatt = RsDomAtt(fv_epg, 'uni/vmmp-VMware/dom-' + vmm_domain,
                               instrImedcy=get_value(args, 'deployment_immediacy', 'lazy'),
                               resImedcy=get_value(args, 'resolution_immediacy', 'lazy'))
    else:
        print 'Fail to find EPG', epg, 'Please make sure you have the EPG in application', application
        return

    print toXMLStr(fv_epg, prettyPrint=True)
    commit_change(modir, fv_epg)
Esempio n. 33
0
def add_vmm_domain_association(modir, tenant_name, application, epg,
                               vmm_domain):
    fv_rsdomatt = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' +
                                   application + '/epg-' + epg +
                                   '/rsdomAtt-[uni/vmmp-VMware/dom-' +
                                   vmm_domain + ']')
    if isinstance(fv_rsdomatt, RsDomAtt):
        fv_rsdomatt.delete()
    else:
        print 'VMM Domain', vmm_domain, ' has been added to EPG', epg
        return

    print toXMLStr(fv_rsdomatt, prettyPrint=True)
    commit_change(modir, fv_rsdomatt)
Esempio n. 34
0
def delete_vlan_pool(modir, vlan_name, allocation_mode):

    # Query to the VLAN pool.
    fvns_vlaninstp = modir.lookupByDn('uni/infra/vlanns-' + vlan_name + '-' + allocation_mode)

    if isinstance(fvns_vlaninstp, VlanInstP):
        # delete the VLAN
        fvns_vlaninstp.delete()
    else:
        print 'There is no VLAN', vlan_name, '(', allocation_mode, ').'
        return

    print toXMLStr(fvns_vlaninstp, prettyPrint=True)
    commit_change(modir, fvns_vlaninstp)
Esempio n. 35
0
def delete_vlan_pool(modir, vlan_name, allocation_mode):

    # Query to the VLAN pool.
    fvns_vlaninstp = modir.lookupByDn('uni/infra/vlanns-' + vlan_name + '-' +
                                      allocation_mode)

    if isinstance(fvns_vlaninstp, VlanInstP):
        # delete the VLAN
        fvns_vlaninstp.delete()
    else:
        print 'There is no VLAN', vlan_name, '(', allocation_mode, ').'
        return

    print toXMLStr(fvns_vlaninstp, prettyPrint=True)
    commit_change(modir, fvns_vlaninstp)
Esempio n. 36
0
def create_application(modir, tenant_name, application_name, **args):
    """Create an application profile"""

    # Query a tenant
    fv_tenant = modir.lookupByDn('uni/tn-' + tenant_name)
    args = args['args_from_CLI'] if 'args_from_CLI' in args.keys() else args
    if isinstance(fv_tenant, Tenant):
        # Create the Application profile
        fv_ap = Ap(fv_tenant, application_name,
                   prio=get_value(args, 'prio', 'unspecified').lower())
    else:
        print 'Tenant', tenant_name, 'does not exist. Please create a tenant first.'
        return

    print toXMLStr(fv_tenant, prettyPrint=True)
    commit_change(modir, fv_tenant)
 def createVlans(self, tenant, vlantuples, nodes):
     fvTenant, successfulnodes, successfulvlans = self.createEPGasVLANTenant(tenant, vlantuples, nodes)
     c = cobra.mit.request.ConfigRequest()
     logging.debug(toXMLStr(fvTenant))
     c.addMo(fvTenant)
     self.md.commit(c)
     return fvTenant, successfulnodes, successfulvlans
Esempio n. 38
0
    def _create_l3_out(self):

        logger1.warning('Provisining L3Out {0}'.format(self._config.object_name['l3out']))

        topMo = cobra.model.pol.Uni('')
        fvTenant = cobra.model.fv.Tenant(topMo, name='infra')
        l3extOut = cobra.model.l3ext.Out(fvTenant, name=self._config.object_name['l3out'])
        bgpExtP = cobra.model.bgp.ExtP(l3extOut, nameAlias='', descr='')
        ospfExtP = cobra.model.ospf.ExtP(l3extOut, areaCtrl='redistribute,summary', areaId=self._config.ospf['area_id'], areaType=self._config.ospf['type'], areaCost='1')
        l3extRsEctx = cobra.model.l3ext.RsEctx(l3extOut, tnFvCtxName='overlay-1')
        l3extRsL3DomAtt = cobra.model.l3ext.RsL3DomAtt(l3extOut, tDn='uni/l3dom-' + self._config.object_name['domain'])
        l3extLNodeP = cobra.model.l3ext.LNodeP(l3extOut, name='Spines')

        l3extLIfP = cobra.model.l3ext.LIfP(l3extLNodeP, name='SpinesInterfaces')
        ospfIfP = cobra.model.ospf.IfP(l3extLIfP, authKeyId='1', authType='none')
        ospfIfPol = cobra.model.ospf.IfPol(fvTenant, nwT='p2p', name=self._config.object_name['ospf'], ctrl='advert-subnet,bfd')
        ospfRsIfPol = cobra.model.ospf.RsIfPol(ospfIfP, tnOspfIfPolName=self._config.object_name['ospf'])

        for pod in self._config.pod_list:
            id = pod['id']

            for spine in pod['spines']:
                tDn = 'topology/pod-' + str(id) + '/node-' + str(spine['id'])
                l3extRsNodeL3OutAtt = cobra.model.l3ext.RsNodeL3OutAtt(l3extLNodeP, rtrIdLoopBack='yes', rtrId=spine['rid'], tDn=tDn)

                for intf in spine['intf']:
                    tDn = 'topology/pod-' + str(id) + '/paths-' + str(spine['id']) + '/pathep-[eth' + str(intf['id'] + ']')
                    l3extRsPathL3OutAtt = cobra.model.l3ext.RsPathL3OutAtt(l3extLIfP, ifInstT='sub-interface', addr=intf['ip'], encap='vlan-4', tDn=tDn)

        l3extInstP = cobra.model.l3ext.InstP(l3extOut, matchT='AtleastOne', name=self._config.object_name['l3out_external_net'])

        logger1.debug(toXMLStr(topMo))
        c = cobra.mit.request.ConfigRequest()
        c.addMo(topMo)
        self._md.commit(c)
Esempio n. 39
0
    def _create_spine(self):

        existing_spine_id = [int(id.nodeId) for id in self._md.lookupByClass('fabricNodeIdentP')]

        for pod in self._config.pod_list:
            id = pod['id']

            for spine in pod['spines']:
                if spine['id'] in existing_spine_id:
                    logger1.warning('Spine {0} already exist. Skipping...'.format(spine['id']))

                elif 'sn' not in spine.keys():
                    logger1.warning('No SN provided for spine {0}. Skipping...'.format(spine['id']))

                else:
                    logger1.warning('Creating new spine with id {0}'.format(spine['id']))

                    polUni = cobra.model.pol.Uni('')
                    ctrlrInst = cobra.model.ctrlr.Inst(polUni)
                    fabricNodeIdentPol = cobra.model.fabric.NodeIdentPol(ctrlrInst)
                    fabricNodeIdentP = cobra.model.fabric.NodeIdentP(fabricNodeIdentPol, podId=id, nodeId=spine['id'],
                                                                     name=spine['name'], role='spine', serial=spine['sn'])

                    logger1.debug(toXMLStr(fabricNodeIdentPol))
                    c = cobra.mit.request.ConfigRequest()
                    c.addMo(fabricNodeIdentPol)
                    self._md.commit(c)
Esempio n. 40
0
def main():
    parser = ArgumentParser('Generate cobra SDK code from APIC')
    parser.add_argument('-a', '--apic', help='Target APIC', required=True)
    parser.add_argument('-p', '--port', help='APIC Port', default=80)
    parser.add_argument('-u', '--user', help='APIC Username', default='admin')
    parser.add_argument('-pw',
                        '--password',
                        help='APIC Password',
                        required=True)
    parser.add_argument('-d', '--dn', help='DN to query', required=True)
    args = parser.parse_args()

    ep = cobra.mit.access.EndPoint(args.apic,
                                   secure=False if args.port == 80 else True,
                                   port=args.port)
    ls = cobra.mit.session.LoginSession(args.user, args.password)
    md = cobra.mit.access.MoDirectory(ep, ls)
    md.login()

    mo = lookupSubtreeByDn(md, args.dn)
    if mo:
        xmlstr = toXMLStr(mo)
        print arya().getpython(xmlstr=xmlstr,
                               apicip=args.apic,
                               apicuser=args.user,
                               apicpassword=args.password)
    else:
        print 'Nothing found for DN {0}'.format(args.dn)
Esempio n. 41
0
    def test_deletetenant(self, moDir, apic, tenantname, getResponseMock):

        tenantname = tenantname[0]
        if apic[0] == 'http://mock':
            getResponseMock.start()
        tenant = moDir.lookupByDn('uni/tn-{0}'.format(tenantname))

        tenant.delete()
        configRequest = cobra.mit.request.ConfigRequest()
        configRequest.addMo(tenant)

        logger.debug('commit body {0}'.format(toXMLStr(tenant)))

        mos = moDir.commit(configRequest)
        assert mos == []

        if apic[0] == 'http://mock':
            getResponseMock.reset()
            getResponseMock.add(responses.GET,
                                '{0}/api/mo/uni/tn-{1}.json'.format(
                                    apic[0], tenantname),
                                body=json.dumps({'imdata': []}))

        tenant = moDir.lookupByDn('uni/tn-{0}'.format(tenantname))
        assert not tenant

        if apic[0] == 'http://mock':
            getResponseMock.stop()
Esempio n. 42
0
def create_vcenter_credential(modir, vm_provider, vmm_domain_name,
                              profile_name, username, password):
    vmm_domp = modir.lookupByDn('uni/vmmp-' + vm_provider + '/dom-' +
                                vmm_domain_name)
    if isinstance(vmm_domp, DomP):
        vmm_usraccp = UsrAccP(vmm_domp,
                              profile_name,
                              usr=username,
                              pwd=password)

    else:
        print 'There is no VMM Domain', vmm_domain_name, 'in', vm_provider
        return

    print toXMLStr(vmm_domp, prettyPrint=True)
    commit_change(modir, vmm_domp)
Esempio n. 43
0
    def test_deletetenant(self, moDir, apic, tenantname, getResponseMock):

        tenantname = tenantname[0]
        if apic[0] == 'http://mock':
            getResponseMock.start()
        tenant = moDir.lookupByDn('uni/tn-{0}'.format(tenantname))

        tenant.delete()
        configRequest = cobra.mit.request.ConfigRequest()
        configRequest.addMo(tenant)

        logger.debug('commit body {0}'.format(toXMLStr(tenant)))

        mos = moDir.commit(configRequest)
        assert mos == []

        if apic[0] == 'http://mock':
            getResponseMock.reset()
            getResponseMock.add(responses.GET,
                                '{0}/api/mo/uni/tn-{1}.json'.format(
                                    apic[0], tenantname
                                ),
                                body=json.dumps({'imdata': []})
                                )

        tenant = moDir.lookupByDn('uni/tn-{0}'.format(tenantname))
        assert not tenant

        if apic[0] == 'http://mock':
            getResponseMock.stop()
Esempio n. 44
0
def VMM_Create2():
	polUni = cobra.model.pol.Uni('')
	vmmProvP = cobra.model.vmm.ProvP(polUni, 'VMware')
	vmmDomP = cobra.model.vmm.DomP(vmmProvP, name=u'e7_VMW1_Guest')
	vmmCtrlrP = cobra.model.vmm.CtrlrP(vmmDomP, name=u'e7vmw1vic01', rootContName=u'e7dc1', dvsVersion=u'6.0', hostOrIp=u'e7vmw1vic01.datalinklabs.local')
	vmmRsAcc = cobra.model.vmm.RsAcc(vmmCtrlrP, tDn=u'uni/vmmp-VMware/dom-e7_VMW1_Guest/usracc-e7_VMW1_Guest_Credentials')
	infraRsVlanNs = cobra.model.infra.RsVlanNs(vmmDomP, tDn=u'uni/infra/vlanns-[VMW1_Data_Vlan_pool]-dynamic')
	vmmUsrAccP = cobra.model.vmm.UsrAccP(vmmDomP, name=u'e7_VMW1_Guest_Credentials', pwd=u'D@talink1', usr=u'*****@*****.**')
	vmmVSwitchPolicyCont = cobra.model.vmm.VSwitchPolicyCont(vmmDomP)
	
	
	# commit the generated code to APIC
	print toXMLStr(vmmProvP)
	c = cobra.mit.request.ConfigRequest()
	c.addMo(vmmProvP)
	md.commit(c)
Esempio n. 45
0
def domain_rpi(cobra_md):
    # the top level object on which operations will be made
    # Confirm the dn below is for your top dn
    topDn = cobra.mit.naming.Dn.fromString('uni/phys-RPIs')
    topParentDn = topDn.getParent()
    topMo = cobra_md.lookupByDn(topParentDn)

    # build the request using cobra syntax
    physDomP = cobra.model.phys.DomP(topMo, ownerKey=u'', name=u'RPIs', ownerTag=u'')
    infraRsVlanNs = cobra.model.infra.RsVlanNs(physDomP, tDn=u'uni/infra/vlanns-[RaspberryPI_Server]-static')


    # commit the generated code to APIC
    print toXMLStr(topMo)
    c = cobra.mit.request.ConfigRequest()
    c.addMo(physDomP)
    cobra_md.commit(c)
Esempio n. 46
0
 def test_classquery_verifyxml(self, moDir):
     """
     verify that the XML returned by lookupByClass is valid
     """
     commonTn = moDir.lookupByClass("fvTenant", propFilter='eq(fvTenant.name, "common")')
     commonTn = commonTn[0]
     xml = ET.fromstring(toXMLStr(commonTn))
     assert xml.tag == "fvTenant"
Esempio n. 47
0
def create_application(modir, tenant_name, application_name, **args):
    """Create an application profile"""

    # Query a tenant
    fv_tenant = modir.lookupByDn('uni/tn-' + tenant_name)
    args = args['args_from_CLI'] if 'args_from_CLI' in args.keys() else args
    if isinstance(fv_tenant, Tenant):
        # Create the Application profile
        fv_ap = Ap(fv_tenant,
                   application_name,
                   prio=get_value(args, 'prio', 'unspecified').lower())
    else:
        print 'Tenant', tenant_name, 'does not exist. Please create a tenant first.'
        return

    print toXMLStr(fv_tenant, prettyPrint=True)
    commit_change(modir, fv_tenant)
Esempio n. 48
0
def delete_application_epg(modir, tenant_name, application_name, epg_name):
    """Delete an EPG"""
    fv_ap = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' + application_name)
    if isinstance(fv_ap, Ap):
        fv_aepg = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' + application_name + '/epg-' + epg_name)
        if isinstance(fv_aepg, AEPg):
            fv_aepg.delete()
        else:
            print 'There is no application epg called', epg_name, 'in application', application_name, '.'
            return
    else:
        print 'There is no application called', application_name, 'in tenant', tenant_name, '.'
        return

    print toXMLStr(fv_aepg, prettyPrint=True)

    commit_change(modir, fv_aepg)
 def createVlans(self, tenant, vlantuples, nodes):
     fvTenant, successfulnodes, successfulvlans = self.createEPGasVLANTenant(
         tenant, vlantuples, nodes)
     c = cobra.mit.request.ConfigRequest()
     logging.debug(toXMLStr(fvTenant))
     c.addMo(fvTenant)
     self.md.commit(c)
     return fvTenant, successfulnodes, successfulvlans
Esempio n. 50
0
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)
Esempio n. 51
0
 def test_classquery_verifyxml(self, moDir):
     """
     verify that the XML returned by lookupByClass is valid
     """
     commonTn = moDir.lookupByClass(
         'fvTenant', propFilter='eq(fvTenant.name, "common")')
     commonTn = commonTn[0]
     xml = ET.fromstring(toXMLStr(commonTn))
     assert xml.tag == 'fvTenant'
Esempio n. 52
0
def addten(tenname):
        ls = cobra.mit.session.LoginSession('https://172.31.216.24', 'admin', 'scotch123')
        md = cobra.mit.access.MoDirectory(ls)
        md.login()

# the top level object on which operations will be made
        polUni = cobra.model.pol.Uni('')

# build the request using cobra syntax
        
        fvTenant = cobra.model.fv.Tenant(polUni, ownerKey=u'', name=tenname, descr=u'', ownerTag=u'')
        fvRsTenantMonPol = cobra.model.fv.RsTenantMonPol(fvTenant, tnMonEPGPolName=u'')


# commit the generated code to APIC
        print toXMLStr(polUni)
        c = cobra.mit.request.ConfigRequest()
        c.addMo(fvTenant)
        md.commit(c)
Esempio n. 53
0
def VMM_Create(vswitch_name, vcenter_name, vdc_name, dvs_version, vlan_pool, vcenter_un, vcenter_pw):
	# the top level object on which operations will be made
	polUni = cobra.model.pol.Uni('')
	vmmProvP = cobra.model.vmm.ProvP(polUni, 'VMware')

	# build the request using cobra syntax
	vmmDomP = cobra.model.vmm.DomP(vmmProvP, name=vswitch_name)
	vmmCtrlrP = cobra.model.vmm.CtrlrP(vmmDomP, name=vcenter_name, rootContName=vdc_name, dvsVersion=dvs_version, hostOrIp=vcenter_name + '.datalinklabs.local')
	vmmRsAcc = cobra.model.vmm.RsAcc(vmmCtrlrP, tDn=u'uni/vmmp-VMware/dom-'+vswitch_name+'/usracc-'+vswitch_name+'_Credentials')
	infraRsVlanNs = cobra.model.infra.RsVlanNs(vmmDomP, tDn=u'uni/infra/vlanns-['+vlan_pool+']-dynamic')
	vmmUsrAccP = cobra.model.vmm.UsrAccP(vmmDomP, name=vswitch_name+'_Credentials', pwd=vcenter_pw, usr=vcenter_un)
	vmmVSwitchPolicyCont = cobra.model.vmm.VSwitchPolicyCont(vmmDomP)


	# commit the generated code to APIC
	print toXMLStr(vmmProvP)
	c = cobra.mit.request.ConfigRequest()
	c.addMo(vmmProvP)
	md.commit(c)
Esempio n. 54
0
def create_application_epg(modir, tenant_name, application_name, epg_name, **args):
    """Create an EPG"""

    # Query the application
    fv_ap = modir.lookupByDn('uni/tn-' + tenant_name + '/ap-' + application_name)
    args = args['args_from_CLI'] if 'args_from_CLI' in args.keys() else args
    if isinstance(fv_ap, Ap):
        # Create an EPG
        fv_aepg = AEPg(fv_ap, epg_name,
                       prio=get_value(args, 'prio', 'unspecified').lower())

        # Provide bridge_domain to the EPG.
        if 'bridge_domain' in args.keys():
            fv_rsbd = RsBd(fv_aepg, tnFvBDName=args['bridge_domain'])
    else:
        print 'There is no application', application_name, 'in tenant', tenant_name, '. Please create an application.'
        return

    print toXMLStr(fv_ap, prettyPrint=True)
    commit_change(modir, fv_ap)
Esempio n. 55
0
    def test_generate_from_apic(self, moDir, dn, codec):
        mo = lookupSubtreeByDn(moDir, dn)
        assert mo
        if codec == 'xml':
            instr = toXMLStr(mo, includeAllProps=True)
            print instr
            pycode = arya.arya().getpython(xmlstr=instr)
        elif codec == 'json':
            instr = toJSONStr(mo, includeAllProps=True)
            print instr
            pycode = arya.arya().getpython(jsonstr=instr)

        assert pycode