Skip to content

Commit 5329d89

Browse files
committed
Add sample: axl_add_sip_trunk.py
1 parent 79fd5fd commit 5329d89

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,6 @@ The concepts and techniques shown can be extended to enable automated management
8989
9090
* `axl_FAC.py` - Adds a new FAC, updates it, then deletes it (`<addFacInfo>`, `<updateFacInfo>`, `<removeFacInfo>`)
9191
92+
* `axl_add_sip_trunk.py` - Adds a new SIP trunk with destination address, then deletes it (`<addSipTrunk`, `<removeSipTrunk>`)
93+
9294
[![published](https://static.production.devnetcloud.com/codeexchange/assets/images/devnet-published.svg)](https://developer.cisco.com/codeexchange/github/repo/CiscoDevNet/axl-python-zeep-sample)

axl_add_sip_trunk.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
"""AXL <addSipTrunk> sample script, using the Zeep SOAP library
2+
3+
Install Python 2.7 or 3.7
4+
On Windows, choose the option to add to PATH environment variable
5+
6+
If this is a fresh installation, update pip (you may need to use `pip3` on Linux or Mac)
7+
8+
$ python -m pip install --upgrade pip
9+
10+
Script Dependencies:
11+
lxml
12+
requests
13+
zeep
14+
15+
Dependency Installation:
16+
17+
$ pip install zeep
18+
19+
This will install automatically all of zeep dependencies, including lxml, requests
20+
21+
Copyright (c) 2018 Cisco and/or its affiliates.
22+
Permission is hereby granted, free of charge, to any person obtaining a copy
23+
of this software and associated documentation files (the "Software"), to deal
24+
in the Software without restriction, including without limitation the rights
25+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26+
copies of the Software, and to permit persons to whom the Software is
27+
furnished to do so, subject to the following conditions:
28+
The above copyright notice and this permission notice shall be included in all
29+
copies or substantial portions of the Software.
30+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
SOFTWARE.
37+
"""
38+
39+
from lxml import etree
40+
from requests import Session
41+
from requests.auth import HTTPBasicAuth
42+
43+
from zeep import Client, Settings, Plugin
44+
from zeep.transports import Transport
45+
from zeep.exceptions import Fault
46+
47+
# Configure CUCM location and AXL credentials in creds.py
48+
import creds
49+
50+
# Change to true to enable output of request/response headers and XML
51+
DEBUG = False
52+
53+
# The WSDL is a local file in the working directory, see README
54+
WSDL_FILE = 'AXLAPI.wsdl'
55+
56+
# This class lets you view the incoming and outgoing http headers and XML
57+
58+
class MyLoggingPlugin(Plugin):
59+
60+
def egress(self, envelope, http_headers, operation, binding_options):
61+
print(
62+
'''Request
63+
-------
64+
Headers:
65+
{headers}
66+
67+
Body:
68+
{xml}
69+
70+
'''.format( headers = http_headers,
71+
xml = etree.tostring( envelope, pretty_print = True, encoding = 'unicode') )
72+
)
73+
74+
def ingress( self, envelope, http_headers, operation ):
75+
print('\n')
76+
print(
77+
'''Response
78+
-------
79+
Headers:
80+
{headers}
81+
82+
Body:
83+
{xml}
84+
85+
'''.format( headers = http_headers,
86+
xml = etree.tostring( envelope, pretty_print = True, encoding = 'unicode') )
87+
)
88+
89+
# The first step is to create a SOAP client session
90+
91+
session = Session()
92+
93+
# We avoid certificate verification by default
94+
95+
session.verify = False
96+
97+
# To enabled SSL cert checking (recommended for production)
98+
# place the CUCM Tomcat cert .pem file in the root of the project
99+
# and uncomment the two lines below
100+
101+
# CERT = 'changeme.pem'
102+
# session.verify = CERT
103+
104+
session.auth = HTTPBasicAuth(creds.AXL_USERNAME, creds.AXL_PASSWORD)
105+
106+
transport = Transport( session = session, timeout = 10 )
107+
108+
# strict=False is not always necessary, but it allows zeep to parse imperfect XML
109+
settings = Settings( strict = False, xml_huge_tree = True )
110+
111+
plugin = [ MyLoggingPlugin() ] if DEBUG else [ ]
112+
113+
client = Client( WSDL_FILE, settings = settings, transport = transport,
114+
plugins = plugin )
115+
116+
service = client.create_service( '{http://www.cisco.com/AXLAPIService/}AXLAPIBinding',
117+
'https://{cucm}:8443/axl/'.format( cucm = creds.CUCM_ADDRESS ))
118+
119+
# Add SIP trunk
120+
121+
sip_trunk_data = {
122+
'name': 'testSipTrunk',
123+
'description': 'testDescription',
124+
'product': 'SIP Trunk',
125+
'class': 'Trunk',
126+
'protocol': 'SIP',
127+
'protocolSide': 'Network',
128+
'devicePoolName': 'Default',
129+
'locationName': 'Hub_None',
130+
'securityProfileName': 'Non Secure SIP Trunk Profile',
131+
'sipProfileName': 'Standard SIP Profile',
132+
'presenceGroupName': 'Standard Presence group',
133+
'callingAndCalledPartyInfoFormat': 'Deliver DN only in connected party',
134+
'destinations': [ ],
135+
}
136+
137+
sip_trunk_data['destinations'].append( { 'destination':
138+
{ 'addressIpv4': '1.1.1.1', 'port': '5060', 'sortOrder': 1 }
139+
} )
140+
141+
try:
142+
resp = service.addSipTrunk( sip_trunk_data )
143+
except Fault as err:
144+
print('Zeep error: addSipTrunk: {err}'.format( err = err))
145+
else:
146+
print('addSipTrunk response:')
147+
print(resp)
148+
149+
input( 'Press Enter to continue...')
150+
151+
# Delete FAC
152+
try:
153+
resp = service.removeSipTrunk( name = 'testSipTrunk' )
154+
except Fault as err:
155+
print('Zeep error: removeSipTrunk: {err}'.format( err = err))
156+
else:
157+
print('removeSipTrunk response:')
158+
print(resp)

0 commit comments

Comments
 (0)