-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcupiRestWriter.py
More file actions
executable file
·195 lines (175 loc) · 7.48 KB
/
Copy pathcupiRestWriter.py
File metadata and controls
executable file
·195 lines (175 loc) · 7.48 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3.6
__version__ = '0.4'
__author__ = 'Christopher Phillips'
# import sys
import logging
import requests
import json
from ucAppConfig import cxnAppConfig
import urllib3 # imported to disable the SAN warning for the cert
urllib3.disable_warnings(urllib3.exceptions.SubjectAltNameWarning)
cupiRLogger = logging.getLogger(__name__)
cupiRLogger.setLevel(logging.DEBUG)
infoHandler = logging.FileHandler('cupiInfo.log', mode='w')
infoHandler.setLevel(logging.INFO)
debugHandler = logging.FileHandler('cupiDebug.log', mode='w')
debugHandler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - \
%(message)s')
infoHandler.setFormatter(formatter)
debugHandler.setFormatter(formatter)
cupiRLogger.addHandler(infoHandler)
cupiRLogger.addHandler(debugHandler)
# cupiRLogger.addHandler(logging.StreamHandler(sys.stdout))
cupiRLogger.info("Begin CUPI Writer Info Logging")
cupiRLogger.debug("Begin CUPI Writer Debug Logging")
class cupiRestWriter:
myCxnConfig = ''
__headers = {"Content-Type": "application/json",
"Accept": "application/json"}
__baseUrl = ''
__auth = ''
__verify = ''
_template = 'voicemailusertemplate'
_newUserData = ''
_alias = ''
_extension = ''
def __init__(self, Alias, Extension, FirstName, LastName, EmailAddress,
Template):
cupiRLogger.info("Rest Writer Started")
self._alias = Alias
self._extension = Extension
self._template = Template
self.myCxnConfig = cxnAppConfig('cxn.cfg')
if '!' in FirstName or '!' in LastName:
# Get from AD User
tempFirstName = "Fake"
tempLastName = "User"
else:
tempFirstName = FirstName
tempLastName = LastName
self._newUserData = self.getNewUserJSON(tempFirstName,
tempLastName,
Alias,
EmailAddress,
Extension)
cupiRLogger.debug(self._newUserData)
self.__baseUrl = self.myCxnConfig.getAppApiUrl()
self.__auth = (self.myCxnConfig.getAppUsername(),
self.myCxnConfig.getAppPassword())
self.__verify = self.myCxnConfig.getAppCert()
def getNewUserJSON(self, FirstName, LastName, Alias, EmailAddress,
DtmfAccessId):
DisplayName = FirstName + " " + LastName
SmtpAddress = Alias + "@" + self.myCxnConfig.getAppHost()
return json.dumps({"FirstName": FirstName,
"LastName": LastName,
"Alias": Alias,
"DisplayName": DisplayName,
"EmailAddress": EmailAddress,
"SmtpAddress": SmtpAddress,
"DtmfAccessId": DtmfAccessId
})
def createNewVoicemail(self):
cupiRLogger.info("Create Voicemail Started")
vmCreateUrl = 'users'
url = self.__baseUrl + vmCreateUrl
querystring = {"templateAlias": self._template}
resp = requests.post(url,
auth=self.__auth,
verify=self.__verify,
headers=self.__headers,
data=self._newUserData,
params=querystring)
if resp.status_code != 201:
# This means something went wrong.
raise Exception('POST {0} {1}'.format(url,
resp.status_code))
def getImportUserPkid(self):
cupiRLogger.debug("Get Import PKID")
vmImportUrl = 'import/users/ldap'
url = self.__baseUrl + vmImportUrl
query = {"limit": "1", "query": "(alias is {0})".format(self._alias)}
resp = requests.get(url,
auth=self.__auth,
verify=self.__verify,
headers=self.__headers,
params=query)
if resp.status_code != 200:
# This means something went wrong.
raise Exception('GET {0} {1}'.format(url,
resp.status_code))
data = resp.json()
try:
pkid = data['ImportUser']['pkid']
except KeyError:
return("KeyError: VM will NOT be created")
return pkid
def importNewVoicemail(self):
status = {}
cupiRLogger.info("Import Voicemail Started")
pkid = self.getImportUserPkid()
status.update({"vmLdapUserFound": "Success"})
userData = json.dumps({"dtmfAccessId": self._extension, "pkid": pkid})
vmCreateUrl = 'import/users/ldap'
url = self.__baseUrl + vmCreateUrl
query = {"templateAlias": self._template}
resp = requests.post(url,
auth=self.__auth,
verify=self.__verify,
headers=self.__headers,
data=userData,
params=query)
if resp.status_code != 201:
# This means something went wrong.
raise Exception('POST {0} {1}'.format(url,
resp.status_code))
status.update({"vmLdapUserImported": "Success"})
return json.dumps(status)
def getTemplate(self):
getTemplateUrl = 'usertemplates'
url = self.__baseUrl + getTemplateUrl
resp = requests.post(url,
auth=self.__auth,
verify=self.__verify,
headers=self.__headers)
if resp.status_code != 201:
# This means something went wrong.
raise Exception('POST {0} {1}'.format(url,
resp.status_code))
def getVmObjectId(self):
vmGetUrl = 'users'
url = self.__baseUrl + vmGetUrl
query = {"query": "(alias is {0})".format(self._alias)}
resp = requests.get(url,
auth=self.__auth,
verify=self.__verify,
headers=self.__headers,
params=query)
data = resp.json()
try:
return data['User']['ObjectId']
except KeyError:
return("KeyError: User NOT found VM cannot be created/deleted")
if resp.status_code != 200:
# This means something went wrong.
raise Exception('GET {0} {1}'.format(url,
resp.status_code))
def deleteVoicemail(self):
status = {}
cupiRLogger.info("Delete Voicemail Started")
userObjectId = self.getVmObjectId()
vmDeleteUrl = 'users/' + userObjectId
url = self.__baseUrl + vmDeleteUrl
resp = requests.delete(url,
auth=self.__auth,
verify=self.__verify,
headers=self.__headers)
if resp.status_code != 204:
# This means something went wrong.
cupiRLogger.info('Delete {0} {1}'.format(url,
resp.status_code))
status.update({"mailboxDeleted": "Fail"})
else:
status.update({"mailboxDeleted": "Success"})
return json.dumps(status)