-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_fabric.py
291 lines (241 loc) · 10.8 KB
/
create_fabric.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (c) 2022 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
"""
__author__ = "Gabriel Zapodeanu TME, ENB"
__email__ = "[email protected]"
__version__ = "0.1.0"
__copyright__ = "Copyright (c) 2022 Cisco and/or its affiliates."
__license__ = "Cisco Sample Code License, Version 1.1"
import os
import time
import requests
import urllib3
import json
import sys
import logging
import datetime
import yaml
from urllib3.exceptions import InsecureRequestWarning # for insecure https warnings
from dotenv import load_dotenv
from dnacentersdk import DNACenterAPI
from datetime import datetime
from pprint import pprint
from requests.auth import HTTPBasicAuth # for Basic Auth
urllib3.disable_warnings(InsecureRequestWarning) # disable insecure https warnings
load_dotenv('environment.env')
DNAC_URL = os.getenv('DNAC_URL')
DNAC_USER = os.getenv('DNAC_USER')
DNAC_PASS = os.getenv('DNAC_PASS')
os.environ['TZ'] = 'America/Los_Angeles' # define the timezone for PST
time.tzset() # adjust the timezone, more info https://help.pythonanywhere.com/pages/SettingTheTimezone/
DNAC_AUTH = HTTPBasicAuth(DNAC_USER, DNAC_PASS)
def get_dnac_token(dnac_auth):
"""
Create the authorization token required to access Cisco DNA Center
Call to Cisco DNA Center - /api/system/v1/auth/login
:param dnac_auth - Cisco DNA Center Basic Auth string
:return Cisco DNA Center Token
"""
url = DNAC_URL + '/dna/system/api/v1/auth/token'
header = {'content-type': 'application/json'}
response = requests.post(url, auth=dnac_auth, headers=header, verify=False)
response_json = response.json()
dnac_jwt_token = response_json['Token']
return dnac_jwt_token
def create_fabric_site(site_hierarchy, dnac_token):
"""
This function will create a new fabric at the site with the hierarchy {site_hierarchy}
:param site_hierarchy: site hierarchy, for example {Global/OR/PDX-1/Floor-2}
:param dnac_token: Cisco DNA Center auth token
:return: response in JSON
"""
payload = {
"siteNameHierarchy": site_hierarchy
}
url = DNAC_URL + '/dna/intent/api/v1/business/sda/fabric-site'
header = {'content-type': 'application/json', 'x-auth-token': dnac_token}
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
response_json = response.json()
return response_json
def add_edge_device(device_ip, site_hierarchy, dnac_token):
"""
This function will add the device with the management IP address {device_ip}, as an edge device, to the fabric at
the site with the hierarchy {site_hierarchy}
:param device_ip: device management IP address
:param site_hierarchy: fabric site hierarchy
:param dnac_token: Cisco DNA Center auth token
:return: API response
"""
url = DNAC_URL + '/dna/intent/api/v1/business/sda/edge-device'
payload = {
'deviceManagementIpAddress': device_ip,
'siteNameHierarchy': site_hierarchy
}
header = {'content-type': 'application/json', 'x-auth-token': dnac_token}
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
response_json = response.json()
return response_json
def add_control_plane_node(device_ip, site_hierarchy, dnac_token):
"""
This function will add the device with the management IP address {device_ip}, as a control-plane node to the fabric
at the site with the hierarchy {site_hierarchy}
:param device_ip: device management IP address
:param site_hierarchy: fabric site hierarchy
:param dnac_token: Cisco DNA Center auth token
:return: API response
"""
url = DNAC_URL + '/dna/intent/api/v1/business/sda/control-plane-device'
payload = {
'deviceManagementIpAddress': device_ip,
'siteNameHierarchy': site_hierarchy
}
header = {'content-type': 'application/json', 'x-auth-token': dnac_token}
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
response_json = response.json()
return response_json
def add_border_device(payload, dnac_token):
"""
This function will add a new border mode device to fabric
:param payload: the required payload per the API docs
:param dnac_token: Cisco DNA Center auth token
:return: API response
"""
url = DNAC_URL + '/dna/intent/api/v1/business/sda/border-device'
header = {'content-type': 'application/json', 'x-auth-token': dnac_token}
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
response_json = response.json()
return response_json
def create_l3_vn(l3_vn_name, site_hierarchy, dnac_token):
"""
This function will create a new L3 virtual network with the name {l3_vn_name} at the site
with the hierarchy {site_hierarchy}
:param l3_vn_name: L3 VN name
:param site_hierarchy: site hierarchy
:param dnac_token: Cisco DNA Center auth token
:return: API response
"""
url = DNAC_URL + '/dna/intent/api/v1/business/sda/virtual-network'
payload = {
'virtualNetworkName': l3_vn_name,
"siteNameHierarchy": site_hierarchy
}
header = {'content-type': 'application/json', 'x-auth-token': dnac_token}
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
response_json = response.json()
return response_json
def create_auth_profile(auth_profile, site_hierarchy, dnac_token):
"""
This function will create a new default auth profile for the fabric at the {site_hierarchy}
:param auth_profile: auth profile, enum { No Authentication , Open Authentication, Closed Authentication, Low Impact}
:param site_hierarchy: site hierarchy
:param dnac_token: Cisco DNA Center auth token
:return: API response
"""
url = DNAC_URL + '/dna/intent/api/v1/business/sda/authentication-profile'
payload = {
'siteNameHierarchy': site_hierarchy,
"authenticateTemplateName": auth_profile
}
header = {'content-type': 'application/json', 'x-auth-token': dnac_token}
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
response_json = response.json()
return response_json
def main():
"""
This app will create a new fabric at the site specified in the param provided.
"""
# logging basic
logging.basicConfig(level=logging.INFO)
current_time = str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
logging.info(' App "create_fabric.py" run start, ' + current_time)
with open('fabric_info.yml', 'r') as file:
project_data = yaml.safe_load(file)
# parse the input data
area_name = project_data['area_info']['name']
building_name = project_data['building_info']['name']
floor_name = project_data['floor_info']['name']
site_hierarchy = 'Global/' + area_name + '/' + building_name + '/' + floor_name
device_ips = project_data['devices_info']['device_ips']
ip_transit_pool_name = project_data['ip_transit_pool']['name']
l3_vn_name = project_data['l3_vn']['name']
border_device_ip = project_data['border_devices']['ip'][0]
routing_protocol = project_data['border_devices']['routing_protocol']
internal_bpg_as = str(project_data['border_devices']['internal_bgp_as'])
external_bpg_as = str(project_data['border_devices']['external_bgp_as'])
external_interface_name = project_data['border_devices']['external_interface']
transit_network = project_data['border_devices']['transit_network']
transit_vlan = str(project_data['border_devices']['transit_vlan'])
control_plane_device_ips = project_data['control_plane_devices']['ip']
edge_device_ips = project_data['edge_devices']['ip']
default_auth_profile = project_data['auth_profile']['name']
# Create a DNACenterAPI "Connection Object"
dnac_api = DNACenterAPI(username=DNAC_USER, password=DNAC_PASS, base_url=DNAC_URL, version='2.2.2.3', verify=False)
# get Cisco DNA Center Auth token
dnac_auth = get_dnac_token(DNAC_AUTH)
# create a new fabric at site
logging.info(' Creating new fabric at site:' + site_hierarchy)
response = create_fabric_site(site_hierarchy, dnac_auth)
time.sleep(15)
# assign Layer 3 VN to fabric
logging.info(' Assign L3 Virtual Network: ' + l3_vn_name)
response = create_l3_vn(l3_vn_name, site_hierarchy, dnac_auth)
time.sleep(5)
# add auth profile to fabric
logging.info(' Adding default auth profile to fabric: ' + default_auth_profile)
response = create_auth_profile(default_auth_profile, site_hierarchy, dnac_auth)
time.sleep(5)
# add control plane node to fabric
for device_ip in control_plane_device_ips:
logging.info(' Adding control plane devices to fabric: ' + device_ip)
response = add_control_plane_node(device_ip, site_hierarchy, dnac_auth)
time.sleep(2)
time.sleep(5)
# add border node to fabric
logging.info(' Adding a border node device: ' + border_device_ip)
border_payload = {
'deviceManagementIpAddress': border_device_ip,
'siteNameHierarchy': site_hierarchy,
'externalDomainRoutingProtocolName': routing_protocol,
'externalConnectivityIpPoolName': ip_transit_pool_name,
'internalAutonomouSystemNumber': internal_bpg_as,
'borderSessionType': 'External',
'connectedToInternet': True,
'externalConnectivitySettings': [
{
'interfaceName': external_interface_name,
'externalAutonomouSystemNumber': external_bpg_as,
'l3Handoff': [
{
'virtualNetwork': {
'virtualNetworkName': l3_vn_name,
'vlanId': transit_vlan
}
}
]
}
]
}
response = add_border_device(border_payload, dnac_auth)
time.sleep(5)
# add edge node devices to fabric
for device_ip in edge_device_ips:
logging.info(' Adding edge node devices to fabric: ' + device_ip)
response = add_edge_device(device_ip, site_hierarchy, dnac_auth)
time.sleep(2)
time.sleep(5)
date_time = str(datetime.now().replace(microsecond=0))
logging.info(' App "create_fabric.py" end, : ' + date_time)
if __name__ == '__main__':
sys.exit(main())