Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2
3 # Copyright: (c) 2021, 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 SDCs 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: sdc
14 version_added: '1.0.0'
15 short_description: Manage SDCs on Dell PowerFlex
16 description:
17 - Managing SDCs on PowerFlex storage system includes getting details of SDC
18 and renaming SDC.
19
20 author:
21 - Akash Shendge (@shenda1) <[email protected]>
22
23 extends_documentation_fragment:
24 - dellemc.powerflex.powerflex
25
26 options:
27 sdc_name:
28 description:
29 - Name of the SDC.
30 - Specify either I(sdc_name), I(sdc_id) or I(sdc_ip) for get/rename operation.
31 - Mutually exclusive with I(sdc_id) and I(sdc_ip).
32 type: str
33 sdc_id:
34 description:
35 - ID of the SDC.
36 - Specify either I(sdc_name), I(sdc_id) or I(sdc_ip) for get/rename operation.
37 - Mutually exclusive with I(sdc_name) and I(sdc_ip).
38 type: str
39 sdc_ip:
40 description:
41 - IP of the SDC.
42 - Specify either I(sdc_name), I(sdc_id) or I(sdc_ip) for get/rename operation.
43 - Mutually exclusive with I(sdc_id) and I(sdc_name).
44 type: str
45 sdc_new_name:
46 description:
47 - New name of the SDC. Used to rename the SDC.
48 type: str
49 performance_profile:
50 description:
51 - Define the performance profile as I(Compact) or I(HighPerformance).
52 - The high performance profile configures a predefined set of parameters for very high performance use cases.
53 choices: ['Compact', 'HighPerformance']
54 type: str
55 state:
56 description:
57 - State of the SDC.
58 choices: ['present', 'absent']
59 required: true
60 type: str
61 notes:
62 - The I(check_mode) is not supported.
63 '''
64
65 EXAMPLES = r'''
66 - name: Get SDC details using SDC ip
67 dellemc.powerflex.sdc:
68 hostname: "{{hostname}}"
69 username: "{{username}}"
70 password: "{{password}}"
71 validate_certs: "{{validate_certs}}"
72 sdc_ip: "{{sdc_ip}}"
73 state: "present"
74
75 - name: Rename SDC using SDC name
76 dellemc.powerflex.sdc:
77 hostname: "{{hostname}}"
78 username: "{{username}}"
79 password: "{{password}}"
80 validate_certs: "{{validate_certs}}"
81 sdc_name: "centos_sdc"
82 sdc_new_name: "centos_sdc_renamed"
83 state: "present"
84
85 - name: Modify performance profile of SDC using SDC name
86 dellemc.powerflex.sdc:
87 hostname: "{{hostname}}"
88 username: "{{username}}"
89 password: "{{password}}"
90 validate_certs: "{{validate_certs}}"
91 sdc_name: "centos_sdc"
92 performance_profile: "Compact"
93 state: "present"
94
95 - name: Remove SDC using SDC name
96 dellemc.powerflex.sdc:
97 hostname: "{{hostname}}"
98 username: "{{username}}"
99 password: "{{password}}"
100 validate_certs: "{{validate_certs}}"
101 sdc_name: "centos_sdc"
102 state: "absent"
103 '''
104
105 RETURN = r'''
106 changed:
107 description: Whether or not the resource has changed.
108 returned: always
109 type: bool
110 sample: 'false'
111
112 sdc_details:
113 description: Details of the SDC.
114 returned: When SDC exists
115 type: dict
116 contains:
117 id:
118 description: The ID of the SDC.
119 type: str
120 name:
121 description: Name of the SDC.
122 type: str
123 sdcIp:
124 description: IP of the SDC.
125 type: str
126 osType:
127 description: OS type of the SDC.
128 type: str
129 mapped_volumes:
130 description: The details of the mapped volumes.
131 type: list
132 contains:
133 id:
134 description: The ID of the volume.
135 type: str
136 name:
137 description: The name of the volume.
138 type: str
139 volumeType:
140 description: Type of the volume.
141 type: str
142 sdcApproved:
143 description: Indicates whether an SDC has approved access to the
144 system.
145 type: bool
146 sample: {
147 "id": "07335d3d00000006",
148 "installedSoftwareVersionInfo": "R3_6.0.0",
149 "kernelBuildNumber": null,
150 "kernelVersion": "3.10.0",
151 "links": [
152 {
153 "href": "/api/instances/Sdc::07335d3d00000006",
154 "rel": "self"
155 },
156 {
157 "href": "/api/instances/Sdc::07335d3d00000006/relationships/
158 Statistics",
159 "rel": "/api/Sdc/relationship/Statistics"
160 },
161 {
162 "href": "/api/instances/Sdc::07335d3d00000006/relationships/
163 Volume",
164 "rel": "/api/Sdc/relationship/Volume"
165 },
166 {
167 "href": "/api/instances/System::4a54a8ba6df0690f",
168 "rel": "/api/parent/relationship/systemId"
169 }
170 ],
171 "mapped_volumes": [],
172 "mdmConnectionState": "Disconnected",
173 "memoryAllocationFailure": null,
174 "name": "LGLAP203",
175 "osType": "Linux",
176 "peerMdmId": null,
177 "perfProfile": "HighPerformance",
178 "sdcApproved": true,
179 "sdcApprovedIps": null,
180 "sdcGuid": "F8ECB844-23B8-4629-92BB-B6E49A1744CB",
181 "sdcIp": "N/A",
182 "sdcIps": null,
183 "sdcType": "AppSdc",
184 "sdrId": null,
185 "socketAllocationFailure": null,
186 "softwareVersionInfo": "R3_6.0.0",
187 "systemId": "4a54a8ba6df0690f",
188 "versionInfo": "R3_6.0.0"
189 }
190 '''
191
192 from ansible.module_utils.basic import AnsibleModule
193 from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell\
194 import utils
195
196 LOG = utils.get_logger('sdc')
197
198
199 class PowerFlexSdc(object):
200 """Class with SDC operations"""
201
202 def __init__(self):
203 """ Define all parameters required by this module"""
204 self.module_params = utils.get_powerflex_gateway_host_parameters()
205 self.module_params.update(get_powerflex_sdc_parameters())
206
207 mutually_exclusive = [['sdc_id', 'sdc_ip', 'sdc_name']]
208
209 required_one_of = [['sdc_id', 'sdc_ip', 'sdc_name']]
210
211 # initialize the Ansible module
212 self.module = AnsibleModule(
213 argument_spec=self.module_params,
214 supports_check_mode=False,
215 mutually_exclusive=mutually_exclusive,
216 required_one_of=required_one_of)
217
218 utils.ensure_required_libs(self.module)
219
220 try:
221 self.powerflex_conn = utils.get_powerflex_gateway_host_connection(
222 self.module.params)
223 LOG.info("Got the PowerFlex system connection object instance")
224 except Exception as e:
225 LOG.error(str(e))
226 self.module.fail_json(msg=str(e))
227
228 def rename_sdc(self, sdc_id, new_name):
229 """Rename SDC
230 :param sdc_id: The ID of the SDC
231 :param new_name: The new name of the SDC
232 :return: Boolean indicating if rename operation is successful
233 """
234
235 try:
236 self.powerflex_conn.sdc.rename(sdc_id=sdc_id, name=new_name)
237 return True
238 except Exception as e:
239 errormsg = "Failed to rename SDC %s with error %s" % (sdc_id,
240 str(e))
241 LOG.error(errormsg)
242 self.module.fail_json(msg=errormsg)
243
244 def get_mapped_volumes(self, sdc_id):
245 """Get volumes mapped to SDC
246 :param sdc_id: The ID of the SDC
247 :return: List containing volume details mapped to SDC
248 """
249
250 try:
251 resp = self.powerflex_conn.sdc.get_mapped_volumes(sdc_id=sdc_id)
252 return resp
253 except Exception as e:
254 errormsg = "Failed to get the volumes mapped to SDC %s with " \
255 "error %s" % (sdc_id, str(e))
256 LOG.error(errormsg)
257 self.module.fail_json(msg=errormsg)
258
259 def get_sdc(self, sdc_name=None, sdc_ip=None, sdc_id=None):
260 """Get the SDC Details
261 :param sdc_name: The name of the SDC
262 :param sdc_ip: The IP of the SDC
263 :param sdc_id: The ID of the SDC
264 :return: The dict containing SDC details
265 """
266
267 if sdc_name:
268 id_ip_name = sdc_name
269 elif sdc_ip:
270 id_ip_name = sdc_ip
271 else:
272 id_ip_name = sdc_id
273
274 try:
275 if sdc_name:
276 sdc_details = self.powerflex_conn.sdc.get(
277 filter_fields={'name': sdc_name})
278 elif sdc_ip:
279 sdc_details = self.powerflex_conn.sdc.get(
280 filter_fields={'sdcIp': sdc_ip})
281 else:
282 sdc_details = self.powerflex_conn.sdc.get(
283 filter_fields={'id': sdc_id})
284
285 if len(sdc_details) == 0:
286 error_msg = "Unable to find SDC with identifier %s" \
287 % id_ip_name
288 LOG.error(error_msg)
289 return None
290 sdc_details[0]['mapped_volumes'] = self.get_mapped_volumes(
291 sdc_details[0]['id'])
292 return sdc_details[0]
293 except Exception as e:
294 errormsg = "Failed to get the SDC %s with error %s" % (
295 id_ip_name, str(e))
296 LOG.error(errormsg)
297 self.module.fail_json(msg=errormsg)
298
299 def validate_parameters(self):
300 """Validate the input parameters"""
301
302 sdc_identifiers = ['sdc_name', 'sdc_id', 'sdc_ip']
303 for param in sdc_identifiers:
304 if self.module.params[param] is not None and \
305 len(self.module.params[param].strip()) == 0:
306 msg = f"Please provide valid {param}"
307 LOG.error(msg)
308 self.module.fail_json(msg=msg)
309
310 def remove(self, sdc_id):
311 """Remove the SDC"""
312 try:
313 LOG.info(msg=f"Removing SDC {sdc_id}")
314 self.powerflex_conn.sdc.delete(sdc_id)
315 return True
316 except Exception as e:
317 errormsg = f"Removing SDC {sdc_id} failed with error {str(e)}"
318 LOG.error(errormsg)
319 self.module.fail_json(msg=errormsg)
320
321 def set_performance_profile(self, sdc_id, performance_profile):
322 """Set performance profile of SDC"""
323 try:
324 LOG.info(msg=f"Setting performance profile of SDC {sdc_id}")
325 self.powerflex_conn.sdc.set_performance_profile(sdc_id, performance_profile)
326 return True
327 except Exception as e:
328 errormsg = f"Modifying performance profile of SDC {sdc_id} failed with error {str(e)}"
329 LOG.error(errormsg)
330 self.module.fail_json(msg=errormsg)
331
332 def validate_input(self, sdc_details, sdc_new_name, state, id_ip_name):
333 if state == 'present' and not sdc_details:
334 error_msg = 'Could not find any SDC instance with ' \
335 'identifier %s.' % id_ip_name
336 LOG.error(error_msg)
337 self.module.fail_json(msg=error_msg)
338
339 if sdc_new_name is not None and len(sdc_new_name.strip()) == 0:
340 self.module.fail_json(msg="Provide valid SDC name to rename to.")
341
342 def perform_modify(self, sdc_details, sdc_new_name, performance_profile):
343 changed = False
344 if sdc_new_name is not None and sdc_new_name != sdc_details['name']:
345 changed = self.rename_sdc(sdc_details['id'], sdc_new_name)
346
347 if performance_profile and performance_profile != sdc_details['perfProfile']:
348 changed = self.set_performance_profile(sdc_details['id'], performance_profile)
349 return changed
350
351 def perform_module_operation(self):
352 """
353 Perform different actions on SDC based on parameters passed in
354 the playbook
355 """
356 sdc_name = self.module.params['sdc_name']
357 sdc_id = self.module.params['sdc_id']
358 sdc_ip = self.module.params['sdc_ip']
359 sdc_new_name = self.module.params['sdc_new_name']
360 performance_profile = self.module.params['performance_profile']
361 state = self.module.params['state']
362
363 # result is a dictionary to contain end state and SDC details
364 changed = False
365 result = dict(
366 changed=False,
367 sdc_details={}
368 )
369
370 self.validate_parameters()
371 sdc_details = self.get_sdc(sdc_name=sdc_name, sdc_id=sdc_id,
372 sdc_ip=sdc_ip)
373 id_ip_name = sdc_name or sdc_ip or sdc_id
374
375 self.validate_input(sdc_details, sdc_new_name, state, id_ip_name)
376
377 if state == 'absent' and sdc_details:
378 changed = self.remove(sdc_details['id'])
379
380 if state == 'present' and sdc_details:
381 changed = self.perform_modify(sdc_details, sdc_new_name, performance_profile)
382
383 if changed:
384 sdc_details = self.get_sdc(sdc_name=sdc_new_name or sdc_name,
385 sdc_id=sdc_id, sdc_ip=sdc_ip)
386 result['sdc_details'] = sdc_details
387 result['changed'] = changed
388 self.module.exit_json(**result)
389
390
391 def get_powerflex_sdc_parameters():
392 """This method provide parameter required for the Ansible SDC module on
393 PowerFlex"""
394 return dict(
395 sdc_id=dict(),
396 sdc_ip=dict(),
397 sdc_name=dict(),
398 sdc_new_name=dict(), performance_profile=dict(choices=['Compact', 'HighPerformance']),
399 state=dict(required=True, type='str', choices=['present', 'absent'])
400 )
401
402
403 def main():
404 """ Create PowerFlex SDC object and perform actions on it
405 based on user input from playbook"""
406 obj = PowerFlexSdc()
407 obj.perform_module_operation()
408
409
410 if __name__ == '__main__':
411 main()