Debian Codegraph @

ansible_collections/netapp/storagegrid/plugins/modules/na_sg_grid_autosupport.py

raw
1 #!/usr/bin/python
2
3 # (c) 2025, NetApp Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 """NetApp StorageGRID - Manage Autosupport configuration"""
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12
13 ANSIBLE_METADATA = {
14 "metadata_version": "1.0",
15 "status": ["preview"],
16 "supported_by": "community",
17 }
18
19
20 DOCUMENTATION = """
21 module: na_sg_grid_autosupport
22 short_description: Configure autosupport on StorageGRID.
23 extends_documentation_fragment:
24 - netapp.storagegrid.netapp.sg
25 version_added: '21.15.0'
26 author: NetApp Ansible Team (@vinaykus) <[email protected]>
27 description:
28 - Configure autosupport on NetApp StorageGRID.
29 options:
30 state:
31 description:
32 - The alert receiver should be present.
33 choices: ['present']
34 default: 'present'
35 type: str
36 aod_enable:
37 description:
38 - Enable or disable sending Autosupport on Demand.
39 type: bool
40 available_updates_enable:
41 description:
42 - Whether to check for software updates.
43 type: bool
44 event_enable:
45 description:
46 - Enable event-triggered AutoSupport messages.
47 type: bool
48 cert_enable:
49 description:
50 - Enable AutoSupport certificate validation.
51 type: bool
52 destinations:
53 description:
54 - AutoSupport destinations.
55 type: list
56 elements: dict
57 suboptions:
58 hostname:
59 description:
60 - Hostname of the destination.
61 type: str
62 required: true
63 port:
64 description:
65 - The port to use to connect to the ASUP destination.
66 type: int
67 required: true
68 ca_cert:
69 description:
70 - The public CA certificate bundle in Privacy-Enhanced Mail (PEM) format.
71 type: str
72 transport:
73 description:
74 - Protocol used for AutoSupport messages.
75 - If you use HTTP, use a proxy to forward the data as HTTPS. NetApp's AutoSupport servers will reject HTTP messages.
76 choices: ['SMTP', 'HTTP', 'HTTPS']
77 type: str
78 weekly_enable:
79 description:
80 - Enable weekly AutoSupport messages.
81 type: bool
82 """
83
84 EXAMPLES = """
85 - name: Configure autosupport
86 netapp.storagegrid.na_sg_grid_autosupport:
87 state: present
88 aod_enable: true
89 available_updates_enable: true
90 event_enable: true
91 cert_enable: true
92 transport: SMTP
93 weekly_enable: true
94
95 - name: Configure autosupport with destinations
96 netapp.storagegrid.na_sg_grid_autosupport:
97 state: present
98 aod_enable: true
99 available_updates_enable: true
100 event_enable: true
101 cert_enable: true
102 destinations:
103 - hostname: "example.com"
104 port: 443
105 ca_cert: "<CA bundle in PEM-encoding>"
106 transport: HTTPS
107 weekly_enable: true
108 """
109
110 RETURN = """
111 resp:
112 description: Returns information about the StorageGRID autosupport.
113 returned: If state is 'present'.
114 type: dict
115 sample: {
116 "aodEnable": true,
117 "availableUpdatesEnable": true,
118 "eventEnable": true,
119 "certEnable": true,
120 "destinations": [
121 {
122 "hostname": "example.com",
123 "port": 443,
124 "caCert": Null
125 }
126 ],
127 "transport": "SMTP",
128 "weeklyEnable": true
129 }
130 """
131
132 import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils
133 from ansible.module_utils.basic import AnsibleModule
134 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule
135 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI
136
137
138 class SgAutosupport:
139 """
140 Configure autosupport for StorageGRID
141 """
142
143 def __init__(self):
144 """
145 Parse arguments, setup state variables,
146 check parameters and ensure request module is installed
147 """
148 self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec()
149 self.argument_spec.update(
150 dict(
151 state=dict(type="str", choices=["present"], default="present"),
152 aod_enable=dict(required=False, type="bool"),
153 available_updates_enable=dict(required=False, type="bool"),
154 event_enable=dict(required=False, type="bool"),
155 cert_enable=dict(required=False, type="bool"),
156 destinations=dict(
157 required=False,
158 type="list",
159 elements="dict",
160 options=dict(
161 hostname=dict(required=True, type="str"),
162 port=dict(required=True, type="int"),
163 ca_cert=dict(required=False, type="str"),
164 )
165 ),
166 transport=dict(required=False, type="str", choices=["SMTP", "HTTP", "HTTPS"]),
167 weekly_enable=dict(required=False, type="bool"),
168 )
169 )
170 self.module = AnsibleModule(
171 argument_spec=self.argument_spec,
172 required_if=[("state", "present", ["aod_enable", "transport", "weekly_enable", "cert_enable", "event_enable"])],
173 supports_check_mode=True
174 )
175 self.na_helper = NetAppModule()
176
177 # set up state variables
178 self.parameters = self.na_helper.set_parameters(self.module.params)
179 # Calling generic SG rest_api class
180 self.rest_api = SGRestAPI(self.module)
181 # Get API version
182 self.rest_api.get_sg_product_version()
183 self.api_version = self.rest_api.get_api_version()
184
185 # Checking for the parameters passed and create new parameters list
186 self.data = {}
187
188 self.data["aodEnable"] = self.parameters["aod_enable"]
189 self.data["eventEnable"] = self.parameters["event_enable"]
190 self.data["certEnable"] = self.parameters["cert_enable"]
191 if self.parameters.get("available_updates_enable") is not None:
192 self.data["availableUpdatesEnable"] = self.parameters["available_updates_enable"]
193 if self.parameters.get("destinations") is not None:
194 self.data["destinations"] = [
195 {
196 "hostname": destination["hostname"],
197 "port": destination["port"],
198 "caCert": destination.get("ca_cert"),
199 }
200 for destination in self.parameters["destinations"]
201 ]
202 else:
203 self.data["destinations"] = []
204 self.data["transport"] = self.parameters["transport"]
205 self.data["weeklyEnable"] = self.parameters["weekly_enable"]
206
207 def get_autosupport(self):
208 """ Get autosupport configuration """
209 api = "api/%s/private/autosupport" % self.api_version
210 response, error = self.rest_api.get(api)
211
212 if error:
213 self.module.fail_json(msg=error)
214 else:
215 return response["data"]
216
217 def update_autosupport(self):
218 """ Update autosupport configuration """
219 api = "api/%s/private/autosupport" % self.api_version
220 response, error = self.rest_api.put(api, self.data)
221
222 if not response or 'data' not in response:
223 self.module.fail_json(msg="Invalid response from API")
224 if error:
225 self.module.fail_json(msg=error)
226 else:
227 return response["data"]
228
229 def apply(self):
230 ''' Apply autosupport configuration '''
231
232 current_autosupport = self.get_autosupport()
233
234 if self.parameters["state"] == "present":
235 # let's see if we need to update parameters
236 modify = self.na_helper.get_modified_attributes(current_autosupport, self.data)
237
238 result_message = ""
239 resp_data = current_autosupport
240 if self.na_helper.changed:
241 if self.module.check_mode:
242 pass
243 elif modify:
244 resp_data = self.update_autosupport()
245 result_message = "Autosupport configuration updated successfully."
246
247 self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data)
248
249
250 def main():
251 """
252 Main function
253 """
254 na_sg_grid_autosupport = SgAutosupport()
255 na_sg_grid_autosupport.apply()
256
257
258 if __name__ == "__main__":
259 main()