library/ceph_crush_rule: module refactor

This refactor makes the 'name' argument not mandatory because when
'state' is 'info' we shouldn't need to pass it.

The second change is just a duplicate code removal.

Signed-off-by: Guillaume Abrioux <gabrioux@ibm.com>
(cherry picked from commit e433d2b955)
pull/7558/head
Guillaume Abrioux 2023-06-02 21:00:35 +02:00 committed by Guillaume Abrioux
parent 310ce9e441
commit 66e32b895c
2 changed files with 6 additions and 8 deletions

View File

@ -190,7 +190,7 @@ def remove_rule(module, container_image=None):
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
name=dict(type='str', required=True), name=dict(type='str', required=False),
cluster=dict(type='str', required=False, default='ceph'), cluster=dict(type='str', required=False, default='ceph'),
state=dict(type='str', required=False, choices=['present', 'absent', 'info'], default='present'), # noqa: E501 state=dict(type='str', required=False, choices=['present', 'absent', 'info'], default='present'), # noqa: E501
rule_type=dict(type='str', required=False, choices=['replicated', 'erasure']), # noqa: E501 rule_type=dict(type='str', required=False, choices=['replicated', 'erasure']), # noqa: E501
@ -203,6 +203,8 @@ def main():
supports_check_mode=True, supports_check_mode=True,
required_if=[ required_if=[
('state', 'present', ['rule_type']), ('state', 'present', ['rule_type']),
('state', 'present', ['name']),
('state', 'absent', ['name']),
('rule_type', 'replicated', ['bucket_root', 'bucket_type']), ('rule_type', 'replicated', ['bucket_root', 'bucket_type']),
('rule_type', 'erasure', ['profile']) ('rule_type', 'erasure', ['profile'])
] ]
@ -230,8 +232,8 @@ def main():
# will return either the image name or None # will return either the image name or None
container_image = is_containerized() container_image = is_containerized()
rc, cmd, out, err = exec_command(module, get_rule(module, container_image=container_image)) # noqa: E501
if state == "present": if state == "present":
rc, cmd, out, err = exec_command(module, get_rule(module, container_image=container_image)) # noqa: E501
if rc != 0: if rc != 0:
rc, cmd, out, err = exec_command(module, create_rule(module, container_image=container_image)) # noqa: E501 rc, cmd, out, err = exec_command(module, create_rule(module, container_image=container_image)) # noqa: E501
changed = True changed = True
@ -239,18 +241,15 @@ def main():
rule = json.loads(out) rule = json.loads(out)
if (rule['type'] == 1 and rule_type == 'erasure') or (rule['type'] == 3 and rule_type == 'replicated'): # noqa: E501 if (rule['type'] == 1 and rule_type == 'erasure') or (rule['type'] == 3 and rule_type == 'replicated'): # noqa: E501
module.fail_json(msg="Can not convert crush rule {} to {}".format(name, rule_type), changed=False, rc=1) # noqa: E501 module.fail_json(msg="Can not convert crush rule {} to {}".format(name, rule_type), changed=False, rc=1) # noqa: E501
elif state == "absent": elif state == "absent":
rc, cmd, out, err = exec_command(module, get_rule(module, container_image=container_image)) # noqa: E501
if rc == 0: if rc == 0:
rc, cmd, out, err = exec_command(module, remove_rule(module, container_image=container_image)) # noqa: E501 rc, cmd, out, err = exec_command(module, remove_rule(module, container_image=container_image)) # noqa: E501
changed = True changed = True
else: else:
rc = 0 rc = 0
out = "Crush Rule {} doesn't exist".format(name) out = "Crush Rule {} doesn't exist".format(name)
else:
elif state == "info": pass
rc, cmd, out, err = exec_command(module, get_rule(module, container_image=container_image)) # noqa: E501
exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed) # noqa: E501 exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed) # noqa: E501

View File

@ -1,7 +1,6 @@
--- ---
- name: get current default crush rule details - name: get current default crush rule details
ceph_crush_rule: ceph_crush_rule:
name: ""
cluster: "{{ cluster }}" cluster: "{{ cluster }}"
state: info state: info
environment: environment: