Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (C) 2020 IBM CORPORATION
5 # Author(s): Shilpi Jain <[email protected]>
6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11 DOCUMENTATION = '''
12 ---
13 module: ibm_svc_manage_cv
14 short_description: This module manages the change volume for a given volume on IBM
15 Storage Virtualize family systems
16 description:
17 - Ansible interface to manage the change volume in remote copy replication on IBM Storage Virtualize family systems.
18 version_added: "1.3.0"
19 options:
20 state:
21 description:
22 - Creates or updates (C(present)) or removes (C(absent)), a change volume.
23 choices: [absent, present]
24 required: true
25 type: str
26 rname:
27 description:
28 - Specifies the name of the remote copy relationship.
29 required: true
30 type: str
31 cvname:
32 description:
33 - Specifies the name to assign or UID to manage to the master or auxiliary change volume.
34 required: true
35 type: str
36 basevolume:
37 description:
38 - Specifies the base volume name (master or auxiliary).
39 - Required when I(state=present), to create the change volume.
40 type: str
41 ismaster:
42 description:
43 - Specifies whether the change volume is being (dis)associated with master cluster.
44 - Required when the change volume is being associated or disassociated from the master cluster.
45 type: bool
46 default: true
47 clustername:
48 description:
49 - The hostname or management IP of the Storage Virtualize system.
50 type: str
51 required: true
52 domain:
53 description:
54 - Domain for the Storage Virtualize system.
55 - Valid when hostname is used for the parameter I(clustername).
56 type: str
57 username:
58 description:
59 - REST API username for the Storage Virtualize system.
60 - The parameters I(username) and I(password) are required if not using I(token) to authenticate a user.
61 type: str
62 password:
63 description:
64 - REST API password for the Storage Virtualize system.
65 - The parameters I(username) and I(password) are required if not using I(token) to authenticate a user.
66 type: str
67 token:
68 description:
69 - The authentication token to verify a user on the Storage Virtualize system.
70 - To generate a token, use the ibm_svc_auth module.
71 type: str
72 version_added: '1.5.0'
73 validate_certs:
74 description:
75 - Validates certification.
76 default: false
77 type: bool
78 log_path:
79 description:
80 - Path of debug log file.
81 type: str
82 author:
83 - Shilpi Jain(@Shilpi-Jain1)
84 notes:
85 - This module supports C(check_mode).
86 '''
87
88 EXAMPLES = '''
89 - name: Create master change volume and associate with rcopy
90 ibm.storage_virtualize.ibm_svc_manage_cv:
91 clustername: "{{ clustername }}"
92 domain: "{{ domain }}"
93 username: "{{ username }}"
94 password: "{{ password }}"
95 log_path: /tmp/playbook.debug
96 state: present
97 rname: sample_rcopy
98 cvname: vol1_cv
99 basevolume: vol1
100 - name: Create auxiliary change volume and associate with rcopy
101 ibm.storage_virtualize.ibm_svc_manage_cv:
102 clustername: "{{ clustername }}"
103 domain: "{{ domain }}"
104 username: "{{ username }}"
105 password: "{{ password }}"
106 log_path: /tmp/playbook.debug
107 state: present
108 rname: sample_rcopy
109 cvname: vol2_aux_cv
110 basevolume: vol2
111 ismaster: false
112 - name: Delete master change volume and disassociate from rcopy
113 ibm.storage_virtualize.ibm_svc_manage_cv:
114 clustername: "{{ clustername }}"
115 domain: "{{ domain }}"
116 username: "{{ username }}"
117 password: "{{ password }}"
118 log_path: /tmp/playbook.debug
119 state: absent
120 rname: sample_rcopy
121 cvname: vol1_cv
122 - name: Delete auxiliary change volume and disassociate from rcopy
123 ibm.storage_virtualize.ibm_svc_manage_cv:
124 clustername: "{{ clustername }}"
125 domain: "{{ domain }}"
126 username: "{{ username }}"
127 password: "{{ password }}"
128 log_path: /tmp/playbook.debug
129 state: absent
130 rname: sample_rcopy
131 cvname: vol2_aux_cv
132 ismaster: false
133 '''
134
135 RETURN = '''#'''
136
137 from traceback import format_exc
138 from ansible.module_utils.basic import AnsibleModule
139 from ansible_collections.ibm.storage_virtualize.plugins.module_utils.ibm_svc_utils import IBMSVCRestApi, svc_argument_spec, get_logger
140 from ansible.module_utils._text import to_native
141
142
143 class IBMSVCchangevolume(object):
144 def __init__(self):
145 argument_spec = svc_argument_spec()
146
147 argument_spec.update(
148 dict(
149 state=dict(type='str',
150 required=True,
151 choices=['present', 'absent']),
152 rname=dict(type='str', required=True),
153 cvname=dict(type='str', required=True),
154 basevolume=dict(type='str'),
155 ismaster=dict(type='bool', default=True)
156 )
157 )
158
159 self.module = AnsibleModule(argument_spec=argument_spec,
160 supports_check_mode=True)
161
162 # logging setup
163 log_path = self.module.params['log_path']
164 log = get_logger(self.__class__.__name__, log_path)
165 self.log = log.info
166
167 # Required
168 self.state = self.module.params['state']
169 self.rname = self.module.params['rname']
170 self.cvname = self.module.params['cvname']
171
172 # Optional
173 self.basevolume = self.module.params['basevolume']
174 self.ismaster = self.module.params['ismaster']
175
176 # Handling missing mandatory parameter rname
177 if not self.rname:
178 self.module.fail_json(msg='Missing mandatory parameter: rname')
179 # Handling missing mandatory parameter cvname
180 if not self.cvname:
181 self.module.fail_json(msg='Missing mandatory parameter: cvname')
182
183 self.restapi = IBMSVCRestApi(
184 module=self.module,
185 clustername=self.module.params['clustername'],
186 domain=self.module.params['domain'],
187 username=self.module.params['username'],
188 password=self.module.params['password'],
189 validate_certs=self.module.params['validate_certs'],
190 log_path=log_path,
191 token=self.module.params['token']
192 )
193
194 def get_existing_rc(self):
195 """
196 find the remote copy relationships such as Metro Mirror, Global Mirror
197 relationships visible to the system.
198
199 Returns:
200 None if no matching instances or a list including all the matching
201 instances
202 """
203 self.log('Trying to get the remote copy relationship %s', self.rname)
204 data = self.restapi.svc_obj_info(cmd='lsrcrelationship',
205 cmdopts=None, cmdargs=[self.rname])
206
207 return data
208
209 def get_existing_vdisk(self, volname):
210 merged_result = {}
211
212 data = self.restapi.svc_obj_info(cmd='lsvdisk', cmdopts={'bytes': True},
213 cmdargs=[volname])
214
215 if not data:
216 self.log("source volume %s does not exist", volname)
217 return
218
219 if isinstance(data, list):
220 for d in data:
221 merged_result.update(d)
222 else:
223 merged_result = data
224
225 return merged_result
226
227 def change_volume_attach(self, rcrelationship_data):
228 cmdopts = {}
229
230 if rcrelationship_data['copy_type'] != 'global':
231 self.module.fail_json(msg="Relationship '%s' type must be global" % self.rname)
232
233 if self.ismaster:
234 cmdopts['masterchange'] = self.cvname
235 else:
236 cmdopts['auxchange'] = self.cvname
237
238 # command
239 cmd = 'chrcrelationship'
240 cmdargs = [self.rname]
241 self.log("updating chrcrelationship %s with properties %s", cmd, cmdopts)
242
243 # Run command
244 self.restapi.svc_run_command(cmd, cmdopts, cmdargs)
245
246 self.changed = True
247 self.log("Updated remote copy relationship ")
248
249 def change_volume_detach(self, rcrelationship_data):
250 cmdopts = {}
251
252 if self.ismaster:
253 cmdopts = {'nomasterchange': True}
254 else:
255 cmdopts = {'noauxchange': True}
256
257 # command
258 cmd = 'chrcrelationship'
259 cmdargs = [self.rname]
260 self.log("updating chrcrelationship %s with properties %s", cmd, cmdopts)
261
262 # Run command
263 self.restapi.svc_run_command(cmd, cmdopts, cmdargs)
264
265 self.changed = True
266 self.log("Updated remote copy relationship ")
267
268 def change_volume_probe(self):
269 is_update_required = False
270
271 rcrelationship_data = self.get_existing_rc()
272 if not rcrelationship_data:
273 self.module.fail_json(msg="Relationship '%s' does not exists, relationship must exists before calling this module" % self.rname)
274
275 if self.ismaster:
276 if self.cvname == rcrelationship_data['master_change_vdisk_name']:
277 self.log("Master change volume %s is already attached to the relationship", self.cvname)
278 elif rcrelationship_data['master_change_vdisk_name'] != '':
279 self.module.fail_json(msg="Master change volume %s is already attached to the relationship" % rcrelationship_data['master_change_vdisk_name'])
280 else:
281 is_update_required = True
282 else:
283 if self.cvname == rcrelationship_data['aux_change_vdisk_name']:
284 self.log("Aux change volume %s is already attached to the relationship", self.cvname)
285 elif rcrelationship_data['aux_change_vdisk_name'] != '':
286 self.module.fail_json(msg="Aux change volume %s is already attached to the relationship" % rcrelationship_data['aux_change_vdisk_name'])
287 else:
288 is_update_required = True
289
290 return is_update_required
291
292 def change_volume_delete(self):
293 # command
294 cmd = 'rmvolume'
295 cmdopts = None
296 cmdargs = [self.cvname]
297
298 # Run command
299 self.restapi.svc_run_command(cmd, cmdopts, cmdargs)
300
301 self.changed = True
302 self.log("Delete vdisk %s", self.cvname)
303
304 def change_volume_create(self):
305 if self.module.check_mode:
306 self.changed = True
307 return
308
309 if not self.basevolume:
310 self.module.fail_json(msg="You must pass in name of the master or auxiliary volume.")
311
312 # lsvdisk <basevolume>
313 vdisk_data = self.get_existing_vdisk(self.basevolume)
314 if not vdisk_data:
315 self.module.fail_json(msg="%s volume does not exist, change volume not created" % self.basevolume)
316
317 # Make command
318 cmd = 'mkvdisk'
319 cmdopts = {}
320 cmdopts['name'] = self.cvname
321 cmdopts['mdiskgrp'] = vdisk_data['mdisk_grp_name']
322 cmdopts['size'] = vdisk_data['capacity']
323 cmdopts['unit'] = 'b'
324 cmdopts['rsize'] = '0%'
325 cmdopts['autoexpand'] = True
326 cmdopts['iogrp'] = vdisk_data['IO_group_name']
327 self.log("creating vdisk command %s opts %s", cmd, cmdopts)
328
329 # Run command
330 result = self.restapi.svc_run_command(cmd, cmdopts, cmdargs=None)
331
332 if 'message' in result:
333 self.changed = True
334 self.log("Create vdisk result message %s", result['message'])
335 else:
336 self.module.fail_json(
337 msg="Failed to create vdisk [%s]" % self.cvname)
338
339 def apply(self):
340 changed = False
341 msg = None
342 modify = []
343
344 vdisk_data = self.get_existing_vdisk(self.cvname)
345
346 if vdisk_data:
347 if self.state == 'absent':
348 self.log(
349 "CHANGED: Change volume exists, requested state is 'absent'")
350 changed = True
351 elif self.state == 'present':
352 modify = self.change_volume_probe()
353 if modify:
354 changed = True
355 else:
356 self.log("No change detected")
357 else:
358 if self.state == 'present':
359 changed = True
360 self.log("CHANGED: Change volume does not exist, but requested state is '%s'", self.state)
361
362 if changed:
363 if self.module.check_mode:
364 msg = 'skipping changes due to check mode.'
365 else:
366 rcrelationship_data = self.get_existing_rc()
367 if not rcrelationship_data:
368 self.module.fail_json(msg="Relationship '%s' does not exists, relationship must exists before calling this module" % self.rname)
369 else:
370 if self.state == 'present' and modify:
371 self.change_volume_attach(rcrelationship_data)
372 msg = "Change volume %s configured to the remote copy relationship." % self.cvname
373 elif self.state == 'present':
374 self.change_volume_create()
375 self.change_volume_attach(rcrelationship_data)
376 msg = "vdisk %s has been created and configured to remote copy relationship." % self.cvname
377 elif self.state == 'absent':
378 self.change_volume_detach(rcrelationship_data)
379 self.change_volume_delete()
380 msg = "vdisk %s has been deleted and detached from remote copy relationship." % self.cvname
381 else:
382 self.log("Exiting with no changes")
383 if self.state in ['absent']:
384 msg = "Change volume [%s] does not exist." % self.cvname
385 else:
386 msg = "No Modifications detected, Change volume [%s] already configured." % self.cvname
387
388 self.module.exit_json(msg=msg, changed=changed)
389
390
391 def main():
392 v = IBMSVCchangevolume()
393 try:
394 v.apply()
395 except Exception as e:
396 v.log("Exception in apply(): \n%s", format_exc())
397 v.module.fail_json(msg="Module failed. Error [%s]." % to_native(e))
398
399
400 if __name__ == '__main__':
401 main()