-
Notifications
You must be signed in to change notification settings - Fork 0
/
gluware_device_api_demo.py
284 lines (247 loc) · 12.2 KB
/
gluware_device_api_demo.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
# /* Copyright (C) 2019 Gluware - All Rights Reserved
# * This code is provided “as is” with no implied warranties or fitness
# * for a particular purpose. Gluware, Inc. is under no obligation to
# * provide maintenance, support, updates, enhancements or modifications.
# */
# REQUIRED PYTHON VERSION: 3.x
# Supports Gluware REST API v1
# Python modules that must be available are:
# requests, urllib3, and Menu.
# To install these modules run pip in your environment
# Ex.
# pip install requests
# pip install urllib3
# pip install Menu
# This file and gluware_device_rest_api_client.py must be in the same directory or in sys.path
# Usage: python GluwareDeviceAPIDemo
#
# This demo uses a set of basic text menus to demonstrate various calls to the Gluware Device REST API
# There are 3 primary submenus: Connection Info, Organizations, and Devices
#
# NOTE: Connection information must be supplied in order to use any of the Organization or Device calls
# Connection info required: Gluware Control username and password and the Gluware Control hostname or IP
# Once the connection information is set it will be automatically used by the demo for each call to the REST API
#
# Demo example flow:
# 1. Run the demo from the command line or using a python IDE of your choice
# 2. Select the Connection Info option from the main menu and enter your Gluware Control credentials and ip/hostname
# 3. Enter the Organizations sub menu by selected the Organizations option in the main menu
# 4. List all of the Organization in your Gluware Control system by choosing the List Organizations option from the
# Organizations sub menu. Highlight and copy one of the organization IDs for use in subsequent steps
# 5. Close the Organizaton sub menu by selecting the Close Organizations sub menu option to return to the main menu
# 6. Select the Devices option from the main menu
# 7. Select the "Retrieve Devices By Org ID" option from the Devices sub menu and enter the Organization ID from step 4
# 8. To create a new device select the "Create Discoverable Device" option from the Devices menu
# 9. Enter the Device Name, Organization ID from step 4, Device Connection Info (IP Address, Username, Password,
# Enable Password (if needed), Connection Type and Port. Highlight and copy the ID value for the newly created device
# 10. Select the Update Device By ID option from the Devices sub menu
# 11. Enter the Device ID from Step 9
# 12. Enter a device element name to update such as "description"
# 13. Enter the new value for the element name selected in step 12
# 14. Select the Discover Device option from the Devices sub menu
# 15. Enter the Device ID from Step 9. This will kick off device discovery. If you repeat the next step quickly you can
# see the discovery status value change.
# 16. Select the Retrieve Device By ID from the Devices sub element
# 17. Enter the Device ID from Step 9. Note the discovery status value as well as the value of whatever element name you
# selected in steps 11 and 12
# 18. Select the Delete Device By ID option from the Devices sub menu
# 19. Enter the Device ID from Step 9
# 20. Select the Retrieve Device By ID option from the Devices sub menu
# 21. Enter the Device ID from Step 9 and note that the response code indicates the resource is not found since it was
# deleted in steps 18 and 19
# 22. Select the Close Devices Sub Menu option from the Devices sub menu
# 23. Select the Quit option from the main menu to exit the demo
import gluware_device_rest_api_client
from menu import Menu
import pprint
def print_orgs():
if connection_info_is_set():
resp = client.get_organizations()
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error:")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def print_orgs_by_name():
if connection_info_is_set():
org_name = input("Enter Organization Name: ")
resp = client.get_organization_id_by_name(org_name, "GluwareSystemOrganization")
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error:")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def print_sub_org_by_name():
if connection_info_is_set():
org_name = input("Enter Organization Name: ")
parent_name = input("Enter Parent Organization Name: ")
resp = client.get_organization_id_by_name(org_name, parent_name)
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error:")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def print_org_by_id():
if connection_info_is_set():
org_id = input("Enter Organization ID: ")
resp = client.get_organization(org_id)
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error:")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def print_devices():
if connection_info_is_set():
resp = client.get_devices(None)
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def print_devices_in_org_id():
if connection_info_is_set():
org_id = input("Enter Organization ID: ")
payload = {"orgId": org_id}
resp = client.get_devices(payload)
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def print_device_by_id():
if connection_info_is_set():
dev_id = input("Enter Device ID: ")
resp = client.get_device(dev_id)
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def create_device():
if connection_info_is_set():
dev_name = input("Enter Device Name: ")
org_id = input("Enter Organization ID: ")
payload = {"name": dev_name, "orgId": org_id}
resp = client.create_device(payload)
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def collect_connection_info():
device_ip_address = input("Enter Device IP Address: ")
device_user_name = input("Enter Device Username: ")
device_password = input("Enter Device Password: ")
device_enable_password = input("Enter Enable Password (Press Enter if not used): ")
device_connect_type = input("Enter Connection Type ('ssh' or 'telnet'): ")
device_connect_port = input("Enter Connection Port (ssh = 22): ")
return {"ip": device_ip_address, "password": device_password, "userName": device_user_name,
"type": device_connect_type, "port": device_connect_port,
"enablePassword": device_enable_password, "proxyList": []}
def create_discoverable_device():
if connection_info_is_set():
dev_name = input("Enter Device Name: ")
org_id = input("Enter Organization ID: ")
connection_information = collect_connection_info()
payload = {"name": dev_name, "orgId": org_id, "connectionInformation": connection_information}
resp = client.create_device(payload)
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def delete_device():
if connection_info_is_set():
dev_id = input("Enter Device ID: ")
resp = client.delete_device(dev_id)
if resp["response_code"] == 200:
print("Device deleted")
else:
print("Error")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def update_device():
if connection_info_is_set():
print("Common elements that can be updated are: name, orgId, description and custom field names")
dev_id = input("Enter Device ID: ")
element_name = input("Enter device element name to be updated: ")
element_value = input("Enter value for device element: ")
payload = {element_name: element_value}
resp = client.update_device(dev_id, payload)
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def update_device_connection_info():
if connection_info_is_set():
dev_id = input("Enter Device ID: ")
connection_information = collect_connection_info()
payload = {"connectionInformation": connection_information}
resp = client.update_device(dev_id, payload)
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def discover_device():
if connection_info_is_set():
dev_id = input("Enter Device ID: ")
payload = {"devices": [dev_id]}
resp = client.discover_devices(payload)
if resp["response_code"] == 200:
pprint.pprint(resp["response_content"])
else:
print("Error")
pprint.pprint(resp)
input("Enter any key to return to menu: ")
def set_connection_info():
global gluware_username, gluware_password, gluware_host_name
gluware_username = input("Enter username for connection: ")
gluware_password = input("Enter password for connection: ")
gluware_host_name = input("Enter hostname or IP for connection: ")
global client
client = gluware_device_rest_api_client.APIClient("https://" + gluware_host_name, gluware_username,
gluware_password, gluware_org_name,
None)
def connection_info_is_set():
global gluware_username, gluware_password, gluware_host_name
if gluware_username == "" or gluware_password == "" or gluware_host_name == "":
print("Please set up Connection Information using the option in the root menu.")
return False
else:
return True
gluware_org_name = ""
gluware_host_name = ""
gluware_username = ""
gluware_password = ""
client = gluware_device_rest_api_client.APIClient("https://" + gluware_host_name, gluware_username, gluware_password,
gluware_org_name, None)
menu = Menu()
orgs = Menu(title="Organizations")
devs = Menu(title="Devices")
menu.set_options([("Set Connection Info", set_connection_info), ("Organization", orgs.open), ("Devices", devs.open),
("Quit", menu.close)])
menu.set_title("Gluware API Demo")
orgs.set_options([("List All Organizations", print_orgs), ("Retrieve Organization ID by Name", print_orgs_by_name),
("Retrieve SubOrganization ID By Name", print_sub_org_by_name),
("Retrieve Organization By ID", print_org_by_id),
("Close Organizations Sub Menu", orgs.close)])
devs.set_options([("List All Devices", print_devices), ("List Devices By Org ID", print_devices_in_org_id),
("Retrieve Device By ID", print_device_by_id), ("Create Basic Device", create_device),
("Create Discoverable Device", create_discoverable_device),
("Discover Device", discover_device), ("Update Device by ID", update_device),
("Update Device Connection Information", update_device_connection_info),
("Delete Device by ID", delete_device), ("Close Devices Sub Menu", devs.close)])
menu.open()