Commit 195159ec authored by Dimitri Savineau's avatar Dimitri Savineau Committed by Guillaume Abrioux
Browse files

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: default avatarDimitri Savineau <dsavinea@redhat.com>
parent 2734a12d
......@@ -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_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:
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:
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
......
......@@ -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))
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment