-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistusersfromCUCDM.py
144 lines (106 loc) · 4.88 KB
/
listusersfromCUCDM.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
import requests
import json
import sys
import base64
import cucdm_conf # CUCDM information is in cucdm_config.py, including CUCDM IP address, Admin user name and password
requests.packages.urllib3.disable_warnings() # Disable certificate warning message
username = cucdm_conf.username # Username imported from cucdm_conf
password = cucdm_conf.password # Password imported from cucdm_conf
_ip = cucdm_conf.ip # IP address of the CUCDM server imported from cucdm_conf
_credentials = ('%s:%s' % (username, password)) # Credentials in a combined form 'username:password'
_credEncoded = base64.b64encode(_credentials.encode('ascii')) # Credentials to 64 based encoding before sending
def get_auth(uname=cucdm_conf.username,pword=_credEncoded,ip=cucdm_conf.ip):
"""
This function make authentication against CUCDM server.
Passing ip, version,username and password when use as standalone function to overwrite the configuration above.
This function returns a new service ticket.
"""
# url for the post ticket API request
post_url = "https://"+ip+"/noninteractivelogin/"
headers = {
'authorization': 'Basic %s' % _credEncoded.decode("ascii"),
'cache-control': "no-cache",
"content-type": "application/json"
}
# POST request and response
try:
r = requests.post(post_url,headers=headers,verify=False) # Using 'verify=False' to ignore the self signed certification of the CUCDM server
# To get response header information and store it in a new variable.
# Response header includes XCSRFToken value for this session.
jsonheader=r.headers
# Remove # to pront all response header information:
print(jsonheader)
# Return XCSRFToken value:
XCSRFToken=jsonheader['X-CSRFToken']
return (XCSRFToken)
except:
# Something wrong, cannot get service ticket
print ("Status: %s"%r.status_code)
print ("Response: %s"%r.text)
sys.exit ()
def getUsers(ip=cucdm_conf.ip,api='', params =''):
"""
To simplify requests.get with default configuration.Return is the same as requests.get
"""
_ref = "https:"+ip
ticket = get_auth()
headers = {"content-type": "application/json",'csrftoken':ticket}
url = "https://"+ip+"/api/"+api
print ("\nExecuting GET '%s'\n"%url)
try:
# The request and response of "GET /network-device" API
resp= requests.get('https://198.18.133.254/api/relation/HcsUserREL/', headers=headers,verify = False)
#return(resp)
print(resp.text)
#print(f)
#putchange=requests.put('https://198.18.133.254/api/relation/HcsUserREL/599f3fedacba0ae6b441121b/?hierarchy=551cbc26acba0a1691197134&policy_name=HcsUserRelFDP',data=json.dumps(f), headers=headers, verify=False)
#print(putchange.text)
except requests.exceptions.HTTPError as errh:
print("Http Error:", errh)
sys.exit()
def post(ip=cucdm_conf.ip,ver=cucdm_conf.version,uname=cucdm_conf.username,pword=cucdm_conf.password,api='',data=''):
"""
To simplify requests.post with default configuration.Return is the same as requests.post
"""
ticket = get_X_auth_token()
headers = {"content-type" : "application/json","X-Auth-Token": ticket}
url = "https://"+ip+"/api/"+ver+"/"+api
print ("\nExecuting POST '%s'\n"%url)
try:
# The request and response of "POST /network-device" API
resp= requests.post(url,json.dumps(data),headers=headers,verify = False)
return(resp)
except:
print ("Something wrong to POST /",api)
sys.exit()
def put(ip=cucdm_conf.ip,ver=cucdm_conf.version,uname=cucdm_conf.username,pword=cucdm_conf.password,api='',data=''):
"""
To simplify requests.put with default configuration.Return is the same as requests.put
"""
ticket = get_X_auth_token()
headers = {"content-type" : "application/json","X-Auth-Token": ticket}
url = "https://"+ip+"/api/"+ver+"/"+api
print ("\nExecuting PUT '%s'\n"%url)
try:
# The request and response of "PUT /network-device" API
resp= requests.put(url,json.dumps(data),headers=headers,verify = False)
return(resp)
except:
print ("Something wrong to PUT /",api)
sys.exit()
def delete(ip=cucdm_conf.ip,ver=cucdm_conf.version,uname=cucdm_conf.username,pword=cucdm_conf.password,api='',params=''):
"""
To simplify requests.delete with default configuration.Return is the same as requests.delete
"""
ticket = get_X_auth_token()
headers = {"X-Auth-Token": ticket,'content-type': 'application/json'}
url = "https://"+ip+"/api/"+ver+"/"+api
print ("\nExecuting DELETE '%s'\n"%url)
try:
# The request and response of "DELETE /network-device" API
resp= requests.delete(url,headers=headers,params=params,verify = False)
return(resp)
except:
print ("Something wrong to DELETE /",api)
sys.exit()
get()