| 1 |
# |
| 2 |
# Copyright 2022 Red Hat Inc. |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
# |
| 19 |
|
| 20 |
""" |
| 21 |
The module file for data_inputs_monitor |
| 22 |
""" |
| 23 |
|
| 24 |
from ansible.errors import AnsibleActionFail |
| 25 |
from ansible.module_utils.connection import Connection |
| 26 |
from ansible.module_utils.six.moves.urllib.parse import quote_plus |
| 27 |
from ansible.plugins.action import ActionBase |
| 28 |
|
| 29 |
from ansible_collections.splunk.es.plugins.module_utils import dict_utils as utils |
| 30 |
from ansible_collections.splunk.es.plugins.module_utils.splunk import ( |
| 31 |
SplunkRequest, |
| 32 |
check_argspec, |
| 33 |
) |
| 34 |
from ansible_collections.splunk.es.plugins.module_utils.splunk_utils import ( |
| 35 |
map_obj_to_params, |
| 36 |
map_params_to_obj, |
| 37 |
remove_get_keys_from_payload_dict, |
| 38 |
set_defaults, |
| 39 |
) |
| 40 |
from ansible_collections.splunk.es.plugins.modules.splunk_data_inputs_monitor import DOCUMENTATION |
| 41 |
|
| 42 |
|
| 43 |
class ActionModule(ActionBase): |
| 44 |
"""action module""" |
| 45 |
|
| 46 |
def __init__(self, *args, **kwargs): |
| 47 |
super().__init__(*args, **kwargs) |
| 48 |
self._result = None |
| 49 |
self.api_object = "servicesNS/nobody/search/data/inputs/monitor" |
| 50 |
self.module_name = "data_inputs_monitor" |
| 51 |
self.key_transform = { |
| 52 |
"blacklist": "blacklist", |
| 53 |
"check-index": "check_index", # not returned |
| 54 |
"check-path": "check_path", # not returned |
| 55 |
"crc-salt": "crc_salt", |
| 56 |
"disabled": "disabled", |
| 57 |
"followTail": "follow_tail", |
| 58 |
"host": "host", |
| 59 |
"host_regex": "host_regex", |
| 60 |
"host_segment": "host_segment", |
| 61 |
"ignore-older-than": "ignore_older_than", # not returned |
| 62 |
"index": "index", |
| 63 |
"name": "name", |
| 64 |
"recursive": "recursive", |
| 65 |
"rename-source": "rename_source", # not returned |
| 66 |
"sourcetype": "sourcetype", |
| 67 |
"time-before-close": "time_before_close", # not returned |
| 68 |
"whitelist": "whitelist", |
| 69 |
} |
| 70 |
|
| 71 |
def fail_json(self, msg): |
| 72 |
"""Replace the AnsibleModule fail_json here for stable-2.16 compatibility |
| 73 |
:param msg: The message for the failure |
| 74 |
:type msg: str |
| 75 |
""" |
| 76 |
msg = msg.replace("(basic.py)", self._task.action) |
| 77 |
raise AnsibleActionFail(msg) |
| 78 |
|
| 79 |
def map_params_to_object(self, config): |
| 80 |
res = {} |
| 81 |
res["name"] = config["name"] |
| 82 |
|
| 83 |
# splunk takes "crc-salt" as input parameter, and returns "crcSalt" in output |
| 84 |
# therefore we can't directly use mapping |
| 85 |
if config["content"].get("crcSalt"): |
| 86 |
config["content"]["crc-salt"] = config["content"]["crcSalt"] |
| 87 |
|
| 88 |
res.update(map_params_to_obj(config["content"], self.key_transform)) |
| 89 |
|
| 90 |
return res |
| 91 |
|
| 92 |
def search_for_resource_name(self, conn_request, directory_name): |
| 93 |
query_dict = conn_request.get_by_path( |
| 94 |
f"{self.api_object}/{quote_plus(directory_name)}", |
| 95 |
) |
| 96 |
|
| 97 |
search_result = {} |
| 98 |
|
| 99 |
if query_dict: |
| 100 |
search_result = self.map_params_to_object(query_dict["entry"][0]) |
| 101 |
|
| 102 |
return search_result |
| 103 |
|
| 104 |
def delete_module_api_config(self, conn_request, config): |
| 105 |
before = [] |
| 106 |
after = None |
| 107 |
changed = False |
| 108 |
for want_conf in config: |
| 109 |
search_by_name = self.search_for_resource_name( |
| 110 |
conn_request, |
| 111 |
want_conf["name"], |
| 112 |
) |
| 113 |
if search_by_name: |
| 114 |
before.append(search_by_name) |
| 115 |
if not self._task.check_mode: |
| 116 |
conn_request.delete_by_path( |
| 117 |
f"{self.api_object}/{quote_plus(want_conf['name'])}", |
| 118 |
) |
| 119 |
changed = True |
| 120 |
after = [] |
| 121 |
|
| 122 |
res_config = {} |
| 123 |
res_config["after"] = after |
| 124 |
res_config["before"] = before |
| 125 |
|
| 126 |
return res_config, changed |
| 127 |
|
| 128 |
def configure_module_api(self, conn_request, config): |
| 129 |
before = [] |
| 130 |
after = [] |
| 131 |
changed = False |
| 132 |
# Add to the THIS list for the value which needs to be excluded |
| 133 |
# from HAVE params when compared to WANT param like 'ID' can be |
| 134 |
# part of HAVE param but may not be part of your WANT param |
| 135 |
defaults = { |
| 136 |
"disabled": False, |
| 137 |
"host": "$decideOnStartup", |
| 138 |
"index": "default", |
| 139 |
} |
| 140 |
remove_from_diff_compare = [ |
| 141 |
"check_path", |
| 142 |
"check_index", |
| 143 |
"ignore_older_than", |
| 144 |
"time_before_close", |
| 145 |
"rename_source", |
| 146 |
] |
| 147 |
for want_conf in config: |
| 148 |
have_conf = self.search_for_resource_name( |
| 149 |
conn_request, |
| 150 |
want_conf["name"], |
| 151 |
) |
| 152 |
|
| 153 |
if have_conf: |
| 154 |
want_conf = set_defaults(want_conf, defaults) |
| 155 |
want_conf = utils.remove_empties(want_conf) |
| 156 |
diff = utils.dict_diff(have_conf, want_conf) |
| 157 |
|
| 158 |
# Check if have_conf has extra parameters |
| 159 |
if self._task.args["state"] == "replaced": |
| 160 |
diff2 = utils.dict_diff(want_conf, have_conf) |
| 161 |
if len(diff) or len(diff2): |
| 162 |
diff.update(diff2) |
| 163 |
|
| 164 |
if diff: |
| 165 |
diff = remove_get_keys_from_payload_dict( |
| 166 |
diff, |
| 167 |
remove_from_diff_compare, |
| 168 |
) |
| 169 |
if diff: |
| 170 |
before.append(have_conf) |
| 171 |
if self._task.args["state"] == "merged": |
| 172 |
want_conf = utils.remove_empties( |
| 173 |
utils.dict_merge(have_conf, want_conf), |
| 174 |
) |
| 175 |
want_conf = remove_get_keys_from_payload_dict( |
| 176 |
want_conf, |
| 177 |
remove_from_diff_compare, |
| 178 |
) |
| 179 |
changed = True |
| 180 |
|
| 181 |
if self._task.check_mode: |
| 182 |
# In check mode, return the expected configuration |
| 183 |
after.append(want_conf) |
| 184 |
else: |
| 185 |
payload = map_obj_to_params( |
| 186 |
want_conf, |
| 187 |
self.key_transform, |
| 188 |
) |
| 189 |
url = f"{self.api_object}/{quote_plus(payload.pop('name'))}" |
| 190 |
api_response = conn_request.create_update( |
| 191 |
url, |
| 192 |
data=payload, |
| 193 |
) |
| 194 |
response_json = self.map_params_to_object( |
| 195 |
api_response["entry"][0], |
| 196 |
) |
| 197 |
|
| 198 |
after.append(response_json) |
| 199 |
elif self._task.args["state"] == "replaced": |
| 200 |
if not self._task.check_mode: |
| 201 |
conn_request.delete_by_path( |
| 202 |
f"{self.api_object}/{quote_plus(want_conf['name'])}", |
| 203 |
) |
| 204 |
changed = True |
| 205 |
|
| 206 |
if self._task.check_mode: |
| 207 |
# In check mode, return the expected configuration |
| 208 |
after.append(want_conf) |
| 209 |
else: |
| 210 |
payload = map_obj_to_params( |
| 211 |
want_conf, |
| 212 |
self.key_transform, |
| 213 |
) |
| 214 |
api_response = conn_request.create_update( |
| 215 |
self.api_object, |
| 216 |
data=payload, |
| 217 |
) |
| 218 |
response_json = self.map_params_to_object( |
| 219 |
api_response["entry"][0], |
| 220 |
) |
| 221 |
|
| 222 |
after.append(response_json) |
| 223 |
else: |
| 224 |
before.append(have_conf) |
| 225 |
after.append(have_conf) |
| 226 |
else: |
| 227 |
before.append(have_conf) |
| 228 |
after.append(have_conf) |
| 229 |
else: |
| 230 |
changed = True |
| 231 |
want_conf = utils.remove_empties(want_conf) |
| 232 |
|
| 233 |
if self._task.check_mode: |
| 234 |
# In check mode, return the expected configuration |
| 235 |
after.extend(before) |
| 236 |
after.append(want_conf) |
| 237 |
else: |
| 238 |
payload = map_obj_to_params(want_conf, self.key_transform) |
| 239 |
api_response = conn_request.create_update( |
| 240 |
self.api_object, |
| 241 |
data=payload, |
| 242 |
) |
| 243 |
response_json = self.map_params_to_object( |
| 244 |
api_response["entry"][0], |
| 245 |
) |
| 246 |
|
| 247 |
after.extend(before) |
| 248 |
after.append(response_json) |
| 249 |
if not changed: |
| 250 |
after = None |
| 251 |
|
| 252 |
res_config = {} |
| 253 |
res_config["after"] = after |
| 254 |
res_config["before"] = before |
| 255 |
|
| 256 |
return res_config, changed |
| 257 |
|
| 258 |
def run(self, tmp=None, task_vars=None): |
| 259 |
self._supports_check_mode = True |
| 260 |
self._result = super().run(tmp, task_vars) |
| 261 |
|
| 262 |
if not check_argspec(self, self._result, DOCUMENTATION): |
| 263 |
return self._result |
| 264 |
|
| 265 |
# self._result[self.module_name] = {} |
| 266 |
|
| 267 |
config = self._task.args.get("config") |
| 268 |
|
| 269 |
conn = Connection(self._connection.socket_path) |
| 270 |
|
| 271 |
conn_request = SplunkRequest( |
| 272 |
action_module=self, |
| 273 |
connection=conn, |
| 274 |
not_rest_data_keys=["state"], |
| 275 |
) |
| 276 |
|
| 277 |
if self._task.args["state"] == "gathered": |
| 278 |
if config: |
| 279 |
self._result["gathered"] = [] |
| 280 |
self._result["changed"] = False |
| 281 |
for item in config: |
| 282 |
result = self.search_for_resource_name( |
| 283 |
conn_request, |
| 284 |
item["name"], |
| 285 |
) |
| 286 |
if result: |
| 287 |
self._result["gathered"].append(result) |
| 288 |
else: |
| 289 |
self._result["gathered"] = conn_request.get_by_path( |
| 290 |
self.api_object, |
| 291 |
)["entry"] |
| 292 |
elif self._task.args["state"] == "merged" or self._task.args["state"] == "replaced": |
| 293 |
( |
| 294 |
self._result[self.module_name], |
| 295 |
self._result["changed"], |
| 296 |
) = self.configure_module_api(conn_request, config) |
| 297 |
if self._result[self.module_name]["after"] is None: |
| 298 |
self._result[self.module_name].pop("after") |
| 299 |
|
| 300 |
elif self._task.args["state"] == "deleted": |
| 301 |
( |
| 302 |
self._result[self.module_name], |
| 303 |
self._result["changed"], |
| 304 |
) = self.delete_module_api_config(conn_request, config) |
| 305 |
if self._result[self.module_name]["after"] is None: |
| 306 |
self._result[self.module_name].pop("after") |
| 307 |
|
| 308 |
return self._result |