2020-09-05 02:49:07 +08:00
|
|
|
# Copyright 2020, Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2020-10-28 00:14:19 +08:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2020-11-05 18:05:34 +08:00
|
|
|
try:
|
2023-01-19 05:44:01 +08:00
|
|
|
from ansible.module_utils.ca_common import generate_ceph_cmd, \
|
2020-11-05 18:05:34 +08:00
|
|
|
is_containerized, \
|
|
|
|
exec_command, \
|
2023-01-19 05:45:07 +08:00
|
|
|
exit_module
|
2020-11-05 18:05:34 +08:00
|
|
|
except ImportError:
|
2023-01-19 05:45:07 +08:00
|
|
|
from module_utils.ca_common import generate_ceph_cmd, is_containerized, exec_command, exit_module # noqa: E501
|
2020-11-05 18:05:34 +08:00
|
|
|
|
2020-10-28 00:14:19 +08:00
|
|
|
import datetime
|
|
|
|
import json
|
|
|
|
|
2020-09-05 02:49:07 +08:00
|
|
|
|
|
|
|
ANSIBLE_METADATA = {
|
|
|
|
'metadata_version': '1.1',
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'
|
|
|
|
}
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: ceph_dashboard_user
|
|
|
|
|
|
|
|
short_description: Manage Ceph Dashboard User
|
|
|
|
|
|
|
|
version_added: "2.8"
|
|
|
|
|
|
|
|
description:
|
|
|
|
- Manage Ceph Dashboard user(s) creation, deletion and updates.
|
|
|
|
options:
|
|
|
|
cluster:
|
|
|
|
description:
|
|
|
|
- The ceph cluster name.
|
|
|
|
required: false
|
|
|
|
default: ceph
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- name of the Ceph Dashboard user.
|
|
|
|
required: true
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
If 'present' is used, the module creates a user if it doesn't
|
|
|
|
exist or update it if it already exists.
|
|
|
|
If 'absent' is used, the module will simply delete the user.
|
|
|
|
If 'info' is used, the module will return all details about the
|
|
|
|
existing user (json formatted).
|
|
|
|
required: false
|
|
|
|
choices: ['present', 'absent', 'info']
|
|
|
|
default: present
|
|
|
|
password:
|
|
|
|
description:
|
|
|
|
- password of the Ceph Dashboard user.
|
|
|
|
required: false
|
|
|
|
roles:
|
|
|
|
description:
|
|
|
|
- roles of the Ceph Dashboard user.
|
|
|
|
required: false
|
|
|
|
default: []
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Dimitri Savineau <dsavinea@redhat.com>
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: create a Ceph Dashboard user
|
|
|
|
ceph_dashboard_user:
|
|
|
|
name: foo
|
|
|
|
password: bar
|
|
|
|
|
|
|
|
- name: create a read-only/block-manager Ceph Dashboard user
|
|
|
|
ceph_dashboard_user:
|
|
|
|
name: foo
|
|
|
|
password: bar
|
|
|
|
roles:
|
|
|
|
- 'read-only'
|
|
|
|
- 'block-manager'
|
|
|
|
|
|
|
|
- name: create a Ceph Dashboard admin user
|
|
|
|
ceph_dashboard_user:
|
|
|
|
name: foo
|
|
|
|
password: bar
|
|
|
|
roles: ['administrator']
|
|
|
|
|
|
|
|
- name: get a Ceph Dashboard user information
|
|
|
|
ceph_dashboard_user:
|
|
|
|
name: foo
|
|
|
|
state: info
|
|
|
|
|
|
|
|
- name: delete a Ceph Dashboard user
|
|
|
|
ceph_dashboard_user:
|
|
|
|
name: foo
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''# '''
|
|
|
|
|
|
|
|
|
|
|
|
def create_user(module, container_image=None):
|
|
|
|
'''
|
|
|
|
Create a new user
|
|
|
|
'''
|
|
|
|
|
|
|
|
cluster = module.params.get('cluster')
|
|
|
|
name = module.params.get('name')
|
|
|
|
|
2021-01-06 21:07:38 +08:00
|
|
|
args = ['ac-user-create', '-i', '-', name]
|
2020-09-05 02:49:07 +08:00
|
|
|
|
2023-01-19 05:44:01 +08:00
|
|
|
cmd = generate_ceph_cmd(sub_cmd=['dashboard'],
|
|
|
|
args=args,
|
|
|
|
cluster=cluster,
|
|
|
|
container_image=container_image,
|
|
|
|
interactive=True)
|
2020-09-05 02:49:07 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
|
|
|
def set_roles(module, container_image=None):
|
|
|
|
'''
|
|
|
|
Set user roles
|
|
|
|
'''
|
|
|
|
|
|
|
|
cluster = module.params.get('cluster')
|
|
|
|
name = module.params.get('name')
|
|
|
|
roles = module.params.get('roles')
|
|
|
|
|
|
|
|
args = ['ac-user-set-roles', name]
|
|
|
|
|
|
|
|
args.extend(roles)
|
|
|
|
|
2023-01-19 05:44:01 +08:00
|
|
|
cmd = generate_ceph_cmd(sub_cmd=['dashboard'],
|
|
|
|
args=args,
|
|
|
|
cluster=cluster,
|
|
|
|
container_image=container_image)
|
2020-09-05 02:49:07 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
|
|
|
def set_password(module, container_image=None):
|
|
|
|
'''
|
|
|
|
Set user password
|
|
|
|
'''
|
|
|
|
|
|
|
|
cluster = module.params.get('cluster')
|
|
|
|
name = module.params.get('name')
|
|
|
|
|
2021-01-06 21:07:38 +08:00
|
|
|
args = ['ac-user-set-password', '-i', '-', name]
|
2020-09-05 02:49:07 +08:00
|
|
|
|
2023-01-19 05:44:01 +08:00
|
|
|
cmd = generate_ceph_cmd(sub_cmd=['dashboard'],
|
|
|
|
args=args,
|
|
|
|
cluster=cluster,
|
|
|
|
container_image=container_image,
|
|
|
|
interactive=True)
|
2020-09-05 02:49:07 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
|
|
|
def get_user(module, container_image=None):
|
|
|
|
'''
|
|
|
|
Get existing user
|
|
|
|
'''
|
|
|
|
|
|
|
|
cluster = module.params.get('cluster')
|
|
|
|
name = module.params.get('name')
|
|
|
|
|
|
|
|
args = ['ac-user-show', name, '--format=json']
|
|
|
|
|
2023-01-19 05:44:01 +08:00
|
|
|
cmd = generate_ceph_cmd(sub_cmd=['dashboard'],
|
|
|
|
args=args,
|
|
|
|
cluster=cluster,
|
|
|
|
container_image=container_image)
|
2020-09-05 02:49:07 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
|
|
|
def remove_user(module, container_image=None):
|
|
|
|
'''
|
|
|
|
Remove a user
|
|
|
|
'''
|
|
|
|
|
|
|
|
cluster = module.params.get('cluster')
|
|
|
|
name = module.params.get('name')
|
|
|
|
|
|
|
|
args = ['ac-user-delete', name]
|
|
|
|
|
2023-01-19 05:44:01 +08:00
|
|
|
cmd = generate_ceph_cmd(sub_cmd=['dashboard'],
|
|
|
|
args=args,
|
|
|
|
cluster=cluster,
|
|
|
|
container_image=container_image)
|
2020-09-05 02:49:07 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
|
|
|
def run_module():
|
|
|
|
module_args = dict(
|
|
|
|
cluster=dict(type='str', required=False, default='ceph'),
|
|
|
|
name=dict(type='str', required=True),
|
2021-06-18 00:18:07 +08:00
|
|
|
state=dict(type='str', required=False, choices=['present', 'absent', 'info'], default='present'), # noqa: E501
|
2020-09-05 02:49:07 +08:00
|
|
|
password=dict(type='str', required=False, no_log=True),
|
2020-09-06 10:17:02 +08:00
|
|
|
roles=dict(type='list',
|
|
|
|
required=False,
|
2021-06-18 00:18:07 +08:00
|
|
|
choices=['administrator', 'read-only', 'block-manager', 'rgw-manager', 'cluster-manager', 'pool-manager', 'cephfs-manager'], # noqa: E501
|
2020-09-06 10:17:02 +08:00
|
|
|
default=[]),
|
2020-09-05 02:49:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=module_args,
|
|
|
|
supports_check_mode=True,
|
|
|
|
required_if=[['state', 'present', ['password']]]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Gather module parameters in variables
|
|
|
|
name = module.params.get('name')
|
|
|
|
state = module.params.get('state')
|
|
|
|
roles = module.params.get('roles')
|
2021-01-06 21:07:38 +08:00
|
|
|
password = module.params.get('password')
|
2020-09-05 02:49:07 +08:00
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(
|
|
|
|
changed=False,
|
|
|
|
stdout='',
|
|
|
|
stderr='',
|
|
|
|
rc=0,
|
|
|
|
start='',
|
|
|
|
end='',
|
|
|
|
delta='',
|
|
|
|
)
|
|
|
|
|
|
|
|
startd = datetime.datetime.now()
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
# will return either the image name or None
|
|
|
|
container_image = is_containerized()
|
|
|
|
|
|
|
|
if state == "present":
|
2021-06-18 00:18:07 +08:00
|
|
|
rc, cmd, out, err = exec_command(module, get_user(module, container_image=container_image)) # noqa: E501
|
2020-09-05 02:49:07 +08:00
|
|
|
if rc == 0:
|
|
|
|
user = json.loads(out)
|
|
|
|
user['roles'].sort()
|
|
|
|
roles.sort()
|
|
|
|
if user['roles'] != roles:
|
2021-06-18 00:18:07 +08:00
|
|
|
rc, cmd, out, err = exec_command(module, set_roles(module, container_image=container_image)) # noqa: E501
|
2020-09-05 02:49:07 +08:00
|
|
|
changed = True
|
2021-06-18 00:18:07 +08:00
|
|
|
rc, cmd, out, err = exec_command(module, set_password(module, container_image=container_image), stdin=password) # noqa: E501
|
2020-09-05 02:49:07 +08:00
|
|
|
else:
|
2021-06-18 00:18:07 +08:00
|
|
|
rc, cmd, out, err = exec_command(module, create_user(module, container_image=container_image), stdin=password) # noqa: E501
|
|
|
|
rc, cmd, out, err = exec_command(module, set_roles(module, container_image=container_image)) # noqa: E501
|
2020-09-05 02:49:07 +08:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
elif state == "absent":
|
2021-06-18 00:18:07 +08:00
|
|
|
rc, cmd, out, err = exec_command(module, get_user(module, container_image=container_image)) # noqa: E501
|
2020-09-05 02:49:07 +08:00
|
|
|
if rc == 0:
|
2021-06-18 00:18:07 +08:00
|
|
|
rc, cmd, out, err = exec_command(module, remove_user(module, container_image=container_image)) # noqa: E501
|
2020-09-05 02:49:07 +08:00
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
rc = 0
|
|
|
|
out = "Dashboard User {} doesn't exist".format(name)
|
|
|
|
|
|
|
|
elif state == "info":
|
2021-06-18 00:18:07 +08:00
|
|
|
rc, cmd, out, err = exec_command(module, get_user(module, container_image=container_image)) # noqa: E501
|
2020-09-05 02:49:07 +08:00
|
|
|
|
2021-06-18 00:18:07 +08:00
|
|
|
exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed) # noqa: E501
|
2020-09-05 02:49:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
run_module()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|