summaryrefslogtreecommitdiff
path: root/ironic/api/controllers/v1/collection.py
diff options
context:
space:
mode:
authorSteve Baker <sbaker@redhat.com>2020-09-11 16:31:13 +1200
committerSteve Baker <sbaker@redhat.com>2020-11-16 10:49:42 +1300
commit236c6b174baf77c88ed112530610d25fc1c11b31 (patch)
treeb2e7ccd9b492a94f291b401a3d4caf81ae3387e4 /ironic/api/controllers/v1/collection.py
parent8677be53b7cffb30544633c060e8650a3935726f (diff)
downloadironic-236c6b174baf77c88ed112530610d25fc1c11b31.tar.gz
Utility functions for REST API JSON handling
collection.list_convert_with_links Build a collection dict including the next link for paging support utils.object_to_dict Helper function to convert RPC objects to REST API dicts utils.populate_node_uuid Look up the node referenced in the object and populate a dict utils.replace_node_uuid_with_id Replace ``node_uuid`` dict value with ``node_id`` utils.replace_node_id_with_uuid Replace ``node_id`` dict value with ``node_uuid` utils.patch_update_changed_fields Update rpc object based on changed fields in a dict. utils.patched_validate_with_schema Validate a patched dict object against a validator or schema. utils.patch_validate_allowed_fields Validate that a patch list only modifies allowed fields utils.sanitize_dict Removes sensitive and unrequested data Change-Id: I39fa73ac9a62d30a3eaa00c75129ac1e00270652 Story: 1651346 Task: 10551
Diffstat (limited to 'ironic/api/controllers/v1/collection.py')
-rw-r--r--ironic/api/controllers/v1/collection.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/ironic/api/controllers/v1/collection.py b/ironic/api/controllers/v1/collection.py
index c669b9309..5d5125c19 100644
--- a/ironic/api/controllers/v1/collection.py
+++ b/ironic/api/controllers/v1/collection.py
@@ -24,6 +24,44 @@ def has_next(collection, limit):
return len(collection) and len(collection) == limit
+def list_convert_with_links(items, item_name, limit, url=None, fields=None,
+ sanitize_func=None, key_field='uuid', **kwargs):
+ """Build a collection dict including the next link for paging support.
+
+ :param items:
+ List of unsanitized items to include in the collection
+ :param item_name:
+ Name of dict key for items value
+ :param limit:
+ Paging limit
+ :param url:
+ Base URL for building next link
+ :param fields:
+ Optional fields to use for sanitize function
+ :param sanitize_func:
+ Optional sanitize function run on each item
+ :param key_field:
+ Key name for building next URL
+ :param kwargs:
+ other arguments passed to ``get_next``
+ :returns:
+ A dict containing ``item_name`` and ``next`` values
+ """
+ items_dict = {
+ item_name: items
+ }
+ next_uuid = get_next(
+ items, limit, url=url, fields=fields, key_field=key_field, **kwargs)
+ if next_uuid:
+ items_dict['next'] = next_uuid
+
+ if sanitize_func:
+ for item in items:
+ sanitize_func(item, fields=fields)
+
+ return items_dict
+
+
def get_next(collection, limit, url=None, key_field='uuid', **kwargs):
"""Return a link to the next subset of the collection."""
if not has_next(collection, limit):