From 6be3448d40142e2c34233f2a830cd3d1958ee637 Mon Sep 17 00:00:00 2001 From: Lucas Alvares Gomes Date: Mon, 9 May 2016 16:11:56 +0100 Subject: [PATCH] Follow-up patch of 8e5e69869df476788b3ccf7e5ba6c2210a98fc8a This patch is a follow-up patch of 8e5e69869df476788b3ccf7e5ba6c2210a98fc8a addressing some the remaining comments after the patch was merged. The changes are: * Optimize the code by generating the list of reserved words for the nodes controller only once. * List the reserved words in the release note. * Improve the error string when someone tries to name a node using a reserved word. * Add a comment for future reference indicating how we should try to model the API in case a new sub-controller needs to be added to the nodes controller class. Change-Id: I43d6d9e2e92cc941a7839c60d9cd361cd94b6208 --- ironic/api/controllers/v1/node.py | 21 ++++++++++++++++--- .../reserved-node-names-67a08012ed1131ae.yaml | 3 ++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ironic/api/controllers/v1/node.py b/ironic/api/controllers/v1/node.py index c28110898a..97796183fb 100644 --- a/ironic/api/controllers/v1/node.py +++ b/ironic/api/controllers/v1/node.py @@ -96,6 +96,16 @@ PROVISION_ACTION_STATES = (ir_states.VERBS['manage'], ir_states.VERBS['provide'], ir_states.VERBS['abort']) +_NODES_CONTROLLER_RESERVED_WORDS = None + + +def get_nodes_controller_reserved_names(): + global _NODES_CONTROLLER_RESERVED_WORDS + if _NODES_CONTROLLER_RESERVED_WORDS is None: + _NODES_CONTROLLER_RESERVED_WORDS = ( + api_utils.get_controller_reserved_names(NodesController)) + return _NODES_CONTROLLER_RESERVED_WORDS + def hide_fields_in_newer_versions(obj): # if requested version is < 1.3, hide driver_internal_info @@ -924,6 +934,12 @@ class NodeMaintenanceController(rest.RestController): class NodesController(rest.RestController): """REST controller for Nodes.""" + # NOTE(lucasagomes): For future reference. If we happen + # to need to add another sub-controller in this class let's + # try to make it a parameter instead of an endpoint due + # https://bugs.launchpad.net/ironic/+bug/1572651, e.g, instead of + # v1/nodes/(ident)/detail we could have v1/nodes/(ident)?detail=True + states = NodeStatesController() """Expose the state controller action as a sub-element of nodes""" @@ -1033,8 +1049,7 @@ class NodesController(rest.RestController): if not api_utils.allow_node_logical_names(): raise exception.NotAcceptable() - reserved_names = api_utils.get_controller_reserved_names( - NodesController) + reserved_names = get_nodes_controller_reserved_names() for name in names: if not api_utils.is_valid_node_name(name): raise wsme.exc.ClientSideError( @@ -1043,7 +1058,7 @@ class NodesController(rest.RestController): if name in reserved_names: raise wsme.exc.ClientSideError( 'The word "%(name)s" is reserved and can not be used as a ' - 'node name. Other reserved words are: %(reserved)s' % + 'node name. Reserved words are: %(reserved)s.' % {'name': name, 'reserved': ', '.join(reserved_names)}, status_code=http_client.BAD_REQUEST) diff --git a/releasenotes/notes/reserved-node-names-67a08012ed1131ae.yaml b/releasenotes/notes/reserved-node-names-67a08012ed1131ae.yaml index c490b74f88..cc3748c491 100644 --- a/releasenotes/notes/reserved-node-names-67a08012ed1131ae.yaml +++ b/releasenotes/notes/reserved-node-names-67a08012ed1131ae.yaml @@ -2,4 +2,5 @@ fixes: - Fixes a problem which allowed nodes to be named with some reserved words that are implicitly not allowed due the way the Ironic API - works. + works. The reserved words are "maintenance", "management", "ports", + "states", "vendor_passthru", "validate" and "detail".