-
Notifications
You must be signed in to change notification settings - Fork 1
/
R1_API.py
302 lines (277 loc) · 11 KB
/
R1_API.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 26 14:29:46 2024
@author: marcelomolinari
"""
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 28 14:18:25 2023.
@author: Marcelo M. Molinari
"""
import requests
import time
class R1_API_calls:
"""R1 and RUCKUS Cloud calls using JWT and the new endpoints."""
def getToken(self, host, username, password):
"""Use your credentials to retrieve the jwt."""
url = "https://" + host + "/token"
body = {'username': username, 'password': password}
r = requests.post(url, json=body, verify=False).json()
return r
def getJWT(self, host, tenantID, clientID, clientSecret):
"""Use your API KEY to retrieve the jwt."""
url = "https://" + host + "/oauth2/token/" + tenantID
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
body = 'grant_type=client_credentials&client_id=' + clientID + \
'&client_secret=' + clientSecret
r = requests.post(url, headers=headers, data=body, verify=False).json()
return r
def getTenantDetails(self, host, jwt):
"""Get the tenant details."""
url = "https://" + host + "/tenants/self?deep=true"
auth = {'Authorization': 'Bearer {}'.format(jwt)}
r = requests.get(url, verify=False, headers=auth).json()
return r
def getBrandDetails(self, host, jwt):
"""Get the tenant details."""
url = "https://" + host + "/mspLabels"
auth = {'Authorization': 'Bearer {}'.format(jwt)}
r = requests.get(url, verify=False, headers=auth).json()
return r
def getBrandAdmins(self, host, jwt):
"""Get the tenant details."""
url = "https://" + host + "/tenants/self?deep=true"
auth = {'Authorization': 'Bearer {}'.format(jwt)}
r = requests.get(url, verify=False, headers=auth).json()
return r
def getMspECs(self, host, tenantID, jwt):
"""Get tall msp customers."""
url = "https://" + host + "/mspCustomers"
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'Content-Type': 'application/json'}
r = requests.get(url, verify=False, headers=reqHeaders).json()
return r
def getMspTechs(self, host, jwt):
"""Get all MSP and Tech partners."""
url = "https://" + host + "/mspCustomers"
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'Content-Type': 'application/json'}
r = requests.get(url, verify=False, headers=reqHeaders).json()
return r
def getProperties(self, host, jwt):
"""Get brand properties."""
url = "https://" + host + "/mspecs/query"
body = {
"searchString": "",
"filters": {
"tenantType": [
"MSP_REC"
]
},
"fields": [
"id",
"name",
"tenantType",
"status",
"alarmCount",
"mspAdminCount",
"mspEcAdminCount",
"creationDate",
"expirationDate",
"wifiLicense",
"streetAddress"
],
"searchTargetFields": [
"name"
],
"page": 1,
"pageSize": 10,
"defaultPageSize": 10,
"total": 0,
"sortField": "name",
"sortOrder": "ASC"
}
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'Content-Type': 'application/json'}
r = requests.post(url, verify=False, headers=reqHeaders, json=body).json()
return r
def getVenues(self, host, tenantID, jwt):
"""Get all venues."""
url = "https://" + host + "/venues"
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'x-rks-tenantid': tenantID}
r = requests.get(url, verify=False, headers=reqHeaders).json()
return r
def getVenueById(self, host, tenantID, venueId, jwt):
"""Get venue with provided Id."""
url = "https://" + host + "/venues"
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'x-rks-tenantid': tenantID}
r = requests.get(url, verify=False, headers=reqHeaders).json()
for item in r:
if item['id'] == venueId:
return item['name']
return 'not found'
def getAPs(self, host, tenantID, jwt):
"""Get all APs."""
url = "https://" + host + "/venues/aps"
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'x-rks-tenantid': tenantID}
r = requests.get(url, verify=False, headers=reqHeaders).json()
return r
def getAPsByVenueId(self, host, tenantID, venueID, jwt):
"""Get all APs in a venue."""
url = "https://" + host + "/venues/aps"
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'x-rks-tenantid': tenantID}
r = requests.get(url, verify=False, headers=reqHeaders).json()
apList = []
for item in r:
if item['venueId'] == venueID:
apList.append(item)
return apList
def getNetworks(self, host, tenantID, jwt):
"""Get all networks."""
url = "https://" + host + "/networks"
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'x-rks-tenantid': tenantID}
r = requests.get(url, verify=False, headers=reqHeaders).json()
return r
def getNetworkDetails(self, host, tenantID, networkID, jwt):
"""Get network details."""
url = "https://" + host + "/networks/" + networkID + "?deep=true"
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'x-rks-tenantid': tenantID}
r = requests.get(url, verify=False, headers=reqHeaders).json()
return r
def getClients(self, host, tenantID, jwt):
"""Get clients."""
url = "https://" + host + "/clients"
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'x-rks-tenantid': tenantID}
r = requests.get(url, verify=False, headers=reqHeaders).json()
return r
def getClientSessions(self, host, tenantID, clientMac, fromTime,
toTime, jwt):
"""List client sessions."""
url = "https://" + host + "/reports/clients/sessionHistories"
body = {
"filters": {
"clientMAC": [
clientMac
],
"fromTime": fromTime,
"toTime": toTime
}
}
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'x-rks-tenantid': tenantID,
'Content-Type': 'application/json'}
r = requests.post(url, verify=False, headers=reqHeaders,
json=body).json()
return r
def getEvents(self, host, tenantID, jwt):
"""List events."""
url = "https://" + host + "/events/query"
body = {
"fields": [
"event_datetime",
"severity",
"entity_type",
"product",
"entity_id",
"message",
"dpName",
"apMac",
"clientMac",
"macAddress",
"apName",
"switchName",
"serialNumber",
"networkName",
"networkId",
"ssid",
"radio",
"raw_event",
"sourceType",
"adminName",
"clientName",
"userName",
"hostname",
"adminEmail",
"administratorEmail",
"venueName",
"venueId",
"apGroupId",
"apGroupName",
"floorPlanName",
"recipientName",
"transactionId",
"name",
"ipAddress",
"detailedDescription",
"Persona-ID"
],
"page": 1,
"pageSize": 300,
"sortField": "event_datetime",
"sortOrder": "DESC",
"filters": {
"entity_type": [
"AP",
"SECURITY",
"CLIENT",
"SWITCH",
"NETWORK",
"EDGE"
]
}
}
reqHeaders = {'Authorization': 'Bearer {}'.format(jwt),
'x-rks-tenantid': tenantID,
'Content-Type': 'application/json'}
r = requests.post(url, verify=False, headers=reqHeaders,
json=body).json()
return r
def configure_802_11k(self, host, enableNeighborReport, jwt):
"""Configure 802_11k in the wlan."""
url = "https://" + host + "/networks"
auth = {'Authorization': 'Bearer {}'.format(jwt)}
r = requests.get(url, verify=False, headers=auth).json()
print(r)
for network in r:
networkId = network['id']
print('\nNetwork: ', network['name'])
network['wlan']['advancedCustomization']['enableNeighborReport'] \
= enableNeighborReport
url = "https://" + host + "/networks/" + networkId
auth = {'Authorization': 'Bearer {}'.format(jwt)}
r = requests.put(url, verify=False, headers=auth, json=network)
print('change response:', r)
self.wait_for_async_response(host, r, jwt)
return 'SUCCESS'
def wait_for_async_response(self, host, response, jwt,
sleep_time=3):
"""Check if the status of the async call."""
http_response = response.status_code
if http_response != 202:
return response
requestId = response.json()['requestId']
print('\nWaiting for request to complete:', requestId)
url = "https://" + host + "/activities/" + requestId
auth = {'Authorization': 'Bearer {}'.format(jwt)}
while True:
try:
r = requests.get(url, verify=False, headers=auth).json()
print('\nrequest:', r['status'])
if r['status'] in ['SUCCESS', 'FAIL']:
break
time.sleep(sleep_time)
except Exception as ex:
print(ex)
print('retrying')
time.sleep(sleep_time)
if r['status'] != 'SUCCESS':
raise Exception(r['status'])