X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=snaps%2Fopenstack%2Futils%2Fneutron_utils.py;h=ab56bbf240046c9b26a31c87138ff6399d5fd08e;hb=429b829fe4a745cdffd6c804a2a5e71d83cfbe0b;hp=b59f811e57e7bb566654a6886261e7144b64adca;hpb=a82209830309354c5bdc7e8b885c51df42c731d1;p=snaps.git diff --git a/snaps/openstack/utils/neutron_utils.py b/snaps/openstack/utils/neutron_utils.py index b59f811..ab56bbf 100644 --- a/snaps/openstack/utils/neutron_utils.py +++ b/snaps/openstack/utils/neutron_utils.py @@ -33,15 +33,18 @@ Utilities for basic neutron API calls """ -def neutron_client(os_creds): +def neutron_client(os_creds, session=None): """ Instantiates and returns a client for communications with OpenStack's Neutron server :param os_creds: the credentials for connecting to the OpenStack remote API + :param session: the keystone session object (optional) :return: the client object """ + if not session: + session = keystone_utils.keystone_session(os_creds) return Client(api_version=os_creds.network_api_version, - session=keystone_utils.keystone_session(os_creds), + session=session, region_name=os_creds.region_name) @@ -101,17 +104,18 @@ def delete_network(neutron, network): neutron.delete_network(network.id) -def get_network(neutron, network_settings=None, network_name=None, - project_id=None): +def get_network(neutron, keystone, network_settings=None, network_name=None, + project_name=None): """ Returns Network SNAPS-OO domain object the first network found with either the given attributes from the network_settings object if not None, else the query will use just the name from the network_name parameter. - When the project_id is included, that will be added to the query filter. - :param neutron: the client + When the project_name is included, that will be added to the query filter. + :param neutron: the Neutron client + :param keystone: the Keystone client :param network_settings: the NetworkConfig object used to create filter :param network_name: the name of the network to retrieve - :param project_id: the id of the network's project + :param project_name: the name of the network's project :return: a SNAPS-OO Network domain object """ net_filter = dict() @@ -120,13 +124,20 @@ def get_network(neutron, network_settings=None, network_name=None, elif network_name: net_filter['name'] = network_name - if project_id: - net_filter['project_id'] = project_id - networks = neutron.list_networks(**net_filter) for network, netInsts in networks.items(): for inst in netInsts: - return __map_network(neutron, inst) + if project_name: + if 'project_id' in inst.keys(): + project = keystone_utils.get_project_by_id( + keystone, inst['project_id']) + else: + project = keystone_utils.get_project_by_id( + keystone, inst['tenant_id']) + if project and project.name == project_name: + return __map_network(neutron, inst) + else: + return __map_network(neutron, inst) def __get_os_network_by_id(neutron, network_id): @@ -199,16 +210,17 @@ def delete_subnet(neutron, subnet): neutron.delete_subnet(subnet.id) -def get_subnet(neutron, subnet_settings=None, subnet_name=None): +def get_subnet(neutron, network, subnet_settings=None, subnet_name=None): """ Returns the first subnet object that fits the query else None including if subnet_settings or subnet_name parameters are None. :param neutron: the client + :param network: the associated SNAPS-OO Network domain object :param subnet_settings: the subnet settings of the object to retrieve :param subnet_name: the name of the subnet to retrieve :return: a SNAPS-OO Subnet domain object or None """ - sub_filter = dict() + sub_filter = {'network_id': network.id} if subnet_settings: sub_filter['name'] = subnet_settings.name sub_filter['cidr'] = subnet_settings.cidr @@ -239,6 +251,30 @@ def get_subnet(neutron, subnet_settings=None, subnet_name=None): return Subnet(**subnet) +def get_subnet_by_name(neutron, keystone, subnet_name, project_name=None): + """ + Returns the first subnet object that fits the query else None including + if subnet_settings or subnet_name parameters are None. + :param neutron: the Neutron client + :param keystone: the Keystone client + :param subnet_name: the name of the subnet to retrieve + :param project_name: the name of the associated project to the subnet to + retrieve + :return: a SNAPS-OO Subnet domain object or None + """ + sub_filter = {'name': subnet_name} + subnets = neutron.list_subnets(**sub_filter) + for subnet in subnets['subnets']: + subnet = Subnet(**subnet) + if project_name: + project = keystone_utils.get_project_by_id( + keystone, subnet.project_id) + if project: + return subnet + else: + return subnet + + def get_subnet_by_id(neutron, subnet_id): """ Returns a SNAPS-OO Subnet domain object for a given ID @@ -321,14 +357,17 @@ def get_router_by_id(neutron, router_id): return __map_router(neutron, router['router']) -def get_router(neutron, router_settings=None, router_name=None): +def get_router(neutron, keystone, router_settings=None, router_name=None, + project_name=None): """ Returns the first router object (dictionary) found the given the settings values if not None, else finds the first with the value of the router_name parameter, else None - :param neutron: the client + :param neutron: the Neutron client + :param keystone: the Keystone client :param router_settings: the RouterConfig object :param router_name: the name of the network to retrieve + :param project_name: the name of the router's project :return: a SNAPS-OO Router domain object """ router_filter = dict() @@ -341,12 +380,17 @@ def get_router(neutron, router_settings=None, router_name=None): else: return None - routers = neutron.list_routers(**router_filter) - - for routerInst in routers['routers']: - return __map_router(neutron, routerInst) - - return None + os_routers = neutron.list_routers(**router_filter) + for os_router in os_routers['routers']: + if project_name: + if 'project_id' in os_router.keys(): + project = keystone_utils.get_project_by_id( + keystone, os_router['project_id']) + else: + project = keystone_utils.get_project_by_id( + keystone, os_router['tenant_id']) + if project and project.name == project_name: + return __map_router(neutron, os_router) def __map_router(neutron, os_router): @@ -460,10 +504,7 @@ def create_port(neutron, os_creds, port_settings): logger.info('Creating port for network with name - %s', port_settings.network_name) os_port = neutron.create_port(body=json_body)['port'] - return Port(name=os_port['name'], id=os_port['id'], - ips=os_port['fixed_ips'], - mac_address=os_port['mac_address'], - allowed_address_pairs=os_port['allowed_address_pairs']) + return Port(**os_port) def delete_port(neutron, port): @@ -476,13 +517,16 @@ def delete_port(neutron, port): neutron.delete_port(port.id) -def get_port(neutron, port_settings=None, port_name=None): +def get_port(neutron, keystone, port_settings=None, port_name=None, + project_name=None): """ Returns the first port object (dictionary) found for the given query - :param neutron: the client + :param neutron: the Neutron client + :param keystone: the Keystone client :param port_settings: the PortConfig object used for generating the query :param port_name: if port_settings is None, this name is the value to place into the query + :param project_name: the associated project name :return: a SNAPS-OO Port domain object """ port_filter = dict() @@ -496,16 +540,30 @@ def get_port(neutron, port_settings=None, port_name=None): port_filter['device_id'] = port_settings.device_id if port_settings.mac_address: port_filter['mac_address'] = port_settings.mac_address + if port_settings.project_name: + project_name = port_settings.project_name if port_settings.network_name: - network = get_network(neutron, - network_name=port_settings.network_name) - port_filter['network_id'] = network.id + network = get_network( + neutron, keystone, network_name=port_settings.network_name, + project_name=project_name) + if network: + port_filter['network_id'] = network.id elif port_name: port_filter['name'] = port_name ports = neutron.list_ports(**port_filter) for port in ports['ports']: - return Port(**port) + if project_name: + if 'project_id' in port.keys(): + project = keystone_utils.get_project_by_id( + keystone, port['project_id']) + else: + project = keystone_utils.get_project_by_id( + keystone, port['tenant_id']) + if project and project.name == project_name: + return Port(**port) + else: + return Port(**port) return None @@ -571,30 +629,31 @@ def delete_security_group(neutron, sec_grp): neutron.delete_security_group(sec_grp.id) -def get_security_group(neutron, sec_grp_settings=None, sec_grp_name=None, - project_id=None): +def get_security_group(neutron, keystone, sec_grp_settings=None, + sec_grp_name=None, project_name=None): """ Returns the first security group for a given query. The query gets built from the sec_grp_settings parameter if not None, else only the name of the security group will be used, else if the query parameters are None then None will be returned - :param neutron: the client - :param sec_grp_settings: an instance of SecurityGroupSettings config object + :param neutron: the neutron client + :param keystone: the keystone client + :param sec_grp_settings: an instance of SecurityGroupConfig object :param sec_grp_name: the name of security group object to retrieve - :param project_id: the ID of the project/tentant object that owns the + :param project_name: the name of the project/tentant object that owns the secuity group to retrieve :return: a SNAPS-OO SecurityGroup domain object or None if not found """ sec_grp_filter = dict() - if project_id: - sec_grp_filter['tenant_id'] = project_id if sec_grp_settings: sec_grp_filter['name'] = sec_grp_settings.name if sec_grp_settings.description: sec_grp_filter['description'] = sec_grp_settings.description + if sec_grp_settings.project_name: + project_name = sec_grp_settings.project_name elif sec_grp_name: sec_grp_filter['name'] = sec_grp_name else: @@ -602,7 +661,17 @@ def get_security_group(neutron, sec_grp_settings=None, sec_grp_name=None, groups = neutron.list_security_groups(**sec_grp_filter) for group in groups['security_groups']: - return __map_os_security_group(neutron, group) + if project_name: + if 'project_id' in group.keys(): + project = keystone_utils.get_project_by_id( + keystone, group['project_id']) + else: + project = keystone_utils.get_project_by_id( + keystone, group['tenant_id']) + if project and project_name == project.name: + return __map_os_security_group(neutron, group) + else: + return __map_os_security_group(neutron, group) def __map_os_security_group(neutron, os_sec_grp): @@ -634,23 +703,26 @@ def get_security_group_by_id(neutron, sec_grp_id): return None -def create_security_group_rule(neutron, sec_grp_rule_settings): +def create_security_group_rule(neutron, keystone, sec_grp_rule_settings, + proj_name): """ - Creates a security group object in OpenStack - :param neutron: the client + Creates a security group rule in OpenStack + :param neutron: the neutron client + :param keystone: the keystone client :param sec_grp_rule_settings: the security group rule settings + :param proj_name: the default project name :return: a SNAPS-OO SecurityGroupRule domain object """ logger.info('Creating security group to security group - %s', sec_grp_rule_settings.sec_grp_name) os_rule = neutron.create_security_group_rule( - sec_grp_rule_settings.dict_for_neutron(neutron)) + sec_grp_rule_settings.dict_for_neutron(neutron, keystone, proj_name)) return SecurityGroupRule(**os_rule['security_group_rule']) def delete_security_group_rule(neutron, sec_grp_rule): """ - Deletes a security group object from OpenStack + Deletes a security group rule object from OpenStack :param neutron: the client :param sec_grp_rule: the SNAPS SecurityGroupRule object to delete """ @@ -670,7 +742,7 @@ def get_rules_by_security_group(neutron, sec_grp): def get_rules_by_security_group_id(neutron, sec_grp_id): """ - Retrieves all of the rules for a given security group + Retrieves all of the rules for a given security group by it's ID :param neutron: the client :param sec_grp_id: the ID of the associated security group """ @@ -687,7 +759,7 @@ def get_rules_by_security_group_id(neutron, sec_grp_id): def get_rule_by_id(neutron, sec_grp, rule_id): """ - Deletes a security group object from OpenStack + Returns a SecurityGroupRule object from OpenStack :param neutron: the client :param sec_grp: the SNAPS SecurityGroup domain object :param rule_id: the rule's ID @@ -715,48 +787,58 @@ def get_external_networks(neutron): return out -def get_floating_ips(neutron, ports=None): +def get_port_floating_ips(neutron, ports): """ - Returns all of the floating IPs - When ports is not None, FIPs returned must be associated with one of the - ports in the list and a tuple 2 where the first element being the port's - ID and the second being the FloatingIp SNAPS-OO domain object. - When ports is None, all known FloatingIp SNAPS-OO domain objects will be - returned in a list + Returns all of the floating IPs associated with the ports returned in a + list of tuples where the port object is in the first position and the + floating IP object is in the second :param neutron: the Neutron client :param ports: a list of tuple 2 where index 0 is the port name and index 1 is the SNAPS-OO Port object :return: a list of tuple 2 (port_id, SNAPS FloatingIp) objects when ports - is not None else a list of Port objects + is not None else a list of FloatingIp objects """ out = list() fips = neutron.list_floatingips() for fip in fips['floatingips']: - if ports: - for port_name, port in ports: - if port and port.id == fip['port_id']: - out.append((port.id, FloatingIp(**fip))) - break - else: - out.append(FloatingIp(**fip)) + for port_name, port in ports: + if port and port.id == fip['port_id']: + out.append((port.id, FloatingIp(**fip))) + break + return out + +def get_floating_ips(neutron): + """ + Returns a list of all of the floating IPs + :param neutron: the Neutron client + """ + out = list() + fips = neutron.list_floatingips() + for fip in fips['floatingips']: + out.append(FloatingIp(**fip)) return out -def create_floating_ip(neutron, ext_net_name): +def create_floating_ip(neutron, keystone, ext_net_name, port_id=None): """ Returns the floating IP object that was created with this call :param neutron: the Neutron client + :param keystone: the Keystone client :param ext_net_name: the name of the external network on which to apply the floating IP address + :param port_id: the ID of the port to which the floating IP will be + associated :return: the SNAPS FloatingIp object """ logger.info('Creating floating ip to external network - ' + ext_net_name) - ext_net = get_network(neutron, network_name=ext_net_name) + ext_net = get_network(neutron, keystone, network_name=ext_net_name) if ext_net: - fip = neutron.create_floatingip( - body={'floatingip': - {'floating_network_id': ext_net.id}}) + body = {'floatingip': {'floating_network_id': ext_net.id}} + if port_id: + body['floatingip']['port_id'] = port_id + + fip = neutron.create_floatingip(body=body) return FloatingIp(id=fip['floatingip']['id'], ip=fip['floatingip']['floating_ip_address']) @@ -811,7 +893,7 @@ def delete_floating_ip(neutron, floating_ip): def get_network_quotas(neutron, project_id): """ - Returns a list of all available keypairs + Returns a list of NetworkQuotas objects :param neutron: the neutron client :param project_id: the project's ID of the quotas to lookup :return: an object of type NetworkQuotas or None if not found