diff --git a/octavia/api/v2/controllers/provider.py b/octavia/api/v2/controllers/provider.py index 1d1d30cab2..29ce8e96f2 100644 --- a/octavia/api/v2/controllers/provider.py +++ b/octavia/api/v2/controllers/provider.py @@ -89,6 +89,25 @@ class FlavorCapabilitiesController(base.BaseController): 'reported: %s', self.provider, e.operator_fault_string) raise exceptions.ProviderNotImplementedError( prov=self.provider, user_msg=e.user_fault_string) + + # Apply any valid filters provided as URL parameters + name_filter = None + description_filter = None + pagination_helper = pecan.request.context.get( + constants.PAGINATION_HELPER) + if pagination_helper: + name_filter = pagination_helper.params.get(constants.NAME) + description_filter = pagination_helper.params.get( + constants.DESCRIPTION) + if name_filter: + metadata_dict = { + key: value for key, value in six.iteritems(metadata_dict) if + key == name_filter} + if description_filter: + metadata_dict = { + key: value for key, value in six.iteritems(metadata_dict) if + value == description_filter} + response_list = [ provider_types.ProviderResponse(name=key, description=value) for key, value in six.iteritems(metadata_dict)] diff --git a/octavia/common/constants.py b/octavia/common/constants.py index f45c90278d..ee02d57af1 100644 --- a/octavia/common/constants.py +++ b/octavia/common/constants.py @@ -308,10 +308,12 @@ CREATED_AT = 'created_at' CRL_CONTAINER_ID = 'crl_container_id' DELTA = 'delta' DELTAS = 'deltas' +DESCRIPTION = 'description' ENABLED = 'enabled' FAILED_AMPHORA = 'failed_amphora' FAILOVER_AMPHORA = 'failover_amphora' FAILOVER_AMPHORA_ID = 'failover_amphora_id' +FIELDS = 'fields' FLAVOR_ID = 'flavor_id' HEALTH_MON = 'health_mon' HEALTH_MONITOR = 'health_monitor' diff --git a/octavia/tests/functional/api/v2/test_provider.py b/octavia/tests/functional/api/v2/test_provider.py index 107160c3a3..a59e8c3a89 100644 --- a/octavia/tests/functional/api/v2/test_provider.py +++ b/octavia/tests/functional/api/v2/test_provider.py @@ -132,3 +132,42 @@ class TestFlavorCapabilities(base.BaseAPITest): self.get(self.FLAVOR_CAPABILITIES_PATH.format(provider='noop_driver'), status=403) self.conf.config(group='api_settings', auth_strategy=auth_strategy) + + def test_amphora_driver_one_filter(self): + ref_description = ("The compute driver flavor ID.") + result = self.get( + self.FLAVOR_CAPABILITIES_PATH.format(provider=constants.AMPHORA), + params={constants.NAME: 'compute_flavor'}) + capabilities = result.json.get(self.root_tag) + self.assertEqual(1, len(capabilities)) + self.assertEqual(2, len(capabilities[0])) + self.assertEqual(ref_description, + capabilities[0][constants.DESCRIPTION]) + + def test_amphora_driver_two_filters(self): + ref_description = ("The compute driver flavor ID.") + result = self.get( + self.FLAVOR_CAPABILITIES_PATH.format(provider=constants.AMPHORA), + params={constants.NAME: 'compute_flavor', + constants.DESCRIPTION: ref_description}) + capabilities = result.json.get(self.root_tag) + self.assertEqual(1, len(capabilities)) + self.assertEqual(ref_description, + capabilities[0][constants.DESCRIPTION]) + + def test_amphora_driver_filter_no_match(self): + result = self.get( + self.FLAVOR_CAPABILITIES_PATH.format(provider=constants.AMPHORA), + params={constants.NAME: 'bogus'}) + capabilities = result.json.get(self.root_tag) + self.assertEqual([], capabilities) + + def test_amphora_driver_one_filter_one_field(self): + result = self.get( + self.FLAVOR_CAPABILITIES_PATH.format(provider=constants.AMPHORA), + params={constants.NAME: 'compute_flavor', + constants.FIELDS: constants.NAME}) + capabilities = result.json.get(self.root_tag) + self.assertEqual(1, len(capabilities)) + self.assertEqual(1, len(capabilities[0])) + self.assertEqual('compute_flavor', capabilities[0][constants.NAME]) diff --git a/releasenotes/notes/fix-provider-capabilities-filtering-8bd12b2cf7b37a84.yaml b/releasenotes/notes/fix-provider-capabilities-filtering-8bd12b2cf7b37a84.yaml new file mode 100644 index 0000000000..017647afe3 --- /dev/null +++ b/releasenotes/notes/fix-provider-capabilities-filtering-8bd12b2cf7b37a84.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fixes the ability to filter on the provider flavor capabilities API.