-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtoggle_poe.py
337 lines (291 loc) · 12.1 KB
/
toggle_poe.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
'''
-------------------------------------------------------------------------------
Written by Thomas Munzer (tmunzer@juniper.net)
Github repository: https://github.com/tmunzer/Mist_library/
This script is licensed under the MIT License.
-------------------------------------------------------------------------------
Python script to enable/disable/toggle PoE for a specified Port Profile in a
Switch Template.
This script will not change/create/delete/touch any existing objects. It will
just retrieve every single object from the organization.
-------
Requirements:
mistapi: https://pypi.org/project/mistapi/
-------
Usage:
This script can be run as is (without parameters), or with the options below.
If no options are defined, or if options are missing, the missing options will
be asked by the script or the default values will be used.
It is recommended to use an environment file to store the required information
to request the Mist Cloud (see https://pypi.org/project/mistapi/ for more
information about the available parameters).
-------
Options:
-o, --org_id= Set the org_id
-t, --tmpl_id= Set the Switch template_id
-p, --profile= Set the Port Profile name
-a, --action= Set the action to execupte. It must be one of the following:
- status: Retrieve the PoE status
- on: Enable PoE on the port profile
- off: Disable PoE on the port profile
- toggle: toogle PoE on the port profile (i.e. turn it off if
currently enable, turn it on if currently enabled)
-e, --env= define the env file to use (see mistapi env file documentation
here: https://pypi.org/project/mistapi/)
default is "~/.mist_env"
-------
Examples:
python3 ./toggle_poe.py
python3 ./toggle_poe.py -o 203d3d02-xxxx-xxxx-xxxx-76896a3330f4 -t 2c57044e-xxxx-xxxx-xxxx-69374b32a070 -p ap -a toggle
'''
#### IMPORTS ####
import logging
import sys
import getopt
MISTAPI_MIN_VERSION = "0.44.1"
try:
import mistapi
from mistapi.__logger import console
except:
print("""
Critical:
\"mistapi\" package is missing. Please use the pip command to install it.
# Linux/macOS
python3 -m pip install mistapi
# Windows
py -m pip install mistapi
""")
sys.exit(2)
#####################################################################
#### PARAMETERS #####
log_file = "./script.log"
env_file = "~/.mist_env"
#####################################################################
#### LOGS ####
logger = logging.getLogger(__name__)
#####################################################################
#### FUNCTIONS ####
def _get_port_usages(apisession:mistapi.APISession, org_id:str, tmpl_id:str):
print("Retrieving data from Mist ".ljust(79, "."), end="", flush=True)
try:
res = mistapi.api.v1.orgs.networktemplates.getOrgNetworkTemplate(apisession, org_id, tmpl_id)
disabled = res.data.get("port_usages", {})
print("\033[92m\u2714\033[0m")
return disabled
except:
print('\033[31m\u2716\033[0m')
sys.exit(4)
def _status(apisession:mistapi.APISession, org_id:str, tmpl_id:str, profile:str):
port_usages = _get_port_usages(apisession, org_id, tmpl_id)
print("Extracting profile data ".ljust(79, "."), end="", flush=True)
profile = port_usages.get(profile, {})
if not profile:
print('\033[31m\u2716\033[0m')
print(f"Profile {profile} not found")
else:
print("\033[92m\u2714\033[0m")
return profile.get("poe_disabled", False)
def _display_status(apisession:mistapi.APISession, org_id:str, tmpl_id:str, profile:str):
disabled = _status(apisession, org_id, tmpl_id, profile)
print()
print(" Result ".center(80, "-"))
print()
if disabled == True:
print(f"PoE Current status: DISABLED")
else:
print(f"PoE Current status: ENABLED")
def _update(apisession:mistapi.APISession, org_id:str, tmpl_id:str, port_usages:dict):
try:
print("Updating PoE Status ".ljust(79, "."), end="", flush=True)
res = mistapi.api.v1.orgs.networktemplates.updateOrgNetworkTemplates(apisession, org_id, tmpl_id, {"port_usages": port_usages})
if res.status_code == 200:
print("\033[92m\u2714\033[0m")
else:
raise Exception
except:
print('\033[31m\u2716\033[0m')
sys.exit(5)
def _change(apisession:mistapi.APISession, org_id:str, tmpl_id:str, profile:str, disabled:bool):
port_usages = _get_port_usages(apisession, org_id, tmpl_id)
print("Extracting profile data ".ljust(79, "."), end="", flush=True)
if not profile in port_usages:
print('\033[31m\u2716\033[0m')
print(f"Profile {profile} not found")
else:
print("\033[92m\u2714\033[0m")
port_usages[profile]["poe_disabled"] = disabled
_update(apisession, org_id, tmpl_id, port_usages)
def _toggle(apisession:mistapi.APISession, org_id:str, tmpl_id:str, profile:str):
port_usages = _get_port_usages(apisession, org_id, tmpl_id)
print("Extracting profile data ".ljust(79, "."), end="", flush=True)
if not profile in port_usages:
print('\033[31m\u2716\033[0m')
print(f"Profile {profile} not found")
else:
print("\033[92m\u2714\033[0m")
port_usages[profile]["poe_disabled"] = not port_usages[profile]["poe_disabled"]
_update(apisession, org_id, tmpl_id, port_usages)
####################
## MENU
def _show_menu(header:str, menu:list) -> str:
print()
print("".center(80, "-"))
resp=None
menu = sorted(menu, key=str.casefold)
while True:
print(f"{header}")
i=0
for entry in menu:
print(f"{i}) {entry}")
i+=1
resp = input(f"Please select an option (0-{i-1}, q to quit): ")
if resp.lower() == "q":
sys.exit(0)
else:
try:
resp=int(resp)
except:
console.error("Please enter a number\r\n ")
if resp < 0 or resp >= i:
console.error(f"Please enter a number between 0 and {i -1}.")
else:
return menu[resp]
def _check_parameters(apisession:mistapi.APISession, org_id:str, tmpl_id:str, profile:str, action:str):
if not org_id:
org_id = mistapi.cli.select_org(apisession)[0]
if not tmpl_id:
response = mistapi.api.v1.orgs.networktemplates.listOrgNetworkTemplates(apisession, org_id)
templates = mistapi.get_all(apisession, response)
template_names = {}
template_menu = []
for template in templates:
template_menu.append(template.get("name"))
template_names[template.get("name")] = template["id"]
tmpl_name = _show_menu("Please select a Switch Template", template_menu)
tmpl_id = template_names[tmpl_name]
if not profile:
template_settings = mistapi.api.v1.orgs.networktemplates.getOrgNetworkTemplate(apisession, org_id, tmpl_id).data
port_usages = template_settings.get("port_usages")
if not port_usages:
console.error("There is no profile to update in this switch template")
sys.exit(2)
else:
profile_names = []
for port_usage in port_usages:
profile_names.append(port_usage)
profile = _show_menu("Please select a Profile", profile_names)
if not action:
action = _show_menu("Please select an Action", ["status", "on", "off", "toggle"])
if action != "status":
action_str = action
if action != "toggle":
action_str = f"turn {action}"
while True:
print()
confirm = input(f"Do you confirm you want to {action_str} PoE on port profile {profile} (y/n)?")
if confirm.lower() == "y":
break
elif confirm.lower() == "n":
sys.exit(0)
return org_id, tmpl_id, profile, action
def start(apisession:mistapi.APISession, org_id:str, tmpl_id:str, profile:str, action:str):
org_id, tmpl_id, profile, action = _check_parameters(apisession, org_id, tmpl_id, profile, action)
print(" Processing ".center(80, "-"))
print()
if action == "status":
_display_status(apisession, org_id, tmpl_id, profile)
if action == "off":
_change(apisession, org_id, tmpl_id, profile, True)
_display_status(apisession, org_id, tmpl_id, profile)
if action == "on":
_change(apisession, org_id, tmpl_id, profile, False)
_display_status(apisession, org_id, tmpl_id, profile)
if action == "toggle":
_toggle(apisession, org_id, tmpl_id, profile)
_display_status(apisession, org_id, tmpl_id, profile)
#####################################################################
##### USAGE ####
def usage():
print('''
-------------------------------------------------------------------------------
Written by Thomas Munzer (tmunzer@juniper.net)
Github repository: https://github.com/tmunzer/Mist_library/
This script is licensed under the MIT License.
-------------------------------------------------------------------------------
Python script to enable/disable/toggle PoE for a specified Port Profile in a
Switch Template.
This script will not change/create/delete/touch any existing objects. It will
just retrieve every single object from the organization.
-------
Requirements:
mistapi: https://pypi.org/project/mistapi/
-------
Usage:
This script can be run as is (without parameters), or with the options below.
If no options are defined, or if options are missing, the missing options will
be asked by the script or the default values will be used.
It is recommended to use an environment file to store the required information
to request the Mist Cloud (see https://pypi.org/project/mistapi/ for more
information about the available parameters).
-------
Options:
-o, --org_id= Set the org_id
-t, --tmpl_id= Set the Switch template_id
-p, --profile= Set the Port Profile name
-a, --action= Set the action to execupte. It must be one of the following:
- status: Retrieve the PoE status
- on: Enable PoE on the port profile
- off: Disable PoE on the port profile
- toggle: toogle PoE on the port profile (i.e. turn it off if
currently enable, turn it on if currently enabled)
-e, --env= define the env file to use (see mistapi env file documentation
here: https://pypi.org/project/mistapi/)
default is "~/.mist_env"
-------
Examples:
python3 ./toggle_poe.py
python3 ./toggle_poe.py -o 203d3d02-xxxx-xxxx-xxxx-76896a3330f4 -t 2c57044e-xxxx-xxxx-xxxx-69374b32a070 -p ap -a toggle
'''
)
sys.exit(0)
#####################################################################
##### ENTRY POINT ####
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "e:o:t:p:a:", ["env=", "org-id=", "tmpl_id=", "profile=", "action="])
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
org_id=None
tmpl_id=None
profile=None
action=None
for o, a in opts:
if o in ["-h", "--help"]:
usage()
sys.exit()
elif o in ["-e", "--env"]:
env_file = a
elif o in ["-o", "--org_id"]:
org_id=a
elif o in ["-t", "--tmpl_id"]:
tmpl_id=a
elif o in ["-p", "--profile"]:
profile=a
elif o in ["-a", "--action"]:
if a not in ["status", "on", "off", "toggle"]:
console.error(f"Unknown action \"{o}\"")
usage()
sys.exit(0)
else:
action=a
else:
assert False, "unhandled option"
#### LOGS ####
logging.basicConfig(filename=log_file, filemode='w')
logger.setLevel(logging.DEBUG)
### START ###
apisession = mistapi.APISession(env_file=env_file)
apisession.login()
start(apisession, org_id, tmpl_id, profile, action)