From 265993316b53abac234600b4b516965b82270e16 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Fri, 24 Nov 2017 17:54:22 +0100 Subject: [PATCH] Add a configuration option for the default resource class If no resource class is provided in a creation request, the value of the new ``default_resource_class`` configuration option is used. While this feature is implemented on the API level, it's not a part of the API contract, so it's not covered by a microversion. It's particularly important, because it allows this change to apply even to requests using an old API version, thus making these versions usable after the mandatory switch to resource classes in nova. Change-Id: I58232d9c92d6ffca49d334e5fb7078bce19f1cb4 Closes-Bug: #1732190 --- ironic/api/controllers/v1/node.py | 3 ++ ironic/conf/default.py | 3 ++ .../unit/api/controllers/v1/test_node.py | 31 +++++++++++++++++++ ...fault-resource-class-e11bacfb01d6841b.yaml | 6 ++++ 4 files changed, 43 insertions(+) create mode 100644 releasenotes/notes/default-resource-class-e11bacfb01d6841b.yaml diff --git a/ironic/api/controllers/v1/node.py b/ironic/api/controllers/v1/node.py index a442c95b3d..312161a13f 100644 --- a/ironic/api/controllers/v1/node.py +++ b/ironic/api/controllers/v1/node.py @@ -1643,6 +1643,9 @@ class NodesController(rest.RestController): self._check_names_acceptable([node.name], error_msg) node.provision_state = api_utils.initial_node_provision_state() + if not node.resource_class: + node.resource_class = CONF.default_resource_class + new_node = objects.Node(context, **node.as_dict()) notify.emit_start_notification(context, new_node, 'create', chassis_uuid=node.chassis_uuid) diff --git a/ironic/conf/default.py b/ironic/conf/default.py index 9862d4ce55..23fc187931 100644 --- a/ironic/conf/default.py +++ b/ironic/conf/default.py @@ -69,6 +69,9 @@ api_opts = [ default=False, help=_('Enable pecan debug mode. WARNING: this is insecure ' 'and should not be used in a production environment.')), + cfg.StrOpt('default_resource_class', + help=_('Resource class to use for new nodes when no resource ' + 'class is provided in the creation request.')), ] driver_opts = [ diff --git a/ironic/tests/unit/api/controllers/v1/test_node.py b/ironic/tests/unit/api/controllers/v1/test_node.py index 9d1985be0e..c183c5d306 100644 --- a/ironic/tests/unit/api/controllers/v1/test_node.py +++ b/ironic/tests/unit/api/controllers/v1/test_node.py @@ -2267,6 +2267,37 @@ class TestPost(test_api_base.BaseApiTest): self.assertEqual(ndict['uuid'], result['uuid']) self.assertEqual(states.ENROLL, result['provision_state']) + def test_create_node_no_default_resource_class(self): + ndict = test_api_utils.post_get_test_node() + self.post_json('/nodes', ndict) + + # newer version is needed to see the resource_class field + result = self.get_json('/nodes/%s' % ndict['uuid'], + headers={api_base.Version.string: "1.21"}) + self.assertIsNone(result['resource_class']) + + def test_create_node_with_default_resource_class(self): + self.config(default_resource_class='class1') + + ndict = test_api_utils.post_get_test_node() + self.post_json('/nodes', ndict) + + # newer version is needed to see the resource_class field + result = self.get_json('/nodes/%s' % ndict['uuid'], + headers={api_base.Version.string: "1.21"}) + self.assertEqual('class1', result['resource_class']) + + def test_create_node_explicit_resource_class(self): + self.config(default_resource_class='class1') + + ndict = test_api_utils.post_get_test_node(resource_class='class2') + self.post_json('/nodes', ndict, + headers={api_base.Version.string: "1.21"}) + + result = self.get_json('/nodes/%s' % ndict['uuid'], + headers={api_base.Version.string: "1.21"}) + self.assertEqual('class2', result['resource_class']) + def test_create_node_doesnt_contain_id(self): # FIXME(comstud): I'd like to make this test not use the # dbapi, however, no matter what I do when trying to mock diff --git a/releasenotes/notes/default-resource-class-e11bacfb01d6841b.yaml b/releasenotes/notes/default-resource-class-e11bacfb01d6841b.yaml new file mode 100644 index 0000000000..92dd0df648 --- /dev/null +++ b/releasenotes/notes/default-resource-class-e11bacfb01d6841b.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Adds new configuration option ``[DEFAULT]default_resource_class`` that + specifies the resource class to use for new nodes when no resource class + is provided in the node creation request.