compute: Add support for server lock reason

Add support for compute API microversion 2.73, which allowed admins to
specify a reason for a server being locked.

Change-Id: I676c3b2dd6b4135fb1ab65cc99315f87c13ec3a8
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2021-11-25 11:04:45 +00:00
parent 0948ee52dd
commit 6fdc97b787
5 changed files with 39 additions and 6 deletions

View File

@ -969,15 +969,17 @@ class Proxy(proxy.Proxy):
server = self._get_resource(_server.Server, server)
server.resume(self)
def lock_server(self, server):
def lock_server(self, server, locked_reason=None):
"""Locks a server.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:class:`~openstack.compute.v2.server.Server` instance.
:param locked_reason: The reason behind locking the server. Limited to
255 characters in length.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.lock(self)
server.lock(self, locked_reason=locked_reason)
def unlock_server(self, server):
"""Unlocks a locked server.

View File

@ -63,7 +63,7 @@ class Server(resource.Resource, metadata.MetadataMixin, resource.TagMixin):
**resource.TagMixin._tag_query_parameters
)
_max_microversion = '2.72'
_max_microversion = '2.73'
#: A list of dictionaries holding links relevant to this server.
links = resource.Body('links')
@ -412,8 +412,12 @@ class Server(resource.Resource, metadata.MetadataMixin, resource.TagMixin):
body = {"resume": None}
self._action(session, body)
def lock(self, session):
def lock(self, session, locked_reason=None):
body = {"lock": None}
if locked_reason is not None:
body["lock"] = {
"locked_reason": locked_reason,
}
self._action(session, body)
def unlock(self, session):

View File

@ -810,7 +810,17 @@ class TestCompute(TestComputeProxy):
"openstack.compute.v2.server.Server.lock",
self.proxy.lock_server,
method_args=["value"],
expected_args=[self.proxy])
expected_args=[self.proxy],
expected_kwargs={"locked_reason": None})
def test_server_lock_with_options(self):
self._verify(
"openstack.compute.v2.server.Server.lock",
self.proxy.lock_server,
method_args=["value"],
method_kwargs={"locked_reason": "Because why not"},
expected_args=[self.proxy],
expected_kwargs={"locked_reason": "Because why not"})
def test_server_unlock(self):
self._verify(

View File

@ -649,6 +649,18 @@ class TestServer(base.TestCase):
self.sess.post.assert_called_with(
url, json=body, headers=headers, microversion=None)
def test_lock_with_options(self):
sot = server.Server(**EXAMPLE)
res = sot.lock(self.sess, locked_reason='Because why not')
self.assertIsNone(res)
url = 'servers/IDENTIFIER/action'
body = {'lock': {'locked_reason': 'Because why not'}}
headers = {'Accept': ''}
self.sess.post.assert_called_with(
url, json=body, headers=headers, microversion=None)
def test_unlock(self):
sot = server.Server(**EXAMPLE)

View File

@ -0,0 +1,5 @@
---
features:
- |
Add support for Compute API microversion 2.73, which allows admins to
specify a reason when locking a server.