Skip to content

Commit

Permalink
library: retrieve realm id for zone/zonegroup
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
dsavineau authored and guits committed Jan 29, 2021
1 parent 2734a12 commit 195159e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 22 deletions.
69 changes: 49 additions & 20 deletions library/radosgw_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 36 additions & 2 deletions library/radosgw_zonegroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
__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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
13 changes: 13 additions & 0 deletions tests/library/test_radosgw_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ def test_get_zonegroup(self):

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
Expand Down
13 changes: 13 additions & 0 deletions tests/library/test_radosgw_zonegroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ def test_get_zonegroup(self):

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
Expand Down

0 comments on commit 195159e

Please sign in to comment.