This is a sample of how to add and / or delete contacts in the Unity Connection database using the CUPI interface.
This was created using Python 3.6.2 and Cisco Unity Connection 12.5.
This script was created after finding Extension was a required field in the Unity Connection BAT process and needs to be unique, creating challenges in environments where single DNs may need multiple names associated with them.
Ultimately I found the Extension field in the BAT file can simply be left blank.
With that, I would suggest using the built in Unity Connection BAT process to manage contacts when possible.
This script was created after finding Extension was a required field in the Unity Connection BAT process and needs to be unique, creating challenges in environments where single DNs may need multiple names associated with them.
Ultimately I found the Extension field in the BAT file can simply be left blank.
With that, I would suggest using the built in Unity Connection BAT process to manage contacts when possible.
This sample assumes your BAT file columns will be Alias, DisplayName, FirstName, LastName, TransferEnabled, TransferExtension and saved in UTF-8 format without BOM.
You can find my Github repository here: https://github.com/raymaslanka/Cisco-UCXN-CUPI-add-delete-contacts, or gist here:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import getpass | |
import csv | |
import requests | |
from requests import Session | |
from requests.auth import HTTPBasicAuth | |
import xml.etree.ElementTree as ET | |
import sys | |
def get_CUC_version(CUC, session): | |
# Using CUC version query to simply test user authentication. We don't really need the version info for anything. | |
request_data = session.get('https://' + CUC +'/vmrest/version/', timeout=2, stream=True) | |
return(request_data.status_code) | |
def insert_contact(CUC, template, row, session, rowNum): | |
if rowNum == 0: | |
# Basic check to see if CSV columns are likely accurate | |
if (row[0],row[1],row[2],row[3],row[4],row[5]) != ('Alias', 'DisplayName', 'FirstName', 'LastName', 'TransferEnabled', 'TransferExtension'): | |
print('There may be a problem with your column order:') | |
print('Correct names and order: [\'Alias\', \'DisplayName\', \'FirstName\', \'LastName\', \'TransferEnabled\', \'TransferExtension\']') | |
print('Your names and order: ', row) | |
sys.exit(0) | |
else: | |
print ('CSV required columns look accurate.') | |
print ('') | |
else: | |
print('Inserting CSV file row number:',rowNum + 1) | |
xml = make_XML(row) | |
headers = {'Content-Type':'application/xml'} | |
try: | |
request_data = session.post('https://' + CUC +'/vmrest/contacts?templateAlias=' + template + '', headers=headers, data=xml, timeout=2) | |
print ('Insert response:',request_data) | |
print ('') | |
except requests.exceptions.RequestException as e: | |
print('Something bad happened inserting:') | |
print(e) | |
return | |
def delete_contact(CUC, template, alias, session,rowNum): | |
headers = {'Content-Type':'application/xml'} | |
try: | |
print('Getting CUC ObjectId using CSV Alias ' + alias + ' in row number:',rowNum + 1) | |
request_data = session.get('https://' + CUC +'/vmrest/contacts?query=(alias%20is%20' + alias + ')', headers=headers, timeout=2, stream=True) | |
tree = ET.fromstring(request_data.text) | |
count = tree.findall('Contact') | |
if len(count) > 0: | |
print ('Found ' + str(len(count)) + ' contact with that alias.') | |
for contact in tree.findall('Contact'): | |
aliastext = contact.find('Alias').text | |
objectidtext = contact.find('ObjectId').text | |
print('Deleting CUC contact ' + aliastext + ' with ObjectId ' + objectidtext) | |
objectid = contact.find('ObjectId').text | |
request_data = session.delete('https://' + CUC +'/vmrest/contacts/' + objectid + '', headers=headers, timeout=2, stream=True) | |
print ('Delete response:',request_data) | |
print ('') | |
else: | |
print ('Found ' + str(len(count)) + ' contacts with that alias. Skipping...') | |
print('') | |
except requests.exceptions.RequestException as e: | |
print('Something bad happened deleting') | |
print(e) | |
return | |
def make_XML(row): | |
print('Creating XML from:',row) | |
xml = '<Contact>' | |
xml = xml + '<Alias>' + row[0] + '</Alias>' | |
xml = xml + '<DisplayName>' + row[1] + '</DisplayName>' | |
xml = xml + '<FirstName>' + row[2] + '</FirstName>' | |
xml = xml + '<LastName>' + row[3] + '</LastName>' | |
xml = xml + '<TransferEnabled>' + row[4] + '</TransferEnabled>' | |
xml = xml + '<TransferExtension>' + row[5] + '</TransferExtension>' | |
xml = xml + '</Contact>' | |
return(xml) | |
def main(): | |
csvFile = 'YOUR BAT FILENAME HERE' | |
CUC = 'YOUR UNITY CONNECTION SERVER FQDN / IP ADDRESS HERE' | |
template = 'YOUR CONTACT BAT TEMPLATE ALIAS HERE' | |
username = input('UCxN username: ') | |
password = getpass.getpass('UCxN password:') | |
adddelete = input('[A]dd / [D]elete: ') | |
print ('') | |
session = Session() | |
session.verify = False | |
session.auth = HTTPBasicAuth(username, password) | |
if get_CUC_version(CUC,session) != 200: | |
print ('Authentication Failed.') | |
sys.exit(0) | |
if adddelete == 'A': | |
csvData = csv.reader(open(csvFile)) | |
rowNum = 0 | |
for row in csvData: | |
insert_contact(CUC, template, row, session, rowNum) | |
rowNum +=1 | |
elif adddelete == 'D': | |
csvData = csv.reader(open(csvFile)) | |
rowNum = 0 | |
for row in csvData: | |
if rowNum == 0: | |
print('Skipping column header row 1.') | |
print('') | |
else: | |
delete_contact(CUC, template, row[0], session, rowNum) | |
rowNum +=1 | |
else: | |
print('Invalid Entry') | |
print('') | |
sys.exit(0) | |
if __name__ == '__main__': | |
main() | |
No comments:
Post a Comment