def create_port(self, context, port): port_body = port['port'] network_id = port_body['network_id'] # get_network will create bottom network if it doesn't exist self.get_network(context, network_id) t_ctx = t_context.get_context_from_neutron_context(context) raw_client = self.neutron_handle._get_client(t_ctx) def get_top_port_by_ip(ip): params = { 'fixed_ips': 'ip_address=%s' % ip, 'network_id': network_id } t_ports = raw_client.list_ports(**params)['ports'] if not t_ports: raise q_exceptions.InvalidIpForNetwork( ip_address=fixed_ip['ip_address']) return t_ports[0] if port_body['fixed_ips'] is not q_constants.ATTR_NOT_SPECIFIED and ( port_body.get('device_owner') != (q_constants.DEVICE_OWNER_LOADBALANCERV2)): if not self._is_special_port(port_body): fixed_ip = port_body['fixed_ips'][0] ip_address = fixed_ip.get('ip_address') if not ip_address: # dhcp agent may request to create a dhcp port without # specifying ip address, we just raise an exception to # reject this request raise q_exceptions.InvalidIpForNetwork(ip_address='None') t_port = get_top_port_by_ip(ip_address) elif helper.NetworkHelper.is_need_top_sync_port( port_body, cfg.CONF.client.bridge_cidr): # for port that needs to be synced with top port, we keep ids # the same ip_address = port_body['fixed_ips'][0]['ip_address'] port_body['id'] = get_top_port_by_ip(ip_address)['id'] t_port = port_body else: self._handle_dvr_snat_port(t_ctx, port_body) t_port = port_body else: self._adapt_port_body_for_client(port['port']) t_port = raw_client.create_port(port)['port'] if not self._is_special_port(port_body): subnet_id = t_port['fixed_ips'][0]['subnet_id'] # get_subnet will create bottom subnet if it doesn't exist self.get_subnet(context, subnet_id) for field in ('name', 'device_id', 'device_owner', 'binding:host_id'): if port_body.get(field): t_port[field] = port_body[field] self._handle_security_group(t_ctx, context, t_port) self._create_shadow_agent(context, port_body) b_port = self.core_plugin.create_port(context, {'port': t_port}) return b_port
def get_top_port_by_ip(ip): params = {'fixed_ips': 'ip_address=%s' % ip, 'network_id': network_id} t_ports = raw_client.list_ports(**params)['ports'] if not t_ports: raise q_exceptions.InvalidIpForNetwork( ip_address=fixed_ip['ip_address']) return t_ports[0]
def create_port(self, context, port): port_body = port['port'] network_id = port_body['network_id'] # get_network will create bottom network if it doesn't exist self.get_network(context, network_id) t_ctx = t_context.get_context_from_neutron_context(context) raw_client = self.neutron_handle._get_client(t_ctx) if port_body['fixed_ips'] is not q_constants.ATTR_NOT_SPECIFIED: if not self._is_special_port(port_body): fixed_ip = port_body['fixed_ips'][0] ip_address = fixed_ip.get('ip_address') if not ip_address: # dhcp agent may request to create a dhcp port without # specifying ip address, we just raise an exception to # reject this request raise q_exceptions.InvalidIpForNetwork(ip_address='None') params = {'fixed_ips': 'ip_address=%s' % ip_address} t_ports = raw_client.list_ports(**params)['ports'] if not t_ports: raise q_exceptions.InvalidIpForNetwork( ip_address=fixed_ip['ip_address']) t_port = t_ports[0] else: self._handle_dvr_snat_port(t_ctx, port_body) t_port = port_body else: self._adapt_port_body_for_client(port['port']) t_port = raw_client.create_port(port)['port'] if not self._is_special_port(port_body): subnet_id = t_port['fixed_ips'][0]['subnet_id'] # get_subnet will create bottom subnet if it doesn't exist self.get_subnet(context, subnet_id) for field in ('name', 'device_id'): if port_body.get(field): t_port[field] = port_body[field] self._handle_security_group(t_ctx, context, t_port) b_port = self.core_plugin.create_port(context, {'port': t_port}) return b_port
def _get_subnet_for_fixed_ip(self, context, fixed, subnets, network_id): # Subnets are all the subnets belonging to the same network. if not subnets: msg = _('IP allocation requires subnets for network') raise exc.InvalidInput(error_message=msg) if 'subnet_id' in fixed: def get_matching_subnet(): for subnet in subnets: if subnet['id'] == fixed['subnet_id']: return subnet subnet = get_matching_subnet() if not subnet: # Call this method to keep original status code # in case subnet is not found self._get_subnet_object(context, fixed['subnet_id']) msg = (_("Failed to create port on network %(network_id)s" ", because fixed_ips included invalid subnet " "%(subnet_id)s") % { 'network_id': network_id, 'subnet_id': fixed['subnet_id'] }) raise exc.InvalidInput(error_message=msg) # Ensure that the IP is valid on the subnet if ('ip_address' in fixed and not ipam_utils.check_subnet_ip( subnet['cidr'], fixed['ip_address'], fixed['device_owner'])): raise exc.InvalidIpForSubnet(ip_address=fixed['ip_address']) return subnet if 'ip_address' not in fixed: msg = _('IP allocation requires subnet_id or ip_address') raise exc.InvalidInput(error_message=msg) for subnet in subnets: if ipam_utils.check_subnet_ip(subnet['cidr'], fixed['ip_address'], fixed['device_owner']): return subnet raise exc.InvalidIpForNetwork(ip_address=fixed['ip_address'])