mirror of
https://opendev.org/openstack/ansible-collections-openstack.git
synced 2026-05-06 13:23:06 +00:00
Refactored {group,role}_assignment modules
Change-Id: I6ec79eb203d0f68661b54bc89a194c366b3574c6
This commit is contained in:
@@ -4,40 +4,40 @@
|
||||
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
DOCUMENTATION = '''
|
||||
DOCUMENTATION = r'''
|
||||
---
|
||||
module: group_assignment
|
||||
short_description: Associate OpenStack Identity users and groups
|
||||
short_description: Assign OpenStack identity users to groups
|
||||
author: OpenStack Ansible SIG
|
||||
description:
|
||||
- Add and remove users from groups
|
||||
- Add and remove OpenStack identity (Keystone) users to/from groups.
|
||||
options:
|
||||
user:
|
||||
description:
|
||||
- Name or id for the user
|
||||
required: true
|
||||
type: str
|
||||
group:
|
||||
description:
|
||||
- Name or id for the group.
|
||||
required: true
|
||||
type: str
|
||||
state:
|
||||
description:
|
||||
- Should the user be present or absent in the group
|
||||
choices: [present, absent]
|
||||
default: present
|
||||
type: str
|
||||
group:
|
||||
description:
|
||||
- Name or ID for the group.
|
||||
required: true
|
||||
type: str
|
||||
state:
|
||||
description:
|
||||
- Should the user be present or absent in the group.
|
||||
choices: [present, absent]
|
||||
default: present
|
||||
type: str
|
||||
user:
|
||||
description:
|
||||
- Name or ID for the user.
|
||||
required: true
|
||||
type: str
|
||||
extends_documentation_fragment:
|
||||
- openstack.cloud.openstack
|
||||
- openstack.cloud.openstack
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Add the demo user to the demo group
|
||||
- openstack.cloud.group_assignment:
|
||||
cloud: mycloud
|
||||
user: demo
|
||||
group: demo
|
||||
EXAMPLES = r'''
|
||||
- name: Add demo_user user to demo_group group
|
||||
openstack.cloud.group_assignment:
|
||||
cloud: mycloud
|
||||
user: demo_user
|
||||
group: demo_group
|
||||
'''
|
||||
|
||||
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
|
||||
@@ -45,44 +45,42 @@ from ansible_collections.openstack.cloud.plugins.module_utils.openstack import O
|
||||
|
||||
class IdentityGroupAssignment(OpenStackModule):
|
||||
argument_spec = dict(
|
||||
user=dict(required=True),
|
||||
group=dict(required=True),
|
||||
state=dict(default='present', choices=['absent', 'present']),
|
||||
user=dict(required=True),
|
||||
)
|
||||
|
||||
module_kwargs = dict(
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
def _system_state_change(self, state, in_group):
|
||||
if state == 'present' and not in_group:
|
||||
return True
|
||||
if state == 'absent' and in_group:
|
||||
return True
|
||||
return False
|
||||
|
||||
def run(self):
|
||||
user = self.params['user']
|
||||
group = self.params['group']
|
||||
user_name_or_id = self.params['user']
|
||||
user = self.conn.identity.find_user(user_name_or_id,
|
||||
ignore_missing=False)
|
||||
|
||||
group_name_or_id = self.params['group']
|
||||
group = self.conn.identity.find_group(group_name_or_id,
|
||||
ignore_missing=False)
|
||||
|
||||
is_user_in_group = \
|
||||
self.conn.identity.check_user_in_group(user, group)
|
||||
|
||||
state = self.params['state']
|
||||
|
||||
in_group = self.conn.is_user_in_group(user, group)
|
||||
|
||||
if self.ansible.check_mode:
|
||||
self.exit_json(changed=self._system_state_change(state, in_group))
|
||||
self.exit_json(
|
||||
changed=(
|
||||
(state == 'present' and not is_user_in_group)
|
||||
or (state == 'absent' and is_user_in_group)))
|
||||
|
||||
changed = False
|
||||
if state == 'present':
|
||||
if not in_group:
|
||||
self.conn.add_user_to_group(user, group)
|
||||
changed = True
|
||||
|
||||
elif state == 'absent':
|
||||
if in_group:
|
||||
self.conn.remove_user_from_group(user, group)
|
||||
changed = True
|
||||
|
||||
self.exit_json(changed=changed)
|
||||
if state == 'present' and not is_user_in_group:
|
||||
self.conn.identity.add_user_to_group(user, group)
|
||||
self.exit_json(changed=True)
|
||||
elif state == 'absent' and is_user_in_group:
|
||||
self.conn.identity.remove_user_from_group(user, group)
|
||||
self.exit_json(changed=True)
|
||||
else:
|
||||
self.exit_json(changed=False)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user