Debian Codegraph @

ansible_collections/netapp/storagegrid/plugins/modules/na_sg_grid_ilm_pool.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 ILM pools"""
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 DOCUMENTATION = """
20 module: na_sg_grid_ilm_pool
21 short_description: Manage ILM pools on StorageGRID.
22 extends_documentation_fragment:
23 - netapp.storagegrid.netapp.sg
24 version_added: '21.14.0'
25 author: Denis Magel (@dmagel-netapp) <[email protected]>
26 description:
27 - Interact with ILM pools on NetApp StorageGRID.
28 options:
29 name:
30 description:
31 - The name of the storage pool
32 - Must be unique
33 required: true
34 type: str
35 disks:
36 description:
37 - A list of the sites and storage grades in the storage pool
38 type: list
39 elements: dict
40 suboptions:
41 description:
42 description:
43 - The IDs of all sites and storage grades for this storage pool.
44 - The null site ("All Sites") is the default site, which includes all current sites.
45 - The null storage grade ("All Storage Nodes") is the default storage grade, which includes all Storage Nodes at the selected site.
46 - Use the All Sites default with care because it will automatically include new sites added in an expansion, which might not be the behavior you want.
47 - To get more information about the sites and storage grades, use the ilm-grade-site and ilm-grades endpoints.
48 required: false
49 type: str
50 group:
51 description:
52 - If both the group and siteId fields are provided, for a disk, the siteId will be used when creating/ editing a storage pool.
53 required: false
54 type: int
55 grade:
56 description:
57 - Storage grade ID
58 required: false
59 type: int
60 siteId:
61 description:
62 - If both the group and siteId fields are provided, for a disk, the siteId will be used when creating/ editing a storage pool.
63 required: false
64 type: str
65 archives:
66 description:
67 - A list of the sites that use the Archive Nodes storage grade
68 type: list
69 elements: dict
70 suboptions:
71 description:
72 description:
73 - The IDs of all sites and storage grades for this storage pool.
74 - The null site ("All Sites") is the default site, which includes all current sites.
75 - The null storage grade ("All Storage Nodes") is the default storage grade, which includes all Storage Nodes at the selected site.
76 - Use the All Sites default with care because it will automatically include new sites added in an expansion, which might not be the behavior you want.
77 - To get more information about the sites and storage grades, use the ilm-grade-site and ilm-grades endpoints.
78 required: false
79 type: str
80 group:
81 description:
82 - If both the group and siteId fields are provided, for a disk, the siteId will be used when creating/ editing a storage pool.
83 required: false
84 type: int
85 grade:
86 description:
87 - Storage grade ID
88 required: false
89 type: int
90 siteId:
91 description:
92 - If both the group and siteId fields are provided, for a disk, the siteId will be used when creating/ editing a storage pool.
93 required: false
94 type: str
95 state:
96 description:
97 - Whether the specified pool should be created or deleted.
98 required: false
99 type: str
100 choices: ['present', 'absent']
101 default: present
102 validate_certs:
103 description:
104 - Should https certificates be validated?
105 required: false
106 type: bool
107 default: true
108 """
109
110 EXAMPLES = """
111 - name: Create ILM pool
112 na_sg_grid_ilm_pool:
113 api_url: "https://<storagegrid-endpoint-url>"
114 auth_token: "storagegrid-auth-token"
115 name: "Data Center 1"
116 disks:
117 - group: 10
118 archives: []
119
120 - name: Delete existing ILM pool
121 na_sg_grid_ilm_pool:
122 api_url: "https://<storagegrid-endpoint-url>"
123 auth_token: "storagegrid-auth-token"
124 name: "profile1"
125 state: absent
126 """
127
128 RETURN = """
129 resp:
130 description: Returns information about the StorageGRID ILM pool.
131 returned: If state is 'present'.
132 type: dict
133 sample: {
134 "archives": [],
135 "disks": [
136 {
137 "grade": null,
138 "group": null,
139 "siteId": null
140 }
141 ],
142 "displayName": "Data Center 1",
143 "id": "p10771105546308032398",
144 "name": "Data Center 1"
145 }
146 """
147
148 from datetime import datetime
149
150 import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils
151 from ansible.module_utils.basic import AnsibleModule
152 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule
153 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI
154 from ansible_collections.netapp.storagegrid.plugins.module_utils.tools import first_inside_second_dict_or_list
155
156 __LOGGING__ = []
157
158
159 class ILM_pool(object):
160 """
161 Create, modify and delete StorageGRID ILM pool
162 """
163
164 def __init__(self):
165 """
166 Parse arguments, setup state variables,
167 check parameters and ensure request module is installed
168 """
169 self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec()
170 self.argument_spec.update(
171 dict(
172 state=dict(required=False, type="str", choices=["present", "absent"], default="present"),
173 name=dict(required=True, type="str"),
174 disks=dict(
175 required=False,
176 type="list",
177 elements="dict",
178 options=dict(
179 description=dict(required=False, type="str"),
180 group=dict(required=False, type="int"),
181 grade=dict(required=False, type="int"),
182 siteId=dict(required=False, type="str"),
183 ),
184 ),
185 archives=dict(
186 required=False,
187 type="list",
188 elements="dict",
189 options=dict(
190 description=dict(required=False, type="str"),
191 group=dict(required=False, type="int"),
192 grade=dict(required=False, type="int"),
193 siteId=dict(required=False, type="str"),
194 ),
195 ),
196 )
197 )
198 self.module = AnsibleModule(
199 argument_spec=self.argument_spec,
200 required_if=[("state", "present", ["disks", "archives"])],
201 supports_check_mode=True,
202 )
203 self.na_helper = NetAppModule()
204
205 # set up state variables
206 self.parameters = self.na_helper.set_parameters(self.module.params)
207 __LOGGING__.append("params: %s" % self.module.params)
208 # Calling generic SG rest_api class
209 self.rest_api = SGRestAPI(self.module)
210 # Get API version
211 self.rest_api.get_sg_product_version()
212 self.api_version = self.rest_api.get_api_version()
213
214 # Create body for creation request (POST with state present)
215 self.data = {}
216 self.data["displayName"] = self.parameters["name"]
217 if self.parameters.get("disks"):
218 self.data["disks"] = self.parameters["disks"]
219 if self.parameters.get("archives"):
220 self.data["archives"] = self.parameters["archives"]
221 __LOGGING__.append("data: %s" % self.data)
222
223 def module_logging_handler(self, log_msg):
224 """Module logging handler"""
225 # Create timestamp for logs
226 date_now = datetime.now()
227 timestamp = date_now.strftime("%Y/%m/%d-%H:%M:%S")
228 # Log events
229 self.module.log(log_msg)
230 __LOGGING__.append("%s: %s" % (timestamp, log_msg))
231
232 def get_ilm_pools(self):
233 # Check if profile exists
234 # Return info if found, or None
235 api = "api/%s/private/ilm-pools" % self.api_version
236 response, error = self.rest_api.get(api)
237 if error:
238 self.module.fail_json(msg=error, log=__LOGGING__)
239 self.module_logging_handler("all ILM pools: %s" % response['data'])
240 # if ILM pool with 'name' exists, return it, else none
241 for pool in response["data"]:
242 if pool["displayName"] == self.parameters["name"]:
243 self.id = pool["id"]
244 self.data.update({"id": pool["id"]})
245 return pool
246 return None
247
248 def create_ilm_pool(self):
249 __LOGGING__.append("creating ILM pool with payload: %s" % self.data)
250 api = "api/%s/private/ilm-pools" % self.api_version
251 response, error = self.rest_api.post(api, self.data)
252 if error:
253 self.module.fail_json(msg=error, log=__LOGGING__)
254 return response["data"]
255
256 def delete_ilm_pool(self):
257 __LOGGING__.append("deactivating ILM pool")
258 api = "api/%s/private/ilm-pools/%s" % (self.api_version, self.id)
259 response, error = self.rest_api.delete(api, None)
260 if error:
261 self.module.fail_json(msg=error, log=__LOGGING__)
262
263 def update_ilm_pool(self):
264 __LOGGING__.append("updating ILM pool with payload: %s" % self.data)
265 api = "api/%s/private/ilm-pools/%s" % (self.api_version, self.id)
266 response, error = self.rest_api.put(api, self.data)
267 if error:
268 self.module.fail_json(msg=error, log=__LOGGING__)
269 return response["data"]
270
271 def apply(self):
272 """
273 Perform pre-checks, call functions and exit
274 """
275
276 ilm_pool = self.get_ilm_pools()
277 self.module_logging_handler("got matching ILM pool: %s" % ilm_pool)
278
279 cd_action = self.na_helper.get_cd_action(ilm_pool, self.parameters)
280
281 if cd_action is None and self.parameters["state"] == "present":
282 # let's see if we need to update parameters
283 if self.data.get("disks") and not first_inside_second_dict_or_list(self.data.get("disks"), ilm_pool.get("disks")):
284 self.na_helper.changed = True
285 if self.data.get("archives") and not first_inside_second_dict_or_list(self.data.get("archives"), ilm_pool.get("archives")):
286 self.na_helper.changed = True
287
288 result_message = ""
289 resp_data = ilm_pool
290 if self.na_helper.changed:
291 if self.module.check_mode:
292 pass
293 else:
294 if cd_action == "delete":
295 self.delete_ilm_pool()
296 resp_data = None
297 result_message = "ILM pool deleted"
298 __LOGGING__.append("ILM pool deleted")
299
300 elif cd_action == "create":
301 resp_data = self.create_ilm_pool()
302 result_message = "ILM pool created"
303 __LOGGING__.append("ILM pool created")
304
305 else:
306 resp_data = self.update_ilm_pool()
307 result_message = "ILM pool updated"
308 __LOGGING__.append("ILM pool updated")
309
310 self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data, log=__LOGGING__)
311
312
313 def main():
314 """
315 Main function
316 """
317 na_sg_ilm_pool = ILM_pool()
318 na_sg_ilm_pool.apply()
319
320
321 if __name__ == "__main__":
322 main()