From e4205085b335aa2c37e60cbfc476fe429f2ff062 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Wed, 22 May 2019 08:50:24 +0200 Subject: [PATCH] baremetal: allow updating name and extra fields of an allocation Support for this was introduced in Bare Metal API 1.57 (Train). Change-Id: I4885d9a710cd6697c241bb1962dbbe2741a049a1 Story: #2005126 Task: #29798 --- doc/source/user/proxies/baremetal.rst | 2 + openstack/baremetal/v1/_proxy.py | 26 +++++++ openstack/baremetal/v1/allocation.py | 9 ++- .../baremetal/test_baremetal_allocation.py | 73 ++++++++++++++++++- .../unit/baremetal/v1/test_allocation.py | 2 +- .../allocation-update-910c36c1290e5121.yaml | 4 + 6 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 releasenotes/notes/allocation-update-910c36c1290e5121.yaml diff --git a/doc/source/user/proxies/baremetal.rst b/doc/source/user/proxies/baremetal.rst index 7c740d92c..0eb6012b7 100644 --- a/doc/source/user/proxies/baremetal.rst +++ b/doc/source/user/proxies/baremetal.rst @@ -87,6 +87,8 @@ Allocation Operations .. autoclass:: openstack.baremetal.v1._proxy.Proxy .. automethod:: openstack.baremetal.v1._proxy.Proxy.create_allocation + .. automethod:: openstack.baremetal.v1._proxy.Proxy.update_allocation + .. automethod:: openstack.baremetal.v1._proxy.Proxy.patch_allocation .. automethod:: openstack.baremetal.v1._proxy.Proxy.delete_allocation .. automethod:: openstack.baremetal.v1._proxy.Proxy.get_allocation .. automethod:: openstack.baremetal.v1._proxy.Proxy.allocations diff --git a/openstack/baremetal/v1/_proxy.py b/openstack/baremetal/v1/_proxy.py index e2e6c6dfc..842fd6241 100644 --- a/openstack/baremetal/v1/_proxy.py +++ b/openstack/baremetal/v1/_proxy.py @@ -838,6 +838,32 @@ class Proxy(proxy.Proxy): """ return self._get(_allocation.Allocation, allocation) + def update_allocation(self, allocation, **attrs): + """Update an allocation. + + :param allocation: The value can be the name or ID of an allocation or + a :class:`~openstack.baremetal.v1.allocation.Allocation` instance. + :param dict attrs: The attributes to update on the allocation + represented by the ``allocation`` parameter. + + :returns: The updated allocation. + :rtype: :class:`~openstack.baremetal.v1.allocation.Allocation` + """ + return self._update(_allocation.Allocation, allocation, **attrs) + + def patch_allocation(self, allocation, patch): + """Apply a JSON patch to the allocation. + + :param allocation: The value can be the name or ID of an allocation or + a :class:`~openstack.baremetal.v1.allocation.Allocation` instance. + :param patch: JSON patch to apply. + + :returns: The updated allocation. + :rtype: :class:`~openstack.baremetal.v1.allocation.Allocation` + """ + return self._get_resource(_allocation.Allocation, + allocation).patch(self, patch) + def delete_allocation(self, allocation, ignore_missing=True): """Delete an allocation. diff --git a/openstack/baremetal/v1/allocation.py b/openstack/baremetal/v1/allocation.py index f7a450c2b..c4cf48762 100644 --- a/openstack/baremetal/v1/allocation.py +++ b/openstack/baremetal/v1/allocation.py @@ -24,17 +24,20 @@ class Allocation(_common.ListMixin, resource.Resource): # capabilities allow_create = True allow_fetch = True - allow_commit = False + allow_commit = True allow_delete = True allow_list = True + allow_patch = True + commit_method = 'PATCH' + commit_jsonpatch = True _query_mapping = resource.QueryParameters( 'node', 'resource_class', 'state', fields={'name': 'fields', 'type': _common.comma_separated_list}, ) - # The allocation API introduced in 1.52. - _max_microversion = '1.52' + # Allocation update is available since 1.57 + _max_microversion = '1.57' #: The candidate nodes for this allocation. candidate_nodes = resource.Body('candidate_nodes', type=list) diff --git a/openstack/tests/functional/baremetal/test_baremetal_allocation.py b/openstack/tests/functional/baremetal/test_baremetal_allocation.py index 1f36985bc..9ffc52560 100644 --- a/openstack/tests/functional/baremetal/test_baremetal_allocation.py +++ b/openstack/tests/functional/baremetal/test_baremetal_allocation.py @@ -16,12 +16,10 @@ from openstack import exceptions from openstack.tests.functional.baremetal import base -class TestBareMetalAllocation(base.BaseBaremetalTest): - - min_microversion = '1.52' +class Base(base.BaseBaremetalTest): def setUp(self): - super(TestBareMetalAllocation, self).setUp() + super(Base, self).setUp() # NOTE(dtantsur): generate a unique resource class to prevent parallel # tests from clashing. self.resource_class = 'baremetal-%d' % random.randrange(1024) @@ -40,6 +38,11 @@ class TestBareMetalAllocation(base.BaseBaremetalTest): instance_id=None)) return node + +class TestBareMetalAllocation(Base): + + min_microversion = '1.52' + def test_allocation_create_get_delete(self): allocation = self.create_allocation(resource_class=self.resource_class) self.assertEqual('allocating', allocation.state) @@ -108,3 +111,65 @@ class TestBareMetalAllocation(base.BaseBaremetalTest): for item in result: self.assertIsNotNone(item.id) self.assertIsNone(item.resource_class) + + +class TestBareMetalAllocationUpdate(Base): + + min_microversion = '1.57' + + def test_allocation_update(self): + name = 'ossdk-name1' + + allocation = self.create_allocation(resource_class=self.resource_class) + allocation = self.conn.baremetal.wait_for_allocation(allocation) + self.assertEqual('active', allocation.state) + self.assertIsNone(allocation.last_error) + self.assertIsNone(allocation.name) + self.assertEqual({}, allocation.extra) + + allocation = self.conn.baremetal.update_allocation( + allocation, name=name, extra={'answer': 42}) + self.assertEqual(name, allocation.name) + self.assertEqual({'answer': 42}, allocation.extra) + + allocation = self.conn.baremetal.get_allocation(name) + self.assertEqual(name, allocation.name) + self.assertEqual({'answer': 42}, allocation.extra) + + self.conn.baremetal.delete_allocation(allocation, ignore_missing=False) + self.assertRaises(exceptions.ResourceNotFound, + self.conn.baremetal.get_allocation, allocation.id) + + def test_allocation_patch(self): + name = 'ossdk-name2' + + allocation = self.create_allocation(resource_class=self.resource_class) + allocation = self.conn.baremetal.wait_for_allocation(allocation) + self.assertEqual('active', allocation.state) + self.assertIsNone(allocation.last_error) + self.assertIsNone(allocation.name) + self.assertEqual({}, allocation.extra) + + allocation = self.conn.baremetal.patch_allocation( + allocation, [{'op': 'replace', 'path': '/name', 'value': name}, + {'op': 'add', 'path': '/extra/answer', 'value': 42}]) + self.assertEqual(name, allocation.name) + self.assertEqual({'answer': 42}, allocation.extra) + + allocation = self.conn.baremetal.get_allocation(name) + self.assertEqual(name, allocation.name) + self.assertEqual({'answer': 42}, allocation.extra) + + allocation = self.conn.baremetal.patch_allocation( + allocation, [{'op': 'remove', 'path': '/name'}, + {'op': 'remove', 'path': '/extra/answer'}]) + self.assertIsNone(allocation.name) + self.assertEqual({}, allocation.extra) + + allocation = self.conn.baremetal.get_allocation(allocation.id) + self.assertIsNone(allocation.name) + self.assertEqual({}, allocation.extra) + + self.conn.baremetal.delete_allocation(allocation, ignore_missing=False) + self.assertRaises(exceptions.ResourceNotFound, + self.conn.baremetal.get_allocation, allocation.id) diff --git a/openstack/tests/unit/baremetal/v1/test_allocation.py b/openstack/tests/unit/baremetal/v1/test_allocation.py index c508cca68..2146eed3d 100644 --- a/openstack/tests/unit/baremetal/v1/test_allocation.py +++ b/openstack/tests/unit/baremetal/v1/test_allocation.py @@ -51,7 +51,7 @@ class TestAllocation(base.TestCase): self.assertEqual('/allocations', sot.base_path) self.assertTrue(sot.allow_create) self.assertTrue(sot.allow_fetch) - self.assertFalse(sot.allow_commit) + self.assertTrue(sot.allow_commit) self.assertTrue(sot.allow_delete) self.assertTrue(sot.allow_list) diff --git a/releasenotes/notes/allocation-update-910c36c1290e5121.yaml b/releasenotes/notes/allocation-update-910c36c1290e5121.yaml new file mode 100644 index 000000000..6b9147a43 --- /dev/null +++ b/releasenotes/notes/allocation-update-910c36c1290e5121.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Allows updating ``name`` and ``extra`` fields of a baremetal allocation.