Debian Codegraph @

ansible_collections/dellemc/powerflex/plugins/modules/fault_set.py

raw symbols SCIP
1 #!/usr/bin/python
2
3 # Copyright: (c) 2024, Dell Technologies
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 """ Ansible module for managing Fault Sets on Dell Technologies (Dell) PowerFlex"""
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 DOCUMENTATION = r'''
13 module: fault_set
14 version_added: '2.2.0'
15 short_description: Manage Fault Sets on Dell PowerFlex
16 description:
17 - Managing fault sets on PowerFlex storage system includes creating,
18 getting details, renaming and deleting a fault set.
19 author:
20 - Carlos Tronco (@ctronco) <[email protected]>
21 - Trisha Datta (@trisha-dell) <[email protected]>
22 extends_documentation_fragment:
23 - dellemc.powerflex.powerflex
24 options:
25 fault_set_name:
26 description:
27 - Name of the Fault Set.
28 - Mutually exclusive with I(fault_set_id).
29 type: str
30 fault_set_id:
31 description:
32 - ID of the Fault Set.
33 - Mutually exclusive with I(fault_set_name).
34 type: str
35 protection_domain_name:
36 description:
37 - Name of protection domain.
38 - Mutually exclusive with I(protection_domain_id).
39 type: str
40 protection_domain_id:
41 description:
42 - ID of the protection domain.
43 - Mutually exclusive with I(protection_domain_name).
44 type: str
45 fault_set_new_name:
46 description:
47 - New name of the fault set.
48 type: str
49 state:
50 description:
51 - State of the Fault Set.
52 choices: ['present', 'absent']
53 default: 'present'
54 type: str
55 notes:
56 - The I(check_mode) is supported.
57 - When I(fault_set_name) is provided, I(protection_domain_name)
58 or I(protection_domain_id) must be provided.
59 '''
60
61
62 EXAMPLES = r'''
63
64 - name: Create Fault Set on Protection Domain
65 dellemc.powerflex.fault_set:
66 hostname: "{{ hostname }}"
67 username: "{{ username }}"
68 password: "{{ password }}"
69 validate_certs: "{{ validate_certs }}"
70 fault_set_name: "{{ fault_set_name }}"
71 protection_domain_name: "{{ pd_name }}"
72 state: present
73
74 - name: Rename Fault Set
75 dellemc.powerflex.fault_set:
76 hostname: "{{ hostname }}"
77 username: "{{ username }}"
78 password: "{{ password }}"
79 validate_certs: "{{ validate_certs }}"
80 fault_set_name: "{{ fault_set_name }}"
81 fault_set_new_name: "{{ fault_set_new_name }}"
82 state: present
83
84 - name: Get details of a Fault Set
85 dellemc.powerflex.fault_set:
86 hostname: "{{ hostname }}"
87 username: "{{ username }}"
88 password: "{{ password }}"
89 validate_certs: "{{ validate_certs }}"
90 fault_set_id: "{{ fault_set_id }}"
91 state: present
92
93 - name: Delete Fault Set
94 dellemc.powerflex.fault_set:
95 hostname: "{{ hostname }}"
96 username: "{{ username }}"
97 password: "{{ password }}"
98 validate_certs: "{{ validate_certs }}"
99 fault_set_id: "{{ fault_set_id }}"
100 state: absent
101 '''
102
103 RETURN = r'''
104 changed:
105 description: Whether or not the resource has changed.
106 returned: always
107 type: bool
108 sample: 'false'
109
110 fault_set_details:
111 description: Details of fault set.
112 returned: always
113 type: dict
114 contains:
115 protectionDomainId:
116 description: Unique identifier of the protection domain.
117 type: str
118 protectionDomainName:
119 description: Name of the protection domain.
120 type: str
121 name:
122 description: Name of the fault set.
123 type: str
124 id:
125 description: Unique identifier of the fault set.
126 type: str
127 SDS:
128 description: List of SDS associated to the fault set.
129 type: list
130 elements: dict
131 links:
132 description: Fault set links.
133 type: list
134 contains:
135 href:
136 description: Fault Set instance URL.
137 type: str
138 rel:
139 description: Relationship of fault set with different
140 entities.
141 type: str
142 sample: {
143 "protectionDomainId": "da721a8300000000",
144 "protectionDomainName": "sample-pd",
145 "name": "fs_001",
146 "id": "eb44b70500000000",
147 "links": []
148 }
149
150 '''
151
152
153 from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell import (
154 utils,
155 )
156 from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell.libraries.powerflex_base \
157 import PowerFlexBase, powerflex_compatibility
158 from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell.libraries.configuration \
159 import Configuration
160 from ansible.module_utils.basic import AnsibleModule
161
162
163 LOG = utils.get_logger("fault_set")
164
165
166 @powerflex_compatibility(min_ver='3.6', max_ver='5.0')
167 class PowerFlexFaultSet(PowerFlexBase):
168 """Class with FaultSet operations"""
169
170 def __init__(self):
171 """Define all parameters required by this module"""
172
173 mutually_exclusive = [
174 ["fault_set_name", "fault_set_id"],
175 ["protection_domain_name", "protection_domain_id"],
176 ]
177 required_one_of = [["fault_set_name", "fault_set_id"]]
178
179 ansible_module_params = {
180 'argument_spec': get_powerflex_fault_set_parameters(),
181 'supports_check_mode': True,
182 'mutually_exclusive': mutually_exclusive,
183 'required_one_of': required_one_of
184 }
185 super().__init__(AnsibleModule, ansible_module_params)
186 super().check_module_compatibility()
187
188 self.result = dict(
189 changed=False,
190 fault_set_details={}
191 )
192
193 def get_protection_domain(
194 self, protection_domain_name=None, protection_domain_id=None
195 ):
196 """Get the details of a protection domain in a given PowerFlex storage
197 system"""
198 return Configuration(self.powerflex_conn, self.module).get_protection_domain(
199 protection_domain_name=protection_domain_name, protection_domain_id=protection_domain_id)
200
201 def get_associated_sds(
202 self, fault_set_id=None
203 ):
204 """Get the details of SDS associated to given fault set in a given PowerFlex storage
205 system"""
206 return Configuration(self.powerflex_conn, self.module).get_associated_sds(
207 fault_set_id=fault_set_id)
208
209 def create_fault_set(self, fault_set_name, protection_domain_id):
210 """
211 Create Fault Set
212 :param fault_set_name: Name of the fault set
213 :type fault_set_name: str
214 :param protection_domain_id: ID of the protection domain
215 :type protection_domain_id: str
216 :return: Boolean indicating if create operation is successful
217 """
218 try:
219 if not self.module.check_mode:
220 msg = (f"Creating fault set with name: {fault_set_name} on "
221 f"protection domain with id: {protection_domain_id}")
222 LOG.info(msg)
223 self.powerflex_conn.fault_set.create(
224 name=fault_set_name, protection_domain_id=protection_domain_id
225 )
226 return self.get_fault_set(
227 fault_set_name=fault_set_name,
228 protection_domain_id=protection_domain_id)
229
230 except Exception as e:
231 error_msg = (f"Create fault set {fault_set_name} operation failed "
232 f"with error {str(e)}")
233 LOG.error(error_msg)
234 self.module.fail_json(msg=error_msg)
235
236 def get_fault_set(self, fault_set_name=None, fault_set_id=None, protection_domain_id=None):
237 """Get fault set details
238 :param fault_set_name: Name of the fault set
239 :param fault_set_id: Id of the fault set
240 :param protection_domain_id: ID of the protection domain
241 :return: Fault set details
242 :rtype: dict
243 """
244 return Configuration(self.powerflex_conn, self.module).get_fault_set(
245 fault_set_name=fault_set_name, fault_set_id=fault_set_id, protection_domain_id=protection_domain_id)
246
247 def is_rename_required(self, fault_set_details, fault_set_params):
248 """To get the details of the fields to be modified."""
249
250 if fault_set_params['fault_set_new_name'] is not None and \
251 fault_set_params['fault_set_new_name'] != fault_set_details['name']:
252 return True
253
254 return False
255
256 def rename_fault_set(self, fault_set_id,
257 new_name):
258 """Perform rename operation on a fault set"""
259
260 try:
261 if not self.module.check_mode:
262 self.powerflex_conn.fault_set.rename(
263 fault_set_id=fault_set_id,
264 name=new_name)
265 return self.get_fault_set(
266 fault_set_id=fault_set_id)
267 except Exception as e:
268 msg = (f'Failed to rename the fault set instance '
269 f'with error {str(e)}')
270 LOG.error(msg)
271 self.module.fail_json(msg=msg)
272
273 def delete_fault_set(self, fault_set_id):
274 """Delete the Fault Set"""
275 try:
276 if not self.module.check_mode:
277 LOG.info(msg=f"Removing Fault Set {fault_set_id}")
278 self.powerflex_conn.fault_set.delete(fault_set_id)
279 LOG.info("returning None")
280 return None
281 return self.get_fault_set(
282 fault_set_id=fault_set_id)
283 except Exception as e:
284 errormsg = f"Removing Fault Set {fault_set_id} failed with error {str(e)}"
285 LOG.error(errormsg)
286 self.module.fail_json(msg=errormsg)
287
288 def validate_parameters(self, fault_set_params):
289 params = [fault_set_params['fault_set_name'], fault_set_params['fault_set_new_name']]
290 for param in params:
291 if param is not None and len(param.strip()) == 0:
292 error_msg = "Provide valid value for name for the " \
293 "creation/modification of the fault set."
294 LOG.error(error_msg)
295 self.module.fail_json(msg=error_msg)
296 if fault_set_params['fault_set_name'] is not None and \
297 fault_set_params['protection_domain_id'] is None and fault_set_params['protection_domain_name'] is None:
298 error_msg = "Provide protection_domain_id/protection_domain_name with fault_set_name."
299 LOG.error(error_msg)
300 self.module.fail_json(msg=error_msg)
301
302
303 def get_powerflex_fault_set_parameters():
304 """This method provide parameter required for the Ansible Fault Set module on
305 PowerFlex"""
306 return dict(
307 fault_set_name=dict(),
308 fault_set_id=dict(),
309 protection_domain_name=dict(),
310 protection_domain_id=dict(),
311 fault_set_new_name=dict(),
312 state=dict(default='present', choices=['present', 'absent'])
313 )
314
315
316 class FaultSetExitHandler():
317 def handle(self, fault_set_obj, fault_set_details):
318 fault_set_obj.result["fault_set_details"] = fault_set_details
319 if fault_set_details:
320 fault_set_obj.result["fault_set_details"]["protectionDomainName"] = \
321 fault_set_obj.get_protection_domain(
322 protection_domain_id=fault_set_details["protectionDomainId"])["name"]
323 fault_set_obj.result["fault_set_details"]["SDS"] = \
324 fault_set_obj.get_associated_sds(
325 fault_set_id=fault_set_details['id'])
326 fault_set_obj.module.exit_json(**fault_set_obj.result)
327
328
329 class FaultSetDeleteHandler():
330 def handle(self, fault_set_obj, fault_set_params, fault_set_details):
331 if fault_set_params['state'] == 'absent' and fault_set_details:
332 fault_set_details = fault_set_obj.delete_fault_set(fault_set_details['id'])
333 fault_set_obj.result['changed'] = True
334
335 FaultSetExitHandler().handle(fault_set_obj, fault_set_details)
336
337
338 class FaultSetRenameHandler():
339 def handle(self, fault_set_obj, fault_set_params, fault_set_details):
340 if fault_set_params['state'] == 'present' and fault_set_details:
341 is_rename_required = fault_set_obj.is_rename_required(fault_set_details, fault_set_params)
342 if is_rename_required:
343 fault_set_details = fault_set_obj.rename_fault_set(fault_set_id=fault_set_details['id'],
344 new_name=fault_set_params['fault_set_new_name'])
345 fault_set_obj.result['changed'] = True
346
347 FaultSetDeleteHandler().handle(fault_set_obj, fault_set_params, fault_set_details)
348
349
350 class FaultSetCreateHandler():
351 def handle(self, fault_set_obj, fault_set_params, fault_set_details, pd_id):
352 if fault_set_params['state'] == 'present' and not fault_set_details:
353 fault_set_details = fault_set_obj.create_fault_set(fault_set_name=fault_set_params['fault_set_name'],
354 protection_domain_id=pd_id)
355 fault_set_obj.result['changed'] = True
356
357 FaultSetRenameHandler().handle(fault_set_obj, fault_set_params, fault_set_details)
358
359
360 class FaultSetHandler():
361 def handle(self, fault_set_obj, fault_set_params):
362 fault_set_obj.validate_parameters(fault_set_params=fault_set_params)
363 pd_id = None
364 if fault_set_params['protection_domain_id'] or fault_set_params['protection_domain_name']:
365 pd_id = fault_set_obj.get_protection_domain(
366 protection_domain_id=fault_set_params['protection_domain_id'],
367 protection_domain_name=fault_set_params['protection_domain_name'])['id']
368 fault_set_details = fault_set_obj.get_fault_set(fault_set_id=fault_set_params['fault_set_id'],
369 fault_set_name=fault_set_params['fault_set_name'],
370 protection_domain_id=pd_id)
371 FaultSetCreateHandler().handle(fault_set_obj, fault_set_params, fault_set_details, pd_id)
372
373
374 def main():
375 """ Create PowerFlex fault set object and perform action on it
376 based on user input from playbook."""
377 obj = PowerFlexFaultSet()
378 FaultSetHandler().handle(obj, obj.module.params)
379
380
381 if __name__ == '__main__':
382 main()