blob: 06f273e5c39941cec0db3e88fdc511bbf499771f [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]})
44 kstone = __salt__['keystone.auth'](**connection_args)
45 token = kstone.auth_token
Richard Felklaac256a2017-03-23 15:43:49 +010046
47 if kwargs.get('connection_endpoint_type') == None:
48 endpoint_type = 'internalURL'
49 else:
50 endpoint_type = kwargs.get('connection_endpoint_type')
51
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010052 endpoint = kstone.service_catalog.url_for(
53 service_type='network',
Richard Felklaac256a2017-03-23 15:43:49 +010054 endpoint_type=endpoint_type)
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010055 neutron_interface = client.Client(
56 endpoint_url=endpoint, token=token)
57 return_data = func_name(neutron_interface, *args, **nkwargs)
Your Name96fdc0a2017-05-05 12:56:28 +000058 # TODO(vsaienko) drop this formatting when all commands are updated
59 # to return dictionary
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010060 if isinstance(return_data, list):
61 # format list as a dict for rendering
62 return {data.get('name', None) or data['id']: data
63 for data in return_data}
64 return return_data
65 return decorator_method
66
67
68@_autheticate
69def list_floatingips(neutron_interface, **kwargs):
70 '''
71 list all floatingips
72 CLI Example:
73 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +000074 salt '*' neutronng.list_floatingips
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010075 '''
76 return neutron_interface.list_floatingips(**kwargs)['floatingips']
77
78
79@_autheticate
80def list_security_groups(neutron_interface, **kwargs):
81 '''
82 list all security_groups
83 CLI Example:
84 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +000085 salt '*' neutronng.list_security_groups
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010086 '''
87 return neutron_interface.list_security_groups(**kwargs)['security_groups']
88
89
90@_autheticate
91def list_subnets(neutron_interface, **kwargs):
92 '''
93 list all subnets
94 CLI Example:
95 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +000096 salt '*' neutronng.list_subnets
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010097 '''
Your Name96fdc0a2017-05-05 12:56:28 +000098 return neutron_interface.list_subnets(**kwargs)
Jiri Broulikf1b3aa42017-01-26 17:08:44 +010099
100
101@_autheticate
102def list_networks(neutron_interface, **kwargs):
103 '''
104 list all networks
105 CLI Example:
106 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000107 salt '*' neutronng.list_networks
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100108 '''
Your Name96fdc0a2017-05-05 12:56:28 +0000109 return neutron_interface.list_networks(**kwargs)
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100110
111
112@_autheticate
113def list_ports(neutron_interface, **kwargs):
114 '''
115 list all ports
116 CLI Example:
117 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000118 salt '*' neutronng.list_ports
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100119 '''
120 return neutron_interface.list_ports(**kwargs)['ports']
121
122
123@_autheticate
124def list_routers(neutron_interface, **kwargs):
125 '''
126 list all routers
127 CLI Example:
128 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000129 salt '*' neutronng.list_routers
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100130 '''
131 return neutron_interface.list_routers(**kwargs)['routers']
132
133@_autheticate
134def update_floatingip(neutron_interface, fip, port_id=None):
135 '''
136 update floating IP. Should be used to associate and disassociate
137 floating IP with instance
138 CLI Example:
139 .. code-block:: bash
140 to associate with an instance's port
Your Name96fdc0a2017-05-05 12:56:28 +0000141 salt '*' neutronng.update_floatingip openstack-floatingip-id port-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100142 to disassociate from an instance's port
Your Name96fdc0a2017-05-05 12:56:28 +0000143 salt '*' neutronng.update_floatingip openstack-floatingip-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100144 '''
145 neutron_interface.update_floatingip(fip, {"floatingip":
146 {"port_id": port_id}})
147
148
149@_autheticate
150def update_subnet(neutron_interface, subnet_id, **subnet_params):
151 '''
152 update given subnet
153 CLI Example:
154 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000155 salt '*' neutronng.update_subnet openstack-subnet-id name='new_name'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100156 '''
Your Name96fdc0a2017-05-05 12:56:28 +0000157 return neutron_interface.update_subnet(subnet_id, {'subnet': subnet_params})
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100158
159
160@_autheticate
161def update_network(neutron_interface, network_id, **net_params):
162 '''
163 Update give network
164 CLI Example:
165 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000166 salt '*' neutronng.update_network openstack-net-id admin_state_up=false
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100167 '''
168 network_params = {}
169 for param in net_params:
170 if 'provider_' in param or 'router_' in param:
171 network_params[param.replace('_', ':', 1)] = net_params[param]
172 else:
173 network_params[param] = net_params[param]
174 LOG.info('ATTRIBUTES ' + str(network_params))
Your Name96fdc0a2017-05-05 12:56:28 +0000175 return neutron_interface.update_network(network_id, {'network': network_params})
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100176
177
178@_autheticate
179def update_router(neutron_interface, router_id, **router_params):
180 '''
181 update given router
182 CLI Example:
183 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000184 salt '*' neutronng.update_router openstack-router-id name='new_name'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100185 external_gateway='openstack-network-id' administrative_state=true
186 '''
187 neutron_interface.update_router(router_id, {'router': router_params})
188
189
190@_autheticate
191def router_gateway_set(neutron_interface, router_id, external_gateway):
192 '''
193 Set external gateway for a router
194 CLI Example:
195 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000196 salt '*' neutronng.update_router openstack-router-id openstack-network-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100197 '''
198 neutron_interface.update_router(
199 router_id, {'router': {'external_gateway_info':
200 {'network_id': external_gateway}}})
201
202
203@_autheticate
204def router_gateway_clear(neutron_interface, router_id):
205 '''
206 Clear external gateway for a router
207 CLI Example:
208 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000209 salt '*' neutronng.update_router openstack-router-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100210 '''
211 neutron_interface.update_router(
212 router_id, {'router': {'external_gateway_info': None}})
213
214
215@_autheticate
216def create_router(neutron_interface, **router_params):
217 '''
218 Create OpenStack Neutron router
219 CLI Example:
220 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000221 salt '*' neutronng.create_router name=R1
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100222 '''
223 response = neutron_interface.create_router({'router': router_params})
224 if 'router' in response and 'id' in response['router']:
225 return response['router']['id']
226
227
228@_autheticate
229def router_add_interface(neutron_interface, router_id, subnet_id):
230 '''
231 Attach router to a subnet
232 CLI Example:
233 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000234 salt '*' neutronng.router_add_interface openstack-router-id subnet-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100235 '''
236 neutron_interface.add_interface_router(router_id, {'subnet_id': subnet_id})
237
238
239@_autheticate
240def router_rem_interface(neutron_interface, router_id, subnet_id):
241 '''
242 Dettach router from a subnet
243 CLI Example:
244 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000245 salt '*' neutronng.router_rem_interface openstack-router-id subnet-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100246 '''
247 neutron_interface.remove_interface_router(
248 router_id, {'subnet_id': subnet_id})
249
250
251@_autheticate
252def create_security_group(neutron_interface, **sg_params):
253 '''
254 Create a new security group
255 CLI Example:
256 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000257 salt '*' neutronng.create_security_group name='new_rule'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100258 description='test rule'
259 '''
260 response = neutron_interface.create_security_group(
261 {'security_group': sg_params})
262 if 'security_group' in response and 'id' in response['security_group']:
263 return response['security_group']['id']
264
265
266@_autheticate
267def create_security_group_rule(neutron_interface, **rule_params):
268 '''
269 Create a rule entry for a security group
270 CLI Example:
271 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000272 salt '*' neutronng.create_security_group_rule
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100273 '''
274 neutron_interface.create_security_group_rule(
275 {'security_group_rule': rule_params})
276
277
278@_autheticate
279def create_floatingip(neutron_interface, **floatingip_params):
280 '''
281 Create a new floating IP
282 CLI Example:
283 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000284 salt '*' neutronng.create_floatingip floating_network_id=ext-net-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100285 '''
286 response = neutron_interface.create_floatingip(
287 {'floatingip': floatingip_params})
288 if 'floatingip' in response and 'id' in response['floatingip']:
Jiri Broulikde2e2902017-02-13 15:03:47 +0100289 return response
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100290
291
292@_autheticate
293def create_subnet(neutron_interface, **subnet_params):
294 '''
295 Create a new subnet in OpenStack
296 CLI Example:
297 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000298 salt '*' neutronng.create_subnet name='subnet name'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100299 network_id='openstack-network-id' cidr='192.168.10.0/24' \\
Your Name96fdc0a2017-05-05 12:56:28 +0000300 gateway_ip='192.168.10.1' ip_version='4' enable_dhcp=false
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100301 '''
Your Name96fdc0a2017-05-05 12:56:28 +0000302 return neutron_interface.create_subnet({'subnet': subnet_params})
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100303
304
305@_autheticate
306def create_network(neutron_interface, **net_params):
307 '''
308 Create a new network segment in OpenStack
309 CLI Example:
310 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000311 salt '*' neutronng.create_network name=External
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100312 provider_network_type=flat provider_physical_network=ext
313 '''
314 network_params = {}
315 for param in net_params:
316 if 'provider_' in param or 'router_' in param:
317 network_params[param.replace('_', ':', 1)] = net_params[param]
318 else:
319 network_params[param] = net_params[param]
Your Name96fdc0a2017-05-05 12:56:28 +0000320 return neutron_interface.create_network({'network': network_params})
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100321
322
323@_autheticate
324def create_port(neutron_interface, **port_params):
325 '''
326 Create a new port in OpenStack
327 CLI Example:
328 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000329 salt '*' neutronng.create_port network_id='openstack-network-id'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100330 '''
Elena Ezhova20456692017-07-10 14:30:27 +0400331 return neutron_interface.create_port({'port': port_params})
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100332
333
334@_autheticate
335def update_port(neutron_interface, port_id, **port_params):
336 '''
337 Create a new port in OpenStack
338 CLI Example:
339 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000340 salt '*' neutronng.update_port name='new_port_name'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100341 '''
342 neutron_interface.update_port(port_id, {'port': port_params})
343
344
345@_autheticate
346def delete_floatingip(neutron_interface, floating_ip_id):
347 '''
348 delete a floating IP
349 CLI Example:
350 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000351 salt '*' neutronng.delete_floatingip openstack-floating-ip-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100352 '''
353 neutron_interface.delete_floatingip(floating_ip_id)
354
355
356@_autheticate
357def delete_security_group(neutron_interface, sg_id):
358 '''
359 delete a security group
360 CLI Example:
361 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000362 salt '*' neutronng.delete_security_group openstack-security-group-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100363 '''
364 neutron_interface.delete_security_group(sg_id)
365
366
367@_autheticate
368def delete_security_group_rule(neutron_interface, rule):
369 '''
370 delete a security group rule. pass all rule params that match the rule
371 to be deleted
372 CLI Example:
373 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000374 salt '*' neutronng.delete_security_group_rule direction='ingress'
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100375 ethertype='ipv4' security_group_id='openstack-security-group-id'
376 port_range_min=100 port_range_max=4096 protocol='tcp'
377 remote_group_id='default'
378 '''
379 sg_rules = neutron_interface.list_security_group_rules(
380 security_group_id=rule['security_group_id'])
381 for sg_rule in sg_rules['security_group_rules']:
382 sgr_id = sg_rule.pop('id')
383 if sg_rule == rule:
384 neutron_interface.delete_security_group_rule(sgr_id)
385
386
387@_autheticate
388def delete_subnet(neutron_interface, subnet_id):
389 '''
390 delete given subnet
391 CLI Example:
392 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000393 salt '*' neutronng.delete_subnet openstack-subnet-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100394 '''
395 neutron_interface.delete_subnet(subnet_id)
396
397
398@_autheticate
399def delete_network(neutron_interface, network_id):
400 '''
401 delete given network
402 CLI Example:
403 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000404 salt '*' neutronng.delete_network openstack-network-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100405 '''
406 neutron_interface.delete_network(network_id)
407
408
409@_autheticate
410def delete_router(neutron_interface, router_id):
411 '''
412 delete given router
413 CLI Example:
414 .. code-block:: bash
Your Name96fdc0a2017-05-05 12:56:28 +0000415 salt '*' neutronng.delete_router openstack-router-id
Jiri Broulikf1b3aa42017-01-26 17:08:44 +0100416 '''
Your Name96fdc0a2017-05-05 12:56:28 +0000417 neutron_interface.delete_router(router_id)
418