api: Simplify API version check for flavor description

Unlike the check for extra specs, the check for whether to include a
description field or not is driven entirely by API version rather than
API version and policy. We can therefore move the checks inside the
functions that generate the response rather than duplicating them
elsewhere.

Change-Id: I86aa4e1c62a0b0e6fa4d27e559d3197fb73851ba
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane
2025-06-20 15:05:20 +01:00
parent 61c1ce6c8e
commit ed83dab5a7
2 changed files with 19 additions and 31 deletions
+5 -8
View File
@@ -106,8 +106,8 @@ class FlavorsController(wsgi.Controller):
# flavor.extra_specs is populated with the empty string. # flavor.extra_specs is populated with the empty string.
flavor.extra_specs = {} flavor.extra_specs = {}
return self._view_builder.show(req, flavor, include_description, return self._view_builder.show(
include_extra_specs=include_extra_specs) req, flavor, include_extra_specs=include_extra_specs)
@wsgi.api_version('2.55') @wsgi.api_version('2.55')
@wsgi.expected_errors((400, 404)) @wsgi.expected_errors((400, 404))
@@ -132,8 +132,8 @@ class FlavorsController(wsgi.Controller):
if api_version_request.is_supported(req, '2.61'): if api_version_request.is_supported(req, '2.61'):
include_extra_specs = context.can( include_extra_specs = context.can(
fes_policies.POLICY_ROOT % 'index', fatal=False) fes_policies.POLICY_ROOT % 'index', fatal=False)
return self._view_builder.show(req, flavor, include_description=True, return self._view_builder.show(
include_extra_specs=include_extra_specs) req, flavor, include_extra_specs=include_extra_specs)
@wsgi.expected_errors(400) @wsgi.expected_errors(400)
@validation.query_schema(schema.index_query, '2.0', '2.74') @validation.query_schema(schema.index_query, '2.0', '2.74')
@@ -183,11 +183,8 @@ class FlavorsController(wsgi.Controller):
include_extra_specs = context.can( include_extra_specs = context.can(
fes_policies.POLICY_ROOT % 'index', fatal=False) fes_policies.POLICY_ROOT % 'index', fatal=False)
include_description = api_version_request.is_supported(req, '2.55')
return self._view_builder.show( return self._view_builder.show(
req, flavor, include_description=include_description, req, flavor, include_extra_specs=include_extra_specs)
include_extra_specs=include_extra_specs)
def _parse_is_public(self, is_public): def _parse_is_public(self, is_public):
"""Parse is_public into something usable.""" """Parse is_public into something usable."""
+14 -23
View File
@@ -21,8 +21,7 @@ class ViewBuilder(common.ViewBuilder):
_collection_name = "flavors" _collection_name = "flavors"
def basic(self, request, flavor, include_description=False, def basic(self, request, flavor, include_extra_specs=False):
include_extra_specs=False):
# include_extra_specs is placeholder param which is not used in # include_extra_specs is placeholder param which is not used in
# this method as basic() method is used by index() (GET /flavors) # this method as basic() method is used by index() (GET /flavors)
# which does not return those keys in response. # which does not return those keys in response.
@@ -36,13 +35,12 @@ class ViewBuilder(common.ViewBuilder):
}, },
} }
if include_description: if api_version_request.is_supported(request, '2.55'):
flavor_dict['flavor']['description'] = flavor.description flavor_dict['flavor']['description'] = flavor.description
return flavor_dict return flavor_dict
def show(self, request, flavor, include_description=False, def show(self, request, flavor, include_extra_specs=False):
include_extra_specs=False):
flavor_dict = { flavor_dict = {
"flavor": { "flavor": {
"id": flavor["flavorid"], "id": flavor["flavorid"],
@@ -61,7 +59,7 @@ class ViewBuilder(common.ViewBuilder):
}, },
} }
if include_description: if api_version_request.is_supported(request, '2.55'):
flavor_dict['flavor']['description'] = flavor.description flavor_dict['flavor']['description'] = flavor.description
if include_extra_specs: if include_extra_specs:
@@ -75,20 +73,17 @@ class ViewBuilder(common.ViewBuilder):
def index(self, request, flavors): def index(self, request, flavors):
"""Return the 'index' view of flavors.""" """Return the 'index' view of flavors."""
coll_name = self._collection_name coll_name = self._collection_name
include_description = api_version_request.is_supported(request, '2.55') return self._list_view(self.basic, request, flavors, coll_name)
return self._list_view(self.basic, request, flavors, coll_name,
include_description=include_description)
def detail(self, request, flavors, include_extra_specs=False): def detail(self, request, flavors, include_extra_specs=False):
"""Return the 'detail' view of flavors.""" """Return the 'detail' view of flavors."""
coll_name = self._collection_name + '/detail' coll_name = self._collection_name + '/detail'
include_description = api_version_request.is_supported(request, '2.55')
return self._list_view(self.show, request, flavors, coll_name, return self._list_view(self.show, request, flavors, coll_name,
include_description=include_description,
include_extra_specs=include_extra_specs) include_extra_specs=include_extra_specs)
def _list_view(self, func, request, flavors, coll_name, def _list_view(
include_description=False, include_extra_specs=False): self, func, request, flavors, coll_name, include_extra_specs=False
):
"""Provide a view for a list of flavors. """Provide a view for a list of flavors.
:param func: Function used to format the flavor data :param func: Function used to format the flavor data
@@ -96,21 +91,17 @@ class ViewBuilder(common.ViewBuilder):
:param flavors: List of flavors in dictionary format :param flavors: List of flavors in dictionary format
:param coll_name: Name of collection, used to generate the next link :param coll_name: Name of collection, used to generate the next link
for a pagination query for a pagination query
:param include_description: If the flavor.description should be
included in the response dict.
:param include_extra_specs: If the flavor.extra_specs should be :param include_extra_specs: If the flavor.extra_specs should be
included in the response dict. included in the response dict.
:returns: Flavor reply data in dictionary format :returns: Flavor reply data in dictionary format
""" """
flavor_list = [func(request, flavor, include_description, flavor_list = [
include_extra_specs)["flavor"] func(request, flavor, include_extra_specs)["flavor"]
for flavor in flavors] for flavor in flavors]
flavors_links = self._get_collection_links(request, flavors_links = self._get_collection_links(
flavors, request, flavors, coll_name, "flavorid")
coll_name, flavors_dict = {"flavors": flavor_list}
"flavorid")
flavors_dict = dict(flavors=flavor_list)
if flavors_links: if flavors_links:
flavors_dict["flavors_links"] = flavors_links flavors_dict["flavors_links"] = flavors_links