-
Notifications
You must be signed in to change notification settings - Fork 0
/
vfx.py
executable file
·230 lines (189 loc) · 8.31 KB
/
vfx.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
#!/usr/bin/env python
"""
Required steps per fabric to create a vxlan overlay gateway
* Define an overlay-gateway name
* Set the gateway type to l2 extension
* Specify a loopback id to use for the VTEP
* Attach an RBridge ID
* Map a VLAN to a VNI
* Activate the overlay gateway
* Define a name for the remote site
* Specify remote site's IP address of VTEP
* Specify which vlans to extend
Example usage:
$ ./vfx.py create --hostname 10.254.11.17 --gw foo --loopback 100 \
--rbridge 100 --vlan 100 --vni 1600000 --remote_site site2 \
--remote_ip 200.200.200.200
$ ./vfx.py show --hostname 10.254.11.17 --gw sko_gateway
"""
import requests
import vfx_payload as PF
import sys
from xml.dom.minidom import parseString
import argparse
import logging
class overlay_gw(object):
"""class to manage vxlan overlay gateways"""
def __init__(self, name, hostname, username, password):
self.gateway_name = name
self.ext_type = 'layer2-extension'
self.config_url = "http://{}/rest/config/running".format(hostname)
self.session = requests.Session()
self.session.auth = (username, password)
self.session.headers = {'Accept': 'application/vnd.configuration.resource+xml'}
def vfextension_create(self, loopback, rbridge):
"""docstring for vfextensionn_create"""
self.tunnel_create()
self.overlay_type_set()
self.vtep_create(loopback)
self.rbridge_attach(rbridge)
def tunnel_create(self):
"""docstring for tunnel_create"""
payload = PF.overlay_gateway(self.gateway_name)
req = self.session.post(self.config_url, data=payload)
self.check_response(req)
return req
def tunnel_delete(self):
"""docstring for _tunnel_del"""
url = self.config_url + "/overlay-gateway/{}".format(self.gateway_name)
logging.info("url: %s", url)
payload = PF.overlay_gateway(self.gateway_name)
req = self.session.delete(url, data=payload)
self.check_response(req)
return req
def check_response(self, rsp):
"""docstring for check_response"""
logging.info("rsp code: %d: %s", rsp.status_code, rsp.reason)
if rsp.status_code not in (200, 201, 204):
logging.error("http request failed: %s" % rsp.reason)
logging.error("rc: %d\n%s", rsp.status_code, rsp.content)
raise Exception('Request Failed')
def overlay_type_set(self):
"""docstring for overlay_type_set"""
url = self.config_url + "/overlay-gateway/{}/type".format(
self.gateway_name)
logging.info("url: %s", url)
payload = PF.set_overlay_type(self.ext_type)
req = self.session.put(url, data=payload)
self.check_response(req)
return req
def vtep_create(self, loopback):
"""docstring for vtep_create"""
url = self.config_url + "/overlay-gateway/{}/type".format(
self.gateway_name)
url += "/ip/interface/Loopback"
logging.info("url: %s", url)
payload = PF.create_vtep(loopback)
req = self.session.put(url, data=payload)
self.check_response(req)
return req
def rbridge_attach(self, rbridge):
"""docstring for _rbridge_attach"""
url = self.config_url + "/overlay-gateway/{}/attach".format(
self.gateway_name)
logging.info("url: %s", url)
payload = PF.attach_rbridge(rbridge)
req = self.session.put(url, data=payload)
self.check_response(req)
return req
def vlan_to_vni_map(self, vlan, vni):
"""docstring for vlan_to_vni_map"""
url = self.config_url + "/overlay-gateway/{}/map".format(
self.gateway_name)
logging.info("url: %s", url)
payload = PF.vlan_to_vni_mapping(vlan, vni)
req = self.session.post(url, data=payload)
self.check_response(req)
return req
def gateway_activate(self):
"""docstring for gateway_activate"""
url = self.config_url + "/overlay-gateway/{}".format(self.gateway_name)
logging.info("url: %s", url)
payload = PF.activate_gw()
req = self.session.post(url, data=payload)
self.check_response(req)
return req
def remote_site_create(self, name, ipaddr):
"""docstring for remote_site_create"""
url = self.config_url + "/overlay-gateway/{}".format(self.gateway_name)
logging.info("url: %s", url)
payload = PF.define_remote_site(name)
req = self.session.post(url, data=payload)
self.check_response(req)
logging.info("rsp code: %d: %s", req.status_code, req.reason)
url += "/site/{}".format(name)
logging.info("url: %s", url)
payload = PF.add_remote_ip(ipaddr)
req = self.session.post(url, data=payload)
self.check_response(req)
return req
def vlan_extend(self, name, vlan):
"""docstring for vlan_extend"""
url = self.config_url + "/overlay-gateway/{}".format(self.gateway_name)
url += "/site/{}/extend".format(name)
logging.info("url: %s", url)
payload = PF.extend_vlan(vlan)
req = self.session.put(url, data=payload)
self.check_response(req)
return req
def tunnel_show(self, gw=None):
"""diplay information about tunnel name"""
url = self.config_url + "/overlay-gateway/"
if gw is not None:
url += self.gateway_name
logging.info("url: %s", url)
req = self.session.get(url)
self.check_response(req)
#NOTE overlay gateway details are not complete
data = "<xml>\n" + req.content + "</xml>\r\n"
dom = parseString(data)
gateway = dom.getElementsByTagName('overlay-gateway')[0]
print "GW Name: " + gateway.getElementsByTagName(
'name')[0].firstChild.nodeValue
if gw is not None:
print "Active: " + gateway.getElementsByTagName(
'activate')[0].firstChild.nodeValue
print "Type: " + gateway.getElementsByTagName(
'type')[0].firstChild.nodeValue
print "RBridge_ID " + gateway.getElementsByTagName(
'add')[0].firstChild.nodeValue
print "VLAN: " + gateway.getElementsByTagName(
'vlan')[0].firstChild.nodeValue
def main():
parser = argparse.ArgumentParser(
description='Brocade VCS Overlay Gateway Builder')
parser.add_argument('action', choices=['create', 'delete', 'update', 'show'])
parser.add_argument("--hostname", required=True, help="VCS Managment IP")
parser.add_argument("--username", default='admin', help="login username")
parser.add_argument("--password", default='password', help="login password")
parser.add_argument("--gw", help="tunnel gateway name")
parser.add_argument('--loopback', help='loopback id to create vtep')
parser.add_argument('--vlan', help='vlan to extend')
parser.add_argument('--vni', help='map vlan to this vni')
parser.add_argument('--rbridge', help='rbridge id to carry gateway')
parser.add_argument('--remote_site', help='remote site name')
parser.add_argument('--remote_ip', help='ip addr of remote site vtep')
parser.add_argument('--verbose', action='store_true',
help='display debug output')
args = parser.parse_args()
if args.verbose == True:
logging.basicConfig(level=logging.INFO)
gw = overlay_gw(args.gw, args.hostname, args.username, args.password)
if args.action == 'create':
try:
gw.vfextension_create(args.loopback, args.rbridge)
gw.vlan_to_vni_map(args.vlan, args.vni)
gw.gateway_activate()
gw.remote_site_create(args.remote_site, args.remote_ip)
gw.vlan_extend(args.remote_site, args.vlan)
except:
print 'something went wrong :('
elif args.action == 'update':
#TODO: update gw supports
print 'not implemented'
elif args.action == 'delete':
gw.tunnel_delete()
elif args.action == 'show':
gw.tunnel_show(gw=args.gw)
if __name__ == '__main__':
main()