blob: 6ea50f9baafef9f9cea4f49cddae38834f378723 [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 '''
Your Name96fdc0a2017-05-05 12:56:28 +000037 connection_args = {'profile': kwargs.pop('profile', None)}
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010038 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]})
Oleg Iurchenko87f56322017-10-20 00:40:50 +030044 kstone = __salt__['keystoneng.auth'](**connection_args)
45 endpoint_type = kwargs.get('connection_endpoint_type', 'internal')
46 neutron_interface = client.Client(session=kstone.session, endpoint_type=endpoint_type)
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010047 return_data = func_name(neutron_interface, *args, **nkwargs)
Your Name96fdc0a2017-05-05 12:56:28 +000048 # TODO(vsaienko) drop this formatting when all commands are updated
49 # to return dictionary
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010050 if isinstance(return_data, list):
51 # format list as a dict for rendering
52 return {data.get('name', None) or data['id']: data
53 for data in return_data}
54 return return_data
55 return decorator_method
56
57
58@_autheticate
59def list_floatingips(neutron_interface, **kwargs):
60 '''
61 list all floatingips
62 CLI Example:
63 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +000064 salt '*' neutronng.list_floatingips
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010065 '''
66 return neutron_interface.list_floatingips(**kwargs)['floatingips']
67
68
69@_autheticate
70def list_security_groups(neutron_interface, **kwargs):
71 '''
72 list all security_groups
73 CLI Example:
74 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +000075 salt '*' neutronng.list_security_groups
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010076 '''
77 return neutron_interface.list_security_groups(**kwargs)['security_groups']
78
79
80@_autheticate
81def list_subnets(neutron_interface, **kwargs):
82 '''
83 list all subnets
84 CLI Example:
85 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +000086 salt '*' neutronng.list_subnets
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010087 '''
Your Name96fdc0a2017-05-05 12:56:28 +000088 return neutron_interface.list_subnets(**kwargs)
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010089
90
91@_autheticate
92def list_networks(neutron_interface, **kwargs):
93 '''
94 list all networks
95 CLI Example:
96 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +000097 salt '*' neutronng.list_networks
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010098 '''
Your Name96fdc0a2017-05-05 12:56:28 +000099 return neutron_interface.list_networks(**kwargs)
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100100
101
102@_autheticate
103def list_ports(neutron_interface, **kwargs):
104 '''
105 list all ports
106 CLI Example:
107 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000108 salt '*' neutronng.list_ports
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100109 '''
110 return neutron_interface.list_ports(**kwargs)['ports']
111
112
113@_autheticate
114def list_routers(neutron_interface, **kwargs):
115 '''
116 list all routers
117 CLI Example:
118 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000119 salt '*' neutronng.list_routers
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100120 '''
121 return neutron_interface.list_routers(**kwargs)['routers']
122
123@_autheticate
124def update_floatingip(neutron_interface, fip, port_id=None):
125 '''
126 update floating IP. Should be used to associate and disassociate
127 floating IP with instance
128 CLI Example:
129 .. code-block:: bash
130 to associate with an instance's port
Your Name96fdc0a2017-05-05 12:56:28 +0000131 salt '*' neutronng.update_floatingip openstack-floatingip-id port-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100132 to disassociate from an instance's port
Your Name96fdc0a2017-05-05 12:56:28 +0000133 salt '*' neutronng.update_floatingip openstack-floatingip-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100134 '''
135 neutron_interface.update_floatingip(fip, {"floatingip":
136 {"port_id": port_id}})
137
138
139@_autheticate
140def update_subnet(neutron_interface, subnet_id, **subnet_params):
141 '''
142 update given subnet
143 CLI Example:
144 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000145 salt '*' neutronng.update_subnet openstack-subnet-id name='new_name'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100146 '''
Your Name96fdc0a2017-05-05 12:56:28 +0000147 return neutron_interface.update_subnet(subnet_id, {'subnet': subnet_params})
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100148
149
150@_autheticate
151def update_network(neutron_interface, network_id, **net_params):
152 '''
153 Update give network
154 CLI Example:
155 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000156 salt '*' neutronng.update_network openstack-net-id admin_state_up=false
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100157 '''
158 network_params = {}
159 for param in net_params:
160 if 'provider_' in param or 'router_' in param:
161 network_params[param.replace('_', ':', 1)] = net_params[param]
162 else:
163 network_params[param] = net_params[param]
164 LOG.info('ATTRIBUTES ' + str(network_params))
Your Name96fdc0a2017-05-05 12:56:28 +0000165 return neutron_interface.update_network(network_id, {'network': network_params})
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100166
167
168@_autheticate
169def update_router(neutron_interface, router_id, **router_params):
170 '''
171 update given router
172 CLI Example:
173 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000174 salt '*' neutronng.update_router openstack-router-id name='new_name'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100175 external_gateway='openstack-network-id' administrative_state=true
176 '''
177 neutron_interface.update_router(router_id, {'router': router_params})
178
179
180@_autheticate
181def router_gateway_set(neutron_interface, router_id, external_gateway):
182 '''
183 Set external gateway for a router
184 CLI Example:
185 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000186 salt '*' neutronng.update_router openstack-router-id openstack-network-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100187 '''
188 neutron_interface.update_router(
189 router_id, {'router': {'external_gateway_info':
190 {'network_id': external_gateway}}})
191
192
193@_autheticate
194def router_gateway_clear(neutron_interface, router_id):
195 '''
196 Clear external gateway for a router
197 CLI Example:
198 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000199 salt '*' neutronng.update_router openstack-router-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100200 '''
201 neutron_interface.update_router(
202 router_id, {'router': {'external_gateway_info': None}})
203
204
205@_autheticate
206def create_router(neutron_interface, **router_params):
207 '''
208 Create OpenStack Neutron router
209 CLI Example:
210 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000211 salt '*' neutronng.create_router name=R1
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100212 '''
213 response = neutron_interface.create_router({'router': router_params})
214 if 'router' in response and 'id' in response['router']:
215 return response['router']['id']
216
217
218@_autheticate
219def router_add_interface(neutron_interface, router_id, subnet_id):
220 '''
221 Attach router to a subnet
222 CLI Example:
223 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000224 salt '*' neutronng.router_add_interface openstack-router-id subnet-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100225 '''
226 neutron_interface.add_interface_router(router_id, {'subnet_id': subnet_id})
227
228
229@_autheticate
230def router_rem_interface(neutron_interface, router_id, subnet_id):
231 '''
232 Dettach router from a subnet
233 CLI Example:
234 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000235 salt '*' neutronng.router_rem_interface openstack-router-id subnet-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100236 '''
237 neutron_interface.remove_interface_router(
238 router_id, {'subnet_id': subnet_id})
239
240
241@_autheticate
242def create_security_group(neutron_interface, **sg_params):
243 '''
244 Create a new security group
245 CLI Example:
246 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000247 salt '*' neutronng.create_security_group name='new_rule'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100248 description='test rule'
249 '''
250 response = neutron_interface.create_security_group(
251 {'security_group': sg_params})
252 if 'security_group' in response and 'id' in response['security_group']:
253 return response['security_group']['id']
254
255
256@_autheticate
257def create_security_group_rule(neutron_interface, **rule_params):
258 '''
259 Create a rule entry for a security group
260 CLI Example:
261 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000262 salt '*' neutronng.create_security_group_rule
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100263 '''
264 neutron_interface.create_security_group_rule(
265 {'security_group_rule': rule_params})
266
267
268@_autheticate
269def create_floatingip(neutron_interface, **floatingip_params):
270 '''
271 Create a new floating IP
272 CLI Example:
273 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000274 salt '*' neutronng.create_floatingip floating_network_id=ext-net-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100275 '''
276 response = neutron_interface.create_floatingip(
277 {'floatingip': floatingip_params})
278 if 'floatingip' in response and 'id' in response['floatingip']:
Jiri Broulikde2e2902017-02-13 15:03:47 +0100279 return response
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100280
281
282@_autheticate
283def create_subnet(neutron_interface, **subnet_params):
284 '''
285 Create a new subnet in OpenStack
286 CLI Example:
287 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000288 salt '*' neutronng.create_subnet name='subnet name'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100289 network_id='openstack-network-id' cidr='192.168.10.0/24' \\
Your Name96fdc0a2017-05-05 12:56:28 +0000290 gateway_ip='192.168.10.1' ip_version='4' enable_dhcp=false
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100291 '''
Your Name96fdc0a2017-05-05 12:56:28 +0000292 return neutron_interface.create_subnet({'subnet': subnet_params})
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100293
294
295@_autheticate
296def create_network(neutron_interface, **net_params):
297 '''
298 Create a new network segment in OpenStack
299 CLI Example:
300 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000301 salt '*' neutronng.create_network name=External
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100302 provider_network_type=flat provider_physical_network=ext
303 '''
304 network_params = {}
305 for param in net_params:
306 if 'provider_' in param or 'router_' in param:
307 network_params[param.replace('_', ':', 1)] = net_params[param]
308 else:
309 network_params[param] = net_params[param]
Your Name96fdc0a2017-05-05 12:56:28 +0000310 return neutron_interface.create_network({'network': network_params})
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100311
312
313@_autheticate
314def create_port(neutron_interface, **port_params):
315 '''
316 Create a new port in OpenStack
317 CLI Example:
318 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000319 salt '*' neutronng.create_port network_id='openstack-network-id'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100320 '''
Elena Ezhova20456692017-07-10 14:30:27 +0400321 return neutron_interface.create_port({'port': port_params})
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100322
323
324@_autheticate
325def update_port(neutron_interface, port_id, **port_params):
326 '''
327 Create a new port in OpenStack
328 CLI Example:
329 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000330 salt '*' neutronng.update_port name='new_port_name'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100331 '''
332 neutron_interface.update_port(port_id, {'port': port_params})
333
334
335@_autheticate
336def delete_floatingip(neutron_interface, floating_ip_id):
337 '''
338 delete a floating IP
339 CLI Example:
340 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000341 salt '*' neutronng.delete_floatingip openstack-floating-ip-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100342 '''
343 neutron_interface.delete_floatingip(floating_ip_id)
344
345
346@_autheticate
347def delete_security_group(neutron_interface, sg_id):
348 '''
349 delete a security group
350 CLI Example:
351 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000352 salt '*' neutronng.delete_security_group openstack-security-group-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100353 '''
354 neutron_interface.delete_security_group(sg_id)
355
356
357@_autheticate
358def delete_security_group_rule(neutron_interface, rule):
359 '''
360 delete a security group rule. pass all rule params that match the rule
361 to be deleted
362 CLI Example:
363 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000364 salt '*' neutronng.delete_security_group_rule direction='ingress'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100365 ethertype='ipv4' security_group_id='openstack-security-group-id'
366 port_range_min=100 port_range_max=4096 protocol='tcp'
367 remote_group_id='default'
368 '''
369 sg_rules = neutron_interface.list_security_group_rules(
370 security_group_id=rule['security_group_id'])
371 for sg_rule in sg_rules['security_group_rules']:
372 sgr_id = sg_rule.pop('id')
373 if sg_rule == rule:
374 neutron_interface.delete_security_group_rule(sgr_id)
375
376
377@_autheticate
378def delete_subnet(neutron_interface, subnet_id):
379 '''
380 delete given subnet
381 CLI Example:
382 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000383 salt '*' neutronng.delete_subnet openstack-subnet-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100384 '''
385 neutron_interface.delete_subnet(subnet_id)
386
387
388@_autheticate
389def delete_network(neutron_interface, network_id):
390 '''
391 delete given network
392 CLI Example:
393 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000394 salt '*' neutronng.delete_network openstack-network-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100395 '''
396 neutron_interface.delete_network(network_id)
397
398
399@_autheticate
400def delete_router(neutron_interface, router_id):
401 '''
402 delete given router
403 CLI Example:
404 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000405 salt '*' neutronng.delete_router openstack-router-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100406 '''
Your Name96fdc0a2017-05-05 12:56:28 +0000407 neutron_interface.delete_router(router_id)
408
Artem27d70bb2018-05-14 10:37:18 +0300409
410@_autheticate
411def list_extensions(neutron_interface, **kwargs):
412 '''
413 list all extensions
414 CLI Example:
415 .. code-block:: bash
416 salt '*' neutronng.list_extensions
417 '''
418 return neutron_interface.list_extensions(**kwargs)