-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathaxl_executeSQLUpdate_Jabber_config.py
More file actions
177 lines (129 loc) · 6.4 KB
/
Copy pathaxl_executeSQLUpdate_Jabber_config.py
File metadata and controls
177 lines (129 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""AXL <executeSQLUpdate>, <executeSQLQuery> sample script, using the Zeep SOAP library
As of CUCM 12.5, <addUcService> and <updateUcService> do not support
defining new services of type 'Jabber', for Jabber configs. This sample
demonstrates provisioning a Jabber config UC Service via <executeSQLUpdate>
operations.
Copyright (c) 2020 Cisco and/or its affiliates.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from lxml import etree
from requests import Session
from requests.auth import HTTPBasicAuth
from zeep import Client, Settings, Plugin
from zeep.transports import Transport
from zeep.exceptions import Fault
import sys
import urllib3
import textwrap
import urllib
# Edit .env file to specify your Webex site/user details
import os
from dotenv import load_dotenv
load_dotenv()
# Change to true to enable output of request/response headers and XML
DEBUG = False
# The WSDL is a local file in the working directory, see README
WSDL_FILE = 'schema/AXLAPI.wsdl'
# This class lets you view the incoming and outgoing http headers and XML
class MyLoggingPlugin( Plugin ):
def egress( self, envelope, http_headers, operation, binding_options ):
# Format the request body as pretty printed XML
xml = etree.tostring( envelope, pretty_print = True, encoding = 'unicode')
print( f'\nRequest\n-------\nHeaders:\n{http_headers}\n\nBody:\n{xml}' )
def ingress( self, envelope, http_headers, operation ):
# Format the response body as pretty printed XML
xml = etree.tostring( envelope, pretty_print = True, encoding = 'unicode')
print( f'\nResponse\n-------\nHeaders:\n{http_headers}\n\nBody:\n{xml}' )
# The first step is to create a SOAP client session
session = Session()
# We avoid certificate verification by default
# And disable insecure request warnings to keep the output clear
session.verify = False
urllib3.disable_warnings( urllib3.exceptions.InsecureRequestWarning )
# To enable SSL cert checking (recommended for production)
# place the CUCM Tomcat cert .pem file in the root of the project
# and uncomment the two lines below
# CERT = 'changeme.pem'
# session.verify = CERT
session.auth = HTTPBasicAuth( os.getenv( 'AXL_USERNAME' ), os.getenv( 'AXL_PASSWORD' ) )
transport = Transport( session = session, timeout = 10 )
# strict=False is not always necessary, but it allows Zeep to parse imperfect XML
settings = Settings( strict = False, xml_huge_tree = True )
# If debug output is requested, add the MyLoggingPlugin callback
plugin = [ MyLoggingPlugin() ] if DEBUG else []
# Create the Zeep client with the specified settings
client = Client( WSDL_FILE, settings = settings, transport = transport,
plugins = plugin )
# Create the Zeep service binding to AXL at the specified CUCM
service = client.create_service( '{http://www.cisco.com/AXLAPIService/}AXLAPIBinding',
f'https://{os.getenv( "CUCM_ADDRESS" )}:8443/axl/' )
# Create an object containing the raw SQL update to execute
# newid() is a built-in stored procedure for generating a new UUID primary key
# tkucproduct=42 is 'Jabber'
# tkreset=2 is 'Restart' (ignored as resettoggle=f)
sql = '''INSERT INTO ucservice ( pkid, name, tkucproduct, tkreset, resettoggle ) VALUES ( newid(), "testJabberConfig", 42, 2, 'f' )'''
# Execute the executeSQLUpdate request
try:
resp = service.executeSQLUpdate( sql )
except Fault as err:
print( f'Zeep error: executeSQLUpdate/insert into ucservice: { err }' )
sys.exit( 1 )
print( '\nexecuteSQLUpdate/insert into ucservice: SUCCESS' )
print( f'Rows updated: { resp[ "return" ][ "rowsUpdated" ] }' )
input( '\nPress Enter to continue...\n' )
# Retrieve the pkid of the new ucservice using executeSqlQuery
sql = 'SELECT pkid from ucservice where name="testJabberConfig"'
try:
resp = service.executeSQLQuery( sql )
except Fault as err:
print( f'Zeep error: executeSqlQuery/select from ucservice: { err }' )
sys.exit( 1 )
# Parse the pkid
ucServiceId = resp[ 'return' ][ 'row' ][ 0 ][ 0 ].text
print( '\nexecuteSqlQuery/select from ucservice response: SUCCESS' )
print( f'UC Service pkid: { ucServiceId }' )
input( '\nPress Enter to continue...\n' )
# CUCM admin appears to URL encode values in the XML
value = urllib.parse.quote_plus( 'dstaudt@ds-cup1251.cisco.com' )
# Define the XML for the Jabber config file
# textwrap.dedent cleans up common leading whitespace from indented multi-line literals
xml = textwrap.dedent( f'''\
<Options>
<AdminConfiguredBot>{ value }</AdminConfiguredBot>
</Options>''')
# Create SQL to add an entry into the ucservicexml table
# newid() is a built-in stored procedure for generating a new UUID primary key
sql = f'''INSERT INTO ucservicexml (pkid,fkucservice,xml)
VALUES ( newid(),"{ ucServiceId }","{ xml }")'''
# Execute the executeSQLUpdate request
try:
resp = service.executeSQLUpdate( sql )
except Fault as err:
print( f'Zeep error: executeSQLUpdate/insert into ucservicexml: { err }' )
sys.exit( 1 )
print( '\nexecuteSQLUpdate/insert into ucservicexml response: SUCCESS' )
print( f'Rows updated: { resp[ "return" ][ "rowsUpdated" ] }' )
input( '\nPress Enter to continue...\n' )
# Cleanup the objects we just created
# Note the database automatically removes records from ucservicexml when the
# related UC Service is removed
try:
resp = service.removeUcService( name = 'testJabberConfig' )
except Fault as err:
print( f'Zeep error: removeUcService: { err }' )
else:
print( '\nremoveUcService response: SUCCESS' )