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