Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2019, Sandeep Kasargod ([email protected])
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 DOCUMENTATION = r"""
10 module: vexata_volume
11 short_description: Manage volumes on Vexata VX100 storage arrays
12 description:
13 - Create, deletes or extend volumes on a Vexata VX100 array.
14 author:
15 - Sandeep Kasargod (@vexata)
16 attributes:
17 check_mode:
18 support: full
19 diff_mode:
20 support: none
21 options:
22 name:
23 description:
24 - Volume name.
25 required: true
26 type: str
27 state:
28 description:
29 - Creates/Modifies volume when present or removes when absent.
30 default: present
31 choices: [present, absent]
32 type: str
33 size:
34 description:
35 - Volume size in M, G, T units. M=2^20, G=2^30, T=2^40 bytes.
36 type: str
37 extends_documentation_fragment:
38 - community.general._vexata.vx100
39 - community.general._attributes
40 """
41
42 EXAMPLES = r"""
43 - name: Create new 2 TiB volume named foo
44 community.general.vexata_volume:
45 name: foo
46 size: 2T
47 state: present
48 array: vx100_ultra.test.com
49 user: admin
50 password: secret
51
52 - name: Expand volume named foo to 4 TiB
53 community.general.vexata_volume:
54 name: foo
55 size: 4T
56 state: present
57 array: vx100_ultra.test.com
58 user: admin
59 password: secret
60
61 - name: Delete volume named foo
62 community.general.vexata_volume:
63 name: foo
64 state: absent
65 array: vx100_ultra.test.com
66 user: admin
67 password: secret
68 """
69
70 RETURN = r"""
71 """
72
73 from ansible.module_utils.basic import AnsibleModule
74
75 from ansible_collections.community.general.plugins.module_utils._vexata import (
76 argument_spec,
77 get_array,
78 required_together,
79 size_to_MiB,
80 )
81
82
83 def get_volume(module, array):
84 """Retrieve a named volume if it exists, None if absent."""
85 name = module.params["name"]
86 try:
87 vols = array.list_volumes()
88 vol = [v for v in vols if v["name"] == name]
89 if len(vol) == 1:
90 return vol[0]
91 else:
92 return None
93 except Exception:
94 module.fail_json(msg="Error while attempting to retrieve volumes.")
95
96
97 def validate_size(module, err_msg):
98 size = module.params.get("size", False)
99 if not size:
100 module.fail_json(msg=err_msg)
101 size = size_to_MiB(size)
102 if size <= 0:
103 module.fail_json(msg="Invalid volume size, must be <integer>[MGT].")
104 return size
105
106
107 def create_volume(module, array):
108 """ "Create a new volume."""
109 changed = False
110 size = validate_size(module, err_msg="Size is required to create volume.")
111 if module.check_mode:
112 module.exit_json(changed=changed)
113
114 try:
115 vol = array.create_volume(module.params["name"], "Ansible volume", size)
116 if vol:
117 module.log(msg=f"Created volume {vol['id']}")
118 changed = True
119 else:
120 module.fail_json(msg="Volume create failed.")
121 except Exception:
122 pass
123 module.exit_json(changed=changed)
124
125
126 def update_volume(module, array, volume):
127 """Expand the volume size."""
128 changed = False
129 size = validate_size(module, err_msg="Size is required to update volume")
130 prev_size = volume["volSize"]
131 if size <= prev_size:
132 module.log(msg="Volume expanded size needs to be larger than current size.")
133 if module.check_mode:
134 module.exit_json(changed=changed)
135
136 try:
137 vol = array.grow_volume(volume["name"], volume["description"], volume["id"], size)
138 if vol:
139 changed = True
140 except Exception:
141 pass
142
143 module.exit_json(changed=changed)
144
145
146 def delete_volume(module, array, volume):
147 changed = False
148 vol_name = volume["name"]
149 if module.check_mode:
150 module.exit_json(changed=changed)
151
152 try:
153 ok = array.delete_volume(volume["id"])
154 if ok:
155 module.log(msg=f"Volume {vol_name} deleted.")
156 changed = True
157 else:
158 raise Exception
159 except Exception:
160 pass
161 module.exit_json(changed=changed)
162
163
164 def main():
165 arg_spec = argument_spec()
166 arg_spec.update(
167 dict(
168 name=dict(type="str", required=True),
169 state=dict(default="present", choices=["present", "absent"]),
170 size=dict(type="str"),
171 )
172 )
173
174 module = AnsibleModule(arg_spec, supports_check_mode=True, required_together=required_together())
175
176 state = module.params["state"]
177 array = get_array(module)
178 volume = get_volume(module, array)
179
180 if state == "present":
181 if not volume:
182 create_volume(module, array)
183 else:
184 update_volume(module, array, volume)
185 elif state == "absent" and volume:
186 delete_volume(module, array, volume)
187 else:
188 module.exit_json(changed=False)
189
190
191 if __name__ == "__main__":
192 main()