-
Notifications
You must be signed in to change notification settings - Fork 15
/
import_sites.py
1263 lines (1110 loc) · 49 KB
/
import_sites.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
-------------------------------------------------------------------------------
Written by Thomas Munzer ([email protected])
Github repository: https://github.com/tmunzer/Mist_library/
This script is licensed under the MIT License.
-------------------------------------------------------------------------------
Python script automate the sites creation in a Mist Org from a CSV file.
**NOTE**
This Script can use Google APIs (optional) to retrieve lat/lng, tz and country code.
To be able to use Google API, you need an API Key first. Mode information available
here: https://developers.google.com/maps/documentation/javascript/get-api-key?hl=en
If the Google API Key is not provided, the script will use geopy and timezonefinder
packages to generate the required information.
-------
Requirements:
mistapi: https://pypi.org/project/mistapi/
-------
Usage:
This script requires a parameter to locate the csv file. Other parameters listed below
are optional. If the optional parameters are not defined, the script will ask for the
additional required settings.
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).
-------
CSV Format:
The first line MUST start with a "#" and defines each columns of the CSV file. The allowed
values are listed below.
When defining the templates, the policies and the sitegroups, it is possible to use the
object name OR the object id (this must be defined in the first line, by appending "_name" or
"_id"). In case both name and id are defined, the name will be used.
-------
CSV Example:
#name,address,sitegroup_names,rftemplate_id,networktemplate_name,gatewaytemplate_name,vars
Juniper France, "41 rue de Villiers, Neuilly sur Seine, France", "test1, test2",39ce2...ab5ee,ex-lab,test,"vlan_guest:3,vlan_corp:2"
-------
CSV Parameters:
Required:
- name
- address
Optional:
- site_id or site_name site used as a template to create
the new site. The name, address,
groups, vars and template will not
be copied from the source site
- sitetemplate_id or sitetemplate_name
- alarmtemplate_id or alarmtemplate_name
- aptemplate_id or aptemplate_name
- gatewaytemplate_id or gatewaytemplate_name
- networktemplate_id or networktemplate_name
- rftemplate_id or rftemplate_name
- secpolicy_id or secpolicy_name
- site_group_ids or site_group_names list of groups. If multiple groups,
must be enclosed by double quote
and comma separated
(ex: "group1, group2")
- country_code can be detected by the script based
on the address
- timezone can be detected by the script based
on the address
- vars dict of all the site variables.
Format must be key1:var1.
If multiple vars, must be enclosed
by double quote and comma separated
(e.g. "key1:var1,key2:var2, ...")
- subnet if set, will add this subnet in the
auto assignment rules to automatically
assign the APs deployed on this subnet
to this site
-------
Script Parameters:
-h, --help display this help
-f, --file= REQUIRED: path to the CSV file
-o, --org_id= Set the org_id (only one of the org_id or site_id can be defined)
-n, --org_name= Org name where to deploy the configuration:
- if org_id is provided (existing org), used to validate
the destination org
- if org_id is not provided (new org), the script will
create a new org and name it with the org_name value
-g, --google_api_key= Google API key used for geocoding
If not set, the script will use timezonefinder and geopy
package to generate the geo information
-l, --log_file= define the filepath/filename where to write the logs
default is "./script.log"
-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 ./import_sites.py -f ./my_new_sites.csv
python3 ./import_sites.py -f ./my_new_sites.csv --org_id=203d3d02-xxxx-xxxx-xxxx-76896a3330f4
'''
#### IMPORTS #####
import time
import sys
import csv
import getopt
import logging
import types
import urllib
from typing import Tuple
import requests
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"
# This Script can use Google APIs (optional) to retrieve lat/lng, tz and country code. To be
# able to use Google API, you need an API Key first. Mode information available here:
# https://developers.google.com/maps/documentation/javascript/get-api-key?hl=en
GOOGLE_API_KEY = ""
MAX_RETRY = 3
#####################################################################
#### LOGS ####
LOGGER = logging.getLogger(__name__)
#####################################################################
#### GLOBALS #####
PARAMETER_TYPES = [
"site",
"sitetemplate",
"alarmtemplate",
"aptemplate",
"gatewaytemplate",
"networktemplate",
"rftemplate",
"secpolicy",
"sitegroup"
]
GEOLOCATOR = None
TZFINDER = None
#####################################################################
# PROGRESS BAR
#####################################################################
# PROGRESS BAR AND DISPLAY
class ProgressBar():
def __init__(self):
self.steps_total = 0
self.steps_count = 0
def _pb_update(self, size: int = 80):
if self.steps_count > self.steps_total:
self.steps_count = self.steps_total
percent = self.steps_count/self.steps_total
delta = 17
x = int((size-delta)*percent)
print("\033[A")
print(f"Progress: ", end="")
print(f"[{'█'*x}{'.'*(size-delta-x)}]", end="")
print(f"{int(percent*100)}%".rjust(5), end="")
def _pb_new_step(self, message: str, result: str, inc: bool = False, size: int = 80, display_pbar: bool = True):
if inc:
self.steps_count += 1
text = f"\033[A\033[F{message}"
print(f"{text} ".ljust(size + 4, "."), result)
print("".ljust(80))
if display_pbar:
self._pb_update(size)
def _pb_title(self, text: str, size: int = 80, end: bool = False, display_pbar: bool = True):
print()
print("\033[A")
print(f" {text} ".center(size, "-"), "\n")
if not end and display_pbar:
print("".ljust(80))
self._pb_update(size)
def inc(self, size: int = 80):
self.steps_count += 1
self._pb_update(size)
def set_steps_total(self, steps_total: int):
self.steps_total = steps_total
def log_message(self, message, display_pbar: bool = True):
self._pb_new_step(message, " ", display_pbar=display_pbar)
def log_success(self, message, inc: bool = False, display_pbar: bool = True):
LOGGER.info(f"{message}: Success")
self._pb_new_step(
message, "\033[92m\u2714\033[0m\n", inc=inc, display_pbar=display_pbar)
def log_warning(self, message, inc: bool = False, display_pbar: bool = True):
LOGGER.warning(f"{message}: Warning")
self._pb_new_step(
message, "\033[93m\u2B58\033[0m\n", inc=inc, display_pbar=display_pbar)
def log_failure(self, message, inc: bool = False, display_pbar: bool = True):
LOGGER.error(f"{message}: Failure")
self._pb_new_step(
message, "\033[31m\u2716\033[0m\n", inc=inc, display_pbar=display_pbar)
def log_title(self, message, end: bool = False, display_pbar: bool = True):
LOGGER.info(message)
self._pb_title(message, end=end, display_pbar=display_pbar)
PB = ProgressBar()
#####################################################################
# Geocoding
#####################################################################
##################
# GOOGLE LAT/LNG
class GoogleGeocoding:
def __init__(self, google_api_key:str) -> None:
self.google_api_key = google_api_key
def _log_url(self, url):
query_params = url.split("?")[1].split("&")
for param in query_params:
key = param.split("=")[0]
value = param.split("=")[1]
if key == "key":
value = f"{value[0:3]}...{value[-3:len(value)]}"
url = url.replace(param, f"{key}:{value}")
return url
def _get_google_geocoding(self, address, retry:int=0):
if retry == MAX_RETRY:
LOGGER.error(f"_get_google_geocoding: too many retries...")
return None
else:
try:
data = {"location": None, "country_code": ""}
url = f"https://maps.googleapis.com/maps/api/geocode/json?address={urllib.parse.quote(address)}&key={self.google_api_key}"
LOGGER.debug(f"_get_google_geocoding: URL: {self._log_url(url)}")
response = requests.get(url)
LOGGER.debug(f"_get_google_geocoding: Response: {response.content}")
if response.status_code == 200:
data = response.json()
LOGGER.debug(f"_get_google_geocoding: Response JSON: {data}")
if data["status"] == "OK":
if len(data["results"]) > 0:
data["location"] = {
"address": data["results"][0]["formatted_address"],
"latitude": data["results"][0]["geometry"]["location"]["lat"],
"longitude": data["results"][0]["geometry"]["location"]["lng"]
}
for entry in data["results"][0]["address_components"]:
if "country" in entry["types"]:
data["country_code"] = entry["short_name"]
LOGGER.info("_get_google_geocoding: Request succeed with Data")
return data
else:
LOGGER.warning("_get_google_geocoding: Request succeed without Data")
LOGGER.info(f"_get_google_geocoding: retrying")
return self._get_google_geocoding(address, retry + 1)
elif data["status"] == "REQUEST_DENIED":
LOGGER.warning(f"_get_google_geocoding: Request failed: {data['error_message']}")
LOGGER.warning(response.content)
LOGGER.info(f"_get_google_geocoding: retrying")
return self._get_google_geocoding(address, retry + 1)
else:
LOGGER.warning("_get_google_geocoding: Request failed without Data")
LOGGER.info(f"_get_google_geocoding: retrying")
return self._get_google_geocoding(address, retry + 1)
else:
LOGGER.warning("_get_google_geocoding: Unable to get the location from Google API")
LOGGER.warning(response.content)
LOGGER.info(f"_get_google_geocoding: retrying")
return self._get_google_geocoding(address, retry + 1)
except:
LOGGER.warning("_get_google_geocoding: Unable to get the location from Google API")
LOGGER.error("Exception occurred", exc_info=True)
LOGGER.info(f"_get_google_geocoding: retrying")
return self._get_google_geocoding(address, retry + 1)
def _get_google_tz(self, location, retry:int=0):
if retry == MAX_RETRY:
LOGGER.error(f"_get_google_tz: too many retries...")
return None
else:
try:
ts = int(time.time())
url = f"https://maps.googleapis.com/maps/api/timezone/json?location={location['latitude']},{location['longitude']}×tamp={ts}&key={self.google_api_key}"
response = requests.get(url)
LOGGER.debug(f"_get_google_tz: Response: {response.content}")
if response.status_code == 200:
data = response.json()
LOGGER.debug(f"_get_google_geocoding: Response JSON: {data}")
if data["status"] == "OK":
tz = data["timeZoneId"]
LOGGER.info("_get_google_tz: Request succeed with Data")
return tz
elif data["status"] == "REQUEST_DENIED":
LOGGER.warning(f"_get_google_tz: Request failed: {data['error_message']}")
LOGGER.info(f"_get_google_tz: retrying")
return self._get_google_tz(location, retry + 1)
else:
LOGGER.warning("_get_google_tz: Request failed without Data")
LOGGER.info(f"_get_google_tz: retrying")
return self._get_google_tz(location, retry + 1)
else:
LOGGER.warning("_get_google_tz: Unable to find the site timezone")
LOGGER.info(f"_get_google_tz: retrying")
return self._get_google_tz(location, retry + 1)
except:
LOGGER.warning("_get_google_tz: Unable to find the site timezone")
LOGGER.error("Exception occurred", exc_info=True)
LOGGER.info(f"_get_google_tz: retrying")
return self._get_google_tz(location, retry + 1)
def geocoding(self, site):
data = self._get_google_geocoding(site["address"])
if data and data.get("location"):
tz = self._get_google_tz(data["location"])
if tz:
data["tz"] = tz
LOGGER.debug(f"geocoding: Returns {data}")
return data
################
# OPEN LAT/LNG
class OpenGeocoding:
def __init__(self) -> None:
try:
from timezonefinder import TimezoneFinder
self.tzfinder = TimezoneFinder()
except:
print("""
Critical:
\"timezonefinder\" package is required when \"google_api_key\" is not defined.
Please use the pip command to install it.
# Linux/macOS
python3 -m pip install timezonefinder
# Windows
py -m pip install timezonefinder
""")
sys.exit(2)
try:
from geopy import Nominatim
self.geolocator = Nominatim(user_agent="import_app")
except:
print("""
Critical:
\"geopy\" package is required when \"google_api_key\" is not defined.
Please use the pip command to install it.
# Linux/macOS
python3 -m pip install geopy
# Windows
py -m pip install geopy
""")
sys.exit(2)
def _get_open_geocoding(self, site):
try:
time.sleep(.01)
location = self.geolocator.geocode(site["address"], addressdetails=True, timeout=5)
if isinstance(location, types.NoneType):
LOGGER.warning(f"_get_open_geocoding: Unable to find the address")
return None
else:
LOGGER.info(f"_get_open_geocoding: Address found")
return location
except:
LOGGER.warning("_get_open_geocoding: Unable to get the location")
LOGGER.error("Exception occurred", exc_info=True)
return None
def _get_open_tz(self, location):
try:
tz = self.tzfinder.timezone_at(lat=location.latitude, lng=location.longitude)
country_code = str(location.raw["address"]["country_code"]).upper()
LOGGER.info(f"_get_open_tz: Timezone found")
return {"tz": tz, "country_code": country_code}
except:
LOGGER.warning("_get_open_tz: Unable to find the site timezone")
LOGGER.error("Exception occurred", exc_info=True)
return None
def geocoding(self, site):
data = {}
location = self._get_open_geocoding(site)
if location:
data = {
"location": {
"latitude": location.latitude,
"longitude": location.longitude
},
**self._get_open_tz(location)
}
LOGGER.debug(f"geocoding: Returns {data}")
return data
#####################################################################
# Auto Assignment Rules
#####################################################################
def _get_current_org_config(apisession:mistapi.APISession, org_id:str):
message = "Retrieving current Org Rules"
PB.log_message(message, display_pbar=False)
try:
res = mistapi.api.v1.orgs.setting.getOrgSettings(apisession, org_id)
if res.status_code == 200:
PB.log_success(message, inc=True)
auto_site_assignment = res.data.get("auto_site_assignment", {"enable": True})
return auto_site_assignment
else:
PB.log_failure(message, inc=True)
except:
PB.log_failure(message, inc=True)
LOGGER.error("Exception occurred", exc_info=True)
def _set_new_org_config(apisession:mistapi.APISession, org_id:str, auto_site_assignment:dict):
message = "Updating Org Rules"
PB.log_message(message, display_pbar=False)
try:
res = mistapi.api.v1.orgs.setting.updateOrgSettings(
apisession, org_id, {"auto_site_assignment": auto_site_assignment}
)
if res.status_code == 200:
PB.log_success(message, inc=True)
else:
PB.log_failure(message, inc=True)
except:
PB.log_failure(message, inc=True)
LOGGER.error("Exception occurred", exc_info=True)
def _compare_rules(org_rules:list, new_rules:list):
errors = []
if not org_rules:
org_rules = []
for rule in new_rules:
message = f"Checking subnet {rule.get('subnet')}"
PB.log_message(message)
try:
subnet_exists = list(r for r in org_rules if r.get("subnet") == rule.get("subnet"))
if subnet_exists:
LOGGER.warning(
f"_compare_rules:subnet {rule.get('subnet')} configured for site {rule.get('value')} already exists: {subnet_exists}"
)
errors.append(
f"subnet {rule.get('subnet')} configured for site {rule.get('value')} already exists: {subnet_exists}"
)
else:
org_rules.append(rule)
except:
LOGGER.error("Exception occurred", exc_info=True)
errors.append(
f"error when processing subnet {rule.get('subnet')} configured for site {rule.get('value')}"
)
if errors:
PB.log_warning(message, inc=True)
return org_rules
def _update_org_rules(apisession:mistapi.APISession, org_id:str, new_rules:list):
PB.log_title("Updating Autoprovisioning Rules")
auto_site_assignment = _get_current_org_config(apisession, org_id)
auto_site_assignment["rules"] = _compare_rules(auto_site_assignment.get("rules", {}), new_rules)
_set_new_org_config(apisession, org_id, auto_site_assignment)
#####################################################################
# Site Management
#####################################################################
def _get_geo_info(site: dict, geocoder: callable) -> dict:
message = f"Site {site['name']}: Retrievning geo information"
PB.log_message(message)
data = geocoder.geocoding(site)
if data:
site["latlng"] = {
"lat": data["location"]["latitude"],
"lng": data["location"]["longitude"]
}
site["timezone"] = data["tz"]
site["country_code"] = data["country_code"]
PB.log_success(message, True)
else:
PB.log_warning(message, True)
return site
def _create_site(apisession: mistapi.APISession, org_id: str, site: dict, geocoder:callable):
site = _get_geo_info(site, geocoder).copy()
if site:
message = f"Site {site['name']}: Site creation"
PB.log_message(message)
try:
if "vars" in site:
del site["vars"]
response = mistapi.api.v1.orgs.sites.createOrgSite(
apisession, org_id, site)
if response.status_code == 200:
PB.log_success(
f"Site {site['name']}: Created (ID {response.data['id']})", True)
return response.data
else:
PB.log_failure(message, True)
except:
PB.log_failure(message, True)
else:
PB.inc()
return None
def _update_site(apisession: mistapi.APISession, site: dict, site_id: str):
if "vars" in site and site["vars"]:
message = f"Site {site['name']}: Updating Site settings"
PB.log_message(message)
try:
vars = site["vars"]
site_vars = {}
for entry in vars.split(","):
key = entry.split(":")[0]
val = entry.split(":")[1]
site_vars[key] = val
mistapi.api.v1.sites.setting.updateSiteSettings(
apisession, site_id, {"vars": site_vars})
PB.log_success(message, True)
except:
PB.log_failure(message, True)
else:
PB.log_success(
f"Site {site['name']}: No Site settings to update", True)
###############################################################################
# CLONING FROM SOURCE SITE
###############################################################################
def _clone_src_site_settings(
apisession:mistapi.APISession, src_site_id:str, dst_site_id:str, site_name:str
):
src_site = None
message= f"Site {site_name}: Retrievning settings from src site"
PB.log_message(message)
try:
resp = mistapi.api.v1.sites.setting.getSiteSetting(apisession, src_site_id)
if resp.status_code != 200:
PB.log_failure(message, True)
else:
src_site = resp.data
if "vars" in src_site:
del src_site["vars"]
except:
PB.log_failure(message, True)
if src_site:
message= f"Site {site_name}: Deploying settings from src site"
PB.log_message(message)
try:
resp = mistapi.api.v1.sites.setting.updateSiteSettings(
apisession,
dst_site_id, src_site
)
if resp.status_code != 200:
PB.log_failure(message, True)
else:
PB.log_success(f"Site {site_name}: Cloning settings from src site", True)
except:
PB.log_failure(message, True)
###############################################################################
# MATCHING OBJECT NAME / OBJECT ID
###############################################################################
# SPECIFIC TO SITE GROUPS
def _replace_sitegroup_names(
apisession: mistapi.APISession, org_id: str, sitegroups: dict, sitegroup_names: dict
) -> Tuple[dict, str]:
sitegroup_ids = []
for sitegroup_name in sitegroup_names:
if sitegroup_name not in sitegroups:
response = mistapi.api.v1.orgs.sitegroups.createOrgSiteGroup(
apisession, org_id, {"name": sitegroup_name})
if response.status_code == 200:
sitegroups[sitegroup_name] = response.data["id"]
else:
PB.log_warning(
f"Unable to create site group {sitegroup_name}", inc=False)
sitegroup_ids.append(sitegroups[sitegroup_name])
return sitegroups, sitegroup_ids
# GENERIC REPLACE FUNCTION
def _replace_object_names_by_ids(
apisession: mistapi.APISession, org_id: str, site: dict, parameters: dict
) -> dict:
'''
replace the template/policy/groups names by the corresponding ids
'''
warning = False
source_site_id = None
message = f"Site {site['name']}: updating IDs"
PB.log_message(message)
if "site_id" in site:
source_site_id = site["site_id"]
del site["site_id"]
elif "site_name" in site:
source_site_id = parameters["site"].get(site["site_name"], None)
if not source_site_id:
PB.log_warning(f"Site {site['name']}: Source site {site['site_name']} not found")
del site["site_name"]
if "sitegroup_names" in site:
parameters["sitegroup"], site["sitegroup_ids"] = _replace_sitegroup_names(
apisession, org_id, parameters["sitegroup"], site["sitegroup_names"])
del site["sitegroup_names"]
for parameter in PARAMETER_TYPES:
try:
if f"{parameter}_name" in site:
name = site[f"{parameter}_name"]
site[f"{parameter}_id"] = parameters[parameter][name]
del site[f"{parameter}_name"]
except:
warning = True
PB.log_warning(
f"Site {site['name']}: Missing {parameter} on dest org", inc=False)
if not warning:
PB.log_success(message, True)
else:
PB.log_warning(message, True)
return site, source_site_id
# GET FROM MIST
def _retrieve_objects(apisession: mistapi.APISession, org_id: str, parameters_in_use:list) -> dict:
'''
Get the list of the current templates, policies and sitegoups
'''
parameters = {}
for parameter_type in parameters_in_use:
message = f"Retrieving {parameter_type} from Mist"
PB.log_message(message, display_pbar=False)
parameter_values = {}
data = []
response = None
try:
if parameter_type == "site":
response = mistapi.api.v1.orgs.sites.listOrgSites(apisession, org_id)
if parameter_type == "alarmtemplate":
response = mistapi.api.v1.orgs.alarmtemplates.listOrgAlarmTemplates(
apisession, org_id)
elif parameter_type == "aptemplate":
response = mistapi.api.v1.orgs.aptemplates.listOrgAptemplates(
apisession, org_id)
elif parameter_type == "gatewaytemplate":
response = mistapi.api.v1.orgs.gatewaytemplates.listOrgGatewayTemplates(
apisession, org_id)
elif parameter_type == "networktemplate":
response = mistapi.api.v1.orgs.networktemplates.listOrgNetworkTemplates(
apisession, org_id)
elif parameter_type == "rftemplate":
response = mistapi.api.v1.orgs.rftemplates.listOrgRfTemplates(
apisession, org_id)
elif parameter_type == "secpolicy":
response = mistapi.api.v1.orgs.secpolicies.listOrgSecPolicies(
apisession, org_id)
elif parameter_type == "sitegroup":
response = mistapi.api.v1.orgs.sitegroups.listOrgSiteGroups(
apisession, org_id)
elif parameter_type == "sitetemplate":
response = mistapi.api.v1.orgs.sitetemplates.listOrgSiteTemplates(
apisession, org_id)
data = mistapi.get_all(apisession, response)
for entry in data:
parameter_values[entry["name"]] = entry["id"]
parameters[parameter_type] = parameter_values
PB.log_success(message, display_pbar=False)
except:
PB.log_failure(message, display_pbar=False)
return parameters
###############################################################################
# PARSE CSV
def _extract_groups(data: str) -> list:
entries = data.split(",")
result = []
for entry in entries:
result.append(entry.strip())
return result
###############################################################################
# Optional site parameters (if id and name is defined, the name will
# be used):
# - sitetemplate_id or sitetemplate_name
# - alarmtemplate_id or alarmtemplate_name
# - aptemplate_id or aptemplate_name
# - gatewaytemplate_id or gatewaytemplate_name
# - networktemplate_id or networktemplate_name
# - rftemplate_id or rftemplate_name
# - secpolicy_id or secpolicy_name
# - site_group_ids or site_group_names list of groups. If multiple groups,
# must be enclosed by double quote and
# comma separated (ex: "group1, group2")
# - country_code can be detected by the script based on
# the address
# - timezone can be detected by the script based on
# the address
# - vars dict of all the site variables. Format
# must be key1:var1. If multiple vars,
# must be enclosed by double quote and
# comma separated (ex: "key1:var1,key2:var2")
#
def _check_settings(sites: list):
message = "Validating Sites data"
PB.log_title(message, display_pbar=False)
site_name = []
parameters_in_use = []
line = 1
for site in sites:
line +=1
if "name" not in site:
PB.log_failure(message, display_pbar=False)
console.error(f"Line {line}: Missing site parameter \"name\"")
sys.exit(0)
elif site["name"] in site_name:
PB.log_failure(message, display_pbar=False)
console.error(f"Line {line}: Site Name \"{site['name']}\" duplicated")
sys.exit(0)
else:
site_name.append(site["name"])
if "address" not in site:
PB.log_failure(message, display_pbar=False)
console.error(f"Line {line}: Missing site parameter \"address\"")
sys.exit(0)
for key in site:
parameter_name = key.split("_")
if key in [ "name", "address", "vars", "country_code", "timezone"]:
pass
elif key in [ "sitegroup_names", "sitegroup_ids"]:
if "sitegroup" not in parameters_in_use:
parameters_in_use.append("sitegroup")
pass
elif parameter_name[0] in PARAMETER_TYPES and parameter_name[1] in ["name", "id"]:
if not parameter_name[0] in parameters_in_use:
parameters_in_use.append(parameter_name[0])
pass
else:
PB.log_failure(message, display_pbar=False)
console.error(f"Line {line}: Invalid site parameter \"{key}\"")
sys.exit(0)
PB.log_success(message, display_pbar=False)
return parameters_in_use
def _process_sites_data(
apisession: mistapi.APISession, org_id: str, sites: list
) -> dict:
'''
Function to validate sites data and retrieve the required object from Mist
'''
parameters = {}
PB.log_title("Preparing Sites Import", display_pbar=False)
parameters_in_use = _check_settings(sites)
parameters = _retrieve_objects(apisession, org_id, parameters_in_use)
return parameters
def _check_sites(apisession:mistapi.APISession, org_id:str, sites:list):
'''
function to remove sites already created in the Org (based on the site name)
'''
message = "Retrieving Sites from Org"
PB.log_message(message)
try:
res = mistapi.api.v1.orgs.sites.listOrgSites(apisession, org_id, limit=1000)
sites_from_mist = mistapi.get_all(apisession, res)
PB.log_success(message, inc=True)
except:
PB.log_failure(message, inc=True)
sys.exit(1)
sites_to_create = []
if not sites_from_mist:
sites_to_create = sites
else:
site_already_created = []
for site in sites:
try:
site_exists = list(s for s in sites_from_mist if s.get("name").lower() == site.get("name").lower())
if site_exists:
LOGGER.warning(
f"_check_sites:site {site.get('name')} already exists. Won't be created..."
)
site_already_created.append(site.get("name"))
else:
sites_to_create.append(site)
except:
sites_to_create.append(site)
return sites_to_create
def _create_sites(
apisession: mistapi.APISession,
org_id: str,
sites: list,
parameters: dict,
geocoder:callable
):
'''
Function to create and update all the sites
'''
PB.log_title("Creating Sites", display_pbar=True)
for site in sites:
site, source_site_id = _replace_object_names_by_ids(
apisession, org_id, site, parameters)
new_site = _create_site(apisession, org_id, site, geocoder)
if not new_site:
PB.inc()
else:
if source_site_id:
_clone_src_site_settings(
apisession, source_site_id, new_site["id"], new_site["name"]
)
else:
PB.inc()
_update_site(apisession, site, new_site["id"])
def _read_csv_file(file_path: str):
with open(file_path, "r") as f:
data = csv.reader(f, skipinitialspace=True, quotechar='"')
data = [[c.replace('\ufeff', '') for c in row] for row in data]
fields = []
sites = []
auto_assignment_rules = []
for line in data:
if not fields:
for column in line:
fields.append(column.strip().replace("#", ""))
else:
site = {}
i = 0
subnet = None
for column in line:
field = fields[i]
if field == "subnet":
subnet = column
else:
if field.startswith("sitegroup_"):
column = _extract_groups(column)
site[field] = column
i += 1
if subnet:
auto_assignment_rules.append({
"src": "subnet",
"subnet": subnet,
"value": site["name"]
})
sites.append(site)
return sites, auto_assignment_rules
def _check_org_name_in_script_param(
apisession: mistapi.APISession, org_id: str, org_name: str = None
):
response = mistapi.api.v1.orgs.orgs.getOrg(apisession, org_id)
if response.status_code != 200:
console.critical(
f"Unable to retrieve the org information: {response.data}")
sys.exit(3)
org_name_from_mist = response.data["name"]
return org_name == org_name_from_mist
def _check_org_name(apisession: mistapi.APISession, org_id: str, org_name: str = None):
if not org_name:
response = mistapi.api.v1.orgs.orgs.getOrg(apisession, org_id)
if response.status_code != 200:
console.critical(
f"Unable to retrieve the org information: {response.data}")
sys.exit(3)
org_name = response.data["name"]
while True:
print()
resp = input(
"To avoid any error, please confirm the current destination orgnization name: ")
if resp == org_name:
return org_id, org_name
else:
print()
print("The orgnization names do not match... Please try again...")
def _create_org(apisession: mistapi.APISession, custom_dest_org_name: str = None):
while True:
if not custom_dest_org_name:
custom_dest_org_name = input("Organization name? ")
if custom_dest_org_name:
org = {
"name": custom_dest_org_name
}
message = f"Creating the organization \"{custom_dest_org_name}\" in {apisession.get_cloud()} "
PB.log_message(message, display_pbar=False)
try:
PB.log_success(message, display_pbar=False)
except Exception as e:
PB.log_failure(message, display_pbar=False)
LOGGER.error("Exception occurred", exc_info=True)
sys.exit(10)
org_id = mistapi.api.v1.orgs.orgs.createOrg(
apisession, org).data["id"]
return org_id, custom_dest_org_name
def _select_dest_org(apisession: mistapi.APISession):
print()
print(" Destination Org ".center(80, "-"))
print()
while True:
res = input(
"Do you want to import into a (n)ew organization or (e)xisting one, (q) to quit? ")
if res.lower() == "q":
sys.exit(0)
elif res.lower() == "e":
org_id = mistapi.cli.select_org(apisession)[0]
org_name = mistapi.api.v1.orgs.orgs.getOrg(
apisession, org_id).data["name"]
if _check_org_name(apisession, org_id, org_name):
return org_id, org_name
elif res.lower() == "n":
return _create_org(apisession)
def start(
apisession: mistapi.APISession,
file_path: str,
org_id: str = None,
org_name: str = None,
google_api_key:str = None
):
'''
Start the process to create the sites
PARAMS
-------
:param mistapi.APISession apisession mistapi session with `Super User` access the source
Org, already logged in
:param str file_path path to the CSV file with all the sites to create
:param str org_id Optional, org_id of the org where to process the sites
:param str org_name Optional, name of the org where to process the sites
(used for validation)
:param str google_api_key Optional, Google API key used for geocoding.
If not set, the script will use timezonefinder and