Debian Codegraph @

ansible_collections/splunk/es/plugins/module_utils/response_plan_execution.py

raw
1 # -*- coding: utf-8 -*-
2 """Splunk Response Plan Execution module utilities for Ansible.
3
4 This module contains shared utilities for response plan execution modules:
5 - splunk_response_plan_execution (apply/remove/manage tasks)
6 - splunk_response_plan_execution_info (query applied plans/tasks/statuses)
7
8 Only include functions and constants that are reusable by both modules.
9 """
10
11 # Copyright 2026 Red Hat Inc.
12 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
13
14
15 from typing import Any
16 from urllib.parse import unquote
17
18
19 # Task status mappings: API value -> module value (for reading from API)
20 TASK_STATUS_FROM_API = {
21 "Started": "started",
22 "Ended": "ended",
23 "Reopened": "reopened",
24 "Pending": "pending",
25 }
26
27
28 def map_task_from_api(task: dict[str, Any]) -> dict[str, Any]:
29 """Convert a task from API format to module format.
30
31 Args:
32 task: Task dictionary from API response.
33
34 Returns:
35 Task in module format with normalized values.
36 """
37 # Decode URL-encoded strings from API
38 result = {
39 "id": task.get("id", ""),
40 "name": unquote(task.get("name", "")),
41 "description": unquote(task.get("description", "")),
42 "owner": task.get("owner", "unassigned"),
43 "is_note_required": task.get("is_note_required", False),
44 }
45
46 # Convert status from API format
47 api_status = task.get("status", "")
48 result["status"] = TASK_STATUS_FROM_API.get(
49 api_status,
50 api_status.lower() if api_status else "",
51 )
52
53 return result
54
55
56 def map_phase_from_api(phase: dict[str, Any]) -> dict[str, Any]:
57 """Convert a phase from API format to module format.
58
59 Args:
60 phase: Phase dictionary from API response.
61
62 Returns:
63 Phase in module format with tasks converted.
64 """
65 tasks = []
66 for task in phase.get("tasks", []) or []:
67 tasks.append(map_task_from_api(task))
68
69 # Decode URL-encoded strings from API
70 return {
71 "id": phase.get("id", ""),
72 "name": unquote(phase.get("name", "")),
73 "tasks": tasks,
74 }
75
76
77 def map_applied_response_plan_from_api(plan: dict[str, Any]) -> dict[str, Any]:
78 """Convert an applied response plan from API format to module format.
79
80 Args:
81 plan: Applied response plan dictionary from API response.
82
83 Returns:
84 Applied response plan in module format.
85 """
86 phases = []
87 for phase in plan.get("phases", []) or []:
88 phases.append(map_phase_from_api(phase))
89
90 # API returns 'template_id' in GET response, 'source_template_id' in POST response
91 template_id = plan.get("source_template_id") or plan.get("template_id", "")
92
93 # Decode URL-encoded strings from API
94 return {
95 "id": plan.get("id", ""),
96 "name": unquote(plan.get("name", "")),
97 "description": unquote(plan.get("description", "")),
98 "source_template_id": template_id,
99 "phases": phases,
100 }