def server_nic_delete(self, context, port_id): with _session_for_write(): query = model_query(context, models.ServerNic).filter_by(port_id=port_id) count = query.delete() if count != 1: raise exception.PortNotFound(port_id=port_id)
def server_nic_get(self, context, port_id): query = model_query(context, models.ServerNic).filter_by(port_id=port_id) try: return query.one() except NoResultFound: raise exception.PortNotFound(port_id=port_id)
def _ports_needed_per_server(self, client, requested_networks): ports_needed_per_server = 0 net_ids_requested = [] port_ids_requested = [] for request in requested_networks: ports_needed_per_server += 1 if request.get('net_id'): net_ids_requested.append(request.get('net_id')) if request.get('port_id'): port_ids_requested.append(request.get('port_id')) # Now check to see if all requested networks exist if net_ids_requested: nets = self._get_available_networks(net_ids_requested, client) for net in nets: if not net.get('subnets'): raise exception.NetworkRequiresSubnet( network_uuid=net['id']) if len(nets) != len(net_ids_requested): requested_netid_set = set(net_ids_requested) returned_netid_set = set([net['id'] for net in nets]) lostid_set = requested_netid_set - returned_netid_set if lostid_set: id_str = '' for _id in lostid_set: id_str = id_str and id_str + ', ' + _id or _id raise exception.NetworkNotFound(network_id=id_str) # Now check to see if all requested ports exist if port_ids_requested: ports = self._get_available_ports(port_ids_requested, client) if len(ports) != len(port_ids_requested): requested_portid_set = set(port_ids_requested) returned_portid_set = set([port['id'] for port in ports]) lostid_set = requested_portid_set - returned_portid_set if lostid_set: id_str = '' for _id in lostid_set: id_str = id_str and id_str + ', ' + _id or _id raise exception.PortNotFound(port_id=id_str) # Check if those ports are used and have FixedIP now. for port in ports: if port.get('device_id', None): raise exception.PortInUse(port_id=request.port_id) deferred_ip = port.get('ip_allocation') == 'deferred' if not deferred_ip and not port.get('fixed_ips'): raise exception.PortRequiresFixedIP(port_id=port['id']) return ports_needed_per_server
def attach_interface(self, context, server, net_id, port_id): # prepare port to be attached if port_id: LOG.debug("Attaching port %(port_id)s to server %(server)s", { 'port_id': port_id, 'server': server }) try: vif_port = self.network_api.show_port(context, port_id) except Exception: raise exception.PortNotFound(port_id=port_id) try: self.network_api.check_port_availability(vif_port) self.network_api.bind_port(context, port_id, server) except Exception as e: raise exception.InterfaceAttachFailed(message=six.text_type(e)) preserve_on_delete = True else: LOG.debug( "Attaching network interface %(net_id)s to server " "%(server)s", { 'net_id': net_id, 'server': server }) vif_port = self.network_api.create_port(context, net_id, server.uuid) preserve_on_delete = False try: self.driver.plug_vif(server.node_uuid, vif_port['id']) nics_obj = objects.ServerNics(context) nic_dict = { 'port_id': vif_port['id'], 'network_id': vif_port['network_id'], 'mac_address': vif_port['mac_address'], 'fixed_ips': vif_port['fixed_ips'], 'preserve_on_delete': preserve_on_delete, 'server_uuid': server.uuid } nics_obj.objects.append(objects.ServerNic(context, **nic_dict)) server.nics = nics_obj server.save() except Exception as e: if port_id: self.network_api.unbind_port(context, vif_port) else: self.network_api.delete_port(context, vif_port['id'], server.uuid) raise exception.InterfaceAttachFailed(message=six.text_type(e)) LOG.info('Attaching interface successfully')
def _show_port(self, client, port_id): """Return the port for the client given the port id.""" try: result = client.show_port(port_id) return result.get('port') except neutron_exceptions.PortNotFoundClient: raise exception.PortNotFound(port_id=port_id) except neutron_exceptions.Unauthorized: raise exception.Forbidden() except neutron_exceptions.NeutronClientException as e: msg = (_("Failed to access port %(port_id)s: %(reason)s") % { 'port_id': port_id, 'reason': e }) raise exception.NetworkError(msg)