From 2b5d989990b14ef81e3ca2a48df8e43b6b205d0e Mon Sep 17 00:00:00 2001 From: Sylvain Bauza Date: Wed, 27 Jul 2022 16:10:30 +0200 Subject: [PATCH] Add support for 2.92 : keypair import mandatory Now, when creating a keypair, the 'public_key' parameter is now mandatory. Depends-On: https://review.opendev.org/c/openstack/nova/+/849133 Implements: blueprint keypair-generation-removal Change-Id: I03570d0a49b73021de91dc50b65b1bbf5d4b878b --- novaclient/__init__.py | 2 +- novaclient/tests/unit/v2/fakes.py | 7 +++++- novaclient/tests/unit/v2/test_keypairs.py | 25 +++++++++++++++++++ novaclient/v2/keypairs.py | 19 +++++++++++++- ...r-generation-removal-1b5d84a8906d3918.yaml | 8 ++++++ 5 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/bp-keypair-generation-removal-1b5d84a8906d3918.yaml diff --git a/novaclient/__init__.py b/novaclient/__init__.py index bbfd95224..afffde0f0 100644 --- a/novaclient/__init__.py +++ b/novaclient/__init__.py @@ -25,4 +25,4 @@ API_MIN_VERSION = api_versions.APIVersion("2.1") # when client supported the max version, and bumped sequentially, otherwise # the client may break due to server side new version may include some # backward incompatible change. -API_MAX_VERSION = api_versions.APIVersion("2.91") +API_MAX_VERSION = api_versions.APIVersion("2.92") diff --git a/novaclient/tests/unit/v2/fakes.py b/novaclient/tests/unit/v2/fakes.py index 059a7147f..c7687c1e9 100644 --- a/novaclient/tests/unit/v2/fakes.py +++ b/novaclient/tests/unit/v2/fakes.py @@ -1228,8 +1228,13 @@ class FakeSessionClient(base_client.SessionClient): def post_os_keypairs(self, body, **kw): assert list(body) == ['keypair'] + if self.api_version >= api_versions.APIVersion("2.92"): + # In 2.92, public_key becomes mandatory + required = ['name', 'public_key'] + else: + required = ['name'] fakes.assert_has_keys(body['keypair'], - required=['name']) + required=required) r = {'keypair': self.get_os_keypairs()[2]['keypairs'][0]['keypair']} return (202, {}, r) diff --git a/novaclient/tests/unit/v2/test_keypairs.py b/novaclient/tests/unit/v2/test_keypairs.py index cf310b0ef..7e16438d8 100644 --- a/novaclient/tests/unit/v2/test_keypairs.py +++ b/novaclient/tests/unit/v2/test_keypairs.py @@ -127,3 +127,28 @@ class KeypairsV35TestCase(KeypairsTest): % self.keypair_prefix) for kp in kps: self.assertIsInstance(kp, keypairs.Keypair) + + +class KeypairsV92TestCase(KeypairsTest): + def setUp(self): + super(KeypairsV92TestCase, self).setUp() + self.cs.api_version = api_versions.APIVersion("2.92") + + def test_create_keypair(self): + name = "foo" + key_type = "some_type" + public_key = "fake-public-key" + kp = self.cs.keypairs.create(name, public_key=public_key, + key_type=key_type) + self.assert_request_id(kp, fakes.FAKE_REQUEST_ID_LIST) + self.assert_called('POST', '/%s' % self.keypair_prefix, + body={'keypair': {'name': name, + 'public_key': public_key, + 'type': key_type}}) + self.assertIsInstance(kp, keypairs.Keypair) + + def test_create_keypair_without_pubkey(self): + name = "foo" + key_type = "some_type" + self.assertRaises(TypeError, + self.cs.keypairs.create, name, key_type=key_type) diff --git a/novaclient/v2/keypairs.py b/novaclient/v2/keypairs.py index 9b2e73b71..5d12f8cd4 100644 --- a/novaclient/v2/keypairs.py +++ b/novaclient/v2/keypairs.py @@ -114,7 +114,7 @@ class KeypairManager(base.ManagerWithFind): body['keypair']['public_key'] = public_key return self._create('/%s' % self.keypair_prefix, body, 'keypair') - @api_versions.wraps("2.10") + @api_versions.wraps("2.10", "2.91") def create(self, name, public_key=None, key_type="ssh", user_id=None): """ Create a keypair @@ -132,6 +132,23 @@ class KeypairManager(base.ManagerWithFind): body['keypair']['user_id'] = user_id return self._create('/%s' % self.keypair_prefix, body, 'keypair') + @api_versions.wraps("2.92") + def create(self, name, public_key, key_type="ssh", user_id=None): + """ + Create a keypair + + :param name: name for the keypair to create + :param public_key: existing public key to import + :param key_type: keypair type to create + :param user_id: user to add. + """ + body = {'keypair': {'name': name, + 'type': key_type, + 'public_key': public_key}} + if user_id: + body['keypair']['user_id'] = user_id + return self._create('/%s' % self.keypair_prefix, body, 'keypair') + @api_versions.wraps("2.0", "2.9") def delete(self, key): """ diff --git a/releasenotes/notes/bp-keypair-generation-removal-1b5d84a8906d3918.yaml b/releasenotes/notes/bp-keypair-generation-removal-1b5d84a8906d3918.yaml new file mode 100644 index 000000000..c7cdb6424 --- /dev/null +++ b/releasenotes/notes/bp-keypair-generation-removal-1b5d84a8906d3918.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + Support has been added for `microversion 2.92`_. This microversion only + accepts to import a public key and no longer to generate one, hence now the + public_key parameter be mandatory. + + .. _microversion 2.92: https://docs.openstack.org/nova/latest/reference/api-microversion-history.html#microversion-2-92