def _get_subnetpools_by_attrs(**attrs): subnetpools = app.neutron.list_subnetpools(**attrs) if len(subnetpools.get('subnetpools', [])) > 1: raise exceptions.DuplicatedResourceException( "Multiple Neutron subnetspool exist for the params {0} ".format( ', '.join(['{0}={1}'.format(k, v) for k, v in attrs.items()]))) return subnetpools['subnetpools']
def _get_ports_by_attrs(**attrs): ports = app.neutron.list_ports(**attrs) if len(ports.get('ports', [])) > 1: raise exceptions.DuplicatedResourceException( "Multiple Neutron ports exist for the params {0} ".format( ', '.join(['{0}={1}'.format(k, v) for k, v in attrs.items()]))) return ports['ports']
def _create_or_update_port(neutron_network_id, endpoint_id, interface_cidrv4, interface_cidrv6, interface_mac): response_interface = {} subnets = [] fixed_ips = [] subnetsv4 = subnetsv6 = [] if interface_cidrv4: subnetsv4 = _get_subnets_by_interface_cidr(neutron_network_id, interface_cidrv4) if interface_cidrv6: subnetsv6 = _get_subnets_by_interface_cidr(neutron_network_id, interface_cidrv6) subnets = subnetsv4 + subnetsv6 if not len(subnets): raise exceptions.NoResourceException( "No subnet exist for the cidrs {0} and {1} ".format( interface_cidrv4, interface_cidrv6)) if len(subnets) > 2: raise exceptions.DuplicatedResourceException( "Multiple subnets exist for the cidrs {0} and {1}".format( interface_cidrv4, interface_cidrv6)) _get_fixed_ips_by_interface_cidr(subnets, interface_cidrv4, interface_cidrv6, fixed_ips) filtered_ports = app.neutron.list_ports(fixed_ips=fixed_ips) num_port = len(filtered_ports.get('ports', [])) if not num_port: fixed_ips = utils.get_dict_format_fixed_ips_from_kv_format(fixed_ips) response_port = _create_port(endpoint_id, neutron_network_id, interface_mac, fixed_ips) elif num_port == 1: port = filtered_ports['ports'][0] response_port = _update_port(port, endpoint_id) else: raise n_exceptions.DuplicatedResourceException( "Multiple ports exist for the cidrs {0} and {1}".format( interface_cidrv4, interface_cidrv6)) created_fixed_ips = response_port['fixed_ips'] subnets_dict_by_id = {subnet['id']: subnet for subnet in subnets} if not interface_mac: response_interface['MacAddress'] = response_port['mac_address'] if not (interface_cidrv4 or interface_cidrv6): if 'ip_address' in response_port: _process_interface_address(response_port, subnets_dict_by_id, response_interface) for fixed_ip in created_fixed_ips: _process_interface_address(fixed_ip, subnets_dict_by_id, response_interface) return response_interface