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