Debian Codegraph @

ansible_collections/community/general/plugins/modules/opendj_backendprop.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2016, Werner Dijkerman ([email protected])
3 # Copyright (c) 2017, Ansible Project
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: opendj_backendprop
11 short_description: Update the backend configuration of OpenDJ using the dsconfig set-backend-prop command
12 description:
13 - This module updates settings for OpenDJ with the command C(set-backend-prop).
14 - It checks first using C(get-backend-prop) if configuration needs to be applied.
15 author:
16 - Werner Dijkerman (@dj-wasabi)
17 extends_documentation_fragment:
18 - community.general._attributes
19 attributes:
20 check_mode:
21 support: full
22 diff_mode:
23 support: none
24 options:
25 opendj_bindir:
26 description:
27 - The path to the bin directory of OpenDJ.
28 default: /opt/opendj/bin
29 type: path
30 hostname:
31 description:
32 - The hostname of the OpenDJ server.
33 required: true
34 type: str
35 port:
36 description:
37 - The Admin port on which the OpenDJ instance is available.
38 required: true
39 type: str
40 username:
41 description:
42 - The username to connect with.
43 default: cn=Directory Manager
44 type: str
45 password:
46 description:
47 - The password for O(username).
48 - Either O(password) or O(passwordfile) is needed.
49 type: str
50 passwordfile:
51 description:
52 - Location to the password file which holds the password for O(username).
53 - Either O(password) or O(passwordfile) is needed.
54 type: path
55 backend:
56 description:
57 - The name of the backend on which the property needs to be updated.
58 required: true
59 type: str
60 name:
61 description:
62 - The configuration setting to update.
63 required: true
64 type: str
65 value:
66 description:
67 - The value for the configuration item.
68 required: true
69 type: str
70 state:
71 description:
72 - If configuration needs to be added/updated.
73 default: "present"
74 type: str
75 """
76
77 EXAMPLES = r"""
78 - name: Add or update OpenDJ backend properties
79 opendj_backendprop:
80 hostname: localhost
81 port: 4444
82 username: "cn=Directory Manager"
83 password: password
84 backend: userRoot
85 name: index-entry-limit
86 value: 5000
87 """
88
89 RETURN = r"""
90 """
91
92 from ansible.module_utils.basic import AnsibleModule
93
94 from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt
95
96
97 class BackendProp:
98 def __init__(self, module):
99 self._module = module
100 self.runner = CmdRunner(
101 module,
102 command=f"{module.params['opendj_bindir']}/dsconfig",
103 arg_formats=dict(
104 get_cmd=cmd_runner_fmt.as_fixed(["get-backend-prop", "-n", "-X", "-s"]),
105 set_cmd=cmd_runner_fmt.as_fixed(["set-backend-prop", "-n", "-X"]),
106 hostname=cmd_runner_fmt.as_opt_val("-h"),
107 port=cmd_runner_fmt.as_opt_val("--port"),
108 username=cmd_runner_fmt.as_opt_val("--bindDN"),
109 password=cmd_runner_fmt.as_opt_val("-w"),
110 passwordfile=cmd_runner_fmt.as_opt_val("-j"),
111 backend=cmd_runner_fmt.as_opt_val("--backend-name"),
112 set_prop=cmd_runner_fmt.as_func(cmd_runner_fmt.unpack_args(lambda n, v: ["--set", f"{n}:{v}"])),
113 ),
114 )
115
116 def get_property(self):
117 with self.runner(
118 "get_cmd hostname port username password passwordfile backend",
119 check_rc=True,
120 ) as ctx:
121 rc, stdout, stderr = ctx.run()
122 return stdout
123
124 def set_property(self, name, value):
125 with self.runner(
126 "set_cmd hostname port username password passwordfile backend set_prop",
127 check_rc=True,
128 ) as ctx:
129 ctx.run(set_prop=[name, value])
130
131 def validate_data(self, data, name, value):
132 for config_line in data.splitlines():
133 if config_line:
134 split_line = config_line.split()
135 if split_line[0] == name:
136 if split_line[1] == value:
137 return True
138 return False
139
140
141 def main():
142 module = AnsibleModule(
143 argument_spec=dict(
144 opendj_bindir=dict(default="/opt/opendj/bin", type="path"),
145 hostname=dict(required=True),
146 port=dict(required=True),
147 username=dict(default="cn=Directory Manager"),
148 password=dict(no_log=True),
149 passwordfile=dict(type="path"),
150 backend=dict(required=True),
151 name=dict(required=True),
152 value=dict(required=True),
153 state=dict(default="present"),
154 ),
155 supports_check_mode=True,
156 mutually_exclusive=[["password", "passwordfile"]],
157 required_one_of=[["password", "passwordfile"]],
158 )
159
160 opendj = BackendProp(module)
161 stdout = opendj.get_property()
162
163 if stdout and not opendj.validate_data(data=stdout, name=module.params["name"], value=module.params["value"]):
164 if module.check_mode:
165 module.exit_json(changed=True)
166 opendj.set_property(module.params["name"], module.params["value"])
167 module.exit_json(changed=True)
168 else:
169 module.exit_json(changed=False)
170
171
172 if __name__ == "__main__":
173 main()