def get(self, context, network_id): for network in self.networks: if network.get('uuid') == network_id: return objects.Network._from_db_object(context, objects.Network(), network) raise exception.NetworkNotFound(network_id=network_id)
def validate_networks(self, context, requested_networks): """Validate that the tenant can use the requested networks.""" LOG.debug(_('validate_networks() for %s'), requested_networks) if not requested_networks: return net_ids = [] for (net_id, _i, port_id) in requested_networks: if not port_id: net_ids.append(net_id) continue port = quantumv2.get_client(context).show_port(port_id).get('port') if not port: raise exception.PortNotFound(port_id=port_id) if port.get('device_id', None): raise exception.PortInUse(port_id=port_id) net_id = port['network_id'] if net_id in net_ids: raise exception.NetworkDuplicated(network_id=net_id) net_ids.append(net_id) nets = self._get_available_networks(context, context.project_id, net_ids) if len(nets) != len(net_ids): requsted_netid_set = set(net_ids) returned_netid_set = set([net['id'] for net in nets]) lostid_set = requsted_netid_set - returned_netid_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)
def validate_networks(self, context, networks): """Validates that this tenant has quantum networks with the associated UUIDs. This is called by the 'os-create-server-ext' API extension code so that we can return an API error code to the caller if they request an invalid network. """ if networks is None: return project_id = context.project_id for (net_id, _i) in networks: # TODO(bgh): At some point we should figure out whether or # not we want the verify_subnet_exists call to be optional. if not self.ipam.verify_subnet_exists(context, project_id, net_id): raise exception.NetworkNotFound(network_id=net_id) if not self.q_conn.network_exists(project_id, net_id): raise exception.NetworkNotFound(network_id=net_id)
def test_network_show_not_found(self): ctxt = self.req.environ['nova.context'] with mock.patch.object(self.controller.network_api, 'get', side_effect=exception.NetworkNotFound( network_id=1)) as get_mock: self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, self.req, 1) get_mock.assert_called_once_with(ctxt, 1)
def associate(self, context, network_uuid, host=_sentinel, project=_sentinel): for network in self.networks: if network.get('uuid') == network_uuid: if host is not FakeNetworkAPI._sentinel: network['host'] = host if project is not FakeNetworkAPI._sentinel: network['project_id'] = project return True raise exception.NetworkNotFound(network_id=network_uuid)
def fake_get_access_pf9(obj, context, network_id): if network_id not in NET_UUIDS: raise exception.NetworkNotFound(network_id=network_id) res = [] for access in ACCESS_LIST: if access['network_id'] == network_id: res.append(access) return res
def get(self, context, network_id): for network in self.networks: if network.get('uuid') == network_id: if 'injected' in network and network['injected'] is None: # NOTE: This is a workaround for passing unit tests. # When using nova-network, 'injected' value should be # boolean because of the definition of objects.Network(). # However, 'injected' value can be None if neutron. # So here changes the value to False just for passing # following _from_db_object(). network['injected'] = False return objects.Network._from_db_object(context, objects.Network(), network) raise exception.NetworkNotFound(network_id=network_id)
def _get_network_from_neutron(self, context, network_info): """send message to neutron server to get network information :param context: :param network_info: :return: """ client = neutron_api.get_client(context) try: network = client.show_network( network_info['id']).get('network') or {} except neutron_client_exc.NetworkNotFoundClient: raise exception.NetworkNotFound(network_id=network_info['id']) network['label'] = network['name'] return network
def fake_attach_interface(self, context, instance, network_id, port_id, requested_ip='192.168.1.3'): if not network_id: # if no network_id is given when add a port to an instance, use the # first default network. network_id = fake_networks[0] if network_id == 'bad_id': raise exception.NetworkNotFound(network_id=network_id) if not port_id: port_id = ports[fake_networks.index(network_id)]['id'] vif = fake_network_cache_model.new_vif() vif['id'] = port_id vif['network']['id'] = network_id vif['network']['subnets'][0]['ips'][0]['address'] = requested_ip return vif
def validate_networks(self, context, requested_networks): """Validate that the tenant can use the requested networks.""" LOG.debug(_('validate_networks() for %s'), requested_networks) if not requested_networks: nets = self._get_available_networks(context, context.project_id) if len(nets) > 1: # Attaching to more than one network by default doesn't # make sense, as the order will be arbitrary and the guest OS # won't know which to configure msg = _("Multiple possible networks found, use a Network " "ID to be more specific.") raise exception.NetworkAmbiguous(msg) return net_ids = [] for (net_id, _i, port_id) in requested_networks: if port_id: try: port = (neutronv2.get_client(context) .show_port(port_id) .get('port')) except neutronv2.exceptions.NeutronClientException as e: if e.status_code == 404: port = None if not port: raise exception.PortNotFound(port_id=port_id) if port.get('device_id', None): raise exception.PortInUse(port_id=port_id) net_id = port['network_id'] if net_id in net_ids: raise exception.NetworkDuplicated(network_id=net_id) net_ids.append(net_id) # Now check to see if all requested networks exist nets = self._get_available_networks(context, context.project_id, net_ids) if len(nets) != len(net_ids): requsted_netid_set = set(net_ids) returned_netid_set = set([net['id'] for net in nets]) lostid_set = requsted_netid_set - returned_netid_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)
def fake_remove_access_pf9(obj, context, network_id, project_id): global ACCESS_LIST if network_id not in NET_UUIDS: raise exception.NetworkNotFound(network_id=network_id) existing_projects = filter(lambda x: x['network_id'] == network_id, ACCESS_LIST) existing_projects = [x['project_id'] for x in existing_projects] if project_id not in existing_projects: raise exception.NetworkAccessNotFound(network_id=network_id, project_id=project_id) ACCESS_LIST.remove({'network_id': network_id, 'project_id': project_id}) return fake_get_access_pf9(obj, context, network_id)
def fake_attach_interface(self, context, instance, network_id, port_id, requested_ip='192.168.1.3', vif_model=None, tag=None): if not network_id: # if no network_id is given when add a port to an instance, use the # first default network. network_id = fake_networks[0] if network_id == FAKE_BAD_NET_ID: raise exception.NetworkNotFound(network_id=network_id) if not port_id: port_id = ports[fake_networks.index(network_id)]['id'] if port_id == FAKE_NOT_FOUND_PORT_ID: raise exception.PortNotFound(port_id=port_id) vif = fake_network_cache_model.new_vif() vif['id'] = port_id vif['network']['id'] = network_id vif['network']['subnets'][0]['ips'][0]['address'] = requested_ip return vif
def validate_networks(self, context, requested_networks): """Validate that the tenant has the requested networks.""" LOG.debug(_('validate_networks() for %s'), requested_networks) if not requested_networks: return search_opts = {"tenant_id": context.project_id} net_ids = [net_id for (net_id, _i) in requested_networks] search_opts['id'] = net_ids data = quantumv2.get_client(context).list_networks(**search_opts) nets = data.get('networks', []) if len(nets) != len(net_ids): requsted_netid_set = set(net_ids) returned_netid_set = set([net['id'] for net in nets]) lostid_set = requsted_netid_set - returned_netid_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)
def _get_available_networks(self, context, project_id, net_ids=None): """Return only specified networks if provided. Standard implementation uses public nets if empty list provided, this is not desirable behavior.""" if net_ids is None: return super(QuantumUdpApi, self)._get_available_networks(context, project_id) elif not net_ids: return [] # If user has specified to attach instance only to specific # networks, add them to **search_opts quantum = quantumv2.get_client(context) search_opts = {"tenant_id": project_id, 'shared': False, 'id': net_ids} nets = quantum.list_networks(**search_opts).get('networks', []) found_nets = map(lambda x: x['id'], nets) if len(nets) != len(found_nets): set(net_ids).difference(found_nets) raise exception.NetworkNotFound(network_id=found_nets) _ensure_requested_network_ordering(lambda x: x['id'], nets, net_ids) LOG.debug("Nets for instance: %s" % nets) return nets
def validate_networks(self, context, requested_networks, num_instances): """Validate that the tenant can use the requested networks. Return the number of instances than can be successfully allocated with the requested network configuration. """ LOG.debug(_('validate_networks() for %s'), requested_networks) neutron = neutronv2.get_client(context) ports_needed_per_instance = 0 if not requested_networks: nets = self._get_available_networks(context, context.project_id, neutron=neutron) if len(nets) > 1: # Attaching to more than one network by default doesn't # make sense, as the order will be arbitrary and the guest OS # won't know which to configure msg = _("Multiple possible networks found, use a Network " "ID to be more specific.") raise exception.NetworkAmbiguous(msg) else: ports_needed_per_instance = 1 else: net_ids = [] for (net_id, _i, port_id) in requested_networks: if port_id: try: port = neutron.show_port(port_id).get('port') except neutronv2.exceptions.NeutronClientException as e: if e.status_code == 404: port = None else: with excutils.save_and_reraise_exception(): LOG.exception(_("Failed to access port %s"), port_id) if not port: raise exception.PortNotFound(port_id=port_id) if port.get('device_id', None): raise exception.PortInUse(port_id=port_id) if not port.get('fixed_ips'): raise exception.PortRequiresFixedIP(port_id=port_id) net_id = port['network_id'] else: ports_needed_per_instance += 1 if net_id in net_ids: raise exception.NetworkDuplicated(network_id=net_id) net_ids.append(net_id) # Now check to see if all requested networks exist nets = self._get_available_networks(context, context.project_id, net_ids, neutron=neutron) for net in nets: if not net.get('subnets'): raise exception.NetworkRequiresSubnet( network_uuid=net['id']) if len(nets) != len(net_ids): requsted_netid_set = set(net_ids) returned_netid_set = set([net['id'] for net in nets]) lostid_set = requsted_netid_set - returned_netid_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) # Note(PhilD): Ideally Nova would create all required ports as part of # network validation, but port creation requires some details # from the hypervisor. So we just check the quota and return # how many of the requested number of instances can be created ports = [] quotas = neutron.show_quota(tenant_id=context.project_id)['quota'] if quotas.get('port', -1) == -1: # Unlimited Port Quota return num_instances else: free_ports = quotas.get('port') - len(ports) ports_needed = ports_needed_per_instance * num_instances if free_ports >= ports_needed: return num_instances else: return free_ports // ports_needed_per_instance
def disassociate(self, context, network_id): for i, network in enumerate(self.networks): if network.get('uuid') == network_id: return True raise common.RemoteError(type(exception.NetworkNotFound()).__name__)
def test_network_show_not_found(self, get_mock): ctxt = self.req.environ['nova.context'] get_mock.side_effect = exception.NetworkNotFound(network_id=1) self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, self.req, 1) get_mock.assert_called_once_with(ctxt, 1)
def test_network_delete_exception_network_not_found(self): ex = exception.NetworkNotFound(network_id=1) expex = webob.exc.HTTPNotFound self._test_network_delete_exception(None, ex, expex)
def get(self, context, network_id): for network in self.networks: if network.get('uuid') == network_id: return network raise exception.NetworkNotFound()
def network_get_fake(context, network_id): nets = [n for n in networks if n['id'] == network_id] if not nets: raise exception.NetworkNotFound(network_id=network_id) return nets[0]
def disassociate(self, context, network_uuid): for network in self.networks: if network.get('uuid') == network_uuid: network['project_id'] = None return True raise exception.NetworkNotFound()
def get(self, context, network_id): for network in self.networks: if network['id'] == network_id: return network raise exception.NetworkNotFound(network_id=network_id)
def delete(self, context, network_id): for i, network in enumerate(self.networks): if network['id'] == network_id: del self.networks[0] return True raise exception.NetworkNotFound()
def network_get_fake(context, network_id, project_only='allow_none'): nets = [n for n in networks if n['id'] == network_id] if not nets: raise exception.NetworkNotFound(network_id=network_id) return nets[0]
def disassociate(self, context, network_id): for i, network in enumerate(self.networks): if network['id'] == network_id: return True raise exception.NetworkNotFound()