blob: e0b6831427e4a8ad2253bd944f1492c01a5d36f5 [file] [log] [blame]
Jiri Broulikf1b3aa42017-01-26 17:08:44 +01001# -*- coding: utf-8 -*-
2
3import logging
4from functools import wraps
5LOG = logging.getLogger(__name__)
Jiri Broulik5368cc52017-02-08 18:53:59 +01006
Jiri Broulikf1b3aa42017-01-26 17:08:44 +01007# Import third party libs
8HAS_NEUTRON = False
9try:
10 from neutronclient.v2_0 import client
11 HAS_NEUTRON = True
12except ImportError:
13 pass
14
15__opts__ = {}
16
17
18def __virtual__():
19 '''
20 Only load this module if neutron
21 is installed on this minion.
22 '''
23 if HAS_NEUTRON:
24 return 'neutronng'
25 return False
26
27
28def _autheticate(func_name):
29 '''
30 Authenticate requests with the salt keystone module and format return data
31 '''
32 @wraps(func_name)
33 def decorator_method(*args, **kwargs):
34 '''
35 Authenticate request and format return data
36 '''
37 connection_args = {'profile': kwargs.get('profile', None)}
38 nkwargs = {}
39 for kwarg in kwargs:
40 if 'connection_' in kwarg:
41 connection_args.update({kwarg: kwargs[kwarg]})
42 elif '__' not in kwarg:
43 nkwargs.update({kwarg: kwargs[kwarg]})
44 kstone = __salt__['keystone.auth'](**connection_args)
45 token = kstone.auth_token
46 endpoint = kstone.service_catalog.url_for(
47 service_type='network',
48 endpoint_type='publicURL')
49 neutron_interface = client.Client(
50 endpoint_url=endpoint, token=token)
51 return_data = func_name(neutron_interface, *args, **nkwargs)
52 if isinstance(return_data, list):
53 # format list as a dict for rendering
54 return {data.get('name', None) or data['id']: data
55 for data in return_data}
56 return return_data
57 return decorator_method
58
59
60@_autheticate
61def list_floatingips(neutron_interface, **kwargs):
62 '''
63 list all floatingips
64 CLI Example:
65 .. code-block:: bash
66 salt '*' neutron.list_floatingips
67 '''
68 return neutron_interface.list_floatingips(**kwargs)['floatingips']
69
70
71@_autheticate
72def list_security_groups(neutron_interface, **kwargs):
73 '''
74 list all security_groups
75 CLI Example:
76 .. code-block:: bash
77 salt '*' neutron.list_security_groups
78 '''
79 return neutron_interface.list_security_groups(**kwargs)['security_groups']
80
81
82@_autheticate
83def list_subnets(neutron_interface, **kwargs):
84 '''
85 list all subnets
86 CLI Example:
87 .. code-block:: bash
88 salt '*' neutron.list_subnets
89 '''
90 return neutron_interface.list_subnets(**kwargs)['subnets']
91
92
93@_autheticate
94def list_networks(neutron_interface, **kwargs):
95 '''
96 list all networks
97 CLI Example:
98 .. code-block:: bash
99 salt '*' neutron.list_networks
100 '''
101 return neutron_interface.list_networks(**kwargs)['networks']
102
103
104@_autheticate
105def list_ports(neutron_interface, **kwargs):
106 '''
107 list all ports
108 CLI Example:
109 .. code-block:: bash
110 salt '*' neutron.list_ports
111 '''
112 return neutron_interface.list_ports(**kwargs)['ports']
113
114
115@_autheticate
116def list_routers(neutron_interface, **kwargs):
117 '''
118 list all routers
119 CLI Example:
120 .. code-block:: bash
121 salt '*' neutron.list_routers
122 '''
123 return neutron_interface.list_routers(**kwargs)['routers']
124
125@_autheticate
126def update_floatingip(neutron_interface, fip, port_id=None):
127 '''
128 update floating IP. Should be used to associate and disassociate
129 floating IP with instance
130 CLI Example:
131 .. code-block:: bash
132 to associate with an instance's port
133 salt '*' neutron.update_floatingip openstack-floatingip-id port-id
134 to disassociate from an instance's port
135 salt '*' neutron.update_floatingip openstack-floatingip-id
136 '''
137 neutron_interface.update_floatingip(fip, {"floatingip":
138 {"port_id": port_id}})
139
140
141@_autheticate
142def update_subnet(neutron_interface, subnet_id, **subnet_params):
143 '''
144 update given subnet
145 CLI Example:
146 .. code-block:: bash
147 salt '*' neutron.update_subnet openstack-subnet-id name='new_name'
148 '''
149 neutron_interface.update_subnet(subnet_id, {'subnet': subnet_params})
150
151
152@_autheticate
153def update_network(neutron_interface, network_id, **net_params):
154 '''
155 Update give network
156 CLI Example:
157 .. code-block:: bash
158 salt '*' neutron.update_network openstack-net-id admin_state_up=false
159 '''
160 network_params = {}
161 for param in net_params:
162 if 'provider_' in param or 'router_' in param:
163 network_params[param.replace('_', ':', 1)] = net_params[param]
164 else:
165 network_params[param] = net_params[param]
166 LOG.info('ATTRIBUTES ' + str(network_params))
167 neutron_interface.update_network(network_id, {'network': network_params})
168
169
170@_autheticate
171def update_router(neutron_interface, router_id, **router_params):
172 '''
173 update given router
174 CLI Example:
175 .. code-block:: bash
176 salt '*' neutron.update_router openstack-router-id name='new_name'
177 external_gateway='openstack-network-id' administrative_state=true
178 '''
179 neutron_interface.update_router(router_id, {'router': router_params})
180
181
182@_autheticate
183def router_gateway_set(neutron_interface, router_id, external_gateway):
184 '''
185 Set external gateway for a router
186 CLI Example:
187 .. code-block:: bash
188 salt '*' neutron.update_router openstack-router-id openstack-network-id
189 '''
190 neutron_interface.update_router(
191 router_id, {'router': {'external_gateway_info':
192 {'network_id': external_gateway}}})
193
194
195@_autheticate
196def router_gateway_clear(neutron_interface, router_id):
197 '''
198 Clear external gateway for a router
199 CLI Example:
200 .. code-block:: bash
201 salt '*' neutron.update_router openstack-router-id
202 '''
203 neutron_interface.update_router(
204 router_id, {'router': {'external_gateway_info': None}})
205
206
207@_autheticate
208def create_router(neutron_interface, **router_params):
209 '''
210 Create OpenStack Neutron router
211 CLI Example:
212 .. code-block:: bash
213 salt '*' neutron.create_router name=R1
214 '''
215 response = neutron_interface.create_router({'router': router_params})
216 if 'router' in response and 'id' in response['router']:
217 return response['router']['id']
218
219
220@_autheticate
221def router_add_interface(neutron_interface, router_id, subnet_id):
222 '''
223 Attach router to a subnet
224 CLI Example:
225 .. code-block:: bash
226 salt '*' neutron.router_add_interface openstack-router-id subnet-id
227 '''
228 neutron_interface.add_interface_router(router_id, {'subnet_id': subnet_id})
229
230
231@_autheticate
232def router_rem_interface(neutron_interface, router_id, subnet_id):
233 '''
234 Dettach router from a subnet
235 CLI Example:
236 .. code-block:: bash
237 salt '*' neutron.router_rem_interface openstack-router-id subnet-id
238 '''
239 neutron_interface.remove_interface_router(
240 router_id, {'subnet_id': subnet_id})
241
242
243@_autheticate
244def create_security_group(neutron_interface, **sg_params):
245 '''
246 Create a new security group
247 CLI Example:
248 .. code-block:: bash
249 salt '*' neutron.create_security_group name='new_rule'
250 description='test rule'
251 '''
252 response = neutron_interface.create_security_group(
253 {'security_group': sg_params})
254 if 'security_group' in response and 'id' in response['security_group']:
255 return response['security_group']['id']
256
257
258@_autheticate
259def create_security_group_rule(neutron_interface, **rule_params):
260 '''
261 Create a rule entry for a security group
262 CLI Example:
263 .. code-block:: bash
264 salt '*' neutron.create_security_group_rule
265 '''
266 neutron_interface.create_security_group_rule(
267 {'security_group_rule': rule_params})
268
269
270@_autheticate
271def create_floatingip(neutron_interface, **floatingip_params):
272 '''
273 Create a new floating IP
274 CLI Example:
275 .. code-block:: bash
276 salt '*' neutron.create_floatingip floating_network_id=ext-net-id
277 '''
278 response = neutron_interface.create_floatingip(
279 {'floatingip': floatingip_params})
280 if 'floatingip' in response and 'id' in response['floatingip']:
Jiri Broulikde2e2902017-02-13 15:03:47 +0100281 return response
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100282
283
284@_autheticate
285def create_subnet(neutron_interface, **subnet_params):
286 '''
287 Create a new subnet in OpenStack
288 CLI Example:
289 .. code-block:: bash
290 salt '*' neutron.create_subnet name='subnet name'
291 network_id='openstack-network-id' cidr='192.168.10.0/24' \\
292 gateway_ip='192.168.10.1' ip_version='4' enable_dhcp=false \\
293 start_ip='192.168.10.10' end_ip='192.168.10.20'
294 '''
295 if 'start_ip' in subnet_params:
296 subnet_params.update(
297 {'allocation_pools': [{'start': subnet_params.pop('start_ip'),
298 'end': subnet_params.pop('end_ip', None)}]})
299 response = neutron_interface.create_subnet({'subnet': subnet_params})
300 if 'subnet' in response and 'id' in response['subnet']:
301 return response['subnet']['id']
302
303
304@_autheticate
305def create_network(neutron_interface, **net_params):
306 '''
307 Create a new network segment in OpenStack
308 CLI Example:
309 .. code-block:: bash
310 salt '*' neutron.create_network name=External
311 provider_network_type=flat provider_physical_network=ext
312 '''
313 network_params = {}
314 for param in net_params:
315 if 'provider_' in param or 'router_' in param:
316 network_params[param.replace('_', ':', 1)] = net_params[param]
317 else:
318 network_params[param] = net_params[param]
319 response = neutron_interface.create_network({'network': network_params})
320 if 'network' in response and 'id' in response['network']:
321 return response['network']['id']
322
323
324@_autheticate
325def create_port(neutron_interface, **port_params):
326 '''
327 Create a new port in OpenStack
328 CLI Example:
329 .. code-block:: bash
330 salt '*' neutron.create_port network_id='openstack-network-id'
331 '''
332 response = neutron_interface.create_port({'port': port_params})
333 if 'port' in response and 'id' in response['port']:
334 return response['port']['id']
335
336
337@_autheticate
338def update_port(neutron_interface, port_id, **port_params):
339 '''
340 Create a new port in OpenStack
341 CLI Example:
342 .. code-block:: bash
343 salt '*' neutron.update_port name='new_port_name'
344 '''
345 neutron_interface.update_port(port_id, {'port': port_params})
346
347
348@_autheticate
349def delete_floatingip(neutron_interface, floating_ip_id):
350 '''
351 delete a floating IP
352 CLI Example:
353 .. code-block:: bash
354 salt '*' neutron.delete_floatingip openstack-floating-ip-id
355 '''
356 neutron_interface.delete_floatingip(floating_ip_id)
357
358
359@_autheticate
360def delete_security_group(neutron_interface, sg_id):
361 '''
362 delete a security group
363 CLI Example:
364 .. code-block:: bash
365 salt '*' neutron.delete_security_group openstack-security-group-id
366 '''
367 neutron_interface.delete_security_group(sg_id)
368
369
370@_autheticate
371def delete_security_group_rule(neutron_interface, rule):
372 '''
373 delete a security group rule. pass all rule params that match the rule
374 to be deleted
375 CLI Example:
376 .. code-block:: bash
377 salt '*' neutron.delete_security_group_rule direction='ingress'
378 ethertype='ipv4' security_group_id='openstack-security-group-id'
379 port_range_min=100 port_range_max=4096 protocol='tcp'
380 remote_group_id='default'
381 '''
382 sg_rules = neutron_interface.list_security_group_rules(
383 security_group_id=rule['security_group_id'])
384 for sg_rule in sg_rules['security_group_rules']:
385 sgr_id = sg_rule.pop('id')
386 if sg_rule == rule:
387 neutron_interface.delete_security_group_rule(sgr_id)
388
389
390@_autheticate
391def delete_subnet(neutron_interface, subnet_id):
392 '''
393 delete given subnet
394 CLI Example:
395 .. code-block:: bash
396 salt '*' neutron.delete_subnet openstack-subnet-id
397 '''
398 neutron_interface.delete_subnet(subnet_id)
399
400
401@_autheticate
402def delete_network(neutron_interface, network_id):
403 '''
404 delete given network
405 CLI Example:
406 .. code-block:: bash
407 salt '*' neutron.delete_network openstack-network-id
408 '''
409 neutron_interface.delete_network(network_id)
410
411
412@_autheticate
413def delete_router(neutron_interface, router_id):
414 '''
415 delete given router
416 CLI Example:
417 .. code-block:: bash
418 salt '*' neutron.delete_router openstack-router-id
419 '''
420 neutron_interface.delete_router(router_id)