Cisco CUCM AXL Python3 – Adding Lists Of Non Contiguous Extensions To CUCM

By 13th October 2017AXL, Cisco, CUCM

The following script was written for a client who had a large number of non contiguous extensions that they needed adding to CUCM. You could of course bulk import the lines attached to dummy devices but you would have to produce an all fields input file and or template before hand. It is much simpler and quicker via the API.

Complete Code
 
  1. __author__ = "Mitch Dawson"
  2. __contact__ = "info@ucdevops.com"
  3. __copyright__ = "Copyright (C) 2017 - 2018 Ucdevops All Rights Reserved"
  4. __license__ = "GPLv3"
  5. """
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see http://www.gnu.org/licenses.
  16. """
  17. from suds.client import Client
  18. from suds.xsd.doctor import ImportDoctor
  19. from suds.xsd.doctor import Import
  20. from suds.cache import NoCache
  21. tns = 'http://www.cisco.com/AXLAPIService/'
  22. imp = Import('http://schemas.xmlsoap.org/soap/encoding/',
  23.              'http://schemas.xmlsoap.org/soap/encoding/')
  24. imp.filter.add(tns)
  25. # Define Authentication Credentials for Cluster
  26. username = 'username'
  27. password = 'password'
  28. # Define AXL URL for Cluster
  29. url = 'https://CUCM:8443/axl/'
  30. # Define WSDL Location
  31. wsdl = 'file:///C:/PathTo/AXLAPI.wsdl'
  32. # Create Suds Client Object
  33. client = Client(location=url, url=wsdl, faults=False, username=username,
  34.                 password=password, plugins=[ImportDoctor(imp)],cache=NoCache()
  35. )
  36. # Define our line object creation function
  37. def lineObject(line):
  38.     """
  39.     Function returns a dictionary object of line attributes and values
  40.     """
  41.     # Create an empty dictionary
  42.     lineObject = {}
  43.     # Create a variable to be used to populate the description,
  44.     # alertingName and asciiAlertingName variables
  45.     description = 'Spare Denver Ext 12.10.17'
  46.     # Format the line value, in this example we prepend '\+' as
  47.     # e164 format is required
  48.     lineObject['pattern'] = '\+' + line
  49.     # Set Urgent Priority
  50.     lineObject['patternUrgency'] = 't'
  51.     # Assign the description variable above
  52.     lineObject['description'] = description
  53.     lineObject['alertingName'] = description
  54.     lineObject['asciiAlertingName'] = description
  55.     # Assign the partition of where the line should reside
  56.     lineObject['routePartitionName'] = 'PAR_RESOURCES'
  57.     # Assign the required Line CSS
  58.     lineObject['shareLineAppearanceCssName'] = 'CSS_US_DEN_839_LINE_IN'
  59.     # Create Call Forward settings object
  60.     cFwdNoVMNoDest = {
  61.         'forwardToVoiceMail': 'f',
  62.         'callingSearchSpaceName': 'CSS_US_DEN_839_LINE_N',
  63.         'destination': ''
  64.     }
  65.     # Create callForward object values
  66.     lineObject['callForwardAll'] = cFwdNoVMNoDest
  67.     lineObject['callForwardBusy'] = cFwdNoVMNoDest
  68.     lineObject['callForwardBusyInt'] = cFwdNoVMNoDest
  69.     lineObject['callForwardNoAnswer'] = cFwdNoVMNoDest
  70.     lineObject['callForwardNoAnswerInt'] = cFwdNoVMNoDest
  71.     lineObject['callForwardNoCoverage'] = cFwdNoVMNoDest
  72.     lineObject['callForwardNoCoverageInt'] = cFwdNoVMNoDest
  73.     lineObject['callForwardOnFailure'] = cFwdNoVMNoDest
  74.     lineObject['callForwardNotRegistered'] = cFwdNoVMNoDest
  75.     lineObject['callForwardNotRegisteredInt'] = cFwdNoVMNoDest
  76.     # Return the lineObject
  77.     return lineObject
  78. # The input list consists of a single column of values in a text file
  79. # Open the input list of numbers, Read it then split on new line
  80. file = open('lines.txt', 'r').read().split('\n')
  81. # Loop through the lines
  82. for line in file:
  83.     # Create a new line object
  84.     lineObj = lineObject(line)
  85.     # Post the new line object to the AXL API
  86.     x = client.service.addLine(lineObj)
  87.     # If the request was successful,
  88.     # the response will return a tuple with status code 200 as the first element
  89.     if x[0] == 200:
  90.         print(
  91.             'Sucessfully added Line {}'.format(lineObj['pattern'])
  92.         )
  93.     else:
  94.         # Anything other than 200 indicates an error
  95.         print(
  96.             'Error adding line {}'.format(lineObj['pattern'])
  97.         )
  98.         print(x[1])
Mitch

Author Mitch

I am an independent IT Consultant with specialities in Software Development, Enterprise Unified Communication, Network and Security platforms. In addition to my day to day work, I develop bespoke applications and I hope that through ucdevops.com I can build relationships with clients, business partners and fellow engineers by providing solutions to complex problems through the use of programming.

More posts by Mitch

Leave a Reply

"