diff options
Diffstat (limited to 'nova')
-rw-r--r-- | nova/api/openstack/compute/contrib/hosts.py | 75 | ||||
-rw-r--r-- | nova/tests/api/openstack/compute/contrib/test_hosts.py | 55 |
2 files changed, 56 insertions, 74 deletions
diff --git a/nova/api/openstack/compute/contrib/hosts.py b/nova/api/openstack/compute/contrib/hosts.py index 53f1a064a..6eee93e7b 100644 --- a/nova/api/openstack/compute/contrib/hosts.py +++ b/nova/api/openstack/compute/contrib/hosts.py @@ -68,6 +68,16 @@ class HostActionTemplate(xmlutil.TemplateBuilder): return xmlutil.MasterTemplate(root, 1) +class HostShowTemplate(xmlutil.TemplateBuilder): + def construct(self): + root = xmlutil.TemplateElement('host') + elem = xmlutil.make_flat_dict('resource', selector='host', + subselector='resource') + root.append(elem) + + return xmlutil.MasterTemplate(root, 1) + + class HostDeserializer(wsgi.XMLDeserializer): def default(self, string): try: @@ -83,15 +93,6 @@ class HostDeserializer(wsgi.XMLDeserializer): return dict(body=updates) -class HostShowTemplate(xmlutil.TemplateBuilder): - def construct(self): - root = xmlutil.TemplateElement('host', selector='host') - root.set('resource') - root.set('usage') - - return xmlutil.MasterTemplate(root, 1) - - def _list_hosts(req, service=None): """Returns a summary list of hosts, optionally filtering by service type. @@ -192,16 +193,10 @@ class HostController(object): :param context: security context :param host: hostname - :returns: - {'host': {'resource':D1, 'usage':{proj_id1:D2,..}}} - - 'resource' shows "available" and "in-use" vcpus, memory and disk. - 'usage' shows "in-use" vcpus, memory and disk per project. - - D1: {'vcpus': 16, 'memory_mb': 2048, 'local_gb': 2048, - 'vcpus_used': 12, 'memory_mb_used': 10240, - 'local_gb_used': 64} - D2: {'vcpus': 1, 'memory_mb': 2048, 'local_gb': 20} + :returns: expected to use HostShowTemplate. + ex. {'host': {'resource':D},..} + D: {'host': 'hostname','project': 'admin', + 'cpu': 1, 'memory_mb': 2048, 'disk_gb': 30} """ host = id context = req.environ['nova.context'] @@ -222,16 +217,28 @@ class HostController(object): # Getting total available/used resource compute_ref = compute_ref['compute_node'][0] - resource = {'vcpus': compute_ref['vcpus'], - 'memory_mb': compute_ref['memory_mb'], - 'local_gb': compute_ref['local_gb'], - 'vcpus_used': compute_ref['vcpus_used'], - 'memory_mb_used': compute_ref['memory_mb_used'], - 'local_gb_used': compute_ref['local_gb_used']} - usage = dict() - if not instance_refs: - return {'host': - {'resource': resource, 'usage': usage}} + resources = [{'resource': {'host': host, 'project': '(total)', + 'cpu': compute_ref['vcpus'], + 'memory_mb': compute_ref['memory_mb'], + 'disk_gb': compute_ref['local_gb']}}, + {'resource': {'host': host, 'project': '(used_now)', + 'cpu': compute_ref['vcpus_used'], + 'memory_mb': compute_ref['memory_mb_used'], + 'disk_gb': compute_ref['local_gb_used']}}] + + cpu_sum = 0 + mem_sum = 0 + hdd_sum = 0 + for i in instance_refs: + cpu_sum += i['vcpus'] + mem_sum += i['memory_mb'] + hdd_sum += i['root_gb'] + i['ephemeral_gb'] + + resources.append({'resource': {'host': host, + 'project': '(used_max)', + 'cpu': cpu_sum, + 'memory_mb': mem_sum, + 'disk_gb': hdd_sum}}) # Getting usage resource per project project_ids = [i['project_id'] for i in instance_refs] @@ -246,11 +253,13 @@ class HostController(object): disk = [i['root_gb'] + i['ephemeral_gb'] for i in instance_refs if i['project_id'] == project_id] - usage[project_id] = {'vcpus': reduce(lambda x, y: x + y, vcpus), - 'memory_mb': reduce(lambda x, y: x + y, mem), - 'local_gb': reduce(lambda x, y: x + y, disk)} + resources.append({'resource': {'host': host, + 'project': project_id, + 'cpu': reduce(lambda x, y: x + y, vcpus), + 'memory_mb': reduce(lambda x, y: x + y, mem), + 'disk_gb': reduce(lambda x, y: x + y, disk)}}) - return {'host': {'resource': resource, 'usage': usage}} + return {'host': resources} class Hosts(extensions.ExtensionDescriptor): diff --git a/nova/tests/api/openstack/compute/contrib/test_hosts.py b/nova/tests/api/openstack/compute/contrib/test_hosts.py index 925dc0d53..a16b8a5f5 100644 --- a/nova/tests/api/openstack/compute/contrib/test_hosts.py +++ b/nova/tests/api/openstack/compute/contrib/test_hosts.py @@ -184,17 +184,6 @@ class HostTestCase(test.TestCase): self.controller.show, self.req, dest) - def _dic_is_equal(self, dic1, dic2, keys=None): - """Compares 2 dictionary contents(Helper method)""" - if not keys: - keys = ['vcpus', 'memory_mb', 'local_gb', - 'vcpus_used', 'memory_mb_used', 'local_gb_used'] - - for key in keys: - if not (dic1[key] == dic2[key]): - return False - return True - def _create_compute_service(self): """Create compute-manager(ComputeNode and Service record).""" ctxt = context.get_admin_context() @@ -218,14 +207,13 @@ class HostTestCase(test.TestCase): result = self.controller.show(self.req, s_ref['host']) - # result checking - c1 = ('resource' in result['host'] and - 'usage' in result['host']) - compute_node = s_ref['compute_node'][0] - c2 = self._dic_is_equal(result['host']['resource'], - compute_node) - c3 = result['host']['usage'] == {} - self.assertTrue(c1 and c2 and c3) + proj = ['(total)', '(used_now)', '(used_max)'] + column = ['host', 'project', 'cpu', 'memory_mb', 'disk_gb'] + self.assertEqual(len(result['host']), 3) + for resource in result['host']: + self.assertTrue(resource['resource']['project'] in proj) + self.assertEqual(len(resource['resource']), 5) + self.assertTrue(set(resource['resource'].keys()) == set(column)) db.service_destroy(ctxt, s_ref['id']) def test_show_works_correctly(self): @@ -238,28 +226,13 @@ class HostTestCase(test.TestCase): result = self.controller.show(self.req, s_ref['host']) - c1 = ('resource' in result['host'] and - 'usage' in result['host']) - compute_node = s_ref['compute_node'][0] - c2 = self._dic_is_equal(result['host']['resource'], - compute_node) - c3 = result['host']['usage'].keys() == ['p-01', 'p-02'] - keys = ['vcpus', 'memory_mb'] - c4 = self._dic_is_equal( - result['host']['usage']['p-01'], i_ref1, keys) - disk = i_ref2['root_gb'] + i_ref2['ephemeral_gb'] - if result['host']['usage']['p-01']['local_gb'] == disk: - c6 = True - else: - c6 = False - c5 = self._dic_is_equal( - result['host']['usage']['p-02'], i_ref2, keys) - if result['host']['usage']['p-02']['local_gb'] == disk: - c7 = True - else: - c7 = False - self.assertTrue(c1 and c2 and c3 and c4 and c5 and c6 and c7) - + proj = ['(total)', '(used_now)', '(used_max)', 'p-01', 'p-02'] + column = ['host', 'project', 'cpu', 'memory_mb', 'disk_gb'] + self.assertEqual(len(result['host']), 5) + for resource in result['host']: + self.assertTrue(resource['resource']['project'] in proj) + self.assertEqual(len(resource['resource']), 5) + self.assertTrue(set(resource['resource'].keys()) == set(column)) db.service_destroy(ctxt, s_ref['id']) db.instance_destroy(ctxt, i_ref1['id']) db.instance_destroy(ctxt, i_ref2['id']) |