mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
[PR #11728/2acb20be backport][stable-12] opendj_backendprop: use CmdRunner (#11731)
opendj_backendprop: use CmdRunner (#11728)
* opendj_backendprop: use CmdRunner
* add changelog frag
(cherry picked from commit 2acb20bec2)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
minor_changes:
|
||||||
|
- opendj_backendprop - refactor to use ``CmdRunner`` (https://github.com/ansible-collections/community.general/pull/11728).
|
||||||
@@ -91,52 +91,45 @@ RETURN = r"""
|
|||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
|
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
|
||||||
|
|
||||||
|
|
||||||
class BackendProp:
|
class BackendProp:
|
||||||
def __init__(self, module):
|
def __init__(self, module):
|
||||||
self._module = module
|
self._module = module
|
||||||
|
self.runner = CmdRunner(
|
||||||
|
module,
|
||||||
|
command=f"{module.params['opendj_bindir']}/dsconfig",
|
||||||
|
arg_formats=dict(
|
||||||
|
get_cmd=cmd_runner_fmt.as_fixed(["get-backend-prop", "-n", "-X", "-s"]),
|
||||||
|
set_cmd=cmd_runner_fmt.as_fixed(["set-backend-prop", "-n", "-X"]),
|
||||||
|
hostname=cmd_runner_fmt.as_opt_val("-h"),
|
||||||
|
port=cmd_runner_fmt.as_opt_val("--port"),
|
||||||
|
username=cmd_runner_fmt.as_opt_val("--bindDN"),
|
||||||
|
password=cmd_runner_fmt.as_opt_val("-w"),
|
||||||
|
passwordfile=cmd_runner_fmt.as_opt_val("-j"),
|
||||||
|
backend=cmd_runner_fmt.as_opt_val("--backend-name"),
|
||||||
|
set_prop=cmd_runner_fmt.as_func(cmd_runner_fmt.unpack_args(lambda n, v: ["--set", f"{n}:{v}"])),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
def get_property(self, opendj_bindir, hostname, port, username, password_method, backend_name):
|
def get_property(self):
|
||||||
my_command = [
|
with self.runner(
|
||||||
f"{opendj_bindir}/dsconfig",
|
"get_cmd hostname port username password passwordfile backend",
|
||||||
"get-backend-prop",
|
check_rc=True,
|
||||||
"-h",
|
) as ctx:
|
||||||
hostname,
|
rc, stdout, stderr = ctx.run()
|
||||||
"--port",
|
|
||||||
str(port),
|
|
||||||
"--bindDN",
|
|
||||||
username,
|
|
||||||
"--backend-name",
|
|
||||||
backend_name,
|
|
||||||
"-n",
|
|
||||||
"-X",
|
|
||||||
"-s",
|
|
||||||
] + password_method
|
|
||||||
rc, stdout, stderr = self._module.run_command(my_command, check_rc=True)
|
|
||||||
return stdout
|
return stdout
|
||||||
|
|
||||||
def set_property(self, opendj_bindir, hostname, port, username, password_method, backend_name, name, value):
|
def set_property(self, name, value):
|
||||||
my_command = [
|
with self.runner(
|
||||||
f"{opendj_bindir}/dsconfig",
|
"set_cmd hostname port username password passwordfile backend set_prop",
|
||||||
"set-backend-prop",
|
check_rc=True,
|
||||||
"-h",
|
) as ctx:
|
||||||
hostname,
|
ctx.run(set_prop=[name, value])
|
||||||
"--port",
|
|
||||||
str(port),
|
|
||||||
"--bindDN",
|
|
||||||
username,
|
|
||||||
"--backend-name",
|
|
||||||
backend_name,
|
|
||||||
"--set",
|
|
||||||
f"{name}:{value}",
|
|
||||||
"-n",
|
|
||||||
"-X",
|
|
||||||
] + password_method
|
|
||||||
self._module.run_command(my_command, check_rc=True)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def validate_data(self, data=None, name=None, value=None):
|
def validate_data(self, data, name, value):
|
||||||
for config_line in data.split("\n"):
|
for config_line in data.splitlines():
|
||||||
if config_line:
|
if config_line:
|
||||||
split_line = config_line.split()
|
split_line = config_line.split()
|
||||||
if split_line[0] == name:
|
if split_line[0] == name:
|
||||||
@@ -164,51 +157,14 @@ def main():
|
|||||||
required_one_of=[["password", "passwordfile"]],
|
required_one_of=[["password", "passwordfile"]],
|
||||||
)
|
)
|
||||||
|
|
||||||
opendj_bindir = module.params["opendj_bindir"]
|
|
||||||
hostname = module.params["hostname"]
|
|
||||||
port = module.params["port"]
|
|
||||||
username = module.params["username"]
|
|
||||||
password = module.params["password"]
|
|
||||||
passwordfile = module.params["passwordfile"]
|
|
||||||
backend_name = module.params["backend"]
|
|
||||||
name = module.params["name"]
|
|
||||||
value = module.params["value"]
|
|
||||||
# state = module.params["state"] TODO - ???
|
|
||||||
|
|
||||||
if module.params["password"] is not None:
|
|
||||||
password_method = ["-w", password]
|
|
||||||
elif module.params["passwordfile"] is not None:
|
|
||||||
password_method = ["-j", passwordfile]
|
|
||||||
|
|
||||||
opendj = BackendProp(module)
|
opendj = BackendProp(module)
|
||||||
validate = opendj.get_property(
|
stdout = opendj.get_property()
|
||||||
opendj_bindir=opendj_bindir,
|
|
||||||
hostname=hostname,
|
|
||||||
port=port,
|
|
||||||
username=username,
|
|
||||||
password_method=password_method,
|
|
||||||
backend_name=backend_name,
|
|
||||||
)
|
|
||||||
|
|
||||||
if validate:
|
if stdout and not opendj.validate_data(data=stdout, name=module.params["name"], value=module.params["value"]):
|
||||||
if not opendj.validate_data(data=validate, name=name, value=value):
|
if module.check_mode:
|
||||||
if module.check_mode:
|
module.exit_json(changed=True)
|
||||||
module.exit_json(changed=True)
|
opendj.set_property(module.params["name"], module.params["value"])
|
||||||
if opendj.set_property(
|
module.exit_json(changed=True)
|
||||||
opendj_bindir=opendj_bindir,
|
|
||||||
hostname=hostname,
|
|
||||||
port=port,
|
|
||||||
username=username,
|
|
||||||
password_method=password_method,
|
|
||||||
backend_name=backend_name,
|
|
||||||
name=name,
|
|
||||||
value=value,
|
|
||||||
):
|
|
||||||
module.exit_json(changed=True)
|
|
||||||
else:
|
|
||||||
module.exit_json(changed=False)
|
|
||||||
else:
|
|
||||||
module.exit_json(changed=False)
|
|
||||||
else:
|
else:
|
||||||
module.exit_json(changed=False)
|
module.exit_json(changed=False)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user