Debian Codegraph @

ansible_collections/ibm/storage_virtualize/plugins/modules/ibm_sv_manage_drive.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (C) 2024 IBM CORPORATION
5 # Author(s): Sumit Kumar Gupta <[email protected]>
6 #
7 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12 DOCUMENTATION = r'''
13 ---
14 module: ibm_sv_manage_drive
15 short_description: This module manages drives on IBM Storage Virtualize family storage systems
16 description:
17 - Ansible interface to manage drive-related operations.
18 version_added: "2.4.0"
19 options:
20 clustername:
21 description:
22 - The hostname or management IP of the Storage Virtualize storage system.
23 required: true
24 type: str
25 domain:
26 description:
27 - Domain for the Storage Virtualize storage system.
28 - Valid when hostname is used for the parameter I(clustername).
29 type: str
30 username:
31 description:
32 - REST API username for the Storage Virtualize storage system.
33 - The parameters I(username) and I(password) are required if not using I(token) to authenticate a user.
34 type: str
35 password:
36 description:
37 - REST API password for the Storage Virtualize storage system.
38 - The parameters I(username) and I(password) are required if not using I(token) to authenticate a user.
39 type: str
40 token:
41 description:
42 - The authentication token to verify a user on the Storage Virtualize storage system.
43 - To generate a token, use the ibm_svc_auth module.
44 type: str
45 drive_state:
46 description:
47 - Specifies the desired usability state of the drive.
48 choices: [ unused, candidate, spare, failed ]
49 type: str
50 task:
51 description:
52 - Specifies a task to be performed on drive.
53 choices: [ format, certify, recover, erase, triggerdump ]
54 type: str
55 drive_id:
56 description:
57 - Specifies the drive id.
58 required: true
59 type: int
60 log_path:
61 description:
62 - Path of debug log file.
63 type: str
64 validate_certs:
65 description:
66 - Validates certification.
67 default: false
68 type: bool
69
70 author:
71 - Sumit Kumar Gupta (@sumitguptaibm)
72 notes:
73 - This module supports C(check_mode).
74 - If error code I(CMMVC6624E) is returned after running a I(recover) task, most likely drive has already
75 been recovered.
76 '''
77
78 EXAMPLES = r'''
79 - name: Change drive state to candidate
80 ibm.storage_virtualize.ibm_sv_manage_drive:
81 clustername: "{{ cluster }}"
82 username: "{{ username }}"
83 password: "{{ password }}"
84 drive_id: 5
85 drive_state: candidate
86 log_path: /tmp/playbook.debug
87
88 - name: Format a drive
89 ibm.storage_virtualize.ibm_sv_manage_drive:
90 clustername: "{{ cluster }}"
91 username: "{{ username }}"
92 password: "{{ password }}"
93 drive_id: 5
94 task: format
95 log_path: /tmp/playbook.debug
96
97 - name: Trigger a drive dump
98 ibm.storage_virtualize.ibm_sv_manage_drive:
99 clustername: "{{ cluster }}"
100 username: "{{ username }}"
101 password: "{{ password }}"
102 drive_id: 5
103 task: triggerdump
104 log_path: /tmp/playbook.debug
105 '''
106
107 RETURN = r'''#'''
108
109 from traceback import format_exc
110 from ansible.module_utils.basic import AnsibleModule
111 from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_utils import (IBMSVCRestApi,
112 svc_argument_spec,
113 get_logger)
114 from ansible.module_utils._text import to_native
115
116
117 class IBMSVDriveMgmt(object):
118 def __init__(self):
119 argument_spec = svc_argument_spec()
120 argument_spec.update(
121 dict(
122 drive_state=dict(type='str', choices=['unused', 'candidate', 'spare', 'failed']),
123 task=dict(type='str', choices=['format', 'certify', 'recover', 'erase', 'triggerdump']),
124 drive_id=dict(type='int', required=True)
125 )
126 )
127
128 self.module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
129
130 # Required parameter(s)
131 self.drive_id = self.module.params['drive_id']
132
133 # Optional parameters
134 self.drive_state = self.module.params['drive_state']
135 self.task = self.module.params['task']
136 self.msg = ""
137
138 self.basic_checks()
139
140 # logging setup
141 log_path = self.module.params['log_path']
142 log = get_logger(self.__class__.__name__, log_path)
143 self.log = log.info
144
145 # Initialize changed variable
146 self.changed = False
147
148 # creating an instance of IBMSVCRestApi
149 self.restapi = IBMSVCRestApi(
150 module=self.module,
151 clustername=self.module.params['clustername'],
152 domain=self.module.params['domain'],
153 username=self.module.params['username'],
154 password=self.module.params['password'],
155 validate_certs=self.module.params['validate_certs'],
156 log_path=log_path,
157 token=self.module.params['token']
158 )
159
160 def basic_checks(self):
161 if self.drive_id is None:
162 # Mandatory parameter drive_id check
163 self.module.fail_json(msg="Missing mandatory parameter: drive_id")
164 if self.drive_state and self.task:
165 # drive_state and task are mutually-exclusive
166 self.module.fail_json(msg='Mutually exclusive parameters: drive_state and task')
167
168 def exec_drive_cmd(self):
169 cmd = 'chdrive'
170 cmdopts = {}
171 cmdargs = []
172 cmdargs.append(self.drive_id)
173
174 if self.task:
175 # If task is to trigger dump, use triggerdrivedump instead of chdrive
176 if self.task == 'triggerdump':
177 cmd = 'triggerdrivedump'
178
179 else:
180 # For other tasks such as format, erase, certify, recover, check currently running tasks if any
181 cmdopts['task'] = self.task
182 currently_running_drive_task = self.restapi.svc_obj_info(
183 cmd='lsdriveprogress',
184 cmdopts=None,
185 cmdargs=cmdargs
186 )
187 task_list = ['format', 'certify', 'recover', 'erase']
188 if currently_running_drive_task:
189 if self.task == currently_running_drive_task['task']:
190 # If same task is already running for this drive_id, declare success and return
191 self.msg = 'Task (%s) is already running on drive (%s).' % (self.task, self.drive_id)
192 return
193
194 if currently_running_drive_task['task'] in task_list:
195 # If any other tasks are being run, return SVC error
196 self.module.fail_json(msg="CMMVC6625E The command cannot be initiated"
197 " because a task is in progress on the drive.")
198
199 self.restapi.svc_run_command(cmd, cmdopts, cmdargs=cmdargs)
200 self.msg = 'Task (%s) started on drive (%s).' % (self.task, self.drive_id)
201 self.changed = True
202 return
203
204 if self.drive_state:
205 cmdopts['use'] = self.drive_state
206 result = self.restapi.svc_run_command(cmd, cmdopts, cmdargs)
207 if result == "":
208 self.changed = True
209 self.msg = "Drive usability state changed successfully for drive ID %s." % self.drive_id
210 else:
211 # Show SVC error message to user
212 self.module.fail_json(msg=result, changed=False)
213 return
214
215 def apply(self):
216 msg = None
217 if self.module.check_mode:
218 self.basic_checks()
219 self.changed = True
220 else:
221 self.exec_drive_cmd()
222 self.module.exit_json(msg=self.msg, changed=self.changed)
223
224
225 def main():
226 v = IBMSVDriveMgmt()
227 try:
228 v.apply()
229 except Exception as e:
230 v.log("Exception in apply(): \n%s", format_exc())
231 v.module.fail_json(msg="Module failed. Error [%s]." % to_native(e))
232
233
234 if __name__ == '__main__':
235 main()