From 67c0d16eb6022551ab4c629ee3901fc9cd58353c Mon Sep 17 00:00:00 2001 From: Jay Lau Date: Sun, 23 Feb 2014 08:52:19 +0800 Subject: [PATCH] Adds service request parameter filter for V3 API os-hosts request Both V2 and V3 API do not support service request parameter filter for os-hosts request. For V2 API, there is no need to having two different clouds having different APIs (one with service filter one without) but just take V2 API as a document bug. This patch will only fix V3 API. DocImpact Adds the ability to filter a hosts list request by service for the V3 API. Change-Id: I42163707049300b0dee677558ed49280bcc7369d Partial-Bug: #1224763 --- .../hosts-list-compute-service-resp.json | 9 +++++++++ nova/api/openstack/compute/plugins/v3/hosts.py | 3 +++ .../openstack/compute/plugins/v3/test_hosts.py | 18 ++++++++++++++++++ .../hosts-list-compute-service-resp.json.tpl | 9 +++++++++ nova/tests/integrated/v3/test_hosts.py | 6 ++++++ 5 files changed, 45 insertions(+) create mode 100644 doc/v3/api_samples/os-hosts/hosts-list-compute-service-resp.json create mode 100644 nova/tests/integrated/v3/api_samples/os-hosts/hosts-list-compute-service-resp.json.tpl diff --git a/doc/v3/api_samples/os-hosts/hosts-list-compute-service-resp.json b/doc/v3/api_samples/os-hosts/hosts-list-compute-service-resp.json new file mode 100644 index 0000000000..7d10989bbb --- /dev/null +++ b/doc/v3/api_samples/os-hosts/hosts-list-compute-service-resp.json @@ -0,0 +1,9 @@ +{ + "hosts": [ + { + "host_name": "09c025b0efc64211bd23fc50fa974cdf", + "service": "compute", + "zone": "nova" + } + ] +} diff --git a/nova/api/openstack/compute/plugins/v3/hosts.py b/nova/api/openstack/compute/plugins/v3/hosts.py index 7bfa6f652c..e638e4ab13 100644 --- a/nova/api/openstack/compute/plugins/v3/hosts.py +++ b/nova/api/openstack/compute/plugins/v3/hosts.py @@ -80,6 +80,9 @@ class HostController(wsgi.Controller): zone = req.GET.get('zone', None) if zone: filters['availability_zone'] = zone + service = req.GET.get('service') + if service: + filters['topic'] = service services = self.api.service_get_all(context, filters=filters, set_zones=True) hosts = [] diff --git a/nova/tests/api/openstack/compute/plugins/v3/test_hosts.py b/nova/tests/api/openstack/compute/plugins/v3/test_hosts.py index 6f3235c69c..5b4da1f826 100644 --- a/nova/tests/api/openstack/compute/plugins/v3/test_hosts.py +++ b/nova/tests/api/openstack/compute/plugins/v3/test_hosts.py @@ -132,6 +132,16 @@ class FakeRequestWithNovaZone(object): GET = {"zone": "nova"} +class FakeRequestWithNovaService(object): + environ = {"nova.context": context_maker.get_admin_context()} + GET = {"service": "compute"} + + +class FakeRequestWithInvalidNovaService(object): + environ = {"nova.context": context_maker.get_admin_context()} + GET = {"service": "invalid"} + + class HostTestCase(test.TestCase): """Test Case for hosts.""" @@ -174,6 +184,14 @@ class HostTestCase(test.TestCase): hosts = result['hosts'] self.assertEqual(fake_hosts.HOST_LIST_NOVA_ZONE, hosts) + def test_list_hosts_with_service(self): + result = self.controller.index(FakeRequestWithNovaService()) + self.assertEqual(fake_hosts.HOST_LIST_NOVA_ZONE, result['hosts']) + + def test_list_hosts_with_invalid_service(self): + result = self.controller.index(FakeRequestWithInvalidNovaService()) + self.assertEqual([], result['hosts']) + def test_disable_host(self): self._test_host_update('host_c1', 'status', 'disable', 'disabled') self._test_host_update('host_c2', 'status', 'disable', 'enabled') diff --git a/nova/tests/integrated/v3/api_samples/os-hosts/hosts-list-compute-service-resp.json.tpl b/nova/tests/integrated/v3/api_samples/os-hosts/hosts-list-compute-service-resp.json.tpl new file mode 100644 index 0000000000..846988bd80 --- /dev/null +++ b/nova/tests/integrated/v3/api_samples/os-hosts/hosts-list-compute-service-resp.json.tpl @@ -0,0 +1,9 @@ +{ + "hosts": [ + { + "host_name": "%(host_name)s", + "service": "compute", + "zone": "nova" + } + ] +} diff --git a/nova/tests/integrated/v3/test_hosts.py b/nova/tests/integrated/v3/test_hosts.py index bf6818f8f6..f86ad63153 100644 --- a/nova/tests/integrated/v3/test_hosts.py +++ b/nova/tests/integrated/v3/test_hosts.py @@ -49,3 +49,9 @@ class HostsSampleJsonTest(api_sample_base.ApiSampleTestBaseV3): response = self._do_get('os-hosts') subs = self._get_regexes() self._verify_response('hosts-list-resp', subs, response, 200) + + def test_hosts_list_compute_service(self): + response = self._do_get('os-hosts?service=compute') + subs = self._get_regexes() + self._verify_response('hosts-list-compute-service-resp', + subs, response, 200)