library: retrieve realm id for zone/zonegroup

When the zonegroup or the zone doesn't have a realm associated then
it's not possible to modify that ressource.
This patch allows to retrieve the current realm id and compare it to
the realm id from the realm in parameter.

Signed-off-by: Dimitri Savineau <dsavinea@redhat.com>
pull/6229/head v6.0.0alpha7
Dimitri Savineau 2021-01-22 12:45:32 -05:00 committed by Guillaume Abrioux
parent 2734a12d44
commit 195159ecef
4 changed files with 112 additions and 23 deletions

View File

@ -329,6 +329,30 @@ def get_zonegroup(module, container_image=None):
return cmd
def get_realm(module, container_image=None):
'''
Get existing realm
'''
cluster = module.params.get('cluster')
realm = module.params.get('realm')
cmd = pre_generate_radosgw_cmd(container_image=container_image)
args = [
'--cluster',
cluster,
'realm',
'get',
'--rgw-realm=' + realm,
'--format=json'
]
cmd.extend(args)
return cmd
def remove_zone(module, container_image=None):
'''
Remove a zone
@ -415,28 +439,33 @@ def run_module():
rc, cmd, out, err = exec_commands(module, get_zone(module, container_image=container_image))
if rc == 0:
zone = json.loads(out)
_rc, _cmd, _out, _err = exec_commands(module, get_zonegroup(module, container_image=container_image))
if _rc == 0:
zonegroup = json.loads(_out)
if not access_key:
access_key = ''
if not secret_key:
secret_key = ''
current = {
'endpoints': next(zone['endpoints'] for zone in zonegroup['zones'] if zone['name'] == name),
'access_key': zone['system_key']['access_key'],
'secret_key': zone['system_key']['secret_key']
}
asked = {
'endpoints': endpoints,
'access_key': access_key,
'secret_key': secret_key
}
if current != asked:
rc, cmd, out, err = exec_commands(module, modify_zone(module, container_image=container_image))
changed = True
else:
_rc, _cmd, _out, _err = exec_commands(module, get_realm(module, container_image=container_image))
if _rc != 0:
fatal(_err, module)
realm = json.loads(_out)
_rc, _cmd, _out, _err = exec_commands(module, get_zonegroup(module, container_image=container_image))
if _rc != 0:
fatal(_err, module)
zonegroup = json.loads(_out)
if not access_key:
access_key = ''
if not secret_key:
secret_key = ''
current = {
'endpoints': next(zone['endpoints'] for zone in zonegroup['zones'] if zone['name'] == name),
'access_key': zone['system_key']['access_key'],
'secret_key': zone['system_key']['secret_key'],
'realm_id': zone['realm_id']
}
asked = {
'endpoints': endpoints,
'access_key': access_key,
'secret_key': secret_key,
'realm_id': realm['id']
}
if current != asked:
rc, cmd, out, err = exec_commands(module, modify_zone(module, container_image=container_image))
changed = True
else:
rc, cmd, out, err = exec_commands(module, create_zone(module, container_image=container_image))
changed = True

View File

@ -16,6 +16,10 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
from ansible.module_utils.basic import AnsibleModule
try:
from ansible.module_utils.ca_common import fatal
except ImportError:
from module_utils.ca_common import fatal
import datetime
import json
import os
@ -251,6 +255,30 @@ def get_zonegroup(module, container_image=None):
return cmd
def get_realm(module, container_image=None):
'''
Get existing realm
'''
cluster = module.params.get('cluster')
realm = module.params.get('realm')
cmd = pre_generate_radosgw_cmd(container_image=container_image)
args = [
'--cluster',
cluster,
'realm',
'get',
'--rgw-realm=' + realm,
'--format=json'
]
cmd.extend(args)
return cmd
def remove_zonegroup(module, container_image=None):
'''
Remove a zonegroup
@ -327,13 +355,19 @@ def run_module():
rc, cmd, out, err = exec_commands(module, get_zonegroup(module, container_image=container_image))
if rc == 0:
zonegroup = json.loads(out)
_rc, _cmd, _out, _err = exec_commands(module, get_realm(module, container_image=container_image))
if _rc != 0:
fatal(_err, module)
realm = json.loads(_out)
current = {
'endpoints': zonegroup['endpoints'],
'master': zonegroup.get('is_master', 'false')
'master': zonegroup.get('is_master', 'false'),
'realm_id': zonegroup['realm_id']
}
asked = {
'endpoints': endpoints,
'master': master
'master': master,
'realm_id': realm['id']
}
if current != asked:
rc, cmd, out, err = exec_commands(module, modify_zonegroup(module, container_image=container_image))

View File

@ -136,6 +136,19 @@ class TestRadosgwZoneModule(object):
assert radosgw_zone.get_zonegroup(fake_module) == expected_cmd
def test_get_realm(self):
fake_module = MagicMock()
fake_module.params = fake_params
expected_cmd = [
fake_binary,
'--cluster', fake_cluster,
'realm', 'get',
'--rgw-realm=' + fake_realm,
'--format=json'
]
assert radosgw_zone.get_realm(fake_module) == expected_cmd
def test_remove_zone(self):
fake_module = MagicMock()
fake_module.params = fake_params

View File

@ -117,6 +117,19 @@ class TestRadosgwZonegroupModule(object):
assert radosgw_zonegroup.get_zonegroup(fake_module) == expected_cmd
def test_get_realm(self):
fake_module = MagicMock()
fake_module.params = fake_params
expected_cmd = [
fake_binary,
'--cluster', fake_cluster,
'realm', 'get',
'--rgw-realm=' + fake_realm,
'--format=json'
]
assert radosgw_zonegroup.get_realm(fake_module) == expected_cmd
def test_remove_zonegroup(self):
fake_module = MagicMock()
fake_module.params = fake_params