Add user_id as optional param to keypair operations

In order to switch OSC to use SDK for keypair operations it is very
helpful to add user_id into the corresponding functions.

Change-Id: Iea463f3a915d1074f8c1bed1dddaa40348a5bf8f
This commit is contained in:
Artem Goncharov 2020-11-09 11:43:36 +01:00
parent 256e25e321
commit 9d0b048fca
2 changed files with 73 additions and 37 deletions

View File

@ -472,48 +472,53 @@ class Proxy(proxy.Proxy):
""" """
return self._create(_keypair.Keypair, **attrs) return self._create(_keypair.Keypair, **attrs)
def delete_keypair(self, keypair, ignore_missing=True): def delete_keypair(self, keypair, ignore_missing=True, user_id=None):
"""Delete a keypair """Delete a keypair
:param keypair: The value can be either the ID of a keypair or a :param keypair: The value can be either the ID of a keypair or a
:class:`~openstack.compute.v2.keypair.Keypair` :class:`~openstack.compute.v2.keypair.Keypair` instance.
instance.
:param bool ignore_missing: When set to ``False`` :param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be :class:`~openstack.exceptions.ResourceNotFound` will be raised when
raised when the keypair does not exist. the keypair does not exist. When set to ``True``, no exception
When set to ``True``, no exception will be set when will be set when attempting to delete a nonexistent keypair.
attempting to delete a nonexistent keypair. :param str user_id: Optional user_id owning the keypair
:returns: ``None`` :returns: ``None``
""" """
self._delete(_keypair.Keypair, keypair, ignore_missing=ignore_missing) attrs = {'user_id': user_id} if user_id else {}
self._delete(_keypair.Keypair, keypair, ignore_missing=ignore_missing,
**attrs)
def get_keypair(self, keypair): def get_keypair(self, keypair, user_id=None):
"""Get a single keypair """Get a single keypair
:param keypair: The value can be the ID of a keypair or a :param keypair: The value can be the ID of a keypair or a
:class:`~openstack.compute.v2.keypair.Keypair` :class:`~openstack.compute.v2.keypair.Keypair` instance.
instance. :param str user_id: Optional user_id owning the keypair
:returns: One :class:`~openstack.compute.v2.keypair.Keypair` :returns: One :class:`~openstack.compute.v2.keypair.Keypair`
:raises: :class:`~openstack.exceptions.ResourceNotFound` :raises: :class:`~openstack.exceptions.ResourceNotFound`
when no resource can be found. when no resource can be found.
""" """
return self._get(_keypair.Keypair, keypair) attrs = {'user_id': user_id} if user_id else {}
return self._get(_keypair.Keypair, keypair, **attrs)
def find_keypair(self, name_or_id, ignore_missing=True): def find_keypair(self, name_or_id, ignore_missing=True, user_id=None):
"""Find a single keypair """Find a single keypair
:param name_or_id: The name or ID of a keypair. :param name_or_id: The name or ID of a keypair.
:param bool ignore_missing: When set to ``False`` :param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be :class:`~openstack.exceptions.ResourceNotFound` will be raised when
raised when the resource does not exist. the resource does not exist. When set to ``True``, None will be
When set to ``True``, None will be returned when returned when attempting to find a nonexistent resource.
attempting to find a nonexistent resource. :param str user_id: Optional user_id owning the keypair
:returns: One :class:`~openstack.compute.v2.keypair.Keypair` or None :returns: One :class:`~openstack.compute.v2.keypair.Keypair` or None
""" """
attrs = {'user_id': user_id} if user_id else {}
return self._find(_keypair.Keypair, name_or_id, return self._find(_keypair.Keypair, name_or_id,
ignore_missing=ignore_missing) ignore_missing=ignore_missing,
**attrs)
def keypairs(self, **query): def keypairs(self, **query):
"""Return a generator of keypairs """Return a generator of keypairs

View File

@ -196,6 +196,55 @@ class TestFlavor(TestComputeProxy):
expected_args=["prop"]) expected_args=["prop"])
class TestKeyPair(TestComputeProxy):
def test_keypair_create(self):
self.verify_create(self.proxy.create_keypair, keypair.Keypair)
def test_keypair_delete(self):
self.verify_delete(self.proxy.delete_keypair, keypair.Keypair, False)
def test_keypair_delete_ignore(self):
self.verify_delete(self.proxy.delete_keypair, keypair.Keypair, True)
def test_keypair_delete_user_id(self):
self.verify_delete(
self.proxy.delete_keypair, keypair.Keypair,
True,
method_kwargs={'user_id': 'fake_user'},
expected_kwargs={'user_id': 'fake_user'}
)
def test_keypair_find(self):
self.verify_find(self.proxy.find_keypair, keypair.Keypair)
def test_keypair_find_user_id(self):
self.verify_find(
self.proxy.find_keypair, keypair.Keypair,
method_kwargs={'user_id': 'fake_user'},
expected_kwargs={'user_id': 'fake_user'}
)
def test_keypair_get(self):
self.verify_get(self.proxy.get_keypair, keypair.Keypair)
def test_keypair_get_user_id(self):
self.verify_get(
self.proxy.get_keypair, keypair.Keypair,
method_kwargs={'user_id': 'fake_user'},
expected_kwargs={'user_id': 'fake_user'}
)
def test_keypairs(self):
self.verify_list_no_kwargs(self.proxy.keypairs, keypair.Keypair)
def test_keypairs_user_id(self):
self.verify_list(
self.proxy.keypairs, keypair.Keypair,
method_kwargs={'user_id': 'fake_user'},
expected_kwargs={'user_id': 'fake_user'}
)
class TestCompute(TestComputeProxy): class TestCompute(TestComputeProxy):
def test_extension_find(self): def test_extension_find(self):
self.verify_find(self.proxy.find_extension, extension.Extension) self.verify_find(self.proxy.find_extension, extension.Extension)
@ -225,24 +274,6 @@ class TestCompute(TestComputeProxy):
method_kwargs={"details": False, "query": 1}, method_kwargs={"details": False, "query": 1},
expected_kwargs={"query": 1}) expected_kwargs={"query": 1})
def test_keypair_create(self):
self.verify_create(self.proxy.create_keypair, keypair.Keypair)
def test_keypair_delete(self):
self.verify_delete(self.proxy.delete_keypair, keypair.Keypair, False)
def test_keypair_delete_ignore(self):
self.verify_delete(self.proxy.delete_keypair, keypair.Keypair, True)
def test_keypair_find(self):
self.verify_find(self.proxy.find_keypair, keypair.Keypair)
def test_keypair_get(self):
self.verify_get(self.proxy.get_keypair, keypair.Keypair)
def test_keypairs(self):
self.verify_list_no_kwargs(self.proxy.keypairs, keypair.Keypair)
def test_limits_get(self): def test_limits_get(self):
self.verify_get(self.proxy.get_limits, limits.Limits, value=[]) self.verify_get(self.proxy.get_limits, limits.Limits, value=[])