Debian Codegraph @

ansible_collections/community/general/plugins/modules/hwc_evs_disk.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2019 Huawei
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 ###############################################################################
10 # Documentation
11 ###############################################################################
12
13 DOCUMENTATION = r"""
14 module: hwc_evs_disk
15 description:
16 - Block storage management.
17 short_description: Creates a resource of Evs/Disk in Huawei Cloud
18 version_added: '0.2.0'
19 author: Huawei Inc. (@huaweicloud)
20 requirements:
21 - keystoneauth1 >= 3.6.0
22 attributes:
23 check_mode:
24 support: full
25 diff_mode:
26 support: none
27 options:
28 state:
29 description:
30 - Whether the given object should exist in Huawei Cloud.
31 type: str
32 choices: ['present', 'absent']
33 default: 'present'
34 timeouts:
35 description:
36 - The timeouts for each operations.
37 type: dict
38 default: {}
39 suboptions:
40 create:
41 description:
42 - The timeouts for create operation.
43 type: str
44 default: '30m'
45 update:
46 description:
47 - The timeouts for update operation.
48 type: str
49 default: '30m'
50 delete:
51 description:
52 - The timeouts for delete operation.
53 type: str
54 default: '30m'
55 availability_zone:
56 description:
57 - Specifies the AZ where you want to create the disk.
58 type: str
59 required: true
60 name:
61 description:
62 - Specifies the disk name. The value can contain a maximum of 255 bytes.
63 type: str
64 required: true
65 volume_type:
66 description:
67 - Specifies the disk type. Currently, the value can be SSD, SAS, or SATA.
68 - SSD specifies the ultra-high I/O disk type.
69 - SAS specifies the high I/O disk type.
70 - SATA specifies the common I/O disk type.
71 - If the specified disk type is not available in the AZ, the disk creation fails. If the EVS disk is created from a
72 snapshot, the volume_type field must be the same as that of the snapshot's source disk.
73 type: str
74 required: true
75 backup_id:
76 description:
77 - Specifies the ID of the backup that can be used to create a disk. This parameter is mandatory when you use a backup
78 to create the disk.
79 type: str
80 description:
81 description:
82 - Specifies the disk description. The value can contain a maximum of 255 bytes.
83 type: str
84 enable_full_clone:
85 description:
86 - If the disk is created from a snapshot and linked cloning needs to be used, set this parameter to True.
87 type: bool
88 enable_scsi:
89 description:
90 - If this parameter is set to V(true), the disk device type is SCSI, which allows ECS OSs to directly access underlying
91 storage media. SCSI reservation command is supported. If this parameter is set to V(false), the disk device type is
92 VBD, which supports only simple SCSI read/write commands.
93 - If parameter enable_share is set to True and this parameter is not specified, shared SCSI disks are created. SCSI
94 EVS disks cannot be created from backups, which means that this parameter cannot be True if backup_id has been specified.
95 type: bool
96 enable_share:
97 description:
98 - Specifies whether the disk is shareable. The default value is False.
99 type: bool
100 encryption_id:
101 description:
102 - Specifies the encryption ID. The length of it fixes at 36 bytes.
103 type: str
104 enterprise_project_id:
105 description:
106 - Specifies the enterprise project ID. This ID is associated with the disk during the disk creation. If it is not specified,
107 the disk is bound to the default enterprise project.
108 type: str
109 image_id:
110 description:
111 - Specifies the image ID. If this parameter is specified, the disk is created from an image. BMS system disks cannot
112 be created from BMS images.
113 type: str
114 size:
115 description:
116 - Specifies the disk size, in GB. Its values are as follows, System disk 1 GB to 1024 GB, Data disk 10 GB to 32768 GB.
117 This parameter is mandatory when you create an empty disk or use an image or a snapshot to create a disk. If you use
118 an image or a snapshot to create a disk, the disk size must be greater than or equal to the image or snapshot size.
119 This parameter is optional when you use a backup to create a disk. If this parameter is not specified, the disk size
120 is equal to the backup size.
121 type: int
122 snapshot_id:
123 description:
124 - Specifies the snapshot ID. If this parameter is specified, the disk is created from a snapshot.
125 type: str
126 extends_documentation_fragment:
127 - community.general._hwc
128 - community.general._attributes
129 """
130
131 EXAMPLES = r"""
132 # test create disk
133 - name: Create a disk
134 community.general.hwc_evs_disk:
135 availability_zone: "cn-north-1a"
136 name: "ansible_evs_disk_test"
137 volume_type: "SATA"
138 size: 10
139 """
140
141 RETURN = r"""
142 availability_zone:
143 description:
144 - Specifies the AZ where you want to create the disk.
145 type: str
146 returned: success
147 name:
148 description:
149 - Specifies the disk name. The value can contain a maximum of 255 bytes.
150 type: str
151 returned: success
152 volume_type:
153 description:
154 - Specifies the disk type. Currently, the value can be SSD, SAS, or SATA.
155 - SSD specifies the ultra-high I/O disk type.
156 - SAS specifies the high I/O disk type.
157 - SATA specifies the common I/O disk type.
158 - If the specified disk type is not available in the AZ, the disk creation fails. If the EVS disk is created from a snapshot,
159 the volume_type field must be the same as that of the snapshot's source disk.
160 type: str
161 returned: success
162 backup_id:
163 description:
164 - Specifies the ID of the backup that can be used to create a disk. This parameter is mandatory when you use a backup
165 to create the disk.
166 type: str
167 returned: success
168 description:
169 description:
170 - Specifies the disk description. The value can contain a maximum of 255 bytes.
171 type: str
172 returned: success
173 enable_full_clone:
174 description:
175 - If the disk is created from a snapshot and linked cloning needs to be used, set this parameter to True.
176 type: bool
177 returned: success
178 enable_scsi:
179 description:
180 - If this parameter is set to V(true), the disk device type is SCSI, which allows ECS OSs to directly access underlying
181 storage media. SCSI reservation command is supported. If this parameter is set to V(false), the disk device type is
182 VBD, which supports only simple SCSI read/write commands.
183 - If parameter enable_share is set to True and this parameter is not specified, shared SCSI disks are created. SCSI EVS
184 disks cannot be created from backups, which means that this parameter cannot be True if backup_id has been specified.
185 type: bool
186 returned: success
187 enable_share:
188 description:
189 - Specifies whether the disk is shareable. The default value is False.
190 type: bool
191 returned: success
192 encryption_id:
193 description:
194 - Specifies the encryption ID. The length of it fixes at 36 bytes.
195 type: str
196 returned: success
197 enterprise_project_id:
198 description:
199 - Specifies the enterprise project ID. This ID is associated with the disk during the disk creation. If it is not specified,
200 the disk is bound to the default enterprise project.
201 type: str
202 returned: success
203 image_id:
204 description:
205 - Specifies the image ID. If this parameter is specified, the disk is created from an image. BMS system disks cannot be
206 created from BMS images.
207 type: str
208 returned: success
209 size:
210 description:
211 - Specifies the disk size, in GB. Its values are as follows, System disk 1 GB to 1024 GB, Data disk 10 GB to 32768 GB.
212 This parameter is mandatory when you create an empty disk or use an image or a snapshot to create a disk. If you use
213 an image or a snapshot to create a disk, the disk size must be greater than or equal to the image or snapshot size.
214 This parameter is optional when you use a backup to create a disk. If this parameter is not specified, the disk size
215 is equal to the backup size.
216 type: int
217 returned: success
218 snapshot_id:
219 description:
220 - Specifies the snapshot ID. If this parameter is specified, the disk is created from a snapshot.
221 type: str
222 returned: success
223 attachments:
224 description:
225 - Specifies the disk attachment information.
226 type: list
227 elements: dict
228 returned: success
229 contains:
230 attached_at:
231 description:
232 - Specifies the time when the disk was attached. Time format is 'UTC YYYY-MM-DDTHH:MM:SS'.
233 type: str
234 returned: success
235 attachment_id:
236 description:
237 - Specifies the ID of the attachment information.
238 type: str
239 returned: success
240 device:
241 description:
242 - Specifies the device name.
243 type: str
244 returned: success
245 server_id:
246 description:
247 - Specifies the ID of the server to which the disk is attached.
248 type: str
249 returned: success
250 backup_policy_id:
251 description:
252 - Specifies the backup policy ID.
253 type: str
254 returned: success
255 created_at:
256 description:
257 - Specifies the time when the disk was created. Time format is 'UTC YYYY-MM-DDTHH:MM:SS'.
258 type: str
259 returned: success
260 is_bootable:
261 description:
262 - Specifies whether the disk is bootable.
263 type: bool
264 returned: success
265 is_readonly:
266 description:
267 - Specifies whether the disk is read-only or read/write. True indicates that the disk is read-only. False indicates that
268 the disk is read/write.
269 type: bool
270 returned: success
271 source_volume_id:
272 description:
273 - Specifies the source disk ID. This parameter has a value if the disk is created from a source disk.
274 type: str
275 returned: success
276 status:
277 description:
278 - Specifies the disk status.
279 type: str
280 returned: success
281 tags:
282 description:
283 - Specifies the disk tags.
284 type: dict
285 returned: success
286 """
287
288 from ansible_collections.community.general.plugins.module_utils._hwc_utils import (
289 Config,
290 HwcClientException,
291 HwcModule,
292 are_different_dicts,
293 build_path,
294 get_region,
295 is_empty_value,
296 navigate_value,
297 wait_to_finish,
298 )
299
300
301 def build_module():
302 return HwcModule(
303 argument_spec=dict(
304 state=dict(default="present", choices=["present", "absent"], type="str"),
305 timeouts=dict(
306 type="dict",
307 options=dict(
308 create=dict(default="30m", type="str"),
309 update=dict(default="30m", type="str"),
310 delete=dict(default="30m", type="str"),
311 ),
312 default=dict(),
313 ),
314 availability_zone=dict(type="str", required=True),
315 name=dict(type="str", required=True),
316 volume_type=dict(type="str", required=True),
317 backup_id=dict(type="str"),
318 description=dict(type="str"),
319 enable_full_clone=dict(type="bool"),
320 enable_scsi=dict(type="bool"),
321 enable_share=dict(type="bool"),
322 encryption_id=dict(type="str"),
323 enterprise_project_id=dict(type="str"),
324 image_id=dict(type="str"),
325 size=dict(type="int"),
326 snapshot_id=dict(type="str"),
327 ),
328 supports_check_mode=True,
329 )
330
331
332 def main():
333 """Main function"""
334
335 module = build_module()
336 config = Config(module, "evs")
337
338 try:
339 _init(config)
340 is_exist = module.params.get("id")
341
342 result = None
343 changed = False
344 if module.params["state"] == "present":
345 if not is_exist:
346 if not module.check_mode:
347 create(config)
348 changed = True
349
350 inputv = user_input_parameters(module)
351 resp, array_index = read_resource(config)
352 result = build_state(inputv, resp, array_index)
353 set_readonly_options(inputv, result)
354 if are_different_dicts(inputv, result):
355 if not module.check_mode:
356 update(config, inputv, result)
357
358 inputv = user_input_parameters(module)
359 resp, array_index = read_resource(config)
360 result = build_state(inputv, resp, array_index)
361 set_readonly_options(inputv, result)
362 if are_different_dicts(inputv, result):
363 raise Exception("Update resource failed, some attributes are not updated")
364
365 changed = True
366
367 result["id"] = module.params.get("id")
368 else:
369 result = dict()
370 if is_exist:
371 if not module.check_mode:
372 delete(config)
373 changed = True
374
375 except Exception as ex:
376 module.fail_json(msg=str(ex))
377
378 else:
379 result["changed"] = changed
380 module.exit_json(**result)
381
382
383 def _init(config):
384 module = config.module
385 if module.params.get("id"):
386 return
387
388 v = search_resource(config)
389 n = len(v)
390 if n > 1:
391 raise Exception(f"find more than one resources({', '.join([navigate_value(i, ['id']) for i in v])})")
392
393 if n == 1:
394 module.params["id"] = navigate_value(v[0], ["id"])
395
396
397 def user_input_parameters(module):
398 return {
399 "availability_zone": module.params.get("availability_zone"),
400 "backup_id": module.params.get("backup_id"),
401 "description": module.params.get("description"),
402 "enable_full_clone": module.params.get("enable_full_clone"),
403 "enable_scsi": module.params.get("enable_scsi"),
404 "enable_share": module.params.get("enable_share"),
405 "encryption_id": module.params.get("encryption_id"),
406 "enterprise_project_id": module.params.get("enterprise_project_id"),
407 "image_id": module.params.get("image_id"),
408 "name": module.params.get("name"),
409 "size": module.params.get("size"),
410 "snapshot_id": module.params.get("snapshot_id"),
411 "volume_type": module.params.get("volume_type"),
412 }
413
414
415 def create(config):
416 module = config.module
417 client = config.client(get_region(module), "volumev3", "project")
418 timeout = 60 * int(module.params["timeouts"]["create"].rstrip("m"))
419 opts = user_input_parameters(module)
420 opts["ansible_module"] = module
421
422 params = build_create_parameters(opts)
423 r = send_create_request(module, params, client)
424
425 client1 = config.client(get_region(module), "volume", "project")
426 client1.endpoint = client1.endpoint.replace("/v2/", "/v1/")
427 obj = async_wait(config, r, client1, timeout)
428 module.params["id"] = navigate_value(obj, ["entities", "volume_id"])
429
430
431 def update(config, expect_state, current_state):
432 module = config.module
433 expect_state["current_state"] = current_state
434 current_state["current_state"] = current_state
435 client = config.client(get_region(module), "evs", "project")
436 timeout = 60 * int(module.params["timeouts"]["update"].rstrip("m"))
437
438 params = build_update_parameters(expect_state)
439 params1 = build_update_parameters(current_state)
440 if params and are_different_dicts(params, params1):
441 send_update_request(module, params, client)
442
443 params = build_extend_disk_parameters(expect_state)
444 params1 = build_extend_disk_parameters(current_state)
445 if params and are_different_dicts(params, params1):
446 client1 = config.client(get_region(module), "evsv2.1", "project")
447 r = send_extend_disk_request(module, params, client1)
448
449 client1 = config.client(get_region(module), "volume", "project")
450 client1.endpoint = client1.endpoint.replace("/v2/", "/v1/")
451 async_wait(config, r, client1, timeout)
452
453
454 def delete(config):
455 module = config.module
456 client = config.client(get_region(module), "evs", "project")
457 timeout = 60 * int(module.params["timeouts"]["delete"].rstrip("m"))
458
459 r = send_delete_request(module, None, client)
460
461 client = config.client(get_region(module), "volume", "project")
462 client.endpoint = client.endpoint.replace("/v2/", "/v1/")
463 async_wait(config, r, client, timeout)
464
465
466 def read_resource(config):
467 module = config.module
468 client = config.client(get_region(module), "volumev3", "project")
469
470 res = {}
471
472 r = send_read_request(module, client)
473 res["read"] = fill_read_resp_body(r)
474
475 return res, None
476
477
478 def build_state(opts, response, array_index):
479 states = flatten_options(response, array_index)
480 set_unreadable_options(opts, states)
481 return states
482
483
484 def _build_query_link(opts):
485 query_params = []
486
487 v = navigate_value(opts, ["enable_share"])
488 if v or v in [False, 0]:
489 query_params.append(f"multiattach={str(v) if v else str(v).lower()}")
490
491 v = navigate_value(opts, ["name"])
492 if v or v in [False, 0]:
493 query_params.append(f"name={str(v) if v else str(v).lower()}")
494
495 v = navigate_value(opts, ["availability_zone"])
496 if v or v in [False, 0]:
497 query_params.append(f"availability_zone={str(v) if v else str(v).lower()}")
498
499 query_link = "?limit=10&offset={start}"
500 if query_params:
501 query_link += f"&{'&'.join(query_params)}"
502
503 return query_link
504
505
506 def search_resource(config):
507 module = config.module
508 client = config.client(get_region(module), "volumev3", "project")
509 opts = user_input_parameters(module)
510 name = module.params.get("name")
511 query_link = _build_query_link(opts)
512 link = f"os-vendor-volumes/detail{query_link}"
513
514 result = []
515 p = {"start": 0}
516 while True:
517 url = link.format(**p)
518 r = send_list_request(module, client, url)
519 if not r:
520 break
521
522 for item in r:
523 if name == item.get("name"):
524 result.append(item)
525
526 if len(result) > 1:
527 break
528
529 p["start"] += len(r)
530
531 return result
532
533
534 def build_create_parameters(opts):
535 params = dict()
536
537 v = navigate_value(opts, ["availability_zone"], None)
538 if not is_empty_value(v):
539 params["availability_zone"] = v
540
541 v = navigate_value(opts, ["backup_id"], None)
542 if not is_empty_value(v):
543 params["backup_id"] = v
544
545 v = navigate_value(opts, ["description"], None)
546 if not is_empty_value(v):
547 params["description"] = v
548
549 v = navigate_value(opts, ["enterprise_project_id"], None)
550 if not is_empty_value(v):
551 params["enterprise_project_id"] = v
552
553 v = navigate_value(opts, ["image_id"], None)
554 if not is_empty_value(v):
555 params["imageRef"] = v
556
557 v = expand_create_metadata(opts, None)
558 if not is_empty_value(v):
559 params["metadata"] = v
560
561 v = navigate_value(opts, ["enable_share"], None)
562 if not is_empty_value(v):
563 params["multiattach"] = v
564
565 v = navigate_value(opts, ["name"], None)
566 if not is_empty_value(v):
567 params["name"] = v
568
569 v = navigate_value(opts, ["size"], None)
570 if not is_empty_value(v):
571 params["size"] = v
572
573 v = navigate_value(opts, ["snapshot_id"], None)
574 if not is_empty_value(v):
575 params["snapshot_id"] = v
576
577 v = navigate_value(opts, ["volume_type"], None)
578 if not is_empty_value(v):
579 params["volume_type"] = v
580
581 if not params:
582 return params
583
584 params = {"volume": params}
585
586 return params
587
588
589 def expand_create_metadata(d, array_index):
590 r = dict()
591
592 v = navigate_value(d, ["encryption_id"], array_index)
593 if not is_empty_value(v):
594 r["__system__cmkid"] = v
595
596 v = expand_create_metadata_system_encrypted(d, array_index)
597 if not is_empty_value(v):
598 r["__system__encrypted"] = v
599
600 v = expand_create_metadata_full_clone(d, array_index)
601 if not is_empty_value(v):
602 r["full_clone"] = v
603
604 v = expand_create_metadata_hw_passthrough(d, array_index)
605 if not is_empty_value(v):
606 r["hw:passthrough"] = v
607
608 return r
609
610
611 def expand_create_metadata_system_encrypted(d, array_index):
612 v = navigate_value(d, ["encryption_id"], array_index)
613 return "1" if v else ""
614
615
616 def expand_create_metadata_full_clone(d, array_index):
617 v = navigate_value(d, ["enable_full_clone"], array_index)
618 return "0" if v else ""
619
620
621 def expand_create_metadata_hw_passthrough(d, array_index):
622 v = navigate_value(d, ["enable_scsi"], array_index)
623 if v is None:
624 return v
625 return "true" if v else "false"
626
627
628 def send_create_request(module, params, client):
629 url = "cloudvolumes"
630 try:
631 r = client.post(url, params)
632 except HwcClientException as ex:
633 msg = f"module(hwc_evs_disk): error running api(create), error: {ex}"
634 module.fail_json(msg=msg)
635
636 return r
637
638
639 def build_update_parameters(opts):
640 params = dict()
641
642 v = navigate_value(opts, ["description"], None)
643 if v is not None:
644 params["description"] = v
645
646 v = navigate_value(opts, ["name"], None)
647 if not is_empty_value(v):
648 params["name"] = v
649
650 if not params:
651 return params
652
653 params = {"volume": params}
654
655 return params
656
657
658 def send_update_request(module, params, client):
659 url = build_path(module, "cloudvolumes/{id}")
660
661 try:
662 r = client.put(url, params)
663 except HwcClientException as ex:
664 msg = f"module(hwc_evs_disk): error running api(update), error: {ex}"
665 module.fail_json(msg=msg)
666
667 return r
668
669
670 def send_delete_request(module, params, client):
671 url = build_path(module, "cloudvolumes/{id}")
672
673 try:
674 r = client.delete(url, params)
675 except HwcClientException as ex:
676 msg = f"module(hwc_evs_disk): error running api(delete), error: {ex}"
677 module.fail_json(msg=msg)
678
679 return r
680
681
682 def build_extend_disk_parameters(opts):
683 params = dict()
684
685 v = expand_extend_disk_os_extend(opts, None)
686 if not is_empty_value(v):
687 params["os-extend"] = v
688
689 return params
690
691
692 def expand_extend_disk_os_extend(d, array_index):
693 r = dict()
694
695 v = navigate_value(d, ["size"], array_index)
696 if not is_empty_value(v):
697 r["new_size"] = v
698
699 return r
700
701
702 def send_extend_disk_request(module, params, client):
703 url = build_path(module, "cloudvolumes/{id}/action")
704
705 try:
706 r = client.post(url, params)
707 except HwcClientException as ex:
708 msg = f"module(hwc_evs_disk): error running api(extend_disk), error: {ex}"
709 module.fail_json(msg=msg)
710
711 return r
712
713
714 def async_wait(config, result, client, timeout):
715 module = config.module
716
717 path_parameters = {
718 "job_id": ["job_id"],
719 }
720 data = {key: navigate_value(result, path) for key, path in path_parameters.items()}
721
722 url = build_path(module, "jobs/{job_id}", data)
723
724 def _query_status():
725 r = None
726 try:
727 r = client.get(url, timeout=timeout)
728 except HwcClientException:
729 return None, ""
730
731 try:
732 s = navigate_value(r, ["status"])
733 return r, s
734 except Exception:
735 return None, ""
736
737 try:
738 return wait_to_finish(["SUCCESS"], ["RUNNING", "INIT"], _query_status, timeout)
739 except Exception as ex:
740 module.fail_json(msg=f"module(hwc_evs_disk): error waiting to be done, error= {ex}")
741
742
743 def send_read_request(module, client):
744 url = build_path(module, "os-vendor-volumes/{id}")
745
746 r = None
747 try:
748 r = client.get(url)
749 except HwcClientException as ex:
750 msg = f"module(hwc_evs_disk): error running api(read), error: {ex}"
751 module.fail_json(msg=msg)
752
753 return navigate_value(r, ["volume"], None)
754
755
756 def fill_read_resp_body(body):
757 result = dict()
758
759 v = fill_read_resp_attachments(body.get("attachments"))
760 result["attachments"] = v
761
762 result["availability_zone"] = body.get("availability_zone")
763
764 result["bootable"] = body.get("bootable")
765
766 result["created_at"] = body.get("created_at")
767
768 result["description"] = body.get("description")
769
770 result["enterprise_project_id"] = body.get("enterprise_project_id")
771
772 result["id"] = body.get("id")
773
774 v = fill_read_resp_metadata(body.get("metadata"))
775 result["metadata"] = v
776
777 result["multiattach"] = body.get("multiattach")
778
779 result["name"] = body.get("name")
780
781 result["size"] = body.get("size")
782
783 result["snapshot_id"] = body.get("snapshot_id")
784
785 result["source_volid"] = body.get("source_volid")
786
787 result["status"] = body.get("status")
788
789 result["tags"] = body.get("tags")
790
791 v = fill_read_resp_volume_image_metadata(body.get("volume_image_metadata"))
792 result["volume_image_metadata"] = v
793
794 result["volume_type"] = body.get("volume_type")
795
796 return result
797
798
799 def fill_read_resp_attachments(value):
800 if not value:
801 return None
802
803 result = []
804 for item in value:
805 val = dict()
806
807 val["attached_at"] = item.get("attached_at")
808
809 val["attachment_id"] = item.get("attachment_id")
810
811 val["device"] = item.get("device")
812
813 val["server_id"] = item.get("server_id")
814
815 result.append(val)
816
817 return result
818
819
820 def fill_read_resp_metadata(value):
821 if not value:
822 return None
823
824 result = dict()
825
826 result["__system__cmkid"] = value.get("__system__cmkid")
827
828 result["attached_mode"] = value.get("attached_mode")
829
830 result["full_clone"] = value.get("full_clone")
831
832 result["hw:passthrough"] = value.get("hw:passthrough")
833
834 result["policy"] = value.get("policy")
835
836 result["readonly"] = value.get("readonly")
837
838 return result
839
840
841 def fill_read_resp_volume_image_metadata(value):
842 if not value:
843 return None
844
845 result = dict()
846
847 result["id"] = value.get("id")
848
849 return result
850
851
852 def flatten_options(response, array_index):
853 r = dict()
854
855 v = flatten_attachments(response, array_index)
856 r["attachments"] = v
857
858 v = navigate_value(response, ["read", "availability_zone"], array_index)
859 r["availability_zone"] = v
860
861 v = navigate_value(response, ["read", "metadata", "policy"], array_index)
862 r["backup_policy_id"] = v
863
864 v = navigate_value(response, ["read", "created_at"], array_index)
865 r["created_at"] = v
866
867 v = navigate_value(response, ["read", "description"], array_index)
868 r["description"] = v
869
870 v = flatten_enable_full_clone(response, array_index)
871 r["enable_full_clone"] = v
872
873 v = flatten_enable_scsi(response, array_index)
874 r["enable_scsi"] = v
875
876 v = navigate_value(response, ["read", "multiattach"], array_index)
877 r["enable_share"] = v
878
879 v = navigate_value(response, ["read", "metadata", "__system__cmkid"], array_index)
880 r["encryption_id"] = v
881
882 v = navigate_value(response, ["read", "enterprise_project_id"], array_index)
883 r["enterprise_project_id"] = v
884
885 v = navigate_value(response, ["read", "volume_image_metadata", "id"], array_index)
886 r["image_id"] = v
887
888 v = flatten_is_bootable(response, array_index)
889 r["is_bootable"] = v
890
891 v = flatten_is_readonly(response, array_index)
892 r["is_readonly"] = v
893
894 v = navigate_value(response, ["read", "name"], array_index)
895 r["name"] = v
896
897 v = navigate_value(response, ["read", "size"], array_index)
898 r["size"] = v
899
900 v = navigate_value(response, ["read", "snapshot_id"], array_index)
901 r["snapshot_id"] = v
902
903 v = navigate_value(response, ["read", "source_volid"], array_index)
904 r["source_volume_id"] = v
905
906 v = navigate_value(response, ["read", "status"], array_index)
907 r["status"] = v
908
909 v = navigate_value(response, ["read", "tags"], array_index)
910 r["tags"] = v
911
912 v = navigate_value(response, ["read", "volume_type"], array_index)
913 r["volume_type"] = v
914
915 return r
916
917
918 def flatten_attachments(d, array_index):
919 v = navigate_value(d, ["read", "attachments"], array_index)
920 if not v:
921 return None
922 n = len(v)
923 result = []
924
925 new_ai = dict()
926 if array_index:
927 new_ai.update(array_index)
928
929 for i in range(n):
930 new_ai["read.attachments"] = i
931
932 val = dict()
933
934 v = navigate_value(d, ["read", "attachments", "attached_at"], new_ai)
935 val["attached_at"] = v
936
937 v = navigate_value(d, ["read", "attachments", "attachment_id"], new_ai)
938 val["attachment_id"] = v
939
940 v = navigate_value(d, ["read", "attachments", "device"], new_ai)
941 val["device"] = v
942
943 v = navigate_value(d, ["read", "attachments", "server_id"], new_ai)
944 val["server_id"] = v
945
946 for v in val.values():
947 if v is not None:
948 result.append(val)
949 break
950
951 return result if result else None
952
953
954 def flatten_enable_full_clone(d, array_index):
955 v = navigate_value(d, ["read", "metadata", "full_clone"], array_index)
956 if v is None:
957 return v
958 return True if v == "0" else False
959
960
961 def flatten_enable_scsi(d, array_index):
962 v = navigate_value(d, ["read", "metadata", "hw:passthrough"], array_index)
963 if v is None:
964 return v
965 return True if v in ["true", "True"] else False
966
967
968 def flatten_is_bootable(d, array_index):
969 v = navigate_value(d, ["read", "bootable"], array_index)
970 if v is None:
971 return v
972 return True if v in ["true", "True"] else False
973
974
975 def flatten_is_readonly(d, array_index):
976 v = navigate_value(d, ["read", "metadata", "readonly"], array_index)
977 if v is None:
978 return v
979 return True if v in ["true", "True"] else False
980
981
982 def set_unreadable_options(opts, states):
983 states["backup_id"] = opts.get("backup_id")
984
985
986 def set_readonly_options(opts, states):
987 opts["attachments"] = states.get("attachments")
988
989 opts["backup_policy_id"] = states.get("backup_policy_id")
990
991 opts["created_at"] = states.get("created_at")
992
993 opts["is_bootable"] = states.get("is_bootable")
994
995 opts["is_readonly"] = states.get("is_readonly")
996
997 opts["source_volume_id"] = states.get("source_volume_id")
998
999 opts["status"] = states.get("status")
1000
1001 opts["tags"] = states.get("tags")
1002
1003
1004 def send_list_request(module, client, url):
1005 r = None
1006 try:
1007 r = client.get(url)
1008 except HwcClientException as ex:
1009 msg = f"module(hwc_evs_disk): error running api(list), error: {ex}"
1010 module.fail_json(msg=msg)
1011
1012 return navigate_value(r, ["volumes"], None)
1013
1014
1015 def expand_list_metadata(d, array_index):
1016 r = dict()
1017
1018 v = navigate_value(d, ["encryption_id"], array_index)
1019 r["__system__cmkid"] = v
1020
1021 r["attached_mode"] = None
1022
1023 v = navigate_value(d, ["enable_full_clone"], array_index)
1024 r["full_clone"] = v
1025
1026 v = navigate_value(d, ["enable_scsi"], array_index)
1027 r["hw:passthrough"] = v
1028
1029 r["policy"] = None
1030
1031 r["readonly"] = None
1032
1033 for v in r.values():
1034 if v is not None:
1035 return r
1036 return None
1037
1038
1039 def expand_list_volume_image_metadata(d, array_index):
1040 r = dict()
1041
1042 v = navigate_value(d, ["image_id"], array_index)
1043 r["id"] = v
1044
1045 for v in r.values():
1046 if v is not None:
1047 return r
1048 return None
1049
1050
1051 def fill_list_resp_body(body):
1052 result = dict()
1053
1054 v = fill_list_resp_attachments(body.get("attachments"))
1055 result["attachments"] = v
1056
1057 result["availability_zone"] = body.get("availability_zone")
1058
1059 result["bootable"] = body.get("bootable")
1060
1061 result["created_at"] = body.get("created_at")
1062
1063 result["description"] = body.get("description")
1064
1065 result["enterprise_project_id"] = body.get("enterprise_project_id")
1066
1067 result["id"] = body.get("id")
1068
1069 v = fill_list_resp_metadata(body.get("metadata"))
1070 result["metadata"] = v
1071
1072 result["multiattach"] = body.get("multiattach")
1073
1074 result["name"] = body.get("name")
1075
1076 result["size"] = body.get("size")
1077
1078 result["snapshot_id"] = body.get("snapshot_id")
1079
1080 result["source_volid"] = body.get("source_volid")
1081
1082 result["status"] = body.get("status")
1083
1084 result["tags"] = body.get("tags")
1085
1086 v = fill_list_resp_volume_image_metadata(body.get("volume_image_metadata"))
1087 result["volume_image_metadata"] = v
1088
1089 result["volume_type"] = body.get("volume_type")
1090
1091 return result
1092
1093
1094 def fill_list_resp_attachments(value):
1095 if not value:
1096 return None
1097
1098 result = []
1099 for item in value:
1100 val = dict()
1101
1102 val["attached_at"] = item.get("attached_at")
1103
1104 val["attachment_id"] = item.get("attachment_id")
1105
1106 val["device"] = item.get("device")
1107
1108 val["server_id"] = item.get("server_id")
1109
1110 result.append(val)
1111
1112 return result
1113
1114
1115 def fill_list_resp_metadata(value):
1116 if not value:
1117 return None
1118
1119 result = dict()
1120
1121 result["__system__cmkid"] = value.get("__system__cmkid")
1122
1123 result["attached_mode"] = value.get("attached_mode")
1124
1125 result["full_clone"] = value.get("full_clone")
1126
1127 result["hw:passthrough"] = value.get("hw:passthrough")
1128
1129 result["policy"] = value.get("policy")
1130
1131 result["readonly"] = value.get("readonly")
1132
1133 return result
1134
1135
1136 def fill_list_resp_volume_image_metadata(value):
1137 if not value:
1138 return None
1139
1140 result = dict()
1141
1142 result["id"] = value.get("id")
1143
1144 return result
1145
1146
1147 if __name__ == "__main__":
1148 main()