def test_create_server_detect_from_image(self): """If user doesn't pass in diskConfig for server, use image metadata to specify AUTO or MANUAL. """ req = fakes.HTTPRequest.blank('/fake/servers') req.method = 'POST' req.content_type = 'application/json' body = {'server': { 'name': 'server_test', 'imageRef': 'a440c04b-79fa-479c-bed1-0b816eaec379', 'flavorRef': '1', }} req.body = jsonutils.dumps(body) res = req.get_response(self.app) server_dict = jsonutils.loads(res.body)['server'] self.assertDiskConfig(server_dict, 'MANUAL') req = fakes.HTTPRequest.blank('/fake/servers') req.method = 'POST' req.content_type = 'application/json' body = {'server': { 'name': 'server_test', 'imageRef': '70a599e0-31e7-49b7-b260-868f441e862b', 'flavorRef': '1', }} req.body = jsonutils.dumps(body) res = req.get_response(self.app) server_dict = jsonutils.loads(res.body)['server'] self.assertDiskConfig(server_dict, 'AUTO')
def test_evacuate_instance_with_empty_host(self): req, app = self._gen_request_with_app({'host': '', 'on_shared_storage': 'False', 'admin_password': '******'}) res = req.get_response(app) jsonutils.loads(res.body) self.assertEqual(400, res.status_int)
def test_create_server_detect_from_image(self): """If user doesn't pass in diskConfig for server, use image metadata to specify AUTO or MANUAL. """ req = fakes.HTTPRequestV3.blank("/v3/servers") req.method = "POST" req.content_type = "application/json" body = { "server": {"name": "server_test", "image_ref": "a440c04b-79fa-479c-bed1-0b816eaec379", "flavor_ref": "1"} } req.body = jsonutils.dumps(body) res = req.get_response(self.app) server_dict = jsonutils.loads(res.body)["server"] self.assertDiskConfig(server_dict, "MANUAL") req = fakes.HTTPRequestV3.blank("/v3/servers") req.method = "POST" req.content_type = "application/json" body = { "server": {"name": "server_test", "image_ref": "70a599e0-31e7-49b7-b260-868f441e862b", "flavor_ref": "1"} } req.body = jsonutils.dumps(body) res = req.get_response(self.app) server_dict = jsonutils.loads(res.body)["server"] self.assertDiskConfig(server_dict, "AUTO")
def test_save_flavor_pci_info(self): self.flags(pci_alias=[_fake_alias1, _fake_alias3]) expect_request = [ {'count': 3, 'spec': [{'vendor_id': '8086', 'product_id': '4443', 'device_type': "ACCEL", 'capability_type': 'pci'}], 'alias_name': 'QuicAssist'}, {'count': 1, 'spec': [{'vendor_id': '8086', 'product_id': '1111', 'device_type': "NIC", 'capability_type': 'pci'}], 'alias_name': 'IntelNIC'}, ] flavor = {'extra_specs': {"pci_passthrough:alias": "QuicAssist:3, IntelNIC: 1"}} meta = {} pci_request.save_flavor_pci_info(meta, flavor) real = jsonutils.loads(meta['pci_requests']) exp_real = zip(expect_request, real) for exp, real in exp_real: self.assertEqual(real, exp) meta = {} pci_request.save_flavor_pci_info(meta, flavor, "old_") real = jsonutils.loads(meta['old_pci_requests']) exp_real = zip(expect_request, real) for exp, real in exp_real: self.assertEqual(real, exp)
def update_from_compute_node(self, compute): """Update information about a host from its compute_node info.""" if (self.updated and compute['updated_at'] and self.updated > compute['updated_at']): return all_ram_mb = compute['memory_mb'] # Assume virtual size is all consumed by instances if use qcow2 disk. free_gb = compute['free_disk_gb'] least_gb = compute.get('disk_available_least') if least_gb is not None: if least_gb > free_gb: # can occur when an instance in database is not on host LOG.warn(_LW("Host has more disk space than database " "expected (%(physical)sgb > %(database)sgb)"), {'physical': least_gb, 'database': free_gb}) free_gb = min(least_gb, free_gb) free_disk_mb = free_gb * 1024 self.disk_mb_used = compute['local_gb_used'] * 1024 # NOTE(jogo) free_ram_mb can be negative self.free_ram_mb = compute['free_ram_mb'] self.total_usable_ram_mb = all_ram_mb self.total_usable_disk_gb = compute['local_gb'] self.free_disk_mb = free_disk_mb self.vcpus_total = compute['vcpus'] self.vcpus_used = compute['vcpus_used'] self.updated = compute['updated_at'] self.numa_topology = compute['numa_topology'] if 'pci_stats' in compute: self.pci_stats = pci_stats.PciDeviceStats(compute['pci_stats']) else: self.pci_stats = None # All virt drivers report host_ip self.host_ip = compute['host_ip'] self.hypervisor_type = compute.get('hypervisor_type') self.hypervisor_version = compute.get('hypervisor_version') self.hypervisor_hostname = compute.get('hypervisor_hostname') self.cpu_info = compute.get('cpu_info') if compute.get('supported_instances'): self.supported_instances = jsonutils.loads( compute.get('supported_instances')) # Don't store stats directly in host_state to make sure these don't # overwrite any values, or get overwritten themselves. Store in self so # filters can schedule with them. stats = compute.get('stats', None) or '{}' self.stats = jsonutils.loads(stats) # Track number of instances on host self.num_instances = int(self.stats.get('num_instances', 0)) self.num_io_ops = int(self.stats.get('io_workload', 0)) # update metrics self._update_metrics_from_compute_node(compute)
def test_keypair_create_without_keypair(self): body = {'foo': None} req = webob.Request.blank('/v3/keypairs') req.method = 'POST' req.body = jsonutils.dumps(body) req.headers['Content-Type'] = 'application/json' res = req.get_response(self.app) self.assertEqual(res.status_int, 400) jsonutils.loads(res.body)
def test_keypair_create_with_invalid_keypair_body(self): body = {'alpha': {'name': 'create_test'}} req = webob.Request.blank('/v3/keypairs') req.method = 'POST' req.body = jsonutils.dumps(body) req.headers['Content-Type'] = 'application/json' res = req.get_response(self.app) jsonutils.loads(res.body) self.assertEqual(res.status_int, 400)
def test_show_server(self): req = fakes.HTTPRequestV3.blank("/v3/servers/%s" % MANUAL_INSTANCE_UUID) res = req.get_response(self.app) server_dict = jsonutils.loads(res.body)["server"] self.assertDiskConfig(server_dict, "MANUAL") req = fakes.HTTPRequestV3.blank("/v3/servers/%s" % AUTO_INSTANCE_UUID) res = req.get_response(self.app) server_dict = jsonutils.loads(res.body)["server"] self.assertDiskConfig(server_dict, "AUTO")
def test_vendor_content_type_json(self): ctype = 'application/vnd.openstack.compute+json' req = webob.Request.blank('/') req.headers['Accept'] = ctype res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) self.assertEqual(res.content_type, ctype) jsonutils.loads(res.body)
def test_get_rdp_console_not_ready(self): self.stubs.Set(compute_api.API, 'get_rdp_console', fake_get_rdp_console_not_ready) body = {'os-getRDPConsole': {'type': 'rdp-html5'}} req = webob.Request.blank(self.url) req.method = "POST" req.body = jsonutils.dumps(body) req.headers["content-type"] = "application/json" res = req.get_response(self.app) jsonutils.loads(res.body) self.assertEqual(res.status_int, 409)
def from_json(cls, json_string): return cls._from_dict(jsonutils.loads(json_string))
def load_json(cls, data, default_rule=None): """Init a brain using json instead of a rules dictionary.""" rules_dict = jsonutils.loads(data) return cls(rules=rules_dict, default_rule=default_rule)
def _get_server(body): return jsonutils.loads(body).get('server')
def __init__(self, stats=None): super(PciDeviceStats, self).__init__() self.pools = jsonutils.loads(stats) if stats else [] self.pools.sort(self.pool_cmp)
def _prepare_handoff_dest(self, end_point, dest_token, instance, dest_vmname=None): # information of current VM at source if dest_vmname: instance_name = dest_vmname else: instance_name = instance['display_name'] + "-handoff" flavor_memory = instance['memory_mb'] flavor_cpu = instance['vcpus'] requested_basevm_id = instance['system_metadata'][ 'image_base_sha256_uuid'] original_overlay_url = \ instance.get("metadata", dict()).get("overlay_url", None) # find matching base VM image_list = self._get_server_info(end_point, dest_token, "images") basevm_uuid = None for image_item in image_list: properties = image_item.get("metadata", None) if properties is None or len(properties) == 0: continue if properties.get(CloudletAPI.PROPERTY_KEY_CLOUDLET_TYPE) != \ CloudletAPI.IMAGE_TYPE_BASE_DISK: continue base_sha256_uuid = properties.get( CloudletAPI.PROPERTY_KEY_BASE_UUID) if base_sha256_uuid == requested_basevm_id: basevm_uuid = image_item['id'] break if basevm_uuid is None: msg = "Cannot find matching Base VM with (%s) at (%s)" %\ (str(requested_basevm_id), end_point.netloc) raise HandoffError(msg) # Find matching flavor. def find_matching_flavor(flavor_list, cpu_count, memory_mb): for flavor in flavor_list: vcpu = int(flavor['vcpus']) ram_mb = int(flavor['ram']) if vcpu == cpu_count and ram_mb == memory_mb: flavor_ref = flavor['links'][0]['href'] flavor_id = flavor['id'] return flavor_ref, flavor_id return None, None flavor_list = self._get_server_info(end_point, dest_token, "flavors") flavor_ref, flavor_id = find_matching_flavor(flavor_list, flavor_cpu, flavor_memory) if flavor_ref is None or flavor_id is None: msg = "Cannot find matching flavor with cpu=%d, memory=%d at %s" %\ (flavor_cpu, flavor_memory, end_point.netloc) raise HandoffError(msg) # generate request meta_data = { "handoff_info": instance_name, "overlay_url": original_overlay_url } s = { "server": { "name": instance_name, "imageRef": str(basevm_uuid), "flavorRef": flavor_id, "metadata": meta_data, "min_count": "1", "max_count": "1", "key_name": None, } } params = jsonutils.dumps(s) headers = { "X-Auth-Token": dest_token, "Content-type": "application/json" } conn = httplib.HTTPConnection(end_point[1]) conn.request("POST", "%s/servers" % end_point[2], params, headers) LOG.info("request handoff to %s" % (end_point.netloc)) response = conn.getresponse() data = response.read() dd = jsonutils.loads(data) conn.close() return dd
def _json_loads(properties, attr): prop = properties[attr] if isinstance(prop, six.string_types): properties[attr] = jsonutils.loads(prop)
def test_get_diagnostics(self): req = fakes.HTTPRequestV3.blank( '/servers/%s/os-server-diagnostics' % UUID) res = req.get_response(self.router) output = jsonutils.loads(res.body) self.assertEqual(output, {'data': 'Some diagnostic info'})
def hydrate(cls, network_info): if isinstance(network_info, basestring): network_info = jsonutils.loads(network_info) return NetworkInfo([VIF.hydrate(vif) for vif in network_info])
def test_multi_choice_server(self): uuid = str(stdlib_uuid.uuid4()) req = webob.Request.blank('/servers/' + uuid) req.accept = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 300) self.assertEqual(res.content_type, "application/json") expected = { "choices": [ { "id": "v2.0", "status": "CURRENT", "links": [ { "href": "http://localhost/v2/servers/" + uuid, "rel": "self", }, ], "media-types": [ { "base": "application/xml", "type": "application/vnd.openstack.compute+xml" ";version=2" }, { "base": "application/json", "type": "application/vnd.openstack.compute+json" ";version=2" }, ], }, { "id": "v2.1", "status": "EXPERIMENTAL", "links": [ { "href": "http://localhost/v2/servers/" + uuid, "rel": "self", }, ], "media-types": [{ "base": "application/json", "type": "application/vnd.openstack.compute+json;version=2.1", }], }, ], } self.assertThat(jsonutils.loads(res.body), matchers.DictMatches(expected))
def test_create_public_default(self): expected = self.expected_flavor res = self._create_flavor_helper(self.base_request_dict) body = jsonutils.loads(res.body) for key in expected["flavor"]: self.assertEqual(body["flavor"][key], expected["flavor"][key])
def update_available_resource(self, context): """Override in-memory calculations of compute node resource usage based on data audited from the hypervisor layer. Add in resource claims in progress to account for operations that have declared a need for resources, but not necessarily retrieved them from the hypervisor layer yet. """ LOG.audit(_("Auditing locally available compute resources")) resources = self.driver.get_available_resource(self.nodename) if not resources: # The virt driver does not support this function LOG.audit(_("Virt driver does not support " "'get_available_resource' Compute tracking is disabled.")) self.compute_node = None return resources['host_ip'] = CONF.my_ip self._verify_resources(resources) self._report_hypervisor_resource_view(resources) if 'pci_passthrough_devices' in resources: if not self.pci_tracker: self.pci_tracker = pci_manager.PciDevTracker() self.pci_tracker.set_hvdevs(jsonutils.loads(resources.pop( 'pci_passthrough_devices'))) # Grab all instances assigned to this node: instances = instance_obj.InstanceList.get_by_host_and_node( context, self.host, self.nodename) # Now calculate usage based on instance utilization: self._update_usage_from_instances(resources, instances) # Grab all in-progress migrations: capi = self.conductor_api migrations = capi.migration_get_in_progress_by_host_and_node(context, self.host, self.nodename) self._update_usage_from_migrations(context, resources, migrations) # Detect and account for orphaned instances that may exist on the # hypervisor, but are not in the DB: orphans = self._find_orphaned_instances() self._update_usage_from_orphans(resources, orphans) # NOTE(yjiang5): Because pci device tracker status is not cleared in # this periodic task, and also because the resource tracker is not # notified when instances are deleted, we need remove all usages # from deleted instances. if self.pci_tracker: self.pci_tracker.clean_usage(instances, migrations, orphans) resources['pci_stats'] = jsonutils.dumps(self.pci_tracker.stats) else: resources['pci_stats'] = jsonutils.dumps({}) self._report_final_resource_view(resources) self._sync_compute_node(context, resources)
def test_get_console_connect_info(self): req = self._create_request() res = req.get_response(self.app) self.assertEqual(200, res.status_int) output = jsonutils.loads(res.body) self.assertEqual(self._EXPECTED_OUTPUT, output)
def update_from_compute_node(self, compute): """Update information about a host from its compute_node info.""" if (self.updated and compute['updated_at'] and self.updated > compute['updated_at']): return all_ram_mb = compute['memory_mb'] # Assume virtual size is all consumed by instances if use qcow2 disk. least = compute.get('disk_available_least') free_disk_mb = least if least is not None else compute['free_disk_gb'] free_disk_mb *= 1024 self.disk_mb_used = compute['local_gb_used'] * 1024 #NOTE(jogo) free_ram_mb can be negative self.free_ram_mb = compute['free_ram_mb'] self.total_usable_ram_mb = all_ram_mb self.total_usable_disk_gb = compute['local_gb'] self.free_disk_mb = free_disk_mb self.vcpus_total = compute['vcpus'] self.vcpus_used = compute['vcpus_used'] self.updated = compute['updated_at'] if 'pci_stats' in compute: self.pci_stats = pci_stats.PciDeviceStats(compute['pci_stats']) else: self.pci_stats = None # All virt drivers report host_ip self.host_ip = compute['host_ip'] self.hypervisor_type = compute.get('hypervisor_type') self.hypervisor_version = compute.get('hypervisor_version') self.hypervisor_hostname = compute.get('hypervisor_hostname') self.cpu_info = compute.get('cpu_info') if compute.get('supported_instances'): self.supported_instances = jsonutils.loads( compute.get('supported_instances')) # Don't store stats directly in host_state to make sure these don't # overwrite any values, or get overwritten themselves. Store in self so # filters can schedule with them. self.stats = self._statmap(compute.get('stats', [])) self.hypervisor_version = compute['hypervisor_version'] # Track number of instances on host self.num_instances = int(self.stats.get('num_instances', 0)) # Track number of instances by project_id project_id_keys = [ k for k in self.stats.keys() if k.startswith("num_proj_") ] for key in project_id_keys: project_id = key[9:] self.num_instances_by_project[project_id] = int(self.stats[key]) # Track number of instances in certain vm_states vm_state_keys = [ k for k in self.stats.keys() if k.startswith("num_vm_") ] for key in vm_state_keys: vm_state = key[7:] self.vm_states[vm_state] = int(self.stats[key]) # Track number of instances in certain task_states task_state_keys = [ k for k in self.stats.keys() if k.startswith("num_task_") ] for key in task_state_keys: task_state = key[9:] self.task_states[task_state] = int(self.stats[key]) # Track number of instances by host_type os_keys = [ k for k in self.stats.keys() if k.startswith("num_os_type_") ] for key in os_keys: os = key[12:] self.num_instances_by_os_type[os] = int(self.stats[key]) self.num_io_ops = int(self.stats.get('io_workload', 0)) # update metrics self._update_metrics_from_compute_node(compute)
def _from_json(self, datastring): try: return jsonutils.loads(datastring) except ValueError: msg = _("cannot understand JSON") raise exception.MalformedRequestBody(reason=msg)
def update_from_compute_node(self, compute, force_update=False): """Update information about a host from its compute_node info.""" if (not force_update and self.updated and compute['updated_at'] and self.updated > compute['updated_at']): return all_ram_mb = compute['memory_mb'] # Assume virtual size is all consumed by instances if use qcow2 disk. free_gb = compute['free_disk_gb'] least_gb = compute.get('disk_available_least') if least_gb is not None: if least_gb > free_gb: # can occur when an instance in database is not on host LOG.warn( _LW("Host has more disk space than database " "expected (%(physical)sgb > %(database)sgb)"), { 'physical': least_gb, 'database': free_gb }) free_gb = min(least_gb, free_gb) free_disk_mb = free_gb * 1024 self.disk_mb_used = compute['local_gb_used'] * 1024 # NOTE(jogo) free_ram_mb can be negative self.free_ram_mb = compute['free_ram_mb'] self.total_usable_ram_mb = all_ram_mb self.total_usable_disk_gb = compute['local_gb'] self.free_disk_mb = free_disk_mb self.vcpus_total = compute['vcpus'] self.vcpus_used = compute['vcpus_used'] self.updated = compute['updated_at'] self.numa_topology = compute['numa_topology'] if 'pci_stats' in compute: self.pci_stats = pci_stats.PciDeviceStats(compute['pci_stats']) else: self.pci_stats = None # All virt drivers report host_ip self.host_ip = compute['host_ip'] self.hypervisor_type = compute.get('hypervisor_type') self.hypervisor_version = compute.get('hypervisor_version') self.hypervisor_hostname = compute.get('hypervisor_hostname') self.cpu_info = compute.get('cpu_info') if compute.get('supported_instances'): self.supported_instances = jsonutils.loads( compute.get('supported_instances')) # Don't store stats directly in host_state to make sure these don't # overwrite any values, or get overwritten themselves. Store in self so # filters can schedule with them. stats = compute.get('stats', None) or '{}' self.stats = jsonutils.loads(stats) # Track number of instances on host self.num_instances = int(self.stats.get('num_instances', 0)) self.num_io_ops = int(self.stats.get('io_workload', 0)) # update metrics self._update_metrics_from_compute_node(compute)
def deserialize(self, data, content_type): return jsonutils.loads(data)
def _deserialize(data): """ Deserialization wrapper """ LOG.debug(_("Deserializing: %s"), data) return jsonutils.loads(data)
def _extend_hypervisor(self, hypervisor, compute_node): hypervisor['%s:pci_stats' % Pci.alias] = jsonutils.loads( compute_node['pci_stats'])
def _get_flavors(self, body): return jsonutils.loads(body).get('flavors')
def _json_loads(properties, attr): prop = properties[attr] if isinstance(prop, basestring): properties[attr] = jsonutils.loads(prop)
def _get_servers(self, body): return jsonutils.loads(body).get('servers')
def test_index_json(self): # Test getting limit details in JSON. request = self._get_index_request() request = self._populate_limits(request) self.absolute_limits = { 'ram': 512, 'instances': 5, 'cores': 21, 'key_pairs': 10, 'floating_ips': 10, 'security_groups': 10, 'security_group_rules': 20, } response = request.get_response(self.controller) expected = { "limits": { "rate": [ { "regex": ".*", "uri": "*", "limit": [ { "verb": "GET", "next-available": "1970-01-01T00:00:00Z", "unit": "MINUTE", "value": 10, "remaining": 10, }, { "verb": "POST", "next-available": "1970-01-01T00:00:00Z", "unit": "HOUR", "value": 5, "remaining": 5, }, ], }, { "regex": "changes-since", "uri": "changes-since*", "limit": [ { "verb": "GET", "next-available": "1970-01-01T00:00:00Z", "unit": "MINUTE", "value": 5, "remaining": 5, }, ], }, ], "absolute": { "maxTotalRAMSize": 512, "maxTotalInstances": 5, "maxTotalCores": 21, "maxTotalKeypairs": 10, "maxTotalFloatingIps": 10, "maxSecurityGroups": 10, "maxSecurityGroupRules": 20, }, }, } body = jsonutils.loads(response.body) self.assertEqual(expected, body)
def hydrate(cls, network_info): if isinstance(network_info, six.string_types): network_info = jsonutils.loads(network_info) return cls([VIF.hydrate(vif) for vif in network_info])
def _test_index_absolute_limits_json(self, expected): request = self._get_index_request() response = request.get_response(self.controller) body = jsonutils.loads(response.body) self.assertEqual(expected, body['limits']['absolute'])
def _get_images(self, body): return jsonutils.loads(body).get('images')
def _call_agent(session, instance, vm_ref, method, addl_args=None, timeout=None, success_codes=None): """Abstracts out the interaction with the agent xenapi plugin.""" if addl_args is None: addl_args = {} if timeout is None: timeout = CONF.xenserver.agent_timeout if success_codes is None: success_codes = ['0'] # always fetch domid because VM may have rebooted dom_id = session.VM.get_domid(vm_ref) args = { 'id': str(uuid.uuid4()), 'dom_id': str(dom_id), 'timeout': str(timeout), } args.update(addl_args) try: ret = session.call_plugin('agent', method, args) except session.XenAPI.Failure as e: err_msg = e.details[-1].splitlines()[-1] if 'TIMEOUT:' in err_msg: LOG.error(_('TIMEOUT: The call to %(method)s timed out. ' 'args=%(args)r'), { 'method': method, 'args': args }, instance=instance) raise exception.AgentTimeout(method=method) elif 'NOT IMPLEMENTED:' in err_msg: LOG.error(_('NOT IMPLEMENTED: The call to %(method)s is not ' 'supported by the agent. args=%(args)r'), { 'method': method, 'args': args }, instance=instance) raise exception.AgentNotImplemented(method=method) else: LOG.error(_('The call to %(method)s returned an error: %(e)s. ' 'args=%(args)r'), { 'method': method, 'args': args, 'e': e }, instance=instance) raise exception.AgentError(method=method) if not isinstance(ret, dict): try: ret = jsonutils.loads(ret) except TypeError: LOG.error(_('The agent call to %(method)s returned an invalid ' 'response: %(ret)r. args=%(args)r'), { 'method': method, 'ret': ret, 'args': args }, instance=instance) raise exception.AgentError(method=method) if ret['returncode'] not in success_codes: LOG.error(_('The agent call to %(method)s returned an ' 'an error: %(ret)r. args=%(args)r'), { 'method': method, 'ret': ret, 'args': args }, instance=instance) raise exception.AgentError(method=method) LOG.debug( 'The agent call to %(method)s was successful: ' '%(ret)r. args=%(args)r', { 'method': method, 'ret': ret, 'args': args }, instance=instance) # Some old versions of the Windows agent have a trailing \\r\\n # (ie CRLF escaped) for some reason. Strip that off. return ret['message'].replace('\\r\\n', '')
def __call__(self, req): request_id = context.generate_request_id() signature = req.params.get('Signature') if not signature: msg = _("Signature not provided") return faults.ec2_error_response(request_id, "AuthFailure", msg, status=400) access = req.params.get('AWSAccessKeyId') if not access: msg = _("Access key not provided") return faults.ec2_error_response(request_id, "AuthFailure", msg, status=400) # Make a copy of args for authentication and signature verification. auth_params = dict(req.params) # Not part of authentication args auth_params.pop('Signature') cred_dict = { 'access': access, 'signature': signature, 'host': req.host, 'verb': req.method, 'path': req.path, 'params': auth_params, } if "ec2" in CONF.keystone_ec2_url: creds = {'ec2Credentials': cred_dict} else: creds = {'auth': {'OS-KSEC2:ec2Credentials': cred_dict}} creds_json = jsonutils.dumps(creds) headers = {'Content-Type': 'application/json'} o = urlparse.urlparse(CONF.keystone_ec2_url) if o.scheme == "http": conn = httplib.HTTPConnection(o.netloc) else: conn = httplib.HTTPSConnection(o.netloc) conn.request('POST', o.path, body=creds_json, headers=headers) response = conn.getresponse() data = response.read() if response.status != 200: if response.status == 401: msg = response.reason else: msg = _("Failure communicating with keystone") return faults.ec2_error_response(request_id, "AuthFailure", msg, status=response.status) result = jsonutils.loads(data) conn.close() try: token_id = result['access']['token']['id'] user_id = result['access']['user']['id'] project_id = result['access']['token']['tenant']['id'] user_name = result['access']['user'].get('name') project_name = result['access']['token']['tenant'].get('name') roles = [ role['name'] for role in result['access']['user']['roles'] ] except (AttributeError, KeyError) as e: LOG.exception(_("Keystone failure: %s") % e) msg = _("Failure communicating with keystone") return faults.ec2_error_response(request_id, "AuthFailure", msg, status=400) remote_address = req.remote_addr if CONF.use_forwarded_for: remote_address = req.headers.get('X-Forwarded-For', remote_address) catalog = result['access']['serviceCatalog'] ctxt = context.RequestContext(user_id, project_id, user_name=user_name, project_name=project_name, roles=roles, auth_token=token_id, remote_address=remote_address, service_catalog=catalog) req.environ['nova.context'] = ctxt return self.application
return {'returncode': 'timeout', 'message': err_msg} elif 'NOT IMPLEMENTED:' in err_msg: LOG.error(_('NOT IMPLEMENTED: The call to %(method)s is not' ' supported by the agent. args=%(args)r'), locals(), instance=instance) return {'returncode': 'notimplemented', 'message': err_msg} else: LOG.error(_('The call to %(method)s returned an error: %(e)s. ' 'args=%(args)r'), locals(), instance=instance) return {'returncode': 'error', 'message': err_msg} return None if isinstance(ret, dict): return ret try: return jsonutils.loads(ret) except TypeError: LOG.error(_('The agent call to %(method)s returned an invalid' ' response: %(ret)r. path=%(path)s; args=%(args)r'), locals(), instance=instance) return {'returncode': 'error', 'message': 'unable to deserialize response'} def _get_agent_version(session, instance, vm_ref): resp = _call_agent(session, instance, vm_ref, 'version') if resp['returncode'] != '0': LOG.error(_('Failed to query agent version: %(resp)r'), locals(), instance=instance) return None
def _test_index_json(self, tenant_id=None): # Test getting limit details in JSON. request = self._get_index_request(tenant_id=tenant_id) context = request.environ["nova.context"] if tenant_id is None: tenant_id = context.project_id request = self._populate_limits(request) self.absolute_limits = { 'ram': 512, 'instances': 5, 'cores': 21, 'key_pairs': 10, 'floating_ips': 10, 'security_groups': 10, 'security_group_rules': 20, } expected = { "limits": { "rate": [ { "regex": ".*", "uri": "*", "limit": [ { "verb": "GET", "next-available": "1970-01-01T00:00:00Z", "unit": "MINUTE", "value": 10, "remaining": 10, }, { "verb": "POST", "next-available": "1970-01-01T00:00:00Z", "unit": "HOUR", "value": 5, "remaining": 5, }, ], }, { "regex": "changes-since", "uri": "changes-since*", "limit": [ { "verb": "GET", "next-available": "1970-01-01T00:00:00Z", "unit": "MINUTE", "value": 5, "remaining": 5, }, ], }, ], "absolute": { "maxTotalRAMSize": 512, "maxTotalInstances": 5, "maxTotalCores": 21, "maxTotalKeypairs": 10, "maxTotalFloatingIps": 10, "maxSecurityGroups": 10, "maxSecurityGroupRules": 20, }, }, } def _get_project_quotas(context, project_id, usages=True): return dict((k, dict(limit=v)) for k, v in self.absolute_limits.items()) with mock.patch('nova.quota.QUOTAS.get_project_quotas') as \ get_project_quotas: get_project_quotas.side_effect = _get_project_quotas response = request.get_response(self.controller) body = jsonutils.loads(response.body) self.assertEqual(expected, body) get_project_quotas.assert_called_once_with(context, tenant_id, usages=False)
def test_get_console_output_with_length_as_str(self): req = self._create_request(length_dict={'length': '3'}) res = req.get_response(self.app) output = jsonutils.loads(res.body) self.assertEqual(res.status_int, 200) self.assertEqual(output, {'output': '2\n3\n4'})