| # -*- coding: utf-8 -*- |
| |
| import logging |
| from functools import wraps |
| LOG = logging.getLogger(__name__) |
| |
| # Import third party libs |
| HAS_NEUTRON = False |
| try: |
| from neutronclient.v2_0 import client |
| HAS_NEUTRON = True |
| except ImportError: |
| pass |
| |
| __opts__ = {} |
| |
| |
| def __virtual__(): |
| ''' |
| Only load this module if neutron |
| is installed on this minion. |
| ''' |
| if HAS_NEUTRON: |
| return 'neutronng' |
| return False |
| |
| |
| def _autheticate(func_name): |
| ''' |
| Authenticate requests with the salt keystone module and format return data |
| ''' |
| @wraps(func_name) |
| def decorator_method(*args, **kwargs): |
| ''' |
| Authenticate request and format return data |
| ''' |
| connection_args = {'profile': kwargs.pop('profile', None)} |
| nkwargs = {} |
| for kwarg in kwargs: |
| if 'connection_' in kwarg: |
| connection_args.update({kwarg: kwargs[kwarg]}) |
| elif '__' not in kwarg: |
| nkwargs.update({kwarg: kwargs[kwarg]}) |
| kstone = __salt__['keystoneng.auth'](**connection_args) |
| endpoint_type = kwargs.get('connection_endpoint_type', 'internal') |
| neutron_interface = client.Client(session=kstone.session, endpoint_type=endpoint_type) |
| return_data = func_name(neutron_interface, *args, **nkwargs) |
| # TODO(vsaienko) drop this formatting when all commands are updated |
| # to return dictionary |
| if isinstance(return_data, list): |
| # format list as a dict for rendering |
| return {data.get('name', None) or data['id']: data |
| for data in return_data} |
| return return_data |
| return decorator_method |
| |
| |
| @_autheticate |
| def list_floatingips(neutron_interface, **kwargs): |
| ''' |
| list all floatingips |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.list_floatingips |
| ''' |
| return neutron_interface.list_floatingips(**kwargs)['floatingips'] |
| |
| |
| @_autheticate |
| def list_security_groups(neutron_interface, **kwargs): |
| ''' |
| list all security_groups |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.list_security_groups |
| ''' |
| return neutron_interface.list_security_groups(**kwargs)['security_groups'] |
| |
| |
| @_autheticate |
| def list_subnets(neutron_interface, **kwargs): |
| ''' |
| list all subnets |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.list_subnets |
| ''' |
| return neutron_interface.list_subnets(**kwargs) |
| |
| |
| @_autheticate |
| def list_networks(neutron_interface, **kwargs): |
| ''' |
| list all networks |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.list_networks |
| ''' |
| return neutron_interface.list_networks(**kwargs) |
| |
| |
| @_autheticate |
| def list_ports(neutron_interface, **kwargs): |
| ''' |
| list all ports |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.list_ports |
| ''' |
| return neutron_interface.list_ports(**kwargs)['ports'] |
| |
| |
| @_autheticate |
| def list_routers(neutron_interface, **kwargs): |
| ''' |
| list all routers |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.list_routers |
| ''' |
| return neutron_interface.list_routers(**kwargs)['routers'] |
| |
| @_autheticate |
| def update_floatingip(neutron_interface, fip, port_id=None): |
| ''' |
| update floating IP. Should be used to associate and disassociate |
| floating IP with instance |
| CLI Example: |
| .. code-block:: bash |
| to associate with an instance's port |
| salt '*' neutronng.update_floatingip openstack-floatingip-id port-id |
| to disassociate from an instance's port |
| salt '*' neutronng.update_floatingip openstack-floatingip-id |
| ''' |
| neutron_interface.update_floatingip(fip, {"floatingip": |
| {"port_id": port_id}}) |
| |
| |
| @_autheticate |
| def update_subnet(neutron_interface, subnet_id, **subnet_params): |
| ''' |
| update given subnet |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.update_subnet openstack-subnet-id name='new_name' |
| ''' |
| return neutron_interface.update_subnet(subnet_id, {'subnet': subnet_params}) |
| |
| |
| @_autheticate |
| def update_network(neutron_interface, network_id, **net_params): |
| ''' |
| Update give network |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.update_network openstack-net-id admin_state_up=false |
| ''' |
| network_params = {} |
| for param in net_params: |
| if 'provider_' in param or 'router_' in param: |
| network_params[param.replace('_', ':', 1)] = net_params[param] |
| else: |
| network_params[param] = net_params[param] |
| LOG.info('ATTRIBUTES ' + str(network_params)) |
| return neutron_interface.update_network(network_id, {'network': network_params}) |
| |
| |
| @_autheticate |
| def update_router(neutron_interface, router_id, **router_params): |
| ''' |
| update given router |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.update_router openstack-router-id name='new_name' |
| external_gateway='openstack-network-id' administrative_state=true |
| ''' |
| neutron_interface.update_router(router_id, {'router': router_params}) |
| |
| |
| @_autheticate |
| def router_gateway_set(neutron_interface, router_id, external_gateway): |
| ''' |
| Set external gateway for a router |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.update_router openstack-router-id openstack-network-id |
| ''' |
| neutron_interface.update_router( |
| router_id, {'router': {'external_gateway_info': |
| {'network_id': external_gateway}}}) |
| |
| |
| @_autheticate |
| def router_gateway_clear(neutron_interface, router_id): |
| ''' |
| Clear external gateway for a router |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.update_router openstack-router-id |
| ''' |
| neutron_interface.update_router( |
| router_id, {'router': {'external_gateway_info': None}}) |
| |
| |
| @_autheticate |
| def create_router(neutron_interface, **router_params): |
| ''' |
| Create OpenStack Neutron router |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.create_router name=R1 |
| ''' |
| response = neutron_interface.create_router({'router': router_params}) |
| if 'router' in response and 'id' in response['router']: |
| return response['router']['id'] |
| |
| |
| @_autheticate |
| def router_add_interface(neutron_interface, router_id, subnet_id): |
| ''' |
| Attach router to a subnet |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.router_add_interface openstack-router-id subnet-id |
| ''' |
| neutron_interface.add_interface_router(router_id, {'subnet_id': subnet_id}) |
| |
| |
| @_autheticate |
| def router_rem_interface(neutron_interface, router_id, subnet_id): |
| ''' |
| Dettach router from a subnet |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.router_rem_interface openstack-router-id subnet-id |
| ''' |
| neutron_interface.remove_interface_router( |
| router_id, {'subnet_id': subnet_id}) |
| |
| |
| @_autheticate |
| def create_security_group(neutron_interface, **sg_params): |
| ''' |
| Create a new security group |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.create_security_group name='new_rule' |
| description='test rule' |
| ''' |
| response = neutron_interface.create_security_group( |
| {'security_group': sg_params}) |
| if 'security_group' in response and 'id' in response['security_group']: |
| return response['security_group']['id'] |
| |
| |
| @_autheticate |
| def create_security_group_rule(neutron_interface, **rule_params): |
| ''' |
| Create a rule entry for a security group |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.create_security_group_rule |
| ''' |
| neutron_interface.create_security_group_rule( |
| {'security_group_rule': rule_params}) |
| |
| |
| @_autheticate |
| def create_floatingip(neutron_interface, **floatingip_params): |
| ''' |
| Create a new floating IP |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.create_floatingip floating_network_id=ext-net-id |
| ''' |
| response = neutron_interface.create_floatingip( |
| {'floatingip': floatingip_params}) |
| if 'floatingip' in response and 'id' in response['floatingip']: |
| return response |
| |
| |
| @_autheticate |
| def create_subnet(neutron_interface, **subnet_params): |
| ''' |
| Create a new subnet in OpenStack |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.create_subnet name='subnet name' |
| network_id='openstack-network-id' cidr='192.168.10.0/24' \\ |
| gateway_ip='192.168.10.1' ip_version='4' enable_dhcp=false |
| ''' |
| return neutron_interface.create_subnet({'subnet': subnet_params}) |
| |
| |
| @_autheticate |
| def create_network(neutron_interface, **net_params): |
| ''' |
| Create a new network segment in OpenStack |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.create_network name=External |
| provider_network_type=flat provider_physical_network=ext |
| ''' |
| network_params = {} |
| for param in net_params: |
| if 'provider_' in param or 'router_' in param: |
| network_params[param.replace('_', ':', 1)] = net_params[param] |
| else: |
| network_params[param] = net_params[param] |
| return neutron_interface.create_network({'network': network_params}) |
| |
| |
| @_autheticate |
| def create_port(neutron_interface, **port_params): |
| ''' |
| Create a new port in OpenStack |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.create_port network_id='openstack-network-id' |
| ''' |
| return neutron_interface.create_port({'port': port_params}) |
| |
| |
| @_autheticate |
| def update_port(neutron_interface, port_id, **port_params): |
| ''' |
| Create a new port in OpenStack |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.update_port name='new_port_name' |
| ''' |
| neutron_interface.update_port(port_id, {'port': port_params}) |
| |
| |
| @_autheticate |
| def delete_floatingip(neutron_interface, floating_ip_id): |
| ''' |
| delete a floating IP |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.delete_floatingip openstack-floating-ip-id |
| ''' |
| neutron_interface.delete_floatingip(floating_ip_id) |
| |
| |
| @_autheticate |
| def delete_security_group(neutron_interface, sg_id): |
| ''' |
| delete a security group |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.delete_security_group openstack-security-group-id |
| ''' |
| neutron_interface.delete_security_group(sg_id) |
| |
| |
| @_autheticate |
| def delete_security_group_rule(neutron_interface, rule): |
| ''' |
| delete a security group rule. pass all rule params that match the rule |
| to be deleted |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.delete_security_group_rule direction='ingress' |
| ethertype='ipv4' security_group_id='openstack-security-group-id' |
| port_range_min=100 port_range_max=4096 protocol='tcp' |
| remote_group_id='default' |
| ''' |
| sg_rules = neutron_interface.list_security_group_rules( |
| security_group_id=rule['security_group_id']) |
| for sg_rule in sg_rules['security_group_rules']: |
| sgr_id = sg_rule.pop('id') |
| if sg_rule == rule: |
| neutron_interface.delete_security_group_rule(sgr_id) |
| |
| |
| @_autheticate |
| def delete_subnet(neutron_interface, subnet_id): |
| ''' |
| delete given subnet |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.delete_subnet openstack-subnet-id |
| ''' |
| neutron_interface.delete_subnet(subnet_id) |
| |
| |
| @_autheticate |
| def delete_network(neutron_interface, network_id): |
| ''' |
| delete given network |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.delete_network openstack-network-id |
| ''' |
| neutron_interface.delete_network(network_id) |
| |
| |
| @_autheticate |
| def delete_router(neutron_interface, router_id): |
| ''' |
| delete given router |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.delete_router openstack-router-id |
| ''' |
| neutron_interface.delete_router(router_id) |
| |
| |
| @_autheticate |
| def list_extensions(neutron_interface, **kwargs): |
| ''' |
| list all extensions |
| CLI Example: |
| .. code-block:: bash |
| salt '*' neutronng.list_extensions |
| ''' |
| return neutron_interface.list_extensions(**kwargs) |