Debian Codegraph @

ansible_collections/purestorage/flashblade/plugins/modules/purefb_groupquota.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2021, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11
12 ANSIBLE_METADATA = {
13 "metadata_version": "1.1",
14 "status": ["preview"],
15 "supported_by": "community",
16 }
17
18
19 DOCUMENTATION = """
20 ---
21 module: purefb_groupquota
22 version_added: "1.7.0"
23 short_description: Manage filesystem group quotas
24 description:
25 - This module manages group quotas for filesystems on Pure Storage FlashBlade.
26 author: Pure Storage Ansible Team (@sdodsley) <[email protected]>
27 options:
28 name:
29 description:
30 - Filesystem Name.
31 required: true
32 type: str
33 state:
34 description:
35 - Create, delete or modifies a quota.
36 required: false
37 default: present
38 type: str
39 choices: [ "present", "absent" ]
40 quota:
41 description:
42 - Group quota in M, G, T or P units. This cannot be 0.
43 - This value will override the file system's default group quota.
44 type: str
45 gid:
46 description:
47 - The group id on which the quota is enforced.
48 - Cannot be combined with I(gname)
49 type: int
50 gname:
51 description:
52 - The group name on which the quota is enforced.
53 - Cannot be combined with I(gid)
54 type: str
55 context:
56 description:
57 - Name of fleet member on which to perform the operation.
58 - This requires the array receiving the request is a member of a fleet
59 and the context name to be a member of the same fleet.
60 type: str
61 default: ""
62 version_added: "1.22.0"
63 extends_documentation_fragment:
64 - purestorage.flashblade.purestorage.fb
65 """
66
67 EXAMPLES = """
68 - name: Create new group (using GID) quota for filesystem named foo
69 purestorage.flashblade.purefb_groupquota:
70 name: foo
71 quota: 1T
72 gid: 1234
73 state: present
74 fb_url: 10.10.10.2
75 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
76
77 - name: Create new group (using groupname) quota for filesystem named foo
78 purestorage.flashblade.purefb_groupquota:
79 name: foo
80 quota: 1T
81 gname: bar
82 state: present
83 fb_url: 10.10.10.2
84 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
85
86 - name: Delete group quota on filesystem foo for group by GID
87 purestorage.flashblade.purefb_groupquota:
88 name: foo
89 gid: 1234
90 state: absent
91 fb_url: 10.10.10.2
92 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
93
94 - name: Delete group quota on filesystem foo for group by groupname
95 purestorage.flashblade.purefb_groupquota:
96 name: foo
97 gname: bar
98 state: absent
99 fb_url: 10.10.10.2
100 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
101
102 - name: Update group quota on filesystem foo for group by groupname
103 purestorage.flashblade.purefb_groupquota:
104 name: foo
105 quota: 20G
106 gname: bar
107 state: present
108 fb_url: 10.10.10.2
109 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
110
111 - name: Update group quota on filesystem foo for group by GID
112 purestorage.flashblade.purefb_groupquota:
113 name: foo
114 quota: 20G
115 gid: bar
116 state: present
117 fb_url: 10.10.10.2
118 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
119 """
120
121 RETURN = """
122 """
123
124 HAS_PURITY_FB = True
125 try:
126 from pypureclient.flashblade import GroupQuotaPost, GroupQuotaPatch
127 except ImportError:
128 HAS_PURITY_FB = False
129
130 CONTEXT_API_VERSION = "2.17"
131
132 from ansible.module_utils.basic import AnsibleModule, human_to_bytes
133 from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import (
134 get_system,
135 purefb_argument_spec,
136 )
137
138
139 def get_fs(module, blade):
140 """Return Filesystem or None"""
141 api_version = list(blade.get_versions().items)
142 if CONTEXT_API_VERSION in api_version:
143 res = blade.get_file_systems(
144 names=[module.params["name"]], context_names=[module.params["context"]]
145 )
146 else:
147 res = blade.get_file_systems(names=[module.params["name"]])
148 if res.status_code == 200:
149 return list(res.items)[0]
150 return None
151
152
153 def get_quota(module, blade):
154 """Return Filesystem User Quota or None"""
155 api_version = list(blade.get_versions().items)
156 if module.params["gid"]:
157 if CONTEXT_API_VERSION in api_version:
158 res = blade.get_quotas_groups(
159 file_system_names=[module.params["name"]],
160 filter="group.id=" + str(module.params["gid"]),
161 context_names=[module.params["context"]],
162 )
163 else:
164 res = blade.get_quotas_groups(
165 file_system_names=[module.params["name"]],
166 filter="group.id=" + str(module.params["gid"]),
167 )
168 else:
169 if CONTEXT_API_VERSION in api_version:
170 res = blade.get_quotas_groups(
171 file_system_names=[module.params["name"]],
172 filter="group.name='" + module.params["gname"] + "'",
173 context_names=[module.params["context"]],
174 )
175 else:
176 res = blade.get_quotas_groups(
177 file_system_names=[module.params["name"]],
178 filter="group.name='" + module.params["gname"] + "'",
179 )
180 if res.status_code == 200 and res.total_item_count != 0:
181 return list(res.items)[0]
182 return None
183
184
185 def create_quota(module, blade):
186 """Create Filesystem User Quota"""
187 changed = True
188 api_version = list(blade.get_versions().items)
189 if not module.check_mode:
190 if module.params["gid"]:
191 if CONTEXT_API_VERSION in api_version:
192 res = blade.post_quotas_groups(
193 file_system_names=[module.params["name"]],
194 gids=[module.params["gid"]],
195 quota=GroupQuotaPost(
196 quota=int(human_to_bytes(module.params["quota"]))
197 ),
198 context_names=[module.params["context"]],
199 )
200 else:
201 res = blade.post_quotas_groups(
202 file_system_names=[module.params["name"]],
203 gids=[module.params["gid"]],
204 quota=GroupQuotaPost(
205 quota=int(human_to_bytes(module.params["quota"]))
206 ),
207 )
208 if res.status_code != 200:
209 module.fail_json(
210 msg="Failed to create quote for UID {0} on filesystem {1}. Error: {2}".format(
211 module.params["gid"],
212 module.params["name"],
213 res.errors[0].message,
214 )
215 )
216 else:
217 if CONTEXT_API_VERSION in api_version:
218 res = blade.post_quotas_groups(
219 file_system_names=[module.params["name"]],
220 group_names=[module.params["gname"]],
221 quota=GroupQuotaPost(
222 quota=int(human_to_bytes(module.params["quota"]))
223 ),
224 context_names=[module.params["context"]],
225 )
226 else:
227 res = blade.post_quotas_groups(
228 file_system_names=[module.params["name"]],
229 group_names=[module.params["gname"]],
230 quota=GroupQuotaPost(
231 quota=int(human_to_bytes(module.params["quota"]))
232 ),
233 )
234 if res.status_code != 200:
235 module.fail_json(
236 msg="Failed to create quote for groupname {0} on filesystem {1}. Error: {2}".format(
237 module.params["gname"],
238 module.params["name"],
239 res.errors[0].message,
240 )
241 )
242 module.exit_json(changed=changed)
243
244
245 def update_quota(module, blade):
246 """Upodate Filesystem User Quota"""
247 changed = False
248 api_version = list(blade.get_versions().items)
249 current_quota = get_quota(module, blade)
250 if current_quota.quota != human_to_bytes(module.params["quota"]):
251 changed = True
252 if not module.check_mode:
253 if module.params["gid"]:
254 if CONTEXT_API_VERSION in api_version:
255 res = blade.patch_quotas_groups(
256 file_system_names=[module.params["name"]],
257 gids=[module.params["gid"]],
258 quota=GroupQuotaPatch(
259 quota=int(human_to_bytes(module.params["quota"]))
260 ),
261 context_names=[module.params["context"]],
262 )
263 else:
264 res = blade.patch_quotas_groups(
265 file_system_names=[module.params["name"]],
266 gids=[module.params["gid"]],
267 quota=GroupQuotaPatch(
268 quota=int(human_to_bytes(module.params["quota"]))
269 ),
270 )
271 if res.status_code != 200:
272 module.fail_json(
273 msg="Failed to update quota for UID {0} on filesystem {1}. Error: {2}".format(
274 module.params["gid"],
275 module.params["name"],
276 res.errors[0].message,
277 )
278 )
279 else:
280 if CONTEXT_API_VERSION in api_version:
281 res = blade.patch_quotas_groups(
282 file_system_names=[module.params["name"]],
283 group_names=[module.params["gname"]],
284 quota=GroupQuotaPatch(
285 quota=int(human_to_bytes(module.params["quota"]))
286 ),
287 context_names=[module.params["context"]],
288 )
289 else:
290 res = blade.patch_quotas_groups(
291 file_system_names=[module.params["name"]],
292 group_names=[module.params["gname"]],
293 quota=GroupQuotaPatch(
294 quota=int(human_to_bytes(module.params["quota"]))
295 ),
296 )
297 if res.status_code != 200:
298 module.fail_json(
299 msg="Failed to update quota for UID {0} on filesystem {1}. Error: {2}".format(
300 module.params["gname"],
301 module.params["name"],
302 res.errors[0].message,
303 )
304 )
305 module.exit_json(changed=changed)
306
307
308 def delete_quota(module, blade):
309 """Delete Filesystem User Quota"""
310 changed = True
311 api_version = list(blade.get_versions().items)
312 if not module.check_mode:
313 if module.params["gid"]:
314 if CONTEXT_API_VERSION in api_version:
315 res = blade.delete_quotas_groups(
316 file_system_names=[module.params["name"]],
317 gids=[module.params["gid"]],
318 context_names=[module.params["context"]],
319 )
320 else:
321 res = blade.delete_quotas_groups(
322 file_system_names=[module.params["name"]],
323 gids=[module.params["gid"]],
324 )
325 if res.status_code != 200:
326 module.fail_json(
327 msg="Failed to delete quota for UID {0} on filesystem {1}.".format(
328 module.params["gid"], module.params["name"]
329 )
330 )
331 else:
332 if CONTEXT_API_VERSION in api_version:
333 res = blade.delete_quotas_groups(
334 file_system_names=[module.params["name"]],
335 group_names=[module.params["gname"]],
336 context_names=[module.params["context"]],
337 )
338 else:
339 res = blade.delete_quotas_groups(
340 file_system_names=[module.params["name"]],
341 group_names=[module.params["gname"]],
342 )
343 if res.status_code != 200:
344 module.fail_json(
345 msg="Failed to delete quota for groupname {0} on filesystem {1}. Error: {2}".format(
346 module.params["gname"],
347 module.params["name"],
348 res.errors[0].message,
349 )
350 )
351 module.exit_json(changed=changed)
352
353
354 def main():
355 argument_spec = purefb_argument_spec()
356 argument_spec.update(
357 dict(
358 name=dict(type="str", required=True),
359 gid=dict(type="int"),
360 gname=dict(type="str"),
361 state=dict(default="present", choices=["present", "absent"]),
362 quota=dict(type="str"),
363 context=dict(type="str", default=""),
364 )
365 )
366
367 mutually_exclusive = [["gid", "gname"]]
368 required_if = [["state", "present", ["quota"]]]
369 module = AnsibleModule(
370 argument_spec,
371 mutually_exclusive=mutually_exclusive,
372 required_if=required_if,
373 supports_check_mode=True,
374 )
375
376 if not HAS_PURITY_FB:
377 module.fail_json(msg="py-pure-client sdk is required for this module")
378
379 state = module.params["state"]
380 blade = get_system(module)
381 fsys = get_fs(module, blade)
382 if not fsys:
383 module.fail_json(
384 msg="Filesystem {0} does not exist.".format(module.params["name"])
385 )
386 quota = get_quota(module, blade)
387
388 if state == "present" and not quota:
389 create_quota(module, blade)
390 elif state == "present" and quota:
391 update_quota(module, blade)
392 elif state == "absent" and quota:
393 delete_quota(module, blade)
394
395 module.exit_json(changed=False)
396
397
398 if __name__ == "__main__":
399 main()