| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Splunk Notes module utilities for Ansible. |
| 3 |
|
| 4 |
This module contains shared utilities for notes management: |
| 5 |
- Path builders for finding/investigation notes and response plan task notes |
| 6 |
- Mapping functions for API responses |
| 7 |
|
| 8 |
Only include functions and constants that are reusable. |
| 9 |
Pure Python functions with no Ansible dependencies for easy unit testing. |
| 10 |
""" |
| 11 |
|
| 12 |
# Copyright 2026 Red Hat Inc. |
| 13 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 14 |
|
| 15 |
|
| 16 |
from typing import Any, Optional |
| 17 |
|
| 18 |
from ansible_collections.splunk.es.plugins.module_utils.splunk_utils import ( |
| 19 |
DEFAULT_API_APP, |
| 20 |
DEFAULT_API_NAMESPACE, |
| 21 |
DEFAULT_API_USER, |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
# Target type constants |
| 26 |
TARGET_FINDING = "finding" |
| 27 |
TARGET_INVESTIGATION = "investigation" |
| 28 |
TARGET_RESPONSE_PLAN_TASK = "response_plan_task" |
| 29 |
|
| 30 |
# Required parameters for each target type |
| 31 |
TARGET_REQUIRED_PARAMS: dict[str, list[str]] = { |
| 32 |
TARGET_FINDING: ["finding_ref_id"], |
| 33 |
TARGET_INVESTIGATION: ["investigation_ref_id"], |
| 34 |
TARGET_RESPONSE_PLAN_TASK: [ |
| 35 |
"investigation_ref_id", |
| 36 |
"response_plan_id", |
| 37 |
"phase_id", |
| 38 |
"task_id", |
| 39 |
], |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
def validate_target_params(target_type: str, args: dict[str, Any]) -> Optional[str]: |
| 44 |
"""Validate required parameters based on target type. |
| 45 |
|
| 46 |
Args: |
| 47 |
target_type: The target type (finding, investigation, response_plan_task). |
| 48 |
args: The task arguments dictionary. |
| 49 |
|
| 50 |
Returns: |
| 51 |
Error message if validation fails, None if valid. |
| 52 |
""" |
| 53 |
required_params = TARGET_REQUIRED_PARAMS.get(target_type, []) |
| 54 |
missing = [param for param in required_params if not args.get(param)] |
| 55 |
|
| 56 |
if not missing: |
| 57 |
return None |
| 58 |
|
| 59 |
return f"Missing required parameters for target_type '{target_type}': {', '.join(missing)}" |
| 60 |
|
| 61 |
|
| 62 |
def build_notes_api_path( |
| 63 |
investigation_id: str, |
| 64 |
namespace: str = DEFAULT_API_NAMESPACE, |
| 65 |
user: str = DEFAULT_API_USER, |
| 66 |
app: str = DEFAULT_API_APP, |
| 67 |
) -> str: |
| 68 |
"""Build the notes API path for findings or investigations. |
| 69 |
|
| 70 |
This path is used for both findings and investigations. The difference |
| 71 |
is in the investigation_id parameter: |
| 72 |
- For findings: use the ref_id (e.g., uuid@@notable@@time{timestamp}) |
| 73 |
- For investigations: use the investigation UUID |
| 74 |
|
| 75 |
Args: |
| 76 |
investigation_id: The finding ref_id or investigation UUID. |
| 77 |
namespace: The namespace portion of the path. Defaults to 'servicesNS'. |
| 78 |
user: The user portion of the path. Defaults to 'nobody'. |
| 79 |
app: The app portion of the path. Defaults to 'missioncontrol'. |
| 80 |
|
| 81 |
Returns: |
| 82 |
The complete notes API path. |
| 83 |
""" |
| 84 |
return f"{namespace}/{user}/{app}/public/v2/investigations/{investigation_id}/notes" |
| 85 |
|
| 86 |
|
| 87 |
def build_note_api_path( |
| 88 |
investigation_id: str, |
| 89 |
note_id: str, |
| 90 |
namespace: str = DEFAULT_API_NAMESPACE, |
| 91 |
user: str = DEFAULT_API_USER, |
| 92 |
app: str = DEFAULT_API_APP, |
| 93 |
) -> str: |
| 94 |
"""Build the API path for a specific note. |
| 95 |
|
| 96 |
Args: |
| 97 |
investigation_id: The finding ref_id or investigation UUID. |
| 98 |
note_id: The note ID. |
| 99 |
namespace: The namespace portion of the path. Defaults to 'servicesNS'. |
| 100 |
user: The user portion of the path. Defaults to 'nobody'. |
| 101 |
app: The app portion of the path. Defaults to 'missioncontrol'. |
| 102 |
|
| 103 |
Returns: |
| 104 |
The API path for the specific note. |
| 105 |
""" |
| 106 |
base_path = build_notes_api_path(investigation_id, namespace, user, app) |
| 107 |
return f"{base_path}/{note_id}" |
| 108 |
|
| 109 |
|
| 110 |
def build_task_notes_api_path( |
| 111 |
investigation_id: str, |
| 112 |
response_plan_id: str, |
| 113 |
phase_id: str, |
| 114 |
task_id: str, |
| 115 |
namespace: str = DEFAULT_API_NAMESPACE, |
| 116 |
user: str = DEFAULT_API_USER, |
| 117 |
app: str = DEFAULT_API_APP, |
| 118 |
) -> str: |
| 119 |
"""Build the notes API path for response plan tasks. |
| 120 |
|
| 121 |
Args: |
| 122 |
investigation_id: The investigation UUID. |
| 123 |
response_plan_id: The applied response plan ID. |
| 124 |
phase_id: The phase ID. |
| 125 |
task_id: The task ID. |
| 126 |
namespace: The namespace portion of the path. Defaults to 'servicesNS'. |
| 127 |
user: The user portion of the path. Defaults to 'nobody'. |
| 128 |
app: The app portion of the path. Defaults to 'missioncontrol'. |
| 129 |
|
| 130 |
Returns: |
| 131 |
The complete task notes API path. |
| 132 |
""" |
| 133 |
return ( |
| 134 |
f"{namespace}/{user}/{app}/public/v2/investigations/{investigation_id}" |
| 135 |
f"/responseplans/{response_plan_id}/phase/{phase_id}/tasks/{task_id}/notes" |
| 136 |
) |
| 137 |
|
| 138 |
|
| 139 |
def build_task_note_api_path( |
| 140 |
investigation_id: str, |
| 141 |
response_plan_id: str, |
| 142 |
phase_id: str, |
| 143 |
task_id: str, |
| 144 |
note_id: str, |
| 145 |
namespace: str = DEFAULT_API_NAMESPACE, |
| 146 |
user: str = DEFAULT_API_USER, |
| 147 |
app: str = DEFAULT_API_APP, |
| 148 |
) -> str: |
| 149 |
"""Build the API path for a specific task note. |
| 150 |
|
| 151 |
Args: |
| 152 |
investigation_id: The investigation UUID. |
| 153 |
response_plan_id: The applied response plan ID. |
| 154 |
phase_id: The phase ID. |
| 155 |
task_id: The task ID. |
| 156 |
note_id: The note ID. |
| 157 |
namespace: The namespace portion of the path. Defaults to 'servicesNS'. |
| 158 |
user: The user portion of the path. Defaults to 'nobody'. |
| 159 |
app: The app portion of the path. Defaults to 'missioncontrol'. |
| 160 |
|
| 161 |
Returns: |
| 162 |
The API path for the specific task note. |
| 163 |
""" |
| 164 |
base_path = build_task_notes_api_path( |
| 165 |
investigation_id, |
| 166 |
response_plan_id, |
| 167 |
phase_id, |
| 168 |
task_id, |
| 169 |
namespace, |
| 170 |
user, |
| 171 |
app, |
| 172 |
) |
| 173 |
return f"{base_path}/{note_id}" |
| 174 |
|
| 175 |
|
| 176 |
def map_note_from_api(note: dict[str, Any]) -> dict[str, Any]: |
| 177 |
"""Convert a note from API format to module format. |
| 178 |
|
| 179 |
Args: |
| 180 |
note: Note dictionary from API response. |
| 181 |
|
| 182 |
Returns: |
| 183 |
Note in module format with normalized values. |
| 184 |
""" |
| 185 |
return { |
| 186 |
"note_id": note.get("id", ""), |
| 187 |
"content": note.get("content", ""), |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
def map_note_to_api(note: dict[str, Any]) -> dict[str, Any]: |
| 192 |
"""Convert a note from module format to API payload format. |
| 193 |
|
| 194 |
Args: |
| 195 |
note: Note dictionary with module parameters. |
| 196 |
|
| 197 |
Returns: |
| 198 |
Note payload formatted for the Splunk API. |
| 199 |
""" |
| 200 |
payload: dict[str, Any] = {} |
| 201 |
|
| 202 |
if "content" in note and note["content"] is not None: |
| 203 |
payload["content"] = note["content"] |
| 204 |
|
| 205 |
return payload |