-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapic_cobra.py
More file actions
91 lines (76 loc) · 3.23 KB
/
apic_cobra.py
File metadata and controls
91 lines (76 loc) · 3.23 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
## Imports
import json
import time
import warnings
from getpass import getpass
from cobra.mit import access, request, session # for accessing the APIC object
from cobra.mit.request import CommitError, RestError
from cobra.model import ctrlr, fabric, pol # for creating new fv objects
from requests.exceptions import ConnectionError, HTTPError, InvalidURL
from rich import print
import nodes
def register_fabric_nodes(): # sourcery skip: extract-method
# Suppress HTTPS warning
warnings.filterwarnings(action="ignore", message=r"Unverified\sHTTPS\srequest\s.*")
## Inputs
nodes_file = input("Nodes Excel file: ").strip() or "Fabric-Nodes.xlsx"
apic = input("APIC IP Address: ").strip() or "sandboxapicdc.cisco.com"
usr = input("Username: ").strip() or "admin"
pwd = getpass(prompt="Password: ").strip() or "!v3G@!4@Y"
## Processing
apic_session = session.LoginSession(
controllerUrl=f"https://{apic}",
user=usr,
password=pwd,
secure=False,
requestFormat="json",
)
moDir = access.MoDirectory(session=apic_session)
try:
print(f"\nAccessing {apic_session.controllerUrl}...", end="\r")
moDir.login()
except (HTTPError, ConnectionError, InvalidURL) as e:
raise SystemExit(e) from e
else:
print(f"[green]Successfully accessed APIC as {apic_session.user}")
# select the top level object
polUni = pol.Uni(parentMoOrDn="")
ctrlrInst = ctrlr.Inst(parentMoOrDn=polUni)
fabric_nodes = nodes.read(nodes_file) # read ACI Nodes from Excel file
start_time = time.perf_counter()
# register fabric nodes
for node in fabric_nodes:
fabricNodeIdentPol = fabric.NodeIdentPol(parentMoOrDn=ctrlrInst)
fabricNodeIdentP = fabric.NodeIdentP(
parentMoOrDn=fabricNodeIdentPol,
name=node.get("name").strip(),
serial=node.get("serial").strip(),
podId=node.get("pod_id") or "1",
nodeId=node.get("node_id"),
nodeType=node.get("type").lower().strip(),
role=node.get("role").lower().strip(),
)
## Output
# Queuing the new configuration
cfg_request = request.ConfigRequest()
try:
cfg_request.addMo(mo=polUni) # offline validation
# Equavilant to POST request
# Commiting (Submitting) new configuration
response = moDir.commit(configObject=cfg_request)
except (CommitError, RestError) as e:
print(
f"[red]{json.loads(e.reason).get('imdata')[0].get('error').get('attributes').get('text')}"
)
else:
if response.status_code in (200, 201):
print(
f"[magenta]Registered {fabricNodeIdentP.name} with ID {fabricNodeIdentP.nodeId} and serial number {fabricNodeIdentP.serial}"
)
print(f"EET: {time.perf_counter() - start_time:.2f} second")
if moDir.session.cookie:
moDir.logout() # logout
print("[yellow]Logged out")
if __name__ == "__main__":
register_fabric_nodes()