Debian Codegraph @

ansible_collections/dellemc/unity/plugins/modules/user_quota.py

raw symbols SCIP
1 #!/usr/bin/python
2 # Copyright: (c) 2021-2025, Dell Technologies
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 """Ansible module for managing User Quota on Unity"""
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 DOCUMENTATION = r'''
12 ---
13 module: user_quota
14 short_description: Manage user quota on the Unity storage system
15 description:
16 - Managing User Quota on the Unity storage system includes
17 Create user quota,
18 Get user quota,
19 Modify user quota,
20 Delete user quota,
21 Create user quota for quota tree,
22 Modify user quota for quota tree and
23 Delete user quota for quota tree.
24 version_added: '1.2.0'
25 extends_documentation_fragment:
26 - dellemc.unity.unity
27 author:
28 - Spandita Panigrahi (@panigs7) <[email protected]>
29 options:
30 filesystem_name:
31 description:
32 - The name of the filesystem for which the user quota is created.
33 - For creation of a user quota either I(filesystem_name) or
34 I(filesystem_id) is required.
35 type: str
36 filesystem_id:
37 description:
38 - The ID of the filesystem for which the user quota is created.
39 - For creation of a user quota either I(filesystem_id) or
40 I(filesystem_name) is required.
41 type: str
42 nas_server_name:
43 description:
44 - The name of the NAS server in which the filesystem is created.
45 - For creation of a user quota either I(nas_server_name) or
46 I(nas_server_id) is required.
47 type: str
48 nas_server_id:
49 description:
50 - The ID of the NAS server in which the filesystem is created.
51 - For creation of a user quota either I(filesystem_id) or
52 I(filesystem_name) is required.
53 type: str
54 hard_limit:
55 description:
56 - Hard limitation for a user on the total space available. If exceeded, user cannot write data.
57 - Value C(0) implies no limit.
58 - One of the values of I(soft_limit) and I(hard_limit) can be C(0), however, both cannot be C(0)
59 during creation or modification of user quota.
60 type: int
61 soft_limit:
62 description:
63 - Soft limitation for a user on the total space available. If exceeded,
64 notification will be sent to the user for the grace period mentioned, beyond
65 which the user cannot use space.
66 - Value C(0) implies no limit.
67 - Both I(soft_limit) and I(hard_limit) cannot be C(0) during creation or modification
68 of user quota.
69 type: int
70 cap_unit:
71 description:
72 - Unit of I(soft_limit) and I(hard_limit) size.
73 - It defaults to C(GB) if not specified.
74 choices: ['MB', 'GB', 'TB']
75 type: str
76 user_type:
77 description:
78 - Type of user creating a user quota.
79 - Mandatory while creating or modifying user quota.
80 choices: ['Unix', 'Windows']
81 type: str
82 win_domain:
83 description:
84 - Fully qualified or short domain name for Windows user type.
85 - Mandatory when I(user_type) is C(Windows).
86 type: str
87 user_name:
88 description:
89 - User name of the user quota when I(user_type) is C(Windows) or C(Unix).
90 - Option I(user_name) must be specified along with I(win_domain) when I(user_type) is C(Windows).
91 type: str
92 uid:
93 description:
94 - User ID of the user quota.
95 type: str
96 user_quota_id:
97 description:
98 - User quota ID generated after creation of a user quota.
99 type: str
100 tree_quota_id:
101 description:
102 - The ID of the quota tree.
103 - Either I(tree_quota_id) or I(path) to quota tree is required to
104 create/modify/delete user quota for a quota tree.
105 type: str
106 path:
107 description:
108 - The path to the quota tree.
109 - Either I(tree_quota_id) or I(path) to quota tree is required to
110 create/modify/delete user quota for a quota tree.
111 - Path must start with a forward slash '/'.
112 type: str
113 state:
114 description:
115 - The I(state) option is used to mention the existence of the user quota.
116 type: str
117 required: true
118 choices: ['absent', 'present']
119
120 notes:
121 - The I(check_mode) is not supported.
122 '''
123
124 EXAMPLES = r'''
125 - name: Get user quota details by user quota id
126 user_quota:
127 unispherehost: "{{unispherehost}}"
128 username: "{{username}}"
129 password: "{{password}}"
130 validate_certs: "{{validate_certs}}"
131 user_quota_id: "userquota_171798700679_0_123"
132 state: "present"
133
134 - name: Get user quota details by user quota uid/user name
135 user_quota:
136 unispherehost: "{{unispherehost}}"
137 username: "{{username}}"
138 password: "{{password}}"
139 validate_certs: "{{validate_certs}}"
140 filesystem_name: "fs_2171"
141 nas_server_id: "nas_21"
142 user_name: "test"
143 state: "present"
144
145 - name: Create user quota for a filesystem with filesystem id
146 user_quota:
147 unispherehost: "{{unispherehost}}"
148 username: "{{username}}"
149 password: "{{password}}"
150 validate_certs: "{{validate_certs}}"
151 filesystem_id: "fs_2171"
152 hard_limit: 6
153 cap_unit: "TB"
154 soft_limit: 5
155 uid: "111"
156 state: "present"
157
158 - name: Create user quota for a filesystem with filesystem name
159 user_quota:
160 unispherehost: "{{unispherehost}}"
161 username: "{{username}}"
162 password: "{{password}}"
163 validate_certs: "{{validate_certs}}"
164 filesystem_name: "Test_filesystem"
165 nas_server_name: "lglad068"
166 hard_limit: 6
167 cap_unit: "TB"
168 soft_limit: 5
169 uid: "111"
170 state: "present"
171
172 - name: Modify user quota limit usage by user quota id
173 user_quota:
174 unispherehost: "{{unispherehost}}"
175 username: "{{username}}"
176 password: "{{password}}"
177 validate_certs: "{{validate_certs}}"
178 user_quota_id: "userquota_171798700679_0_123"
179 hard_limit: 10
180 cap_unit: "TB"
181 soft_limit: 8
182 state: "present"
183
184 - name: Modify user quota by filesystem id and user quota uid/user_name
185 user_quota:
186 unispherehost: "{{unispherehost}}"
187 username: "{{username}}"
188 password: "{{password}}"
189 validate_certs: "{{validate_certs}}"
190 filesystem_id: "fs_2171"
191 user_type: "Windows"
192 win_domain: "prod"
193 user_name: "sample"
194 hard_limit: 12
195 cap_unit: "TB"
196 soft_limit: 10
197 state: "present"
198
199 - name: Delete user quota
200 user_quota:
201 unispherehost: "{{unispherehost}}"
202 username: "{{username}}"
203 password: "{{password}}"
204 validate_certs: "{{validate_certs}}"
205 filesystem_id: "fs_2171"
206 win_domain: "prod"
207 user_name: "sample"
208 state: "absent"
209
210 - name: Create user quota of a quota tree
211 user_quota:
212 unispherehost: "{{unispherehost}}"
213 username: "{{username}}"
214 password: "{{password}}"
215 validate_certs: "{{validate_certs}}"
216 tree_quota_id: "treequota_171798700679_4"
217 user_type: "Windows"
218 win_domain: "prod"
219 user_name: "sample"
220 soft_limit: 9
221 cap_unit: "TB"
222 state: "present"
223
224 - name: Create user quota of a quota tree by quota tree path
225 user_quota:
226 unispherehost: "{{unispherehost}}"
227 username: "{{username}}"
228 password: "{{password}}"
229 validate_certs: "{{validate_certs}}"
230 filesystem_id: "fs_2171"
231 path: "/sample"
232 user_type: "Unix"
233 user_name: "test"
234 hard_limit: 2
235 cap_unit: "TB"
236 state: "present"
237
238 - name: Modify user quota of a quota tree
239 user_quota:
240 unispherehost: "{{unispherehost}}"
241 username: "{{username}}"
242 password: "{{password}}"
243 validate_certs: "{{validate_certs}}"
244 tree_quota_id: "treequota_171798700679_4"
245 user_type: "Windows"
246 win_domain: "prod"
247 user_name: "sample"
248 soft_limit: 10
249 cap_unit: "TB"
250 state: "present"
251
252 - name: Modify user quota of a quota tree by quota tree path
253 user_quota:
254 unispherehost: "{{unispherehost}}"
255 username: "{{username}}"
256 password: "{{password}}"
257 validate_certs: "{{validate_certs}}"
258 filesystem_id: "fs_2171"
259 path: "/sample"
260 user_type: "Windows"
261 win_domain: "prod"
262 user_name: "sample"
263 hard_limit: 12
264 cap_unit: "TB"
265 state: "present"
266
267 - name: Delete user quota of a quota tree by quota tree path
268 user_quota:
269 unispherehost: "{{unispherehost}}"
270 username: "{{username}}"
271 password: "{{password}}"
272 validate_certs: "{{validate_certs}}"
273 filesystem_id: "fs_2171"
274 path: "/sample"
275 win_domain: "prod"
276 user_name: "sample"
277 state: "absent"
278
279 - name: Delete user quota of a quota tree by quota tree id
280 user_quota:
281 unispherehost: "{{unispherehost}}"
282 username: "{{username}}"
283 password: "{{password}}"
284 validate_certs: "{{validate_certs}}"
285 tree_quota_id: "treequota_171798700679_4"
286 win_domain: "prod"
287 user_name: "sample"
288 state: "absent"
289 '''
290
291 RETURN = r'''
292 changed:
293 description: Whether or not the resource has changed.
294 returned: always
295 type: bool
296 sample: true
297
298 get_user_quota_details:
299 description: Details of the user quota.
300 returned: When user quota exists
301 type: dict
302 contains:
303 filesystem:
304 description: Filesystem details for which the user quota is
305 created.
306 type: dict
307 contains:
308 UnityFileSystem:
309 description: Filesystem details for which the
310 user quota is created.
311 type: dict
312 contains:
313 id:
314 description: ID of the filesystem for
315 which the user quota is created.
316 type: str
317 name:
318 description: Name of filesystem.
319 type: str
320 nas_server:
321 description: Nasserver details where
322 filesystem is created.
323 type: dict
324 contains:
325 name:
326 description: Name of nasserver.
327 type: str
328 id:
329 description: ID of nasserver.
330 type: str
331 tree_quota:
332 description: Quota tree details for which the user quota is
333 created.
334 type: dict
335 contains:
336 UnityTreeQuota:
337 description: Quota tree details for which the user
338 quota is created.
339 type: dict
340 contains:
341 id:
342 description: ID of the quota tree.
343 type: str
344 path:
345 description: Path to quota tree.
346 type: str
347 gp_left:
348 description: The grace period left after the soft limit
349 for the user quota is exceeded.
350 type: int
351 hard_limit:
352 description: Hard limitation for a user on the total space
353 available. If exceeded, user cannot write data.
354 type: int
355 hard_ratio:
356 description: The hard ratio is the ratio between the
357 hard limit size of the user quota
358 and the amount of storage actually consumed.
359 type: str
360 soft_limit:
361 description: Soft limitation for a user on the total space
362 available. If exceeded, notification will be
363 sent to user for the grace period mentioned, beyond
364 which user cannot use space.
365 type: int
366 soft_ratio:
367 description: The soft ratio is the ratio between
368 the soft limit size of the user quota
369 and the amount of storage actually consumed.
370 type: str
371 id:
372 description: User quota ID.
373 type: str
374 size_used:
375 description: Size of used space in the filesystem
376 by the user files.
377 type: int
378 state:
379 description: State of the user quota.
380 type: int
381 uid:
382 description: User ID of the user.
383 type: int
384 unix_name:
385 description: Unix user name for this user quota's uid.
386 type: str
387 windows_names:
388 description: Windows user name that maps to this quota's uid.
389 type: str
390 windows_sids:
391 description: Windows SIDs that maps to this quota's uid
392 type: str
393 sample: {
394 "existed": true,
395 "filesystem": {
396 "UnityFileSystem": {
397 "hash": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
398 "id": "fs_120",
399 "name": "nfs-multiprotocol",
400 "nas_server": {
401 "id": "nas_1",
402 "name": "lglad072"
403 }
404 }
405 },
406 "gp_left": null,
407 "hard_limit": "10.0 GB",
408 "hard_ratio": null,
409 "hash": 8752448438089,
410 "id": "userquota_171798694698_0_60000",
411 "size_used": 0,
412 "soft_limit": "10.0 GB",
413 "soft_ratio": null,
414 "state": 0,
415 "tree_quota": null,
416 "uid": 60000,
417 "unix_name": null,
418 "windows_names": null,
419 "windows_sids": null
420 }
421 '''
422
423 from ansible.module_utils.basic import AnsibleModule
424 from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell \
425 import utils
426
427 LOG = utils.get_logger('user_quota')
428
429 application_type = "Ansible/1.7.1"
430
431
432 class UserQuota(object):
433 """Class with User Quota operations"""
434
435 def __init__(self):
436 """Define all parameters required by this module"""
437 self.module_params = utils.get_unity_management_host_parameters()
438 self.module_params.update(get_user_quota_parameters())
439
440 mutually_exclusive = [['user_name', 'uid'], ['uid', 'win_domain'],
441 ['filesystem_name', 'filesystem_id'],
442 ['nas_server_name', 'nas_server_id'],
443 ['user_name', 'user_quota_id'],
444 ['uid', 'user_quota_id']]
445
446 required_if = [('user_type', 'Windows', ['win_domain', 'user_name'], False),
447 ('user_type', 'Unix', ['user_name'], False)]
448
449 # initialize the Ansible module
450 self.module = AnsibleModule(
451 argument_spec=self.module_params,
452 supports_check_mode=False,
453 mutually_exclusive=mutually_exclusive,
454 required_if=required_if)
455 utils.ensure_required_libs(self.module)
456
457 self.unity_conn = utils.get_unity_unisphere_connection(
458 self.module.params, application_type)
459
460 def check_user_is_present(self, fs_id, uid, unix, win_name, user_quota_id):
461 """
462 Check if user quota is present in filesystem.
463 :param fs_id: ID of filesystem where user quota is searched.
464 :param uid: UID of the user quota
465 :param unix: Unix user name of user quota
466 :param win_name: Windows user name of user quota
467 :param user_quota_id: ID of the user quota
468 :return: ID of user quota if it exists else None.
469 """
470
471 if not self.check_user_type_provided(win_name, uid, unix):
472 return None
473
474 user_name_or_uid_or_id = unix if unix else win_name if win_name else uid if \
475 uid else user_quota_id
476
477 # All user quotas in the given filesystem
478 all_user_quota = self.unity_conn.get_user_quota(filesystem=fs_id, id=user_quota_id,
479 unix_name=unix, windows_names=win_name,
480 uid=uid)
481
482 for user_quota in range(len(all_user_quota)):
483
484 if all_user_quota[user_quota].tree_quota is None:
485 msg = "User quota %s with id %s " \
486 "is present in filesystem %s" \
487 % (user_name_or_uid_or_id, all_user_quota[user_quota].id, fs_id)
488 LOG.info(msg)
489 return all_user_quota[user_quota].id
490
491 return None
492
493 def check_quota_tree_is_present(self, fs_id, path, tree_quota_id):
494 """
495 Check if quota tree is present in filesystem.
496 :param fs_id: ID of filesystem where quota tree is searched.
497 :param path: Path to quota tree
498 :param tree_quota_id: ID of the quota tree
499 :return: ID of quota tree if it exists.
500 """
501
502 path_or_id = path if path else tree_quota_id
503 tree_quota_obj = self.unity_conn.get_tree_quota(filesystem=fs_id, path=path,
504 id=tree_quota_id)
505 if len(tree_quota_obj) > 0:
506 msg = "Tree quota id %s present in filesystem %s" % (tree_quota_obj[0].id, fs_id)
507 LOG.info(msg)
508 return tree_quota_obj[0].id
509 else:
510 errormsg = "The quota tree '%s' does not exist" % path_or_id
511 LOG.error(errormsg)
512 self.module.fail_json(msg=errormsg)
513
514 def check_user_quota_in_quota_tree(self, tree_quota_id, uid, unix, win_name, user_quota_id):
515 """
516 Check if user quota is present in quota tree.
517 :param tree_quota_id: ID of quota tree where user quota is searched.
518 :param uid: UID of user quota
519 :param unix: Unix name of user quota
520 :param win_name: Windows name of user quota
521 :param user_quota_id: ID of the user quota
522 :return: ID of user quota if it exists in quota tree else None.
523 """
524 if not self.check_user_type_provided(win_name, uid, unix):
525 return None
526
527 user_quota_name = uid if uid else unix if unix else win_name \
528 if win_name else user_quota_id
529 user_quota_obj = self.unity_conn.get_user_quota(tree_quota=tree_quota_id,
530 uid=uid, windows_names=win_name,
531 unix_name=unix,
532 id=user_quota_id)
533 if len(user_quota_obj) > 0:
534 msg = "User quota %s is present in quota tree %s " % (user_quota_name, tree_quota_id)
535 LOG.info(msg)
536 return user_quota_obj[0].id
537 else:
538 return None
539
540 def create_user_quota(self, fs_id, soft_limit, hard_limit, unit, uid, unix, win_name, tree_quota_id):
541 """
542 Create user quota of a filesystem.
543 :param fs_id: ID of filesystem where user quota is to be created.
544 :param soft_limit: Soft limit
545 :param hard_limit: Hard limit
546 :param unit: Unit of soft limit and hard limit
547 :param uid: UID of the user quota
548 :param unix: Unix user name of user quota
549 :param win_name: Windows user name of user quota
550 :param tree_quota_id: ID of tree quota
551 :return: Object containing new user quota details.
552 """
553
554 unix_or_uid_or_win = uid if uid else unix if unix else win_name
555 fs_id_or_tree_quota_id = fs_id if fs_id else tree_quota_id
556 if soft_limit is None and hard_limit is None:
557 errormsg = "Both soft limit and hard limit cannot be empty. " \
558 "Please provide atleast one to create user quota."
559 LOG.error(errormsg)
560 self.module.fail_json(msg=errormsg)
561
562 soft_limit_in_bytes = utils.get_size_bytes(soft_limit, unit)
563 hard_limit_in_bytes = utils.get_size_bytes(hard_limit, unit)
564 try:
565 if self.check_user_type_provided(win_name, uid, unix):
566 obj_user_quota = self.unity_conn.create_user_quota(filesystem_id=fs_id,
567 hard_limit=hard_limit_in_bytes,
568 soft_limit=soft_limit_in_bytes,
569 uid=uid, unix_name=unix,
570 win_name=win_name,
571 tree_quota_id=tree_quota_id)
572 LOG.info("Successfully created user quota")
573 return obj_user_quota
574
575 except Exception as e:
576 errormsg = "Create quota for user {0} on {1} , failed with error {2} "\
577 .format(unix_or_uid_or_win, fs_id_or_tree_quota_id, str(e))
578 LOG.error(errormsg)
579 self.module.fail_json(msg=errormsg)
580
581 def get_filesystem_user_quota_display_attributes(self, user_quota_id):
582 """Get display user quota attributes
583 :param user_quota_id: User quota ID
584 :return: User quota dict to display
585 """
586 try:
587 user_quota_obj = self.unity_conn.get_user_quota(user_quota_id)
588 user_quota_details = user_quota_obj._get_properties()
589
590 if user_quota_obj and user_quota_obj.existed:
591 user_quota_details['soft_limit'] = utils. \
592 convert_size_with_unit(int(user_quota_details['soft_limit']))
593 user_quota_details['hard_limit'] = utils. \
594 convert_size_with_unit(int(user_quota_details['hard_limit']))
595
596 user_quota_details['filesystem']['UnityFileSystem']['name'] = \
597 user_quota_obj.filesystem.name
598 user_quota_details['filesystem']['UnityFileSystem'].update(
599 {'nas_server': {'name': user_quota_obj.filesystem.nas_server.name,
600 'id': user_quota_obj.filesystem.nas_server.id}})
601
602 if user_quota_obj.tree_quota:
603 user_quota_details['tree_quota']['UnityTreeQuota']['path'] = \
604 user_quota_obj.tree_quota.path
605
606 return user_quota_details
607 else:
608 errormsg = "User quota does not exist."
609 LOG.error(errormsg)
610 self.module.fail_json(msg=errormsg)
611
612 except Exception as e:
613 errormsg = "Failed to display the details of user quota {0} with " \
614 "error {1}".format(user_quota_obj.id, str(e))
615 LOG.error(errormsg)
616 self.module.fail_json(msg=errormsg)
617
618 def get_filesystem(self, nas_server=None, name=None, id=None):
619 """
620 Get filesystem details.
621 :param nas_server: Nas server object.
622 :param name: Name of filesystem.
623 :param id: ID of filesystem.
624 :return: Object containing filesystem details if it exists.
625 """
626 id_or_name = id if id else name
627 try:
628 obj_fs = None
629 if name:
630 if not nas_server:
631 err_msg = "NAS Server is required to get the FileSystem."
632 LOG.error(err_msg)
633 self.module.fail_json(msg=err_msg)
634 obj_fs = self.unity_conn.get_filesystem(name=name,
635 nas_server=nas_server)
636 if obj_fs and obj_fs.existed:
637 LOG.info("Successfully got the filesystem object %s.",
638 obj_fs)
639 return obj_fs
640 if id:
641 if nas_server:
642 obj_fs = self.unity_conn \
643 .get_filesystem(id=id, nas_server=nas_server)
644 else:
645 obj_fs = self.unity_conn.get_filesystem(id=id)
646 if obj_fs and obj_fs.existed:
647 LOG.info("Successfully got the filesystem object %s.",
648 obj_fs)
649 return obj_fs
650 except Exception as e:
651 error_msg = "Failed to get filesystem %s with error %s." \
652 % (id_or_name, str(e))
653 LOG.error(error_msg)
654 self.module.fail_json(msg=error_msg)
655
656 def get_nas_server_obj(self, name=None, id=None):
657 """
658 Get nas server details.
659 :param name: Nas server name.
660 :param id: Nas server ID.
661 :return: Object containing nas server details if it exists.
662 """
663 nas_server = id if id else name
664 error_msg = ("Failed to get NAS server %s." % nas_server)
665 try:
666 obj_nas = self.unity_conn.get_nas_server(_id=id, name=name)
667 if name and obj_nas.existed:
668 LOG.info("Successfully got the NAS server object %s.",
669 obj_nas)
670 return obj_nas
671 elif id and obj_nas.existed:
672 LOG.info("Successfully got the NAS server object %s.",
673 obj_nas)
674 return obj_nas
675 else:
676 LOG.error(error_msg)
677 self.module.fail_json(msg=error_msg)
678 except Exception as e:
679 error_msg = "Failed to get NAS server %s with error %s." \
680 % (nas_server, str(e))
681 LOG.error(error_msg)
682 self.module.fail_json(msg=error_msg)
683
684 def modify_user_quota(self, user_quota_id, soft_limit, hard_limit, unit):
685 """
686 Modify user quota of filesystem by its uid/username/user quota id.
687 :param user_quota_id: ID of the user quota
688 :param soft_limit: Soft limit
689 :param hard_limit: Hard limit
690 :param unit: Unit of soft limit and hard limit
691 :return: Boolean value whether modify user quota operation is successful.
692 """
693
694 if soft_limit is None and hard_limit is None:
695 return False
696
697 user_quota_obj = self.unity_conn.get_user_quota(user_quota_id)._get_properties()
698
699 if soft_limit is None:
700 soft_limit_in_bytes = user_quota_obj['soft_limit']
701 else:
702 soft_limit_in_bytes = utils.get_size_bytes(soft_limit, unit)
703
704 if hard_limit is None:
705 hard_limit_in_bytes = user_quota_obj['hard_limit']
706 else:
707 hard_limit_in_bytes = utils.get_size_bytes(hard_limit, unit)
708
709 if user_quota_obj:
710 if user_quota_obj['soft_limit'] == soft_limit_in_bytes and \
711 user_quota_obj['hard_limit'] == hard_limit_in_bytes:
712 return False
713 else:
714 error_msg = "The user quota does not exist."
715 LOG.error(error_msg)
716 self.module.fail_json(msg=error_msg)
717
718 try:
719 obj_user_quota = self.unity_conn.modify_user_quota(user_quota_id=user_quota_id,
720 hard_limit=hard_limit_in_bytes,
721 soft_limit=soft_limit_in_bytes)
722 LOG.info("Successfully modified user quota")
723 if obj_user_quota:
724 return True
725 except Exception as e:
726 errormsg = "Modify user quota {0} failed" \
727 " with error {1}".format(user_quota_id, str(e))
728 LOG.error(errormsg)
729 self.module.fail_json(msg=errormsg)
730
731 def check_user_type_provided(self, win_name, uid, unix_name):
732 """Checks if user type or uid is provided
733 :param win_name: Windows name of user quota
734 :param uid: UID of user quota
735 :param unix_name: Unix name of user quota"""
736 if win_name is None and uid is None and unix_name is None:
737 return False
738 else:
739 return True
740
741 def perform_module_operation(self):
742 """
743 Perform different actions on user quota module based on parameters
744 passed in the playbook
745 """
746 filesystem_id = self.module.params['filesystem_id']
747 filesystem_name = self.module.params['filesystem_name']
748 nas_server_name = self.module.params['nas_server_name']
749 nas_server_id = self.module.params['nas_server_id']
750 cap_unit = self.module.params['cap_unit']
751 state = self.module.params['state']
752 user_quota_id = self.module.params['user_quota_id']
753 hard_limit = self.module.params['hard_limit']
754 soft_limit = self.module.params['soft_limit']
755 user_type = self.module.params['user_type']
756 uid = self.module.params['uid']
757 user_name = self.module.params['user_name']
758 win_domain = self.module.params['win_domain']
759 tree_quota_id = self.module.params['tree_quota_id']
760 path = self.module.params['path']
761 create_user_quota_obj = None
762 win_name = None
763 unix_name = None
764 nas_server_resource = None
765 fs_id = None
766 user_quota_details = ''
767 filesystem_obj = None
768
769 '''
770 result is a dictionary to contain end state and user quota details
771 '''
772 result = dict(
773 changed=False,
774 create_user_quota=False,
775 modify_user_quota=False,
776 get_user_quota_details={},
777 delete_user_quota=False
778 )
779
780 if (soft_limit or hard_limit) and cap_unit is None:
781 cap_unit = 'GB'
782
783 if soft_limit == 0 and hard_limit == 0:
784 error_message = 'Both soft limit and hard limit cannot be unlimited'
785 LOG.error(error_message)
786 self.module.fail_json(msg=error_message)
787
788 if soft_limit and utils.is_size_negative(soft_limit):
789 error_message = "Invalid soft_limit provided, " \
790 "must be greater than 0"
791 LOG.error(error_message)
792 self.module.fail_json(msg=error_message)
793
794 if hard_limit and utils.is_size_negative(hard_limit):
795 error_message = "Invalid hard_limit provided, " \
796 "must be greater than 0"
797 LOG.error(error_message)
798 self.module.fail_json(msg=error_message)
799
800 if (user_type or uid) and filesystem_id is None and \
801 filesystem_name is None and tree_quota_id is None:
802 error_message = 'Please provide either ' \
803 'filesystem_name or filesystem_id'
804 LOG.error(error_message)
805 self.module.fail_json(msg=error_message)
806
807 if (nas_server_name or nas_server_id) \
808 and (filesystem_id is None and filesystem_name is None):
809 error_message = 'Please provide either ' \
810 'filesystem_name or filesystem_id'
811 LOG.error(error_message)
812 self.module.fail_json(msg=error_message)
813
814 '''
815 Validate path to quota tree
816 '''
817 if path is not None:
818 if utils.is_input_empty(path):
819 self.module.fail_json(msg=" Please provide a valid path.")
820 elif not path.startswith('/'):
821 self.module.fail_json(msg="The path is relative to the root of the file system "
822 "and must start with a forward slash.")
823
824 if filesystem_id is None and filesystem_name is None:
825 self.module.fail_json(msg="Please provide either filesystem_name or fileystem_id.")
826
827 if user_type and filesystem_id is None and filesystem_name is None and tree_quota_id is None:
828 error_message = 'Please provide either ' \
829 'filesystem_name or filesystem_id to create user quota for a' \
830 'filesystem. Or provide tree_quota_id to create user quota for a quota tree.'
831 LOG.error(error_message)
832 self.module.fail_json(msg=error_message)
833
834 '''
835 Get NAS server Object
836 '''
837
838 if nas_server_name is not None:
839 if utils.is_input_empty(nas_server_name):
840 self.module.fail_json(msg="Invalid nas_server_name given,"
841 " Please provide a valid name.")
842 nas_server_resource = self \
843 .get_nas_server_obj(name=nas_server_name)
844 elif nas_server_id is not None:
845 if utils.is_input_empty(nas_server_id):
846 self.module.fail_json(msg="Invalid nas_server_id given,"
847 " Please provide a valid ID.")
848 nas_server_resource = self.get_nas_server_obj(id=nas_server_id)
849
850 '''
851 Get filesystem Object
852 '''
853 if filesystem_name is not None:
854 if utils.is_input_empty(filesystem_name):
855 self.module.fail_json(msg="Invalid filesystem_name given,"
856 " Please provide a valid name.")
857 filesystem_obj = self \
858 .get_filesystem(nas_server=nas_server_resource,
859 name=filesystem_name)
860 fs_id = filesystem_obj.id
861 elif filesystem_id is not None:
862 if utils.is_input_empty(filesystem_id):
863 self.module.fail_json(msg="Invalid filesystem_id given,"
864 " Please provide a valid ID.")
865 filesystem_obj = self \
866 .get_filesystem(id=filesystem_id)
867 if filesystem_obj:
868 filesystem_obj = filesystem_obj[0]
869 fs_id = filesystem_obj.id
870 else:
871 self.module.fail_json(msg="Filesystem does not exist.")
872
873 if (user_name or win_domain) and (soft_limit or hard_limit) \
874 and user_type is None:
875 self.module.fail_json(msg="Invalid user_type given,"
876 " Please provide a valid user_type.")
877
878 # Check the sharing protocol supported by the filesystem
879 # while creating a user quota
880 if filesystem_obj and (soft_limit is not None or hard_limit is not None):
881 supported_protocol = filesystem_obj.supported_protocols
882
883 if supported_protocol == utils.FSSupportedProtocolEnum["CIFS"] \
884 and (user_type == "Unix" or uid):
885 self.module.fail_json(msg="This filesystem supports only SMB protocol "
886 "and applicable only for windows users. "
887 "Please provide valid windows details.")
888 elif supported_protocol == utils.FSSupportedProtocolEnum["NFS"] \
889 and user_type == "Windows":
890 self.module.fail_json(msg="This filesystem supports only NFS protocol "
891 "and applicable only for unix users. "
892 "Please provide valid uid or unix details.")
893
894 '''
895 Validate user type or uid
896 '''
897 if uid and (utils.is_input_empty(uid) or not uid.isnumeric()):
898 self.module.fail_json(msg=" UID is empty. Please provide valid UID.")
899 if user_type:
900 if user_type == "Unix":
901 if user_name is None or utils.is_input_empty(user_name):
902 self.module.fail_json(msg=" 'user_name' is empty. Please provide valid user_name.")
903
904 if user_type == "Windows":
905 if win_domain is None or utils.is_input_empty(win_domain):
906 self.module.fail_json(msg=" 'win_domain' is empty. Please provide valid win_domain.")
907 elif user_name is None or utils.is_input_empty(user_name):
908 self.module.fail_json(msg=" 'user_name' is empty. Please provide valid user_name.")
909
910 if user_type != "Unix" and win_domain:
911 win_domain = win_domain.replace(".com", "")
912 win_name = win_domain + '\\' + user_name
913
914 if win_name is None and user_name:
915 unix_name = user_name
916
917 '''
918 Check if quota tree is already present in the filesystem
919 '''
920 if tree_quota_id or path:
921 quota_tree_id_present = self.check_quota_tree_is_present(fs_id, path, tree_quota_id)
922 tree_quota_id = quota_tree_id_present
923
924 '''
925 Check if the user quota is already present in the filesystem/ quota tree
926 '''
927 if tree_quota_id:
928 user_id_present = self.check_user_quota_in_quota_tree(tree_quota_id, uid, unix_name, win_name,
929 user_quota_id)
930 fs_id = None if tree_quota_id is not None else fs_id
931 else:
932 user_id_present = self.check_user_is_present(fs_id, uid, unix_name, win_name, user_quota_id)
933
934 if user_id_present:
935 user_quota_id = user_id_present
936
937 if state == "present":
938 if user_quota_id:
939 # Modify user quota. If no change modify_user_quota is false.
940 result['modify_user_quota'] = self.modify_user_quota(user_quota_id, soft_limit,
941 hard_limit, cap_unit)
942
943 else:
944 LOG.info("Creating user quota")
945 create_user_quota_obj = self.create_user_quota(fs_id, soft_limit, hard_limit,
946 cap_unit, uid, unix_name, win_name,
947 tree_quota_id)
948 if create_user_quota_obj:
949 user_quota_id = create_user_quota_obj.id
950 result['create_user_quota'] = True
951 else:
952 user_quota_id = None
953 '''
954 Deleting user quota.
955 When both soft limit and hard limit are set to 0, it implies the user quota has
956 unlimited quota. Thereby, Unity removes the user quota id.
957 '''
958
959 if state == "absent" and user_quota_id:
960 soft_limit = 0
961 hard_limit = 0
962 err_msg = "Deleting user quota %s" % user_quota_id
963 LOG.info(err_msg)
964 result['delete_user_quota'] = self.modify_user_quota(user_quota_id,
965 soft_limit, hard_limit, cap_unit)
966 '''
967 Get user details
968 '''
969
970 if state == "present" and user_quota_id:
971 user_quota_details = self.get_filesystem_user_quota_display_attributes(user_quota_id)
972
973 result['get_user_quota_details'] = user_quota_details
974 if result['create_user_quota'] or result['modify_user_quota'] or result['delete_user_quota']:
975 result['changed'] = True
976
977 self.module.exit_json(**result)
978
979
980 def get_user_quota_parameters():
981 """This method provide parameters required for the ansible filesystem
982 user quota module on Unity"""
983 return dict(
984 filesystem_id=dict(required=False, type='str'),
985 filesystem_name=dict(required=False, type='str'),
986 state=dict(required=True, type='str', choices=['present', 'absent']),
987 user_type=dict(required=False, type='str',
988 choices=['Windows', 'Unix']),
989 user_name=dict(required=False, type='str'),
990 uid=dict(required=False, type='str'),
991 win_domain=dict(required=False, type='str'),
992 hard_limit=dict(required=False, type='int'),
993 soft_limit=dict(required=False, type='int'),
994 cap_unit=dict(required=False, type='str', choices=['MB', 'GB', 'TB']),
995 user_quota_id=dict(required=False, type='str'),
996 nas_server_name=dict(required=False, type='str'),
997 nas_server_id=dict(required=False, type='str'),
998 tree_quota_id=dict(required=False, type='str'),
999 path=dict(required=False, type='str', no_log=True)
1000 )
1001
1002
1003 def main():
1004 """ Create Unity user quota object and perform action on it
1005 based on user input from playbook"""
1006 obj = UserQuota()
1007 obj.perform_module_operation()
1008
1009
1010 if __name__ == '__main__':
1011 main()