Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/modules/ome_alert_policies_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 #
5 # Dell OpenManage Ansible Modules
6 # Version 9.3.0
7 # Copyright (C) 2023-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
8
9 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
10 #
11
12
13 from __future__ import (absolute_import, division, print_function)
14 __metaclass__ = type
15
16 DOCUMENTATION = r'''
17 ---
18 module: ome_alert_policies_info
19 short_description: Retrieves information of one or more OME alert policies.
20 version_added: "8.2.0"
21 description:
22 - This module retrieves the information of alert policies for OpenManage Enterprise
23 and OpenManage Enterprise Modular.
24 - A list of information about a specific OME alert policy using the policy name.
25 - A list of all the OME alert policies with their information when the policy name is not provided.
26 extends_documentation_fragment:
27 - dellemc.openmanage.ome_auth_options
28 options:
29 policy_name:
30 description: Name of the policy.
31 type: str
32 requirements:
33 - "python >= 3.9.6"
34 author: "Abhishek Sinha(@ABHISHEK-SINHA10)"
35 notes:
36 - Run this module from a system that has direct access to Dell OpenManage Enterprise
37 or OpenManage Enterprise Modular.
38 - This module supports both IPv4 and IPv6 addresses.
39 - This module supports C(check_mode).
40 '''
41
42 EXAMPLES = """
43 ---
44 - name: Retrieve information about all OME alert policies.
45 dellemc.openmanage.ome_alert_policies_info:
46 hostname: "192.168.0.1"
47 username: "username"
48 password: "password"
49 ca_path: "/path/to/ca_cert.pem"
50
51 - name: Retrieve information about a specific OME alert policy using the policy name.
52 dellemc.openmanage.ome_alert_policies_info:
53 hostname: "192.168.0.1"
54 username: "username"
55 password: "password"
56 ca_path: "/path/to/ca_cert.pem"
57 policy_name: "Mobile Push Notification - Critical Alerts"
58 """
59
60 RETURN = '''
61 ---
62 msg:
63 type: str
64 description: Status of the alert policies info fetch operation.
65 returned: always
66 sample: "Successfully retrieved all the OME alert policies information."
67 policies:
68 type: list
69 description: Retrieve information about all the OME alert policies.
70 returned: success
71 sample: [
72 {
73 "Id": 10006,
74 "Name": "Mobile Push Notification - Critical Alerts",
75 "Description": "This policy is applicable to critical alerts. Associated actions will be taken when a critical alert is received.",
76 "Enabled": true,
77 "DefaultPolicy": true,
78 "PolicyData": {
79 "Catalogs": [],
80 "Severities": [
81 16
82 ],
83 "MessageIds": [],
84 "Devices": [],
85 "DeviceTypes": [],
86 "Groups": [],
87 "AllTargets": false,
88 "Schedule": {
89 "StartTime": null,
90 "EndTime": null,
91 "CronString": null,
92 "Interval": false
93 },
94 "Actions": [
95 {
96 "Id": 5,
97 "Name": "Mobile",
98 "ParameterDetails": [],
99 "TemplateId": 112
100 }
101 ],
102 "UndiscoveredTargets": []
103 },
104 "State": true,
105 "Visible": true,
106 "Owner": null,
107 }
108 ]
109 error_info:
110 description: Details of the HTTP Error.
111 returned: on HTTP error
112 type: dict
113 sample: {
114 "error": {
115 "code": "Base.1.0.GeneralError",
116 "message": "A general error has occurred. See ExtendedInfo for more information.",
117 "@Message.ExtendedInfo": [
118 {
119 "MessageId": "GEN1234",
120 "RelatedProperties": [],
121 "Message": "Unable to process the request because an error occurred.",
122 "MessageArgs": [],
123 "Severity": "Critical",
124 "Resolution": "Retry the operation. If the issue persists, contact your system administrator."
125 }
126 ]
127 }
128 }
129 '''
130
131 import json
132 from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule
133 from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import remove_key
134 from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
135 from ansible.module_utils.urls import ConnectionError, SSLValidationError
136
137
138 ALERT_POLICY_URI = "AlertService/AlertPolicies"
139 MODULE_SUCCESS_MESSAGE_ALL = "Successfully retrieved all the OME alert policies information."
140 MODULE_SUCCESS_MESSAGE_SPECIFIC = "Successfully retrieved {0} OME alert policy information."
141 POLICY_NAME_NOT_FOUND_OR_EMPTY = "The OME alert policy name {0} provided does not exist or empty."
142
143
144 class OMEAlertPolicyInfo:
145
146 def __init__(self) -> None:
147 self.module = get_module_parameters()
148
149 def get_all_alert_policy_info(self, rest_obj) -> dict:
150 resp = rest_obj.invoke_request("GET", ALERT_POLICY_URI)
151 value = resp.json_data["value"]
152 output_all = {'msg': MODULE_SUCCESS_MESSAGE_ALL, 'value': remove_key(value)}
153 return output_all
154
155 def get_alert_policy_info(self, rest_obj) -> dict:
156 policy_name = self.module.params.get("policy_name")
157 if policy_name is not None:
158 output_not_found_or_empty = {'msg': POLICY_NAME_NOT_FOUND_OR_EMPTY.format(policy_name),
159 'value': []}
160 if policy_name == "":
161 return output_not_found_or_empty
162 policies = self.get_all_alert_policy_info(rest_obj)
163 for each_element in policies["value"]:
164 if each_element["Name"] == policy_name:
165 output_specific = {'msg': MODULE_SUCCESS_MESSAGE_SPECIFIC.format(policy_name),
166 'value': [each_element]}
167 return output_specific
168 return output_not_found_or_empty
169 return self.get_all_alert_policy_info(rest_obj)
170
171 def perform_module_operation(self) -> None:
172 try:
173 with RestOME(self.module.params, req_session=True) as rest_obj:
174 result = self.get_alert_policy_info(rest_obj)
175 self.module.exit_json(msg=result['msg'], policies=result['value'])
176 except HTTPError as err:
177 self.module.exit_json(msg=str(err), error_info=json.load(err), failed=True)
178 except URLError as err:
179 self.module.exit_json(msg=str(err), unreachable=True)
180 except (SSLValidationError, ConnectionError, TypeError, ValueError, OSError) as err:
181 self.module.exit_json(msg=str(err), failed=True)
182
183
184 def get_module_parameters() -> OmeAnsibleModule:
185 specs = {
186 "policy_name": {"type": 'str'}
187 }
188
189 module = OmeAnsibleModule(argument_spec=specs,
190 supports_check_mode=True)
191 return module
192
193
194 def main():
195 obj = OMEAlertPolicyInfo()
196 obj.perform_module_operation()
197
198
199 if __name__ == '__main__':
200 main()