Skip to content
Snippets Groups Projects
Commit 5986de85 authored by Thomas Woerner's avatar Thomas Woerner
Browse files

topologysegment: Use IPAAnsibleModule class

ipaadmin_variables are handled by IPAAnsibleModule,
ansible_module.params_get is used to get the parameters and
ansible_module.ipa_connect is used to simplify the module.
parent 683d1c97
No related branches found
No related tags found
No related merge requests found
...@@ -31,13 +31,9 @@ DOCUMENTATION = """ ...@@ -31,13 +31,9 @@ DOCUMENTATION = """
module: ipatopologysegment module: ipatopologysegment
short description: Manage FreeIPA topology segments short description: Manage FreeIPA topology segments
description: Manage FreeIPA topology segments description: Manage FreeIPA topology segments
extends_documentation_fragment:
- ipamodule_base_docs
options: options:
ipaadmin_principal:
description: The admin principal
default: admin
ipaadmin_password:
description: The admin password
required: false
suffix: suffix:
description: Topology suffix description: Topology suffix
required: true required: true
...@@ -67,35 +63,41 @@ author: ...@@ -67,35 +63,41 @@ author:
EXAMPLES = """ EXAMPLES = """
- ipatopologysegment: - ipatopologysegment:
ipaadmin_password: SomeADMINpassword
suffix: domain suffix: domain
left: ipaserver.test.local left: ipaserver.test.local
right: ipareplica1.test.local right: ipareplica1.test.local
state: present state: present
- ipatopologysegment: - ipatopologysegment:
ipaadmin_password: SomeADMINpassword
suffix: domain suffix: domain
name: ipaserver.test.local-to-replica1.test.local name: ipaserver.test.local-to-replica1.test.local
state: absent state: absent
- ipatopologysegment: - ipatopologysegment:
ipaadmin_password: SomeADMINpassword
suffix: domain suffix: domain
left: ipaserver.test.local left: ipaserver.test.local
right: ipareplica1.test.local right: ipareplica1.test.local
state: absent state: absent
- ipatopologysegment: - ipatopologysegment:
ipaadmin_password: SomeADMINpassword
suffix: ca suffix: ca
name: ipaserver.test.local-to-replica1.test.local name: ipaserver.test.local-to-replica1.test.local
direction: left-to-right direction: left-to-right
state: reinitialized state: reinitialized
- ipatopologysegment: - ipatopologysegment:
ipaadmin_password: SomeADMINpassword
suffix: domain+ca suffix: domain+ca
left: ipaserver.test.local left: ipaserver.test.local
right: ipareplica1.test.local right: ipareplica1.test.local
state: absent state: absent
- ipatopologysegment: - ipatopologysegment:
ipaadmin_password: SomeADMINpassword
suffix: domain+ca suffix: domain+ca
left: ipaserver.test.local left: ipaserver.test.local
right: ipareplica1.test.local right: ipareplica1.test.local
...@@ -113,19 +115,16 @@ not-found: ...@@ -113,19 +115,16 @@ not-found:
type: list type: list
""" """
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.ansible_freeipa_module import IPAAnsibleModule
from ansible.module_utils._text import to_text
from ansible.module_utils.ansible_freeipa_module import temp_kinit, \
temp_kdestroy, valid_creds, api_connect, api_command
def find_left_right(module, suffix, left, right): def find_left_right(module, suffix, left, right):
_args = { _args = {
"iparepltoposegmentleftnode": to_text(left), "iparepltoposegmentleftnode": left,
"iparepltoposegmentrightnode": to_text(right), "iparepltoposegmentrightnode": right,
} }
_result = api_command(module, "topologysegment_find", _result = module.ipa_command("topologysegment_find",
to_text(suffix), _args) suffix, _args)
if len(_result["result"]) > 1: if len(_result["result"]) > 1:
module.fail_json( module.fail_json(
msg="Combination of left node '%s' and right node '%s' is " msg="Combination of left node '%s' and right node '%s' is "
...@@ -138,10 +137,10 @@ def find_left_right(module, suffix, left, right): ...@@ -138,10 +137,10 @@ def find_left_right(module, suffix, left, right):
def find_cn(module, suffix, name): def find_cn(module, suffix, name):
_args = { _args = {
"cn": to_text(name), "cn": name,
} }
_result = api_command(module, "topologysegment_find", _result = module.ipa_command("topologysegment_find",
to_text(suffix), _args) suffix, _args)
if len(_result["result"]) > 1: if len(_result["result"]) > 1:
module.fail_json( module.fail_json(
msg="CN '%s' is not unique for suffix '%s'" % (name, suffix)) msg="CN '%s' is not unique for suffix '%s'" % (name, suffix))
...@@ -156,7 +155,7 @@ def find_left_right_cn(module, suffix, left, right, name): ...@@ -156,7 +155,7 @@ def find_left_right_cn(module, suffix, left, right, name):
left_right = find_left_right(module, suffix, left, right) left_right = find_left_right(module, suffix, left, right)
if left_right is not None: if left_right is not None:
if name is not None and \ if name is not None and \
left_right["cn"][0] != to_text(name): left_right["cn"][0] != name:
module.fail_json( module.fail_json(
msg="Left and right nodes do not match " msg="Left and right nodes do not match "
"given name name (cn) '%s'" % name) "given name name (cn) '%s'" % name)
...@@ -174,10 +173,8 @@ def find_left_right_cn(module, suffix, left, right, name): ...@@ -174,10 +173,8 @@ def find_left_right_cn(module, suffix, left, right, name):
def main(): def main():
ansible_module = AnsibleModule( ansible_module = IPAAnsibleModule(
argument_spec=dict( argument_spec=dict(
ipaadmin_principal=dict(type="str", default="admin"),
ipaadmin_password=dict(type="str", required=False, no_log=True),
suffix=dict(choices=["domain", "ca", "domain+ca"], required=True), suffix=dict(choices=["domain", "ca", "domain+ca"], required=True),
name=dict(type="str", aliases=["cn"], default=None), name=dict(type="str", aliases=["cn"], default=None),
left=dict(type="str", aliases=["leftnode"], default=None), left=dict(type="str", aliases=["leftnode"], default=None),
...@@ -195,14 +192,12 @@ def main(): ...@@ -195,14 +192,12 @@ def main():
# Get parameters # Get parameters
ipaadmin_principal = ansible_module.params.get("ipaadmin_principal") suffixes = ansible_module.params_get("suffix")
ipaadmin_password = ansible_module.params.get("ipaadmin_password") name = ansible_module.params_get("name")
suffixes = ansible_module.params.get("suffix") left = ansible_module.params_get("left")
name = ansible_module.params.get("name") right = ansible_module.params_get("right")
left = ansible_module.params.get("left") direction = ansible_module.params_get("direction")
right = ansible_module.params.get("right") state = ansible_module.params_get("state")
direction = ansible_module.params.get("direction")
state = ansible_module.params.get("state")
# Check parameters # Check parameters
...@@ -214,14 +209,8 @@ def main(): ...@@ -214,14 +209,8 @@ def main():
changed = False changed = False
exit_args = {} exit_args = {}
ccache_dir = None
ccache_name = None
try:
if not valid_creds(ansible_module, ipaadmin_principal):
ccache_dir, ccache_name = temp_kinit(ipaadmin_principal,
ipaadmin_password)
api_connect()
with ansible_module.ipa_connect():
commands = [] commands = []
for suffix in suffixes.split("+"): for suffix in suffixes.split("+"):
...@@ -233,17 +222,17 @@ def main(): ...@@ -233,17 +222,17 @@ def main():
ansible_module.fail_json( ansible_module.fail_json(
msg="Left and right need to be set.") msg="Left and right need to be set.")
args = { args = {
"iparepltoposegmentleftnode": to_text(left), "iparepltoposegmentleftnode": left,
"iparepltoposegmentrightnode": to_text(right), "iparepltoposegmentrightnode": right,
} }
if name is not None: if name is not None:
args["cn"] = to_text(name) args["cn"] = name
res_left_right = find_left_right(ansible_module, suffix, res_left_right = find_left_right(ansible_module, suffix,
left, right) left, right)
if res_left_right is not None: if res_left_right is not None:
if name is not None and \ if name is not None and \
res_left_right["cn"][0] != to_text(name): res_left_right["cn"][0] != name:
ansible_module.fail_json( ansible_module.fail_json(
msg="Left and right nodes already used with " msg="Left and right nodes already used with "
"different name (cn) '%s'" % res_left_right["cn"]) "different name (cn) '%s'" % res_left_right["cn"])
...@@ -260,7 +249,7 @@ def main(): ...@@ -260,7 +249,7 @@ def main():
# else: Nothing to change # else: Nothing to change
else: else:
if name is None: if name is None:
args["cn"] = to_text("%s-to-%s" % (left, right)) args["cn"] = "%s-to-%s" % (left, right)
commands.append(["topologysegment_add", args, suffix]) commands.append(["topologysegment_add", args, suffix])
elif state in ["absent", "disabled"]: elif state in ["absent", "disabled"]:
...@@ -333,15 +322,9 @@ def main(): ...@@ -333,15 +322,9 @@ def main():
# Execute command # Execute command
for command, args, _suffix in commands: for command, args, _suffix in commands:
api_command(ansible_module, command, to_text(_suffix), args) ansible_module.ipa_command(command, _suffix, args)
changed = True changed = True
except Exception as e:
ansible_module.fail_json(msg=str(e))
finally:
temp_kdestroy(ccache_dir, ccache_name)
# Done # Done
ansible_module.exit_json(changed=changed, **exit_args) ansible_module.exit_json(changed=changed, **exit_args)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment