blob: f2c07d0aa3f79b9679665fe1aad0da9b6fde2383 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Chetan Gaonkera0e8cce2017-01-20 23:27:29 +000017#
18# Copyright 2016-present Ciena Corporation
19#
20# Licensed under the Apache License, Version 2.0 (the "License");
21# you may not use this file except in compliance with the License.
22# You may obtain a copy of the License at
23#
24# http://www.apache.org/licenses/LICENSE-2.0
25#
26# Unless required by applicable law or agreed to in writing, software
27# distributed under the License is distributed on an "AS IS" BASIS,
28# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29# See the License for the specific language governing permissions and
30# limitations under the License.
31#
32import os
33import sys
34import glanceclient
35
36from keystoneclient.v2_0 import client
37from keystoneclient import utils
38from novaclient import client as novaclient
39from keystoneclient import client as keystoneclient
40from neutronclient.neutron import client as neutronclient
41
42
43def keystone_client_version():
44 api_version = os.getenv('OS_IDENTITY_API_VERSION')
45 if api_version is not None:
46 log.info("Version is set in env as '%s'",
47 api_version)
48 return api_version
49 return SET_API_VERSION
50
51
52def keystone_client(other_creds={}):
53 sess = session(other_creds)
54 return keystoneclient.Client(keystone_client_version(), session=sess)
55
56
57def nova_client_version():
58 api_version = os.getenv('OS_COMPUTE_API_VERSION')
59 if api_version is not None:
60 log.info("OS_COMPUTE_API_VERSION is set in env as '%s'",
61 api_version)
62 return api_version
63 return SET_API_VERSION
64
65
66def nova_client(other_creds={}):
67 sess = session(other_creds)
68 return novaclient.Client(nova_client_version(), session=sess)
69
70
71def neutron_client_version():
72 api_version = os.getenv('OS_NETWORK_API_VERSION')
73 if api_version is not None:
74 log.info("OS_NETWORK_API_VERSION is set in env as '%s'",
75 api_version)
76 return api_version
77 return SET_API_VERSION
78
79
80def neutron_client(other_creds={}):
81 sess = session(other_creds)
82 return neutronclient.Client(neutron_client_version(), session=sess)
83
84
85def glance_client_version():
86 api_version = os.getenv('OS_IMAGE_API_VERSION')
87 if api_version is not None:
88 log.info("OS_IMAGE_API_VERSION is set in env as '%s'", api_version)
89 return api_version
90 return SET_API_VERSION
91
92
93def glance_client(other_creds={}):
94 sess = session(other_creds)
95 return glanceclient.Client(glance_client_version(), session=sess)
96
97def network_list(neutron_client):
98 network_list = neutron_client.list_networks()['networks']
99 if len(network_list) == 0:
100 return None
101 else:
102 return network_list
103
104
105def router_list(neutron_client):
106 router_list = neutron_client.list_routers()['routers']
107 if len(router_list) == 0:
108 return None
109 else:
110 return router_list
111
112
113def port_list(neutron_client):
114 port_list = neutron_client.list_ports()['ports']
115 if len(port_list) == 0:
116 return None
117 else:
118 return port_list
119
120
121def network_id(neutron_client, network_name):
122 networks = neutron_client.list_networks()['networks']
123 id = ''
124 for n in networks:
125 if n['name'] == network_name:
126 id = n['id']
127 break
128 return id
129
130
131def subnet_id(neutron_client, subnet_name):
132 subnets = neutron_client.list_subnets()['subnets']
133 id = ''
134 for s in subnets:
135 if s['name'] == subnet_name:
136 id = s['id']
137 break
138 return id
139
140
141def router_id(neutron_client, router_name):
142 routers = neutron_client.list_routers()['routers']
143 id = ''
144 for r in routers:
145 if r['name'] == router_name:
146 id = r['id']
147 break
148 return id
149
150
151def private_net(neutron_client):
152 networks = neutron_client.list_networks()['networks']
153 if len(networks) == 0:
154 return None
155 for net in networks:
156 if (net['router:external'] is False) and (net['shared'] is True):
157 return net
158 return None
159
160
161def external_net(neutron_client):
162 for network in neutron_client.list_networks()['networks']:
163 if network['router:external']:
164 return network['name']
165 return None
166
167
168def external_net_id(neutron_client):
169 for network in neutron_client.list_networks()['networks']:
170 if network['router:external']:
171 return network['id']
172 return None
173
174
175def check_neutron_net(neutron_client, net_name):
176 for network in neutron_client.list_networks()['networks']:
177 if network['name'] == net_name:
178 for subnet in network['subnets']:
179 return True
180 return False
181
182
183def create_neutron_net(neutron_client, name):
184 json_body = {'network': {'name': name,
185 'admin_state_up': True}}
186 try:
187 network = neutron_client.create_network(body=json_body)
188 net_sett = network['network']
189 return net_sett['id']
190 except Exception, e:
191 log.info("Error [create_neutron_net(neutron_client, '%s')]: %s"
192 % (name, e))
193 return None
194
195
196def create_neutron_subnet(neutron_client, name, cidr, net_id):
197 json_body = {'subnets': [{'name': name, 'cidr': cidr,
198 'ip_version': 4, 'network_id': net_id}]}
199 try:
200 subnet = neutron_client.create_subnet(body=json_body)
201 return subnet['subnets'][0]['id']
202 except Exception, e:
203 log.info("Error [create_neutron_subnet(neutron_client, '%s', "
204 "'%s', '%s')]: %s" % (name, cidr, net_id, e))
205 return None
206
207
208def create_neutron_router(neutron_client, name):
209 json_body = {'router': {'name': name, 'admin_state_up': True}}
210 try:
211 router = neutron_client.create_router(json_body)
212 return router['router']['id']
213 except Exception, e:
214 log.info("Error [create_neutron_router(neutron_client, '%s')]: %s"
215 % (name, e))
216 return None
217
218
219def create_neutron_port(neutron_client, name, network_id, ip):
220 json_body = {'port': {
221 'admin_state_up': True,
222 'name': name,
223 'network_id': network_id,
224 'fixed_ips': [{"ip_address": ip}]
225 }}
226 try:
227 port = neutron_client.create_port(body=json_body)
228 return port['port']['id']
229 except Exception, e:
230 log.info("Error [create_neutron_port(neutron_client, '%s', '%s', "
231 "'%s')]: %s" % (name, network_id, ip, e))
232 return None
233
234
235def update_neutron_net(neutron_client, network_id, shared=False):
236 json_body = {'network': {'shared': shared}}
237 try:
238 neutron_client.update_network(network_id, body=json_body)
239 return True
240 except Exception, e:
241 log.info("Error [update_neutron_net(neutron_client, '%s', '%s')]: "
242 "%s" % (network_id, str(shared), e))
243 return False
244
245
246def update_neutron_port(neutron_client, port_id, device_owner):
247 json_body = {'port': {
248 'device_owner': device_owner,
249 }}
250 try:
251 port = neutron_client.update_port(port=port_id,
252 body=json_body)
253 return port['port']['id']
254 except Exception, e:
255 log.info("Error [update_neutron_port(neutron_client, '%s', '%s')]:"
256 " %s" % (port_id, device_owner, e))
257 return None
258
259
260def add_interface_router(neutron_client, router_id, subnet_id):
261 json_body = {"subnet_id": subnet_id}
262 try:
263 neutron_client.add_interface_router(router=router_id, body=json_body)
264 return True
265 except Exception, e:
266 log.info("Error [add_interface_router(neutron_client, '%s', "
267 "'%s')]: %s" % (router_id, subnet_id, e))
268 return False
269
270
271def add_gateway_router(neutron_client, router_id):
272 ext_net_id = external_net_id(neutron_client)
273 router_dict = {'network_id': ext_net_id}
274 try:
275 neutron_client.add_gateway_router(router_id, router_dict)
276 return True
277 except Exception, e:
278 log.info("Error [add_gateway_router(neutron_client, '%s')]: %s"
279 % (router_id, e))
280 return False
281
282
283def delete_neutron_net(neutron_client, network_id):
284 try:
285 neutron_client.delete_network(network_id)
286 return True
287 except Exception, e:
288 log.info("Error [delete_neutron_net(neutron_client, '%s')]: %s"
289 % (network_id, e))
290 return False
291
292
293def delete_neutron_subnet(neutron_client, subnet_id):
294 try:
295 neutron_client.delete_subnet(subnet_id)
296 return True
297 except Exception, e:
298 log.info("Error [delete_neutron_subnet(neutron_client, '%s')]: %s"
299 % (subnet_id, e))
300 return False
301
302
303def delete_neutron_router(neutron_client, router_id):
304 try:
305 neutron_client.delete_router(router=router_id)
306 return True
307 except Exception, e:
308 log.info("Error [delete_neutron_router(neutron_client, '%s')]: %s"
309 % (router_id, e))
310 return False
311
312
313def delete_neutron_port(neutron_client, port_id):
314 try:
315 neutron_client.delete_port(port_id)
316 return True
317 except Exception, e:
318 log.info("Error [delete_neutron_port(neutron_client, '%s')]: %s"
319 % (port_id, e))
320 return False
321
322
323def remove_interface_router(neutron_client, router_id, subnet_id):
324 json_body = {"subnet_id": subnet_id}
325 try:
326 neutron_client.remove_interface_router(router=router_id,
327 body=json_body)
328 return True
329 except Exception, e:
330 log.info("Error [remove_interface_router(neutron_client, '%s', "
331 "'%s')]: %s" % (router_id, subnet_id, e))
332 return False
333
334
335def remove_gateway_router(neutron_client, router_id):
336 try:
337 neutron_client.remove_gateway_router(router_id)
338 return True
339 except Exception, e:
340 log.info("Error [remove_gateway_router(neutron_client, '%s')]: %s"
341 % (router_id, e))
342 return False
343
344
345def create_network_full(neutron_client,
346 net_name,
347 subnet_name,
348 router_name,
349 cidr):
350
351 # Check if the network already exists
352 network_id = network_id(neutron_client, net_name)
353 subnet_id = subnet_id(neutron_client, subnet_name)
354 router_id = router_id(neutron_client, router_name)
355
356 if network_id != '' and subnet_id != '' and router_id != '':
357 log.info("A network with name '%s' already exists..." % net_name)
358 else:
359 neutron_client.format = 'json'
360 log.info('Creating neutron network %s...' % net_name)
361 network_id = create_neutron_net(neutron_client, net_name)
362
363 if not network_id:
364 return False
365
366 log.info("Network '%s' created successfully" % network_id)
367 log.info('Creating Subnet....')
368 subnet_id = create_neutron_subnet(neutron_client, subnet_name,
369 cidr, network_id)
370 if not subnet_id:
371 return None
372
373 log.info("Subnet '%s' created successfully" % subnet_id)
374 log.info('Creating Router...')
375 router_id = create_neutron_router(neutron_client, router_name)
376
377 if not router_id:
378 return None
379
380 log.info("Router '%s' created successfully" % router_id)
381 log.info('Adding router to subnet...')
382
383 if not add_interface_router(neutron_client, router_id, subnet_id):
384 return None
385
386 log.info("Interface added successfully.")
387
388 log.info('Adding gateway to router...')
389 if not add_gateway_router(neutron_client, router_id):
390 return None
391
392 log.info("Gateway added successfully.")
393
394 net_set = {'net_id': network_id,
395 'subnet_id': subnet_id,
396 'router_id': router_id}
397 return net_set
398
399
400def create_shared_network_full(net_name, subnt_name, router_name, subnet_cidr):
401 neutron_client = neutron_client()
402
403 net_set = create_network_full(neutron_client,
404 net_name,
405 subnt_name,
406 router_name,
407 subnet_cidr)
408 if net_set:
409 if not update_neutron_net(neutron_client,
410 net_set['net_id'],
411 shared=True):
412 log.info("Failed to update network %s..." % net_name)
413 return None
414 else:
415 log.info("Network '%s' is available..." % net_name)
416 else:
417 log.info("Network %s creation failed" % net_name)
418 return None
419 return net_set
420