Skip to content

Commit

Permalink
radosgw_user: support caps on user
Browse files Browse the repository at this point in the history
Supporting adding/removing caps on the user.

Signed-off-by: Seena Fallah <[email protected]>
  • Loading branch information
clwluvw committed Jul 31, 2024
1 parent e85060c commit 25d44c5
Showing 1 changed file with 104 additions and 4 deletions.
108 changes: 104 additions & 4 deletions library/radosgw_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ def generate_radosgw_cmd(cluster, args, container_image=None):
return cmd


def generate_caps_cmd(cluster, args, container_image=None):
'''
Generate 'radosgw' command line to execute for caps
'''

cmd = pre_generate_radosgw_cmd(container_image=container_image)

base_cmd = [
'--cluster',
cluster,
'caps'
]

cmd.extend(base_cmd + args)

return cmd


def exec_commands(module, cmd):
'''
Execute command(s)
Expand Down Expand Up @@ -223,6 +241,7 @@ def create_user(module, container_image=None):
zone = module.params.get('zone', None)
system = module.params.get('system', False)
admin = module.params.get('admin', False)
caps = module.params.get('caps')

args = ['create', '--uid=' + name, '--display_name=' + display_name]

Expand Down Expand Up @@ -250,13 +269,81 @@ def create_user(module, container_image=None):
if admin:
args.append('--admin')

if caps:
caps_args = [f"{cap['type']}={cap['perm']}" for cap in caps]
args.extend(['--caps', ';'.join(caps_args)])

cmd = generate_radosgw_cmd(cluster=cluster,
args=args,
container_image=container_image)

return cmd


def caps_add(module, caps, container_image=None):
'''
Create a new user
'''

cluster = module.params.get('cluster')
name = module.params.get('name')
realm = module.params.get('realm', None)
zonegroup = module.params.get('zonegroup', None)
zone = module.params.get('zone', None)

args = ['add', '--uid=' + name]

if realm:
args.extend(['--rgw-realm=' + realm])

if zonegroup:
args.extend(['--rgw-zonegroup=' + zonegroup])

if zone:
args.extend(['--rgw-zone=' + zone])

caps_args = [f"{cap['type']}={cap['perm']}" for cap in caps]
args.extend(['--caps', ';'.join(caps_args)])

cmd = generate_caps_cmd(cluster=cluster,
args=args,
container_image=container_image)

return cmd


def caps_rm(module, caps, container_image=None):
'''
Create a new user
'''

cluster = module.params.get('cluster')
name = module.params.get('name')
realm = module.params.get('realm', None)
zonegroup = module.params.get('zonegroup', None)
zone = module.params.get('zone', None)

args = ['rm', '--uid=' + name]

if realm:
args.extend(['--rgw-realm=' + realm])

if zonegroup:
args.extend(['--rgw-zonegroup=' + zonegroup])

if zone:
args.extend(['--rgw-zone=' + zone])

caps_args = [f"{cap['type']}={cap['perm']}" for cap in caps]
args.extend(['--caps', ';'.join(caps_args)])

cmd = generate_caps_cmd(cluster=cluster,
args=args,
container_image=container_image)

return cmd


def modify_user(module, container_image=None):
'''
Modify an existing user
Expand Down Expand Up @@ -398,7 +485,8 @@ def run_module():
zonegroup=dict(type='str', required=False),
zone=dict(type='str', required=False),
system=dict(type='bool', required=False, default=False),
admin=dict(type='bool', required=False, default=False)
admin=dict(type='bool', required=False, default=False),
caps=dict(type='list', required=False),
)

module = AnsibleModule(
Expand All @@ -417,6 +505,7 @@ def run_module():
secret_key = module.params.get('secret_key')
system = module.params.get('system')
admin = module.params.get('admin')
caps = module.params.get('caps')

startd = datetime.datetime.now()
changed = False
Expand All @@ -431,16 +520,19 @@ def run_module():
current = {
'display_name': user['display_name'],
'system': user.get('system', False),
'admin': user.get('admin', False)
'admin': user.get('admin', False),
}
asked = {
'display_name': display_name,
'system': system,
'admin': admin
'admin': admin,
}
if email:
current['email'] = user['email']
asked['email'] = email
if caps:
current['caps'] = user['caps']
asked['caps'] = caps

if access_key and secret_key:
asked['access_key'] = access_key
Expand All @@ -453,7 +545,15 @@ def run_module():

changed = current != asked
if changed and not module.check_mode:
rc, cmd, out, err = exec_commands(module, modify_user(module, container_image=container_image)) # noqa: E501
rc, cmd, out, err = exec_commands(module, modify_user(module, container_image=container_image))

if caps:
missing_caps = [cap for cap in asked['caps'] if cap not in current['caps']]
extra_caps = [cap for cap in current['caps'] if cap not in asked['caps']]
if extra_caps:
rc, cmd, out, err = exec_commands(module, caps_rm(module, extra_caps, container_image=container_image))
if missing_caps:
rc, cmd, out, err = exec_commands(module, caps_add(module, missing_caps, container_image=container_image))
else:
changed = True
if not module.check_mode:
Expand Down

0 comments on commit 25d44c5

Please sign in to comment.