From 17b22bccf9298b2ddabcbb0626234ae5a7dc614f Mon Sep 17 00:00:00 2001 From: Steve McLellan Date: Wed, 22 Jun 2016 15:13:45 -0500 Subject: [PATCH] Support new facet response format The facet response format is changing as part of https://blueprints.launchpad.net/searchlight/+spec/count-endpoint such that the value for each resource type becomes a dictionary (instead of a list) with keys 'facets' and 'doc_count'. This patch allows python-searchlightclient to parse either response in preparation for the change. Change-Id: Iddd3ad4bfe277c513a304ff03eccffa8ba928973 Partially-implements: blueprint count-endpoint --- searchlightclient/osc/v1/facet.py | 7 ++++++- searchlightclient/tests/unit/osc/v1/fakes.py | 16 +++++++++++++++- .../tests/unit/osc/v1/test_facet.py | 16 +++++++++++++--- searchlightclient/v1/facets.py | 3 ++- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/searchlightclient/osc/v1/facet.py b/searchlightclient/osc/v1/facet.py index 02216c2..47f9634 100644 --- a/searchlightclient/osc/v1/facet.py +++ b/searchlightclient/osc/v1/facet.py @@ -64,7 +64,12 @@ class ListFacet(lister.Lister): data = search_client.facets.list(**params) result = [] for resource_type, values in six.iteritems(data): - for s in values: + if isinstance(values, list): + # Cope with pre-1.0 service APIs + facets = values + else: + facets = values['facets'] + for s in facets: options = [] for o in s.get('options', []): options.append( diff --git a/searchlightclient/tests/unit/osc/v1/fakes.py b/searchlightclient/tests/unit/osc/v1/fakes.py index 6e4b301..b74deba 100644 --- a/searchlightclient/tests/unit/osc/v1/fakes.py +++ b/searchlightclient/tests/unit/osc/v1/fakes.py @@ -26,7 +26,7 @@ ResourceType = { } -Facet = { +OldFacet = { "OS::Nova::Server": [ {"type": "string", "name": "id"}, @@ -35,6 +35,20 @@ Facet = { } +# The alternate facet format introduced in +# https://blueprints.launchpad.net/searchlight/+spec/count-endpoint +Facet = { + "OS::Nova::Server": + { + "doc_count": 2, + "facets": [ + {"type": "string", "name": "id"}, + {"type": "date", "name": "created_at"}, + ] + } +} + + Resource = { "hits": {"hits": diff --git a/searchlightclient/tests/unit/osc/v1/test_facet.py b/searchlightclient/tests/unit/osc/v1/test_facet.py index 090476e..d8c1f2f 100644 --- a/searchlightclient/tests/unit/osc/v1/test_facet.py +++ b/searchlightclient/tests/unit/osc/v1/test_facet.py @@ -21,10 +21,9 @@ class TestFacet(searchlight_fakes.TestSearchv1): self.facet_client = self.app.client_manager.search.facets -class TestFacetList(TestFacet): - +class TestFacetListBase(TestFacet): def setUp(self): - super(TestFacetList, self).setUp() + super(TestFacetListBase, self).setUp() self.cmd = facet.ListFacet(self.app, None) self.facet_client.list.return_value = searchlight_fakes.Facet @@ -42,6 +41,8 @@ class TestFacetList(TestFacet): ) self.assertEqual(datalist, tuple(data)) + +class TestFacetList(TestFacetListBase): def test_list(self): self._test_list([], all_projects=False, limit_terms=None, type=None) @@ -58,3 +59,12 @@ class TestFacetList(TestFacet): self._test_list(['--limit-terms', 'fake_limit'], all_projects=False, limit_terms='fake_limit', type=None) + + +class TestOldFacetList(TestFacetListBase): + def setUp(self): + super(TestOldFacetList, self).setUp() + self.facet_client.list.return_value = searchlight_fakes.OldFacet + + def test_list(self): + self._test_list([], all_projects=False, limit_terms=None, type=None) diff --git a/searchlightclient/v1/facets.py b/searchlightclient/v1/facets.py index 49ebab5..85badf8 100644 --- a/searchlightclient/v1/facets.py +++ b/searchlightclient/v1/facets.py @@ -37,7 +37,8 @@ class FacetsManager(base.BaseManager): that support facet terms. :param type: Request facets for a particular type by adding a type query parameter. - :rtype: list of :class:`Facets` + :rtype: dict of {resource_type: {'facets': [:class:`Facets`], + 'doc_count':, :class:int}} """ params = {} if kwargs.get('index'):