-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportUser.py
279 lines (269 loc) · 14.5 KB
/
ImportUser.py
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import os
import requests
import xml.etree.ElementTree as ET
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import time
import json
import logging
# Initialize logging
logging.basicConfig(filename='script.log', level=logging.INFO, format='%(asctime)s - %(message)s') # Add timestamp
# Load User to query
sAMAccountName = "NetoworkName" # Define the samaccountname variable
template_alias = "template name" # Replace with the desired template alias
dtmf_access_id = "5551212" # Replace with the desired dtmfAccessId
payload = {
"dtmfAccessId": dtmf_access_id,
"pkid": "" # Initialize pkid to an empty string
# Include other necessary payload data
}
# Function to create a session with retries
def create_session(retries, backoff_factor, status_forcelist):
session = requests.Session()
retry_strategy = Retry(
total=retries,
status_forcelist=status_forcelist,
backoff_factor=backoff_factor,
allowed_methods=frozenset(['GET', 'POST'])
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
# Configure retries for 503 errors with a delay
retries = 3 # Number of retries
backoff_factor = 2 # Delay factor (exponential backoff)
status_forcelist = [503] # Retry on 503 errors
# Create a session with retries
session = create_session(retries, backoff_factor, status_forcelist)
logging.info("Session Start") # Changed error to info
# Load configuration from a JSON file
config_file = 'config.json'
if os.path.exists(config_file):
with open(config_file, 'r') as file:
config = json.load(file)
username = config.get('username')
password = config.get('password')
base_url = config.get('base_url')
if not (username and password and base_url):
logging.error("Invalid or incomplete configuration in config.json")
exit(1)
else:
logging.error("Config file 'config.json' not found.")
exit(1)
# Summarize User pass
authentication = (username, password)
# Function for logging and reporting errors
def report_error(message, error):
logging.error(message)
if error:
logging.exception(error)
print(f"Error: {message}")
# Define the URLs
get_import_url = f"{base_url}vmrest/import/users/ldap?query=(alias%20is%20{sAMAccountName})"
current_user_url = f"{base_url}vmrest/users?query=(alias%20is%20{sAMAccountName})"
number_exists_url = f"{base_url}vmrest/users?query=(DtmfAccessId is {dtmf_access_id})"
template_exists_url = f"{base_url}vmrest/usertemplates?query=(Alias%20is%20{template_alias})"
post_import_url = f"{base_url}vmrest/import/users/ldap?templateAlias={template_alias}"
####################################
# First Let's Make sure the number does not already Exist
current_number_found = False
for retry in range(retries + 1):
number_exists = session.get(number_exists_url, auth=authentication)
if number_exists.status_code == 503:
logging.info(f"Received 503 error. Retrying attempt {retry + 1}/{retries + 1}...")
time.sleep(backoff_factor * (2 ** retry))
elif number_exists.status_code == 200:
logging.info("Get current_number Connection is 200 OK")
number_exists_root = ET.fromstring(number_exists.text)
number_exists_total = int(number_exists_root.get('total'))
if number_exists_total == 0:
logging.info("Current Number Not In Use")
logging.info(number_exists.text)
break
elif number_exists_total == 1:
user_element = number_exists_root.find('User')
if user_element is not None:
number_alias = user_element.find('Alias').text
number_first_name = user_element.find('FirstName').text
number_last_name = user_element.find('LastName').text
number_pkid = user_element.find('ObjectId').text
logging.error("Existing Number found:")
logging.info(f"Alias: {number_alias}")
logging.info(f"First Name: {number_first_name}")
logging.info(f"Last Name: {number_last_name}")
logging.info(f"PKID: {number_pkid}")
logging.info(f"XML: {number_exists.text}")
current_number_found = True
break # Number found, exit the loop
else:
report_error(f"Request failed with status code {number_exists.status_code}", None)
break
if current_number_found:
print("Number is already in use")
else:
print("Number is Available")
logging.error("Number is Available")
####################################
#Second Let's Query if the Template Exists
current_template_found = False
for retry in range(retries + 1):
template_exists = session.get(template_exists_url, auth=authentication)
if template_exists.status_code == 503:
logging.info(f"Received 503 error. Retrying attempt {retry + 1}/{retries + 1}...")
time.sleep(backoff_factor * (2 ** retry))
elif template_exists.status_code == 200:
logging.info("Get template_exists Connection is 200 OK")
template_exists_root = ET.fromstring(template_exists.text)
template_exists_total = int(template_exists_root.get('total'))
if template_exists_total == 0:
logging.error("template is not Valid")
logging.info(template_exists.text)
break
elif template_exists_total == 1:
user_element = template_exists_root.find('UserTemplate')
if user_element is not None:
template_exists_alias = user_element.find('Alias').text
template_exists_pkid = user_element.find('ObjectId').text
print("Template Found")
logging.info("Template Found:")
logging.info(f"Alias: {template_exists_alias}")
logging.info(f"PKID: {template_exists_pkid}")
logging.info(f"XML: {template_exists.text}")
current_template_found = True
break # Number found, exit the loop
else:
report_error(f"Request failed with status code {template_exists.status_code}", None)
break
if not current_template_found:
print("Error: template is not Valid")
else:
####################################
# Third Let's Query if the user Exists
current_user_found = False
for retry in range(retries + 1):
is_current_user = session.get(current_user_url, auth=authentication)
if is_current_user.status_code == 503:
logging.info(f"Received 503 error. Retrying attempt {retry + 1}/{retries + 1}...")
time.sleep(backoff_factor * (2 ** retry))
elif is_current_user.status_code == 200:
logging.info("Get current_user Connection is 200 OK")
is_current_user_root = ET.fromstring(is_current_user.text)
is_current_user_total = int(is_current_user_root.get('total'))
if is_current_user_total == 0:
print("The user does not already Exist")
logging.info("Current User Not Found")
logging.info(is_current_user.text)
break
elif is_current_user_total == 1:
user_element = is_current_user_root.find('User')
if user_element is not None:
current_alias = user_element.find('Alias').text
current_first_name = user_element.find('FirstName').text
current_last_name = user_element.find('LastName').text
current_pkid = user_element.find('ObjectId').text
logging.error("Existing User found:")
logging.info(f"Alias: {current_alias}")
logging.info(f"First Name: {current_first_name}")
logging.info(f"Last Name: {current_last_name}")
logging.info(f"PKID: {current_pkid}")
logging.info(f"XML: {current_alias.text}")
current_user_found = True
break # User found, exit the loop
else:
report_error(f"Request failed with status code {is_current_user.status_code}", None)
break
if current_user_found:
print("Error: User Already Configured")
else:
####################################
# Fourth Let's Query if that user is avalable for import
import_user_found = False
importable_user = None
for retry in range(retries + 1):
importable_user = session.get(get_import_url, auth=authentication)
if importable_user.status_code == 503:
logging.info(f"Received 503 error. Retrying attempt {retry + 1}/{retries + 1}...")
time.sleep(backoff_factor * (2 ** retry)) # Exponential backoff
elif importable_user.status_code == 200:
logging.info("import_url 200 OK")
root = ET.fromstring(importable_user.text)
importable_user_total = int(root.get('total'))
if importable_user_total == 0:
logging.error("Import User Not Found")
# If The user does not exits there is no one to import
break
if importable_user_total == 1:
for user_element in root.findall('.//ImportUser'):
importable_alias = user_element.find('alias').text
importable_first_name = user_element.find('firstName').text
importable_last_name = user_element.find('lastName').text
importable_pkid = user_element.find('pkid').text
print("Import User found:")
logging.info("Import User found:")
logging.info(f"Alias: {importable_alias}")
logging.info(f"First Name: {importable_first_name}")
logging.info(f"Last Name: {importable_last_name}")
logging.info(f"PKID: {importable_pkid}")
logging.info(f"XML: {importable_user.text}")
payload["pkid"] = importable_pkid
import_user_found = True
break # Exit the loop
if not import_user_found:
print("Error: Import User Not Found")
else:
# Perform the POST request to the import URL with the JSON payload and retries
for retry in range(retries + 1):
post_import = session.post(post_import_url, json=payload, auth=authentication)
if post_import.status_code == 503:
logging.info(f"Received 503 error. Retrying attempt {retry + 1}/{retries + 1}...")
time.sleep(backoff_factor * (2 ** retry)) # Exponential backoff
elif post_import.status_code == 201:
print("User Created")
logging.info("User Created")
# Extract and log user details from the import response
for user_element in root.findall('.//ImportUser'):
alias = user_element.find('alias').text
first_name = user_element.find('firstName').text
last_name = user_element.find('lastName').text
pkid = user_element.find('pkid').text
logging.info("Import User found:")
logging.info(f"Alias: {alias}")
logging.info(f"First Name: {first_name}")
logging.info(f"Last Name: {last_name}")
logging.info(f"PKID: {pkid}")
logging.info(f"XML: {post_import.text}")
break # Exit the loop since the user was successfully created
elif post_import.status_code == 200:
logging.info(f"Import URL 200 OK for templateAlias '{template_alias}':")
root = ET.fromstring(post_import.text)
total = int(root.get('total'))
if total == 0:
print("No users found for the templateAlias")
logging.info("No users found for the templateAlias")
break # Exit the loop
elif post_import.status_code == 400:
logging.info(f"Bad Request (400):")
try:
error_root = ET.fromstring(post_import.text)
error_code = error_root.find('.//code').text
error_message = error_root.find('.//message').text
logging.info(f"Error Code: {error_code}")
logging.info(f"Error Message: {error_message}")
except Exception as e:
report_error("Failed to parse error post_import:", e)
break # Exit the loop
elif post_import.status_code == 404:
logging.info(f"Resource Not Found (404):")
try:
error_root = ET.fromstring(post_import.text)
error_code = error_root.find('.//code').text
error_message = error_root.find('.//message').text
logging.info(f"Error Code: {error_code}")
logging.info(f"Error Message: {error_message}")
except Exception as e:
report_error("Failed to parse error post_import:", e)
break # Exit the loop
else:
report_error(f"Request failed with status code {post_import.status_code}", None)
break # Exit the loop since there was an unexpected error