-
Notifications
You must be signed in to change notification settings - Fork 0
/
devclass.py
76 lines (67 loc) · 3.03 KB
/
devclass.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
import json
import requests
import time
class APIC_CLUSTER():
def __init__(self, ip_add, username="cisco", password="cisco", token="x", cookie={}):
self.ip_add = ip_add
self.username = username
self.password = password
self.url = "https://" + ip_add + "/api/"
self.token = token
self.cookie = cookie
def acilogin(self):
name_pwd = {'aaaUser': {'attributes': {'name': self.username, 'pwd': self.password}}}
json_cred = json.dumps(name_pwd)
# log in to API
login_url = self.url + 'aaaLogin.json'
post_response = requests.post(login_url, data=json_cred, verify=False)
# get token from login response structure
print(post_response)
auth_push = json.loads(post_response.text)
login_attributes = auth_push['imdata'][0]['aaaLogin']['attributes']
auth_token_fetch = login_attributes['token']
## Creates cookie array from token ##
self.token = auth_token_fetch
def printtoken(self):
print("The APIC session token is " + self.token)
def printurl(self):
print("The APIC login url is " + self.url)
class EPG():
def __init__(self, apic, tenant, full_dn):
self.apic = apic
self.tenant = tenant
self.full_dn = full_dn
for value in self.full_dn.split("/"):
if "ap-" in value:
self.fullapname = value
self.ap = value[3:]
if "epg-" in value:
self.fullepgname = value
self.name = value[4:]
def getConfigAPIC(self, config="x"):
COOKIES_VAR = {}
COOKIES_VAR['APIC-Cookie'] = self.apic.token
url = "https://" + self.apic.ip_add + "/api/mo/" + self.full_dn + ".xml?rsp-subtree=full&rsp-prop-include=config-only"
config_xml = requests.get(url, cookies=COOKIES_VAR, verify=False).text
# Strips XML headers from response #
config = "<fvAEPg" + config_xml.split("fvAEPg")[1] + "fvAEPg>"
self.config = config
return self.config
def deleteEPG(self):
COOKIES_VAR = {}
COOKIES_VAR['APIC-Cookie'] = self.apic.token
# Prepares URL for deletion #
POST_URL = "https://" + self.apic.ip_add + "/api/mo/uni.xml"
print("Deleting OLD epg.................")
DELETE = '<fvTenant name="' + self.tenant + '"><fvAp name="' + self.ap + '"><fvAEPg descr="" dn="' + self.full_dn + '" name="' + self.name + '" status=deleted></fvAEPg></fvAp></fvTenant>'
POST = requests.post(POST_URL, data=DELETE, cookies=COOKIES_VAR, verify=False)
time.sleep(1)
def addEPG(self, cfg):
COOKIES_VAR = {}
COOKIES_VAR['APIC-Cookie'] = self.apic.token
# Prepares URL for add #
POST_URL = "https://" + self.apic.ip_add + "/api/mo/uni.xml"
print("Adding NEW epg.................")
EPG_POST = requests.post(POST_URL, data=cfg, cookies=COOKIES_VAR, verify=False)
time.sleep(1)
print("\n\n\nNew EPG (Rename) has been pushed. Please verify on APIC GUI or CLI\n\n\n")