Skip to content

Commit 25d44c5

Browse files
committed
radosgw_user: support caps on user
Supporting adding/removing caps on the user. Signed-off-by: Seena Fallah <[email protected]>
1 parent e85060c commit 25d44c5

File tree

1 file changed

+104
-4
lines changed

1 file changed

+104
-4
lines changed

library/radosgw_user.py

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,24 @@ def generate_radosgw_cmd(cluster, args, container_image=None):
195195
return cmd
196196

197197

198+
def generate_caps_cmd(cluster, args, container_image=None):
199+
'''
200+
Generate 'radosgw' command line to execute for caps
201+
'''
202+
203+
cmd = pre_generate_radosgw_cmd(container_image=container_image)
204+
205+
base_cmd = [
206+
'--cluster',
207+
cluster,
208+
'caps'
209+
]
210+
211+
cmd.extend(base_cmd + args)
212+
213+
return cmd
214+
215+
198216
def exec_commands(module, cmd):
199217
'''
200218
Execute command(s)
@@ -223,6 +241,7 @@ def create_user(module, container_image=None):
223241
zone = module.params.get('zone', None)
224242
system = module.params.get('system', False)
225243
admin = module.params.get('admin', False)
244+
caps = module.params.get('caps')
226245

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

@@ -250,13 +269,81 @@ def create_user(module, container_image=None):
250269
if admin:
251270
args.append('--admin')
252271

272+
if caps:
273+
caps_args = [f"{cap['type']}={cap['perm']}" for cap in caps]
274+
args.extend(['--caps', ';'.join(caps_args)])
275+
253276
cmd = generate_radosgw_cmd(cluster=cluster,
254277
args=args,
255278
container_image=container_image)
256279

257280
return cmd
258281

259282

283+
def caps_add(module, caps, container_image=None):
284+
'''
285+
Create a new user
286+
'''
287+
288+
cluster = module.params.get('cluster')
289+
name = module.params.get('name')
290+
realm = module.params.get('realm', None)
291+
zonegroup = module.params.get('zonegroup', None)
292+
zone = module.params.get('zone', None)
293+
294+
args = ['add', '--uid=' + name]
295+
296+
if realm:
297+
args.extend(['--rgw-realm=' + realm])
298+
299+
if zonegroup:
300+
args.extend(['--rgw-zonegroup=' + zonegroup])
301+
302+
if zone:
303+
args.extend(['--rgw-zone=' + zone])
304+
305+
caps_args = [f"{cap['type']}={cap['perm']}" for cap in caps]
306+
args.extend(['--caps', ';'.join(caps_args)])
307+
308+
cmd = generate_caps_cmd(cluster=cluster,
309+
args=args,
310+
container_image=container_image)
311+
312+
return cmd
313+
314+
315+
def caps_rm(module, caps, container_image=None):
316+
'''
317+
Create a new user
318+
'''
319+
320+
cluster = module.params.get('cluster')
321+
name = module.params.get('name')
322+
realm = module.params.get('realm', None)
323+
zonegroup = module.params.get('zonegroup', None)
324+
zone = module.params.get('zone', None)
325+
326+
args = ['rm', '--uid=' + name]
327+
328+
if realm:
329+
args.extend(['--rgw-realm=' + realm])
330+
331+
if zonegroup:
332+
args.extend(['--rgw-zonegroup=' + zonegroup])
333+
334+
if zone:
335+
args.extend(['--rgw-zone=' + zone])
336+
337+
caps_args = [f"{cap['type']}={cap['perm']}" for cap in caps]
338+
args.extend(['--caps', ';'.join(caps_args)])
339+
340+
cmd = generate_caps_cmd(cluster=cluster,
341+
args=args,
342+
container_image=container_image)
343+
344+
return cmd
345+
346+
260347
def modify_user(module, container_image=None):
261348
'''
262349
Modify an existing user
@@ -398,7 +485,8 @@ def run_module():
398485
zonegroup=dict(type='str', required=False),
399486
zone=dict(type='str', required=False),
400487
system=dict(type='bool', required=False, default=False),
401-
admin=dict(type='bool', required=False, default=False)
488+
admin=dict(type='bool', required=False, default=False),
489+
caps=dict(type='list', required=False),
402490
)
403491

404492
module = AnsibleModule(
@@ -417,6 +505,7 @@ def run_module():
417505
secret_key = module.params.get('secret_key')
418506
system = module.params.get('system')
419507
admin = module.params.get('admin')
508+
caps = module.params.get('caps')
420509

421510
startd = datetime.datetime.now()
422511
changed = False
@@ -431,16 +520,19 @@ def run_module():
431520
current = {
432521
'display_name': user['display_name'],
433522
'system': user.get('system', False),
434-
'admin': user.get('admin', False)
523+
'admin': user.get('admin', False),
435524
}
436525
asked = {
437526
'display_name': display_name,
438527
'system': system,
439-
'admin': admin
528+
'admin': admin,
440529
}
441530
if email:
442531
current['email'] = user['email']
443532
asked['email'] = email
533+
if caps:
534+
current['caps'] = user['caps']
535+
asked['caps'] = caps
444536

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

454546
changed = current != asked
455547
if changed and not module.check_mode:
456-
rc, cmd, out, err = exec_commands(module, modify_user(module, container_image=container_image)) # noqa: E501
548+
rc, cmd, out, err = exec_commands(module, modify_user(module, container_image=container_image))
549+
550+
if caps:
551+
missing_caps = [cap for cap in asked['caps'] if cap not in current['caps']]
552+
extra_caps = [cap for cap in current['caps'] if cap not in asked['caps']]
553+
if extra_caps:
554+
rc, cmd, out, err = exec_commands(module, caps_rm(module, extra_caps, container_image=container_image))
555+
if missing_caps:
556+
rc, cmd, out, err = exec_commands(module, caps_add(module, missing_caps, container_image=container_image))
457557
else:
458558
changed = True
459559
if not module.check_mode:

0 commit comments

Comments
 (0)