def evacuate_host(request, host, target=None, on_shared_storage=False): # TODO(jmolle) This should be change for nova atomic api host_evacuate hypervisors = novaclient(request).hypervisors.search(host, True) response = [] err_code = None for hypervisor in hypervisors: hyper = Hypervisor(hypervisor) # if hypervisor doesn't have servers, the attribute is not present for server in hyper.servers: try: novaclient(request).servers.evacuate(server['uuid'], target, on_shared_storage) except nova_exceptions.ClientException as err: err_code = err.code msg = _("Name: %(name)s ID: %(uuid)s") msg = msg % {'name': server['name'], 'uuid': server['uuid']} response.append(msg) if err_code: msg = _('Failed to evacuate instances: %s') % ', '.join(response) raise nova_exceptions.ClientException(err_code, msg) return True
def test_associate_post_with_exception(self): server = self.server self.mox.StubOutWithMock(api, 'server_list') api.server_list = self.mox.CreateMockAnything() api.server_list(IsA(http.HttpRequest)).AndReturn(self.servers) self.mox.StubOutWithMock(api, 'tenant_floating_ip_list') api.tenant_floating_ip_list(IsA(http.HttpRequest)).\ AndReturn(self.floating_ips) self.mox.StubOutWithMock(api, 'server_add_floating_ip') api.server_add_floating_ip = self.mox.CreateMockAnything() exception = novaclient_exceptions.ClientException('ClientException', message='clientException') api.server_add_floating_ip(IsA(http.HttpRequest), IsA(unicode), IsA(unicode)).\ AndRaise(exception) self.mox.StubOutWithMock(messages, 'error') messages.error(IsA(http.HttpRequest), IsA(basestring)) self.mox.StubOutWithMock(api, 'tenant_floating_ip_get') api.tenant_floating_ip_get = self.mox.CreateMockAnything() api.tenant_floating_ip_get(IsA(http.HttpRequest), IsA(unicode)).\ AndReturn(self.floating_ip) self.mox.ReplayAll() res = self.client.post(reverse('horizon:nova:floating_ips:associate', args=[1]), {'instance_id': 1, 'floating_ip_id': self.floating_ip.id, 'floating_ip': self.floating_ip.ip, 'method': 'FloatingIpAssociate'}) self.assertRaises(novaclient_exceptions.ClientException) self.assertRedirects(res, FLOATING_IPS_INDEX)
def test_create_security_groups_post_exception(self): SECGROUP_NAME = 'fakegroup' SECGROUP_DESC = 'fakegroup_desc' exception = novaclient_exceptions.ClientException( 'ClientException', message='ClientException') formData = { 'method': 'CreateGroup', 'tenant_id': self.TEST_TENANT, 'name': SECGROUP_NAME, 'description': SECGROUP_DESC, } self.mox.StubOutWithMock(api, 'security_group_create') api.security_group_create(IsA(http.HttpRequest), SECGROUP_NAME, SECGROUP_DESC).AndRaise(exception) self.mox.ReplayAll() res = self.client.post(SG_CREATE_URL, formData) self.assertTemplateUsed( res, 'nova/access_and_security/security_groups/create.html')
def test_disassociate_post_with_exception(self): floating_ip = self.floating_ips.first() server = self.servers.first() self.mox.StubOutWithMock(api.nova, 'keypair_list') self.mox.StubOutWithMock(api, 'security_group_list') self.mox.StubOutWithMock(api, 'tenant_floating_ip_list') self.mox.StubOutWithMock(api, 'tenant_floating_ip_get') self.mox.StubOutWithMock(api, 'server_remove_floating_ip') api.nova.keypair_list(IsA(http.HttpRequest)) \ .AndReturn(self.keypairs.list()) api.security_group_list(IsA(http.HttpRequest)) \ .AndReturn(self.security_groups.list()) api.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) exc = novaclient_exceptions.ClientException('ClientException') api.server_remove_floating_ip(IsA(http.HttpRequest), server.id, floating_ip.id).AndRaise(exc) self.mox.ReplayAll() action = "floating_ips__disassociate__%s" % floating_ip.id res = self.client.post(INDEX_URL, {"action": action}) self.assertRaises(novaclient_exceptions.ClientException) self.assertRedirectsNoFollow(res, INDEX_URL)
def find_images(self, names_or_ids): """Find multiple images by name or id (user provided input). :param names_or_ids: A list of strings to use to find images. :returns: novaclient.v2.images.Image objects for each images found :raises exceptions.NotFound: If one or more images is not found :raises exceptions.ClientException: If the image service returns any unexpected images. NOTE: This method always makes two calls to the image service, even if only one image is provided by ID and is returned in the first query. """ with self.alternate_service_type('image', allowed_types=('image', )): matches = self._list( '/v2/images?id=in:%s' % ','.join(names_or_ids), 'images') matches.extend( self._list('/v2/images?names=in:%s' % ','.join(names_or_ids), 'images')) missed = (set(names_or_ids) - set(m.name for m in matches) - set(m.id for m in matches)) if missed: msg = _("Unable to find image(s): %(images)s") % { "images": ",".join(missed) } raise exceptions.NotFound(404, msg) for match in matches: match.append_request_ids(matches.request_ids) additional = [] for i in matches: if i.name not in names_or_ids and i.id not in names_or_ids: additional.append(i) if additional: msg = _('Additional images found in response') raise exceptions.ClientException(500, msg) return matches
def _raise_nova_client_exception(*args, **kwargs): raise nova_exceptions.ClientException("Boom!")