Search code examples
python-3.xzeepcucmcisco-axl

Creating SOAP request via Zeep with variable number of XML tags


All, I am using Zeep to connect to CUCM to perform bulk AXL transactions. Some of the objects that I need to modify accept a variable number of XML tags. For example:

I want to add an entity(calling search space), which could have a variable number of partitions associated to it. Per the WSDL:


    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/11.5">
       <soapenv:Header/>
       <soapenv:Body>
          <ns:addCss sequence="?">
             <css>
                <!--Optional:-->
                <description>?</description>
                <!--Optional:-->
                <members>
                   <!--Zero or more repetitions:--> <------------------------------------------
                   <member>
                      <routePartitionName uuid="?">?</routePartitionName>
                      <index>?</index>
                   </member>
                </members>
                <!--Optional:-->
                <partitionUsage>General</partitionUsage>
                <name>?</name>
             </css>
          </ns:addCss>
       </soapenv:Body>
    </soapenv:Envelope>

I can easily script a fixed number of members:


    result = service.addCss({
        'name': 'SampleCSS',
        'description': 'Sample CSS Description',
        'members': {
            'member': [
                {'routePartitionName':{
                    '_value_1':None, 'uuid':'{07614566-ADC4-1ABB-3EB3-C08650E11CBE}'},
                    'index':1},
                {'routePartitionName': 'Auto Register','index': 2},
                {'routePartitionName': 'DNS External', 'index':3},
                {'routePartitionName':{
                    '_value_1':'On Cluster', 'uuid':'{F7FF933E-2B63-25DB-11AF-1DDDC8F9A52E}'},
                    'index':4},
            ]}
         })
    print (result)
    print(type(result))
    res = result['return']
    print("\n\n", res)

However, I am having a hard time scripting a variable number of members

I have my members stored on a dictionary:


    CSSes = {
        'Test1': ['Test1','Test1','07614566-ADC4-1ABB-3EB3-C08650E11CBE','Staging',1],
        'Test2': ['Test2','Test2','F7FF933E-2B63-25DB-11AF-1DDDC8F9A52E', 'On Cluster',1]
    }

If I am adding multiple CSS's and each has a different number of partitions(members) how do I accomplish this?

I've only been able to come up with something where every CSS has the same number of partitions assigned to it:

for css in CSSes:
    result = service.addCss({
        'name': CSSes[css][0],
        'description': CSSes[css][1],
        'members': {
            'member': [
                {'routePartitionName':{
                        '_value_1':CSSes[css][3], 'uuid':CSSes[css][1]},
                        'index':CSSes[css][4]}
        ]}
    })

Solution

  • I'm not 100% sure I understand your scenario (mostly based on the snippets provided), but given this data set, where one CSS to be created has 2 partitions and the other has 3:

    CSSs = [
        {
            'name': 'CSS1',
            'description': 'CSS1 description',
            'members': [
                {
                    'name': 'Partition1',
                    'index': 1
                },
                {
                    'name': 'Partition2',
                    'index': 2
                }
            ]
        },
        {
            'name': 'CSS2',
            'description': 'CSS2 description',
            'members': [
                {
                    'name': 'Partition1',
                    'index': 1
                },
                {
                    'name': 'Partition2',
                    'index': 2
                },
                {
                    'name': 'Partition3',
                    'index': 3
                }
            ]
        }
    ]
    

    I was able to get this working by dynamically appending members, like so:

    for css in CSSs:
        css_data = {
            'name': css['name'],
            'description': css['description'],
            'members': {
                'member': []
            }
        }
        for memb in css['members']:
            css_data['members']['member'].append(
                {
                    'routePartitionName': memb['name'],
                    'index': memb['index']
                }
            )
        css_resp = service.addCss(css_data)