-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab3_task4_create_interface_policy_ESXi-1.py
125 lines (113 loc) · 3.87 KB
/
lab3_task4_create_interface_policy_ESXi-1.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
import requests
import json
from pprint import pprint
requests.packages.urllib3.disable_warnings()
USERNAME = "admin"
PASSWORD = "1234QWer"
URL = "https://192.168.10.1/"
# 1. Authentication
payload = {
'aaaUser': {
'attributes': {
'name': USERNAME,
'pwd': PASSWORD
}
}
}
auth = requests.post(f'{URL}api/aaaLogin.json', data=json.dumps(payload), verify=False)
COOKIES = auth.cookies
# Create interface policy
INTERFACE_POLICY_NAME = 'APILAB-ESXi-1'
LACP_POLICY = 'APILAB-LACP-active'
CDP_POLICY = 'APILAB-CDP-ON'
LLDP_POLICY = 'APILAB-LLDP-TX-RX'
STP_POLICY = 'APILAB-BPDUguard-enabled'
payload_task_ifpol = {
'infraAccBndlGrp' : {
'attributes': {
'dn': f'uni/infra/funcprof/accbundle-{INTERFACE_POLICY_NAME}',
'lagT': 'node',
'name': INTERFACE_POLICY_NAME,
'rn': 'accbundle-{INTERFACE_POLICY_NAME}',
'status': 'created'
},
'children': [
{'infraRsLacpPol': {
'attributes': {
'tnLacpLagPolName': LACP_POLICY,
'status': 'created,modified'
},
'children': []
}
},
{'infraRsCdpIfPol': {
'attributes': {
'tnCdpIfPolName': CDP_POLICY,
'status': 'created,modified'
},
'children': []
}
},
{'infraRsLldpIfPol': {
'attributes': {
'tnLldpIfPolName': LLDP_POLICY,
'status': 'created,modified'
},
'children': []
}
},
{'infraRsStpIfPol': {
'attributes': {
'tnStpIfPolName': STP_POLICY,
'status': 'created,modified'
},
'children': []
}
},
]
}
}
result_ifpol = requests.post(f'{URL}api/node/mo/uni/infra/funcprof/accbundle-{INTERFACE_POLICY_NAME}.json', data=json.dumps(payload_task_ifpol), verify = False, cookies=COOKIES)
print('Return code: {code}'.format(code=result_ifpol.status_code))
pprint(result_ifpol.content)
# Attach policy to interface
LEAF_INTERFACE_PROFILE = 'APILAB-Leaf-101-102'
INTERFACE_POLICY_GROUP = 'APILAB-ESXi-1'
ACCESS_GROUP = 'APILAB-ESXi-1'
PORT_BLOCK = 'block1'
INTERFACE = '4'
payload_task_attach_policy = {
'infraHPortS' : {
'attributes': {
'dn': f'uni/infra/accportprof-{LEAF_INTERFACE_PROFILE}/hports-{INTERFACE_POLICY_GROUP}-typ-range',
'name': INTERFACE_POLICY_GROUP,
'rn': f'hports-{INTERFACE_POLICY_GROUP}-typ-range',
'status': 'created,modified',
},
'children': [
{'infraPortBlk': {
'attributes': {
'dn': f'uni/infra/accportprof-{LEAF_INTERFACE_PROFILE}/hports-{INTERFACE_POLICY_GROUP}-typ-range/portblk-{PORT_BLOCK}',
'name': PORT_BLOCK,
'fromPort': INTERFACE,
'toPort': INTERFACE,
'rn': f'portblk-{PORT_BLOCK}',
'status': 'created,modified'
},
'children': []
}
},
{'infraRsAccBaseGrp': {
'attributes': {
'tDn': f'uni/infra/funcprof/accbundle-{ACCESS_GROUP}',
'status': 'created,modified'
},
'children': []
}
}
]
}
}
result_attach_pol = requests.post(f'{URL}api/node/mo/uni/infra/accportprof-{LEAF_INTERFACE_PROFILE}/hports-{INTERFACE_POLICY_GROUP}-typ-range.json', data=json.dumps(payload_task_attach_policy), verify = False, cookies=COOKIES)
print('Return code: {code}'.format(code=result_attach_pol.status_code))
pprint(result_attach_pol.content)