mirror of
https://opendev.org/openstack/ansible-collections-openstack.git
synced 2026-11-22 07:00:23 +00:00
Merge "identity_user: support Keystone user resource options"
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
minor_changes:
|
||||||
|
- identity_user - Added an ``options`` parameter to set Keystone user
|
||||||
|
resource options, e.g. ``ignore_password_expiry`` and
|
||||||
|
``ignore_change_password_upon_first_use``. On update, the provided
|
||||||
|
options are merged with the user's existing options so options not
|
||||||
|
managed by this module are preserved.
|
||||||
@@ -40,6 +40,13 @@ options:
|
|||||||
- I(name) cannot be updated without deleting and re-creating the user.
|
- I(name) cannot be updated without deleting and re-creating the user.
|
||||||
required: true
|
required: true
|
||||||
type: str
|
type: str
|
||||||
|
options:
|
||||||
|
description:
|
||||||
|
- A dictionary of Keystone user options to set on the user, e.g.
|
||||||
|
C(ignore_password_expiry) or C(ignore_change_password_upon_first_use).
|
||||||
|
- On update, the provided options are merged with the user's existing
|
||||||
|
options, so options not specified here are preserved.
|
||||||
|
type: dict
|
||||||
password:
|
password:
|
||||||
description:
|
description:
|
||||||
- Password for the user.
|
- Password for the user.
|
||||||
@@ -99,6 +106,17 @@ EXAMPLES = r'''
|
|||||||
email: demo@example.com
|
email: demo@example.com
|
||||||
domain: default
|
domain: default
|
||||||
default_project: demo
|
default_project: demo
|
||||||
|
|
||||||
|
- name: Create a user with password policy options
|
||||||
|
openstack.cloud.identity_user:
|
||||||
|
cloud: mycloud
|
||||||
|
state: present
|
||||||
|
name: demouser
|
||||||
|
password: secret
|
||||||
|
domain: default
|
||||||
|
options:
|
||||||
|
ignore_password_expiry: true
|
||||||
|
ignore_change_password_upon_first_use: true
|
||||||
'''
|
'''
|
||||||
|
|
||||||
RETURN = r'''
|
RETURN = r'''
|
||||||
@@ -138,6 +156,10 @@ user:
|
|||||||
description: Unique user name, within the owning domain
|
description: Unique user name, within the owning domain
|
||||||
type: str
|
type: str
|
||||||
sample: "demouser"
|
sample: "demouser"
|
||||||
|
options:
|
||||||
|
description: The resource options for the user (e.g.
|
||||||
|
ignore_password_expiry).
|
||||||
|
type: dict
|
||||||
password:
|
password:
|
||||||
description: Credential used during authentication
|
description: Credential used during authentication
|
||||||
type: str
|
type: str
|
||||||
@@ -159,6 +181,7 @@ class IdentityUserModule(OpenStackModule):
|
|||||||
email=dict(),
|
email=dict(),
|
||||||
is_enabled=dict(default=True, type='bool', aliases=['enabled']),
|
is_enabled=dict(default=True, type='bool', aliases=['enabled']),
|
||||||
name=dict(required=True),
|
name=dict(required=True),
|
||||||
|
options=dict(type='dict'),
|
||||||
password=dict(no_log=True),
|
password=dict(no_log=True),
|
||||||
state=dict(default='present', choices=['absent', 'present']),
|
state=dict(default='present', choices=['absent', 'present']),
|
||||||
update_password=dict(default='on_create',
|
update_password=dict(default='on_create',
|
||||||
@@ -177,6 +200,16 @@ class IdentityUserModule(OpenStackModule):
|
|||||||
elif update_password == 'on_create' and 'password' in attributes:
|
elif update_password == 'on_create' and 'password' in attributes:
|
||||||
attributes.pop('password')
|
attributes.pop('password')
|
||||||
|
|
||||||
|
if 'options' in attributes:
|
||||||
|
# Merge desired options with the user's existing options so
|
||||||
|
# that options not managed by this module are preserved, and
|
||||||
|
# so the update is idempotent when the desired options already
|
||||||
|
# match the current values.
|
||||||
|
current = self.get_function(resource['id'])
|
||||||
|
merged = dict(current['options'] or {})
|
||||||
|
merged.update(attributes['options'])
|
||||||
|
attributes['options'] = merged
|
||||||
|
|
||||||
return super()._build_update(resource, attributes,
|
return super()._build_update(resource, attributes,
|
||||||
updateable_attributes,
|
updateable_attributes,
|
||||||
non_updateable_attributes, **kwargs)
|
non_updateable_attributes, **kwargs)
|
||||||
@@ -202,7 +235,7 @@ class IdentityUserModule(OpenStackModule):
|
|||||||
kwargs['attributes'] = \
|
kwargs['attributes'] = \
|
||||||
dict((k, self.params[k])
|
dict((k, self.params[k])
|
||||||
for k in ['description', 'email', 'is_enabled', 'name',
|
for k in ['description', 'email', 'is_enabled', 'name',
|
||||||
'password']
|
'options', 'password']
|
||||||
if self.params[k] is not None)
|
if self.params[k] is not None)
|
||||||
|
|
||||||
domain_name_or_id = self.params['domain']
|
domain_name_or_id = self.params['domain']
|
||||||
|
|||||||
@@ -0,0 +1,170 @@
|
|||||||
|
import unittest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
from ansible_collections.openstack.cloud.plugins.modules import identity_user
|
||||||
|
from ansible_collections.openstack.cloud.tests.unit.modules.utils import (
|
||||||
|
AnsibleExitJson,
|
||||||
|
ModuleTestCase,
|
||||||
|
set_module_args,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class FakeUser(dict):
|
||||||
|
'''A dict-like stand-in for an openstacksdk identity.v3.user.User.'''
|
||||||
|
|
||||||
|
def to_dict(self, computed=False):
|
||||||
|
return dict(self)
|
||||||
|
|
||||||
|
|
||||||
|
class FakeSDK(object):
|
||||||
|
class exceptions:
|
||||||
|
class OpenStackCloudException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class TestIdentityUser(ModuleTestCase):
|
||||||
|
|
||||||
|
def _run_module(self, module_args, identity):
|
||||||
|
set_module_args(module_args)
|
||||||
|
conn = mock.Mock()
|
||||||
|
conn.identity = identity
|
||||||
|
with mock.patch.object(
|
||||||
|
identity_user.IdentityUserModule,
|
||||||
|
'openstack_cloud_from_module',
|
||||||
|
return_value=(FakeSDK(), conn),
|
||||||
|
):
|
||||||
|
identity_user.main()
|
||||||
|
|
||||||
|
def _new_identity(self, existing_user=None):
|
||||||
|
identity = mock.Mock()
|
||||||
|
if existing_user is None:
|
||||||
|
identity.find_user.return_value = None
|
||||||
|
else:
|
||||||
|
identity.find_user.return_value = existing_user
|
||||||
|
identity.get_user.return_value = existing_user
|
||||||
|
return identity
|
||||||
|
|
||||||
|
# -- create -----------------------------------------------------------
|
||||||
|
|
||||||
|
def test_create_user_with_options(self):
|
||||||
|
identity = self._new_identity(existing_user=None)
|
||||||
|
identity.create_user.return_value = FakeUser(
|
||||||
|
id='1', name='demouser', is_enabled=True,
|
||||||
|
options={'ignore_password_expiry': True,
|
||||||
|
'ignore_change_password_upon_first_use': True},
|
||||||
|
)
|
||||||
|
|
||||||
|
with self.assertRaises(AnsibleExitJson) as ex:
|
||||||
|
self._run_module({
|
||||||
|
'name': 'demouser',
|
||||||
|
'options': {
|
||||||
|
'ignore_password_expiry': True,
|
||||||
|
'ignore_change_password_upon_first_use': True,
|
||||||
|
},
|
||||||
|
}, identity)
|
||||||
|
|
||||||
|
result = ex.exception.args[0]
|
||||||
|
self.assertTrue(result['changed'])
|
||||||
|
identity.create_user.assert_called_once()
|
||||||
|
_, kwargs = identity.create_user.call_args
|
||||||
|
self.assertEqual(
|
||||||
|
{'ignore_password_expiry': True,
|
||||||
|
'ignore_change_password_upon_first_use': True},
|
||||||
|
kwargs['options'])
|
||||||
|
identity.update_user.assert_not_called()
|
||||||
|
|
||||||
|
def test_create_user_without_options(self):
|
||||||
|
identity = self._new_identity(existing_user=None)
|
||||||
|
identity.create_user.return_value = FakeUser(
|
||||||
|
id='1', name='demouser', is_enabled=True, options={})
|
||||||
|
|
||||||
|
with self.assertRaises(AnsibleExitJson) as ex:
|
||||||
|
self._run_module({'name': 'demouser'}, identity)
|
||||||
|
|
||||||
|
result = ex.exception.args[0]
|
||||||
|
self.assertTrue(result['changed'])
|
||||||
|
identity.create_user.assert_called_once()
|
||||||
|
_, kwargs = identity.create_user.call_args
|
||||||
|
self.assertNotIn('options', kwargs)
|
||||||
|
|
||||||
|
# -- update -----------------------------------------------------------
|
||||||
|
|
||||||
|
def _existing_user(self, **options):
|
||||||
|
return FakeUser(
|
||||||
|
id='1', name='demouser', is_enabled=True, options=options)
|
||||||
|
|
||||||
|
def test_update_user_options_idempotent(self):
|
||||||
|
user = self._existing_user(
|
||||||
|
ignore_password_expiry=True,
|
||||||
|
ignore_change_password_upon_first_use=True,
|
||||||
|
)
|
||||||
|
identity = self._new_identity(existing_user=user)
|
||||||
|
|
||||||
|
with self.assertRaises(AnsibleExitJson) as ex:
|
||||||
|
self._run_module({
|
||||||
|
'name': 'demouser',
|
||||||
|
'options': {
|
||||||
|
'ignore_password_expiry': True,
|
||||||
|
'ignore_change_password_upon_first_use': True,
|
||||||
|
},
|
||||||
|
}, identity)
|
||||||
|
|
||||||
|
result = ex.exception.args[0]
|
||||||
|
self.assertFalse(result['changed'])
|
||||||
|
identity.update_user.assert_not_called()
|
||||||
|
|
||||||
|
def test_update_user_options_change(self):
|
||||||
|
user = self._existing_user(ignore_password_expiry=False)
|
||||||
|
identity = self._new_identity(existing_user=user)
|
||||||
|
identity.update_user.return_value = FakeUser(
|
||||||
|
id='1', name='demouser', is_enabled=True,
|
||||||
|
options={'ignore_password_expiry': True})
|
||||||
|
|
||||||
|
with self.assertRaises(AnsibleExitJson) as ex:
|
||||||
|
self._run_module({
|
||||||
|
'name': 'demouser',
|
||||||
|
'options': {'ignore_password_expiry': True},
|
||||||
|
}, identity)
|
||||||
|
|
||||||
|
result = ex.exception.args[0]
|
||||||
|
self.assertTrue(result['changed'])
|
||||||
|
identity.update_user.assert_called_once_with(
|
||||||
|
'1', options={'ignore_password_expiry': True})
|
||||||
|
|
||||||
|
def test_update_user_options_merge_preserves_existing(self):
|
||||||
|
user = self._existing_user(
|
||||||
|
lock_password=True,
|
||||||
|
ignore_password_expiry=False,
|
||||||
|
)
|
||||||
|
identity = self._new_identity(existing_user=user)
|
||||||
|
identity.update_user.return_value = FakeUser(
|
||||||
|
id='1', name='demouser', is_enabled=True,
|
||||||
|
options={'lock_password': True, 'ignore_password_expiry': True})
|
||||||
|
|
||||||
|
with self.assertRaises(AnsibleExitJson) as ex:
|
||||||
|
self._run_module({
|
||||||
|
'name': 'demouser',
|
||||||
|
'options': {'ignore_password_expiry': True},
|
||||||
|
}, identity)
|
||||||
|
|
||||||
|
result = ex.exception.args[0]
|
||||||
|
self.assertTrue(result['changed'])
|
||||||
|
identity.update_user.assert_called_once_with(
|
||||||
|
'1',
|
||||||
|
options={'lock_password': True, 'ignore_password_expiry': True})
|
||||||
|
|
||||||
|
def test_update_user_without_options_preserves_existing(self):
|
||||||
|
# When no options are specified, existing options must not be touched.
|
||||||
|
user = self._existing_user(ignore_password_expiry=True)
|
||||||
|
identity = self._new_identity(existing_user=user)
|
||||||
|
|
||||||
|
with self.assertRaises(AnsibleExitJson) as ex:
|
||||||
|
self._run_module({'name': 'demouser'}, identity)
|
||||||
|
|
||||||
|
result = ex.exception.args[0]
|
||||||
|
self.assertFalse(result['changed'])
|
||||||
|
identity.update_user.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user