読者です 読者をやめる 読者になる 読者になる

らくがきちょう

なんとなく

Cisco ACI で L3Out / ExtEpg 設定用 XML を出力する Python スクリプトサンプル

以前に Cisco ACI で Contract 設定用 XML を出力する Python スクリプトサンプルというメモを書きました。 ついでに L3out や ExtEpg を設定するスクリプトを書いたので、(役に立つか?は置いておいて…)メモしておきます。

l3out-builder.py

#!/usr/bin/env python

"""Usage:
l3out-builder.py [-t <TENANT>] <INPUTFILE>
l3out-builder.py -h | --help

options:
    <INPUTFILE>              Specify input xml file.
    -h, --help               Show this help message and exit.
    -t, --tenant <TENANT>    Specify tenant name [default: Tenant-1].
"""

from docopt import docopt
import xml.dom.minidom as minidom
import xml.etree.ElementTree as ET

if __name__ == '__main__':
    args = docopt(__doc__)
    inputfile = open(args.get('<INPUTFILE>'))
    line = inputfile.readline()

    imdata = ET.Element('imdata')
    imdata.set('totalCount', '1')

    fvTenant = ET.SubElement(imdata, 'fvTenant')
    fvTenant.set('descr', '')
    fvTenant.set('dn', 'uni/tn-' + args['--tenant'])
    fvTenant.set('name', args['--tenant'])
    fvTenant.set('ownerKey', '')
    fvTenant.set('ownerTag', '')

    while line:
        l3out_name = line.rstrip()

        l3extOut = ET.SubElement(fvTenant, 'l3extOut')
        l3extOut.set('descr', '')
        l3extOut.set('dn', 'uni/tn-' + args['--tenant'] + '/out-' + l3out_name)
        l3extOut.set('enforceRtctrl', 'export')
        l3extOut.set('name', l3out_name)
        l3extOut.set('nameAlias', '')
        l3extOut.set('ownerKey', '')
        l3extOut.set('ownerTag', '')
        l3extOut.set('targetDscp', 'unspecified')

        l3extRsEctx = ET.SubElement(l3extOut, 'l3extRsEctx')
        l3extRsEctx.set('tnFvCtxName', '')

        line = inputfile.readline()
    inputfile.close()
    string = ET.tostring(imdata, 'utf-8')
    pretty_string = minidom.parseString(string).toprettyxml(indent='  ')
    print(pretty_string)

extepg-builder.py

#!/usr/bin/env python

"""Usage:
extepg-builder.py [[-l <L3OUT>] [-t <TENANT>]] <INPUTFILE>
extepg-builder.py -h | --help

options:
    <INPUTFILE>              Specify input xml file.
    -h, --help               Show this help message and exit.
    -l, --l3out <L3OUT>      Specify L3Out name [default: L3out-1000].
    -t, --tenant <TENANT>    Specify tenant name [default: Tenant-1].
"""

from docopt import docopt
import xml.dom.minidom as minidom
import xml.etree.ElementTree as ET

if __name__ == '__main__':
    args = docopt(__doc__)
    inputfile = open(args.get('<INPUTFILE>'))
    line = inputfile.readline()

    imdata = ET.Element('imdata')
    imdata.set('totalCount', '1')

    fvTenant = ET.SubElement(imdata, 'fvTenant')
    fvTenant.set('descr', '')
    fvTenant.set('dn', 'uni/tn-' + args['--tenant'])
    fvTenant.set('name', args['--tenant'])
    fvTenant.set('ownerKey', '')
    fvTenant.set('ownerTag', '')

    l3out_name = args['--l3out']
    l3extOut = ET.SubElement(fvTenant, 'l3extOut')
    l3extOut.set('descr', '')
    l3extOut.set('dn', 'uni/tn-' + args['--tenant'] + '/out-' + l3out_name)
    l3extOut.set('enforceRtctrl', 'export')
    l3extOut.set('name', l3out_name)
    l3extOut.set('nameAlias', '')
    l3extOut.set('ownerKey', '')
    l3extOut.set('ownerTag', '')
    l3extOut.set('targetDscp', 'unspecified')

    l3extRsEctx = ET.SubElement(l3extOut, 'l3extRsEctx')
    l3extRsEctx.set('tnFvCtxName', '')

    while line:
        extepg = line.rstrip()

        l3extInstP = ET.SubElement(l3extOut, 'l3extInstP')
        l3extInstP.set('descr', '')
        l3extInstP.set('dn', 'uni/tn-' + args['--tenant'] + '/out-' + l3out_name + '/instP-' + extepg)
        l3extInstP.set('matchT', 'AtleastOne')
        l3extInstP.set('name', extepg)
        l3extInstP.set('nameAlias', '')
        l3extInstP.set('prefGrMemb', 'exclude')
        l3extInstP.set('prio', 'unspecified')
        l3extInstP.set('targetDscp', 'unspecified')

        fvRsCustQosPol = ET.SubElement(l3extInstP, 'fvRsCustQosPol')
        fvRsCustQosPol.set('tnQosCustomPolName', '')

        line = inputfile.readline()
    inputfile.close()
    string = ET.tostring(imdata, 'utf-8')
    pretty_string = minidom.parseString(string).toprettyxml(indent='  ')
    print(pretty_string)