-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsn-new-seg.py
executable file
·168 lines (159 loc) · 6.53 KB
/
bsn-new-seg.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
#!/usr/bin/env python3
import argparse
import os
import sys
import requests
import re
requests.packages.urllib3.disable_warnings()
username = os.getenv('BSNUSER')
password = os.getenv('BSNPASS')
p_name = os.path.basename(sys.argv[0])
parser = argparse.ArgumentParser(description='BCF build segment')
parser.add_argument('controller', type=str, help='lab, dc01, dc02')
args = parser.parse_args()
base_url = ''
cookie = ''
def clear():
_ = (os.system('clear') if os.name =='posix' else os.system('cls'))
def display_error(error_message):
print(error_message)
print("[OPTION] lab")
print("[OPTION] dc01")
print("[OPTION] dc02")
print("example: %s lab" % p_name)
sys.exit(1)
def controller_check(controller):
global base_url
if controller == 'lab':
base_url = "mn-pcclab-pnet-ctlr:8443"
elif controller == 'dc01':
base_url = "dc01-bcf-ctrl:8443"
elif controller == 'dc02':
base_url = "dc02-bcf-ctrl:8443"
else:
error_message = "ERROR: incorrect controller"
display_error(error_message)
return base_url
def add_ntnx(vlan_id, tenant, segment):
if cookie:
session_cookie = 'session_cookie=%s' % cookie
headers = {"content-type": "application/json", 'Cookie': session_cookie}
ig_list = []
path = '/api/v1/data/controller/applications/bcf/info/fabric/interface-group/summary'
groups = requests.get('https://' + base_url + path, headers=headers, verify=False).json()
nutanix_clusters = []
path = '/api/v1/data/controller/applications/vendor/nutanix/prism-server'
prism = requests.get('https://' + base_url + path, headers=headers, verify=False).json()
ahv_regex = re.compile('^.+ahv.+')
nutanix_regex = re.compile('^nutanix.+')
for ig in groups:
if nutanix_regex.match(ig['name']):
ig.pop("name", None)
elif ahv_regex.match(ig['name']):
ig_list.append(ig['name'])
sorted_ig = sorted(set(ig_list))
for i in sorted_ig:
print(" member interface-group %s vlan %s" % (i, vlan_id))
for cluster in prism:
print()
print("nutanix-prism %s" % cluster['name'])
print(" manage-segment-for-vlan vlan %s tenant %s segment %s" % (vlan_id, tenant, segment))
kill_session()
else:
print("I've just picked up a fault in the AE-35 unit.")
sys.exit(1)
def base(base_url):
tenant = str(input("tenant name: "))
segment = str(input("segment name: "))
vlan_id = int(input("vlan id: "))
l3_needed = str(input("configure L3 subnet for this segment: (y/n) "))
if l3_needed =='y':
l3_addrmask = str(input("address and mask in the form of x.x.x.x/xx: "))
dhcp_needed = str(input("configure DHCP relay: (y/n) "))
system_needed = str(input("connect to system tenant: (y/n) "))
core_needed = str(input("add core L2 interface: (y/n) "))
seg_needed = str(input("add seg-fw L2 interface: (y/n) "))
f5_needed = str(input("add F5 L2 interface: (y/n) "))
ntnx_needed = str(input("add to nutanix: (y/n) "))
if base_url == 'mn-pcclab-pnet-ctlr:8443':
corel2 = "mn-pcclab-core-sw-vpc17"
fw1l2 = "mn-pcclab-eia-fw01-Internal-Seg-pri"
fw2l2 = "mn-pcclab-eia-fw01-Internal-Seg-sec"
f51l2 = "NO_LAB_F5"
f52l2 = "NO_LAB_F5"
elif base_url == 'dc01-bcf-ctrl:8443':
corel2 = "dc01-7k-core-vpc70"
fw1l2 = "dc01-intsegfw01-a-ae3"
fw2l2 = "dc01-intsegfw01-b-ae3"
f51l2 = "DOES_NOT_YET_EXIST"
f52l2 = "DOES_NOT_YET_EXIST"
elif base_url == 'dc02-bcf-ctrl:8443':
corel2 = "dc02-n7k-vpc71"
fw1l2 = "dc02-intsegfw01-a-ae2"
fw2l2 = "dc02-intsegfw01-b-ae2"
f51l2 = "dc02-lb-01a"
f52l2 = "dc02-lb-01b"
clear()
print("----------")
if system_needed == 'y':
print("tenant system")
print(" logical-router")
print(" interface tenant %s" % tenant)
print(" export-route")
print("tenant %s" % tenant)
if system_needed == 'y' and l3_needed == 'y':
print(" logical-router")
print(" route 0.0.0.0/0 next-hop tenant system")
print(" interface tenant system")
print(" import-route")
print(" interface segment %s" % segment)
print(" ip address %s" % l3_addrmask)
if dhcp_needed == 'y':
print(" dhcp-relay server-ip 172.16.241.8")
print(" dhcp-relay server-ip 172.16.250.254")
elif system_needed == 'n' and l3_needed == 'y':
print(" logical-router")
print(" interface segment %s" % segment)
print(" ip address %s" % l3_addrmask)
if dhcp_needed == 'y':
print(" dhcp-relay server-ip 172.16.241.8")
print(" dhcp-relay server-ip 172.16.250.254")
elif system_needed == 'y' and l3_needed == 'n':
print(" logical-router")
print(" route 0.0.0.0/0 next-hop tenant system")
print(" interface tenant system")
print(" import-route")
print(" segment %s" % segment)
if core_needed == 'y':
print(" member interface-group %s vlan %s" % (corel2, vlan_id))
if seg_needed == 'y':
print(" member interface-group %s vlan %s" % (fw1l2, vlan_id))
print(" member interface-group %s vlan %s" % (fw2l2, vlan_id))
if f5_needed == 'y':
print(" member interface-group %s vlan %s" % (f51l2, vlan_id))
print(" member interface-group %s vlan %s" % (f52l2, vlan_id))
if ntnx_needed == 'y':
add_ntnx(vlan_id, tenant, segment)
kill_session()
def get_cookie(base_url):
global cookie
login_payload = '{"user":"%s", "password":"%s"}' % (username, password)
login_headers = {'Content-Type': "application/json"}
path = '/api/v1/auth/login'
l = requests.post(
'https://' + base_url + path, headers=login_headers, data=login_payload, verify=False)
if l.cookies:
cookie = l.cookies['session_cookie']
else:
print("Couldn't get a session cookie, check username/password")
sys.exit(1)
return cookie
def kill_session():
session_cookie = 'session_cookie=%s' % cookie
headers = {"content-type": "application/json", 'Cookie': session_cookie}
path = '/api/v1/data/controller/core/aaa/session[auth-token="%s"]' % cookie
requests.delete('https://' + base_url + path, headers=headers, verify=False)
if __name__ == '__main__':
get_cookie(controller_check(args.controller))
base(base_url)
kill_session()