From cfb7a39221d5ae633d5edba506b9b65d81b3e863 Mon Sep 17 00:00:00 2001 From: Maximilian Stinsky Date: Tue, 22 Sep 2026 10:21:29 +0200 Subject: [PATCH] identity_user: support Keystone user resource options Add an `options` parameter to the identity_user module, mapping 1:1 to Keystone's user resource `options` field exposed by openstacksdk (`User.options`). This allows setting options such as `ignore_password_expiry` and `ignore_change_password_upon_first_use`. On update, desired options are merged with the user's existing options so that options not managed by the module are preserved, and the operation is idempotent when the desired values already match. Change-Id: I16cff99e86f0676a5c16121e8eca2b7a13ea9d6e Signed-off-by: Maximilian Stinsky --- .../fragments/identity_user_options.yaml | 6 + plugins/modules/identity_user.py | 35 +++- .../cloud/openstack/test_identity_user.py | 170 ++++++++++++++++++ 3 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/identity_user_options.yaml create mode 100644 tests/unit/modules/cloud/openstack/test_identity_user.py diff --git a/changelogs/fragments/identity_user_options.yaml b/changelogs/fragments/identity_user_options.yaml new file mode 100644 index 00000000..780f5e11 --- /dev/null +++ b/changelogs/fragments/identity_user_options.yaml @@ -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. diff --git a/plugins/modules/identity_user.py b/plugins/modules/identity_user.py index da4def4a..5f05c226 100644 --- a/plugins/modules/identity_user.py +++ b/plugins/modules/identity_user.py @@ -40,6 +40,13 @@ options: - I(name) cannot be updated without deleting and re-creating the user. required: true 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: description: - Password for the user. @@ -99,6 +106,17 @@ EXAMPLES = r''' email: demo@example.com domain: default 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''' @@ -138,6 +156,10 @@ user: description: Unique user name, within the owning domain type: str sample: "demouser" + options: + description: The resource options for the user (e.g. + ignore_password_expiry). + type: dict password: description: Credential used during authentication type: str @@ -159,6 +181,7 @@ class IdentityUserModule(OpenStackModule): email=dict(), is_enabled=dict(default=True, type='bool', aliases=['enabled']), name=dict(required=True), + options=dict(type='dict'), password=dict(no_log=True), state=dict(default='present', choices=['absent', 'present']), update_password=dict(default='on_create', @@ -177,6 +200,16 @@ class IdentityUserModule(OpenStackModule): elif update_password == 'on_create' and 'password' in attributes: 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, updateable_attributes, non_updateable_attributes, **kwargs) @@ -202,7 +235,7 @@ class IdentityUserModule(OpenStackModule): kwargs['attributes'] = \ dict((k, self.params[k]) for k in ['description', 'email', 'is_enabled', 'name', - 'password'] + 'options', 'password'] if self.params[k] is not None) domain_name_or_id = self.params['domain'] diff --git a/tests/unit/modules/cloud/openstack/test_identity_user.py b/tests/unit/modules/cloud/openstack/test_identity_user.py new file mode 100644 index 00000000..b1ad8ce6 --- /dev/null +++ b/tests/unit/modules/cloud/openstack/test_identity_user.py @@ -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()