-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeraki_object_loader.py
More file actions
executable file
·75 lines (59 loc) · 2.17 KB
/
Copy pathmeraki_object_loader.py
File metadata and controls
executable file
·75 lines (59 loc) · 2.17 KB
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
#!/usr/bin/env python3
import os
import sys
import time
import meraki
from meraki_utils import (
create_network_object,
create_network_object_group,
load_and_validate,
save_results,
)
def main():
api_key = os.getenv("MERAKI_API_KEY")
org_id = os.getenv("MERAKI_ORG_ID")
group_name = os.getenv("MERAKI_GROUP_NAME", "G_POLICY_OBJECTS")
input_file = os.getenv("MERAKI_INPUT_FILE", "lista.txt")
if not api_key:
print("Missing MERAKI_API_KEY environment variable.")
return 1
if not org_id:
print("Missing MERAKI_ORG_ID environment variable.")
return 1
print("Meraki Object Loader - START")
objects, errors = load_and_validate(input_file)
if errors:
print("Validation errors found in input file:")
for error in errors:
print(f" - {error}")
return 1
print(f"Loaded {len(objects)} valid entries.")
dashboard = meraki.DashboardAPI(api_key=api_key, print_console=False, suppress_logging=True)
created_ids = []
failed = []
for index, obj in enumerate(objects, start=1):
try:
print(f"[{index}/{len(objects)}] Creating: {obj['name']} ({obj['value']}) ... ", end="")
result = create_network_object(dashboard, org_id, obj)
if result and "id" in result:
created_ids.append(result["id"])
print("OK")
else:
print("Skipped (duplicate)")
time.sleep(0.2)
except Exception as error:
print("ERROR")
failed.append({"object": obj, "error": str(error)})
print(f"Created objects: {len(created_ids)}, failures: {len(failed)}")
if created_ids:
try:
group_result = create_network_object_group(api_key, org_id, group_name, created_ids)
if group_result:
print(f"Group '{group_name}' created successfully (ID: {group_result['id']})")
except Exception as error:
print(f"Group creation error: {error}")
save_results("objects_created.json", created_ids, failed)
print("Saved report to objects_created.json")
return 0
if __name__ == "__main__":
sys.exit(main())