
    Vhh                         d Z ddlmZmZmZ eZdZdZdZ	ddl
mZ ddlmZ  ej                  d      Zd	Z G d
 de      Zd Zd Zedk(  r e        yy)z/Ansible module for managing quota tree on Unity    )absolute_importdivisionprint_functionao  
---
module: tree_quota
short_description: Manage quota tree on the Unity storage system
description:
- Managing Quota tree on the Unity storage system includes
  Create quota tree,
  Get quota tree,
  Modify quota tree and
  Delete quota tree.
version_added: '1.2.0'
extends_documentation_fragment:
  - dellemc.unity.unity
author:
- Spandita Panigrahi (@panigs7) <ansible.team@dell.com>
options:
  filesystem_name:
    description:
    - The name of the filesystem for which quota tree is created.
    - For creation or modification of a quota tree either I(filesystem_name) or
      I(filesystem_id) is required.
    type: str
  filesystem_id:
    description:
    - The ID of the filesystem for which the quota tree is created.
    - For creation of a quota tree either I(filesystem_id) or
      I(filesystem_name) is required.
    type: str
  nas_server_name:
    description:
    - The name of the NAS server in which the filesystem is created.
    - For creation of a quota tree either I(nas_server_name) or
      I(nas_server_id) is required.
    type: str
  nas_server_id:
    description:
    - The ID of the NAS server in which the filesystem is created.
    - For creation of a quota tree either I(filesystem_id) or
      I(filesystem_name) is required.
    type: str
  tree_quota_id:
    description:
    - The ID of the quota tree.
    - Either I(tree_quota_id) or I(path) to quota tree is required to
      view/modify/delete quota tree.
    type: str
  path:
    description:
    - The path to the quota tree.
    - Either I(tree_quota_id) or I(path) to quota tree is required to
      create/view/modify/delete a quota tree.
    - Path must start with a forward slash '/'.
    type: str
  hard_limit:
    description:
    - Hard limitation for a quota tree on the total space available. If exceeded,
      users in quota tree cannot write data.
    - Value C(0) implies no limit.
    - One of the values of I(soft_limit) and I(hard_limit) can be C(0), however, both cannot be both C(0)
      during creation of a quota tree.
    type: int
  soft_limit:
    description:
    - Soft limitation for a quota tree on the total space available. If exceeded,
      notification will be sent to users in the quota tree for the grace period mentioned, beyond
      which users cannot use space.
    - Value C(0) implies no limit.
    - Both I(soft_limit) and I(hard_limit) cannot be C(0) during creation of quota tree.
    type: int
  cap_unit:
    description:
    - Unit of I(soft_limit) and I(hard_limit) size.
    - It defaults to C(GB) if not specified.
    choices: ['MB', 'GB', 'TB']
    type: str
  description:
    description:
    - Description of a quota tree.
    type: str
  state:
    description:
    - The state option is used to mention the existence of the filesystem
      quota tree.
    type: str
    required: true
    choices: ['absent', 'present']

notes:
  - The I(check_mode) is not supported.
a;
  
- name: Get quota tree details by quota tree id
  dellemc.unity.tree_quota:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    tree_quota_id: "treequota_171798700679_10"
    state: "present"

- name: Get quota tree details by quota tree path
  dellemc.unity.tree_quota:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    filesystem_name: "fs_2171"
    nas_server_id: "nas_21"
    path: "/test"
    state: "present"

- name: Create quota tree for a filesystem with filesystem id
  dellemc.unity.tree_quota:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    filesystem_id: "fs_2171"
    hard_limit: 6
    cap_unit: "TB"
    soft_limit: 5
    path: "/test_new"
    state: "present"

- name: Create quota tree for a filesystem with filesystem name
  dellemc.unity.tree_quota:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    filesystem_name: "Test_filesystem"
    nas_server_name: "lglad068"
    hard_limit: 6
    cap_unit: "TB"
    soft_limit: 5
    path: "/test_new"
    state: "present"

- name: Modify quota tree limit usage by quota tree path
  dellemc.unity.tree_quota:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    path: "/test_new"
    hard_limit: 10
    cap_unit: "TB"
    soft_limit: 8
    state: "present"

- name: Modify quota tree by quota tree id
  dellemc.unity.tree_quota:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    filesystem_id: "fs_2171"
    tree_quota_id: "treequota_171798700679_10"
    hard_limit: 12
    cap_unit: "TB"
    soft_limit: 10
    state: "present"

- name: Delete quota tree by quota tree id
  dellemc.unity.tree_quota:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    filesystem_id: "fs_2171"
    tree_quota_id: "treequota_171798700679_10"
    state: "absent"

- name: Delete quota tree by path
  dellemc.unity.tree_quota:
    unispherehost: "{{unispherehost}}"
    username: "{{username}}"
    password: "{{password}}"
    validate_certs: "{{validate_certs}}"
    filesystem_id: "fs_2171"
    path: "/test_new"
    state: "absent"
a
  
changed:
    description: Whether or not the resource has changed.
    returned: always
    type: bool
    sample: true

get_tree_quota_details:
    description: Details of the quota tree.
    returned: When quota tree exists
    type: dict
    contains:
        filesystem:
            description: Filesystem details for which the quota
                         tree is created.
            type: dict
            contains:
                UnityFileSystem:
                    description: Filesystem details for which the
                                 quota tree is created.
                    type: dict
                    contains:
                        id:
                            description: ID of the filesystem for
                                         which the quota tree is create.
                            type: str
        description:
            description: Description of the quota tree.
            type: str
        path:
            description: Path to quota tree.
                         A valid path must start with a forward slash '/'.
                         It is mandatory while creating a quota tree.
            type: str
        hard_limit:
            description: Hard limit of quota tree.
                         If the quota tree's space usage exceeds
                         the hard limit, users in quota tree cannot write data.
            type: int
        soft_limit:
            description: Soft limit of the quota tree.
                         If the quota tree's space usage exceeds the soft limit,
                         the storage system starts to count down based
                         on the specified grace period.
            type: int
        id:
            description: Quota tree ID.
            type: str
        size_used:
            description: Size of used space in the filesystem by the user files.
            type: int
        gp_left:
            description: The grace period left after the
                         soft limit for the user quota is exceeded.
            type: int
        state:
            description: State of the quota tree.
            type: int
    sample: {
        "description": "",
        "existed": true,
        "filesystem": {
            "UnityFileSystem": {
                "hash": 8788549469862,
                "id": "fs_137",
                "name": "test",
                "nas_server": {
                    "id": "nas_1",
                    "name": "lglad072"
                }
            }
        },
        "gp_left": null,
        "hard_limit": "6.0 TB",
        "hash": 8788549497558,
        "id": "treequota_171798694897_1",
        "path": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
        "size_used": 0,
        "soft_limit": "5.0 TB",
        "state": 0
    }
)AnsibleModule)utils
tree_quotazAnsible/1.7.1c                   J    e Zd ZdZd Zd Zd Zd ZddZddZ	d	 Z
d
 Zd Zy)	QuotaTreez Class with Quota Tree operationsc                 j   t        j                         | _        | j                  j                  t	                      ddgddgg}t        | j                  d|      | _        t        j                  | j                         t        j                  | j                  j                  t              | _        y)z-Define all parameters required by this modulefilesystem_namefilesystem_idnas_server_namenas_server_idF)argument_specsupports_check_modemutually_exclusiveN)r   $get_unity_management_host_parametersmodule_paramsupdateget_quota_tree_parametersr   moduleensure_required_libsget_unity_unisphere_connectionparamsapplication_type
unity_conn)selfr   s     l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/dellemc/unity/plugins/modules/tree_quota.py__init__zQuotaTree.__init__$  s    "GGI!!";"=>0/B0/BD $,, %13 	""4;;/>>KK 02    c                    ||y| j                   j                  |||      }|r`t        |      dk(  rR| j                  j                  d   dk(  r6d|z  }t
        j                  |       | j                  j                  |       t        |      dkD  r9d|d   j                  d	|}t
        j                  |       |d   j                  S y)
a1  
            Check if quota tree is present in filesystem.
            :param fs_id: ID of filesystem where quota tree is searched.
            :param path: Path to the quota tree
            :param tree_quota_id: ID of the quota tree
            :return: ID of quota tree if it exists else None.
        N)
filesystemidpathr   statepresentzTree quota %s does not exist.msgzQuota tree with id z is present in filesystem )
r   get_tree_quotalenr   r   LOGerror	fail_jsonr#   info)r   fs_idr$   tree_quota_idall_tree_quotaerrormsgr(   s          r   check_quota_tree_is_presentz%QuotaTree.check_quota_tree_is_present6  s      T\775;H=A 8 C S0A5KK&&w/9<6FHIIhKK!!h!/~"IWXYIZI]I]INPCHHSM!!$'''r    c                    |5|3d}t         j                  |       | j                  j                  |       t	        j
                  ||      }t	        j
                  ||      }		 | j                  j                  ||	|||      }
t         j                  d       |
r|
S y# t        $ rW}dj                  ||t        |            }t         j                  |       | j                  j                  |       Y d}~yd}~ww xY w)a  
            Create quota tree of a filesystem.
            :param fs_id: ID of filesystem where quota tree is to be created.
            :param soft_limit: Soft limit
            :param hard_limit: Hard limit
            :param unit: Unit of soft limit and hard limit
            :param path: Path to quota tree
            :param description: Description for quota tree
            :return: Dict containing new quota tree details.
        Nz`Both soft limit and hard limit cannot be empty. Please provide atleast one to create quota tree.r'   )r   
hard_limit
soft_limitr$   descriptionzSuccessfully created quota treezOCreate quota tree operation at path {0} failed in filesystem {1} with error {2})r+   r,   r   r-   r   get_size_bytesr   create_tree_quotar.   	Exceptionformatstr)r   r/   r6   r5   unitr$   r7   r2   soft_limit_in_byteshard_limit_in_bytesobj_tree_quotaes               r   create_quota_treezQuotaTree.create_quota_treeS  s     *"4JHIIhKK!!h!/#22:tD#22:tD	0!__>>U_rJ]dhKV ? XN HH67%% 	0))/eSV)D IIhKK!!h!//		0s   %8B 	C?(AC::C?c                    	 | j                   j                  |      }|j                         }|r|j                  rt	        j
                  t        |d               |d<   t	        j
                  t        |d               |d<   |j                  j                  |d   d   d<   |d   d   j                  d|j                  j                  j                  |j                  j                  j                  di       |S yy# t        $ r`}d	j                  j                  t        |            }t        j!                  |       | j"                  j%                  |
       Y d}~yd}~ww xY w)zDisplay quota tree attributes
            :param tree_quota_id: Quota tree ID
            :return: Quota tree dict to display
        )_idr6   r5   r"   UnityFileSystemname
nas_server)rF   r#   z7Failed to display quota tree details {0} with error {1}r'   N)r   r)   _get_propertiesexistedr   convert_size_with_unitintr"   rF   r   rG   r#   r:   r;   r<   r+   r,   r   r-   )r   r0   tree_quota_objtree_quota_detailsrA   r2   s         r   ,get_filesystem_tree_quota_display_attributesz6QuotaTree.get_filesystem_tree_quota_display_attributesx  s]   
	0!__;;;NN!/!?!?!A."8"838**3/A,/O+P4R"<038**3/A,/O+P4R"<0 #--22 #<01BCFK"<01BCJJ!N,E,E,P,P,U,U*8*C*C*N*N*Q*Q$S TU *) #9~  	0##)6.*;*;SV#D IIhKK!!h!//		0s   C:C? ?	E(AE##E(Nc                    |r|n|}	 d}|rx|s3d}t         j                  |       | j                  j                  |       | j                  j                  ||      }|r$|j                  rt         j                  d|       |S |rd|r| j                  j                  ||      }n| j                  j                  |      }|r%|j                  rt         j                  d|       |S yyy# t        $ rM}d|d	t        |      d
}t         j                  |       | j                  j                  |       Y d}~yd}~ww xY w)z
            Get filesystem details.
            :param nas_server: Nas server object.
            :param name: Name of filesystem.
            :param id: ID of filesystem.
            :return: Dict containing filesystem details if it exists.
        Nz-NAS Server is required to get the FileSystem.r'   )rF   rG   z*Successfully got the filesystem object %s.)r#   rG   r#   zFailed to get filesystem  with error .)
r+   r,   r   r-   r   get_filesystemrI   r.   r:   r<   )	r   rG   rF   r#   
id_or_nameobj_fserr_msgrA   	error_msgs	            r   rS   zQuotaTree.get_filesystem  s-    R4
	1F!MGIIg&KK))g)677TCM 8 OfnnHHI#%!M!__'2*E  "__;;r;BFfnnHHI#%!M -6   	1%s1v/IIIi KK!!i!00		1s    A;C+ A#C+ +	E4AD<<Ec                    |r|n|}d|z  }	 | j                   j                  ||      }|r$|j                  rt        j	                  d|       |S |r$|j                  rt        j	                  d|       |S t        j                  |       | j                  j                  |       y# t        $ rM}d|dt        |      d}t        j                  |       | j                  j                  |       Y d}~yd}~ww xY w)	z
            Get nas server details.
            :param name: Nas server name.
            :param id: Nas server ID.
            :return: Dict containing nas server details if it exists.
        zFailed to get NAS server %s.)rD   rF   z*Successfully got the NAS server object %s.r'   zFailed to get NAS server rQ   rR   N)
r   get_nas_serverrI   r+   r.   r,   r   r-   r:   r<   )r   rF   r#   rG   rW   obj_nasrA   s          r   get_nas_server_objzQuotaTree.get_nas_server_obj  s     R4
3j@		1oo44$4GGE "E "		)$%%)%4 	1%s1v/IIIi KK!!i!00		1s%   AB( %B( 61B( (	C>1AC99C>c                 R   	 ||y| j                   j                  |      j                         }||d   }nt        j                  ||      }||d   }nt        j                  ||      }||d   }|rQ|d   |k(  r|d   |k(  r	|d   |k(  ry| j                   j                  ||||      }	t        j                  d       |	ryyy# t        $ rV}
d	j                  |t        |
            }t        j                  |       | j                  j                  |
       Y d}
~
yd}
~
ww xY w)a  
            Modify quota tree of filesystem.
            :param tree_quota_id: ID of the quota tree
            :param soft_limit: Soft limit
            :param hard_limit: Hard limit
            :param unit: Unit of soft limit and hard limit
            :param description: Description of quota tree
            :return: Boolean value whether modify quota tree operation is successful.
        NFr6   r5   r7   )r0   r5   r6   r7   z Successfully modified quota treeTz5Modify quota tree operation {0} failed with error {1}r'   )r   r)   rH   r   r8   modify_tree_quotar+   r.   r:   r;   r<   r,   r   r-   )r   r0   r6   r5   r=   r7   rL   r>   r?   r]   rA   r2   s               r   r]   zQuotaTree.modify_tree_quota  sP   !	0!j&8!__;;MJZZ\N!&4\&B#&+&:&::t&L#!&4\&B#&+&:&::t&L#",];!,/3FF&|48KK&}5D (,(I(IXeUhUhVa )J )c% HH?@(# )   	0))/s1v)F IIhKK!!h!//		0s$   C BC 6C 	D&AD!!D&c                    	 | j                   j                  |      }|ryy# t        $ rV}dj                  |t	        |            }t
        j                  |       | j                  j                  |       Y d}~yd}~ww xY w)z
        Delete quota tree of a filesystem.
        :param tree_quota_id: ID of quota tree
        :return: Boolean whether quota tree is deleted
        )r0   Tz;Delete operation of quota tree id:{0} failed with error {1}r'   N)	r   delete_tree_quotar:   r;   r<   r+   r,   r   r-   )r   r0   delete_tree_quota_objrA   r2   s        r   r_   zQuotaTree.delete_tree_quota  s    	0$(OO$E$ETa$E$b!$ %  	0//5vm69!f0>  IIhKK!!h!//	0s   " 	BAA<<Bc                 l	   | j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d   }| j                   j                  d	   }	| j                   j                  d
   }
| j                   j                  d   }d}d}d}	 t        dddi d      }|s|r|d}|rHt        j                  |      r3d}t
        j                  |       | j                   j                  |       |rHt        j                  |      r3d}t
        j                  |       | j                   j                  |       	 |Dt        j                  |      r| j                   j                  d       | j                  |      }nE|Ct        j                  |      r| j                   j                  d       | j                  |      }	 |Qt        j                  |      r| j                   j                  d       | j                  ||      }|j                  }ns|qt        j                  |      r| j                   j                  d       | j                  |      }|r|d   j                  }n| j                   j                  d       	 |	t        j                  |	      r| j                   j                  d       n-|	j                  d      s| j                   j                  d       ||| j                   j                  d       | j                  ||	|      }|}	 |s|r4|	2|d k(  r-|s+t
        j                  d!       | j                  |||||	|
      }|r|j                  }d"|d#<   	 |r2|d k(  r-t
        j                  d$       | j!                  |||||
      |d%<   	 |.|d&k(  r)t
        j                  d'       | j#                  |      |d(<   	 |d k(  r|| j%                  |      |d)<   ni |d)<   |d#   s
|d%   s|d(   rd"|d*<    | j                   j&                  d+i | y),zs
        Perform different actions on quota tree module based on parameters
        passed in the playbook
        r   r   r   r   cap_unitr%   r5   r6   r$   r7   r0   NF)changedr9   r]   get_tree_quota_detailsr_   GBz?Invalid soft_limit provided, must be greater than or equal to 0r'   z?Invalid hard_limit provided, must be greater than or equal to 0z;Invalid nas_server_name given, Please provide a valid name.)rF   z7Invalid nas_server_id given, Please provide a valid ID.rP   z;Invalid filesystem_name given, Please provide a valid name.)rG   rF   z7Invalid filesystem_id given, Please provide a valid ID.r   zFilesystem does not exist.z Please provide a valid path./z\The path is relative to the root of the file system and must start with a forward slash '/'.z6Please provide either filesystem_name or fileystem_id.r&   zCreating quota treeTr9   zModifying quota treer]   absentzDeleting quota treer_   rd   rc    )r   r   dictr   is_size_negativer+   r,   r-   is_input_emptyr[   rS   r#   
startswithr3   r.   rB   r]   r_   rN   	exit_json)r   r   r   r   r   rb   r%   r5   r6   r$   r7   r0   create_tree_quota_objnas_server_resourcer/   resulterror_messagefilesystem_objquota_tree_id_presents                      r   perform_module_operationz"QuotaTree.perform_module_operation  s   
 **?;++,,->?++,,->?**?;;;%%j1""7+[[''5
[[''5
{{!!&)kk((7**?; $"	 ###%#
 *(*:H%00<AMIIm$KK!!m!4%00<AMIIm$KK!!m!4	 &##O4%% +J% K"&###9  &##M2%% +H% I"&"9"9]"9"K	 &##O4%% +J% K!+>%4   6  #%%E&##M2%% +H% I!=1 &q),,%%*F%G	 ##D)%%*I%J__S)%% +U% V $)@%%*b%c $ @ @m \-	 _$2BuPYGY ./(,(>(>ujR\?G{)\% !144M*.F&'	 Ui/HH+,*.*@*@PZ\fhpAL+NF&'	 $():HH*+*.*@*@*OF&'	 I-";/3/`/`an/oF+,/1F+,%&&1D*EPcId $F9''r    )NNN)NN)__name__
__module____qualname____doc__r   r3   rB   rN   rS   r[   r]   r_   rt   rh   r    r   r
   r
   !  s6    *2$:#0J06$1L18+0Z0(O(r    r
   c                  .   t        t        dd      t        dd      t        ddddg      t        dd      t        dd      t        ddg d	      t        dd      t        dd      t        dd      t        ddd
      t        dd            S )zYThis method provide parameters required for the ansible
       quota tree module on UnityFr<   )requiredtypeTr&   rg   )rz   r{   choicesrK   )MBre   TB)rz   r{   no_log)r   r   r%   r5   r6   rb   r0   r   r   r$   r7   )ri   rh   r    r   r   r     s     E6e%8Duy(6KLU3U3u5:LME6e%8E65uT:%e4 r    c                  8    t               } | j                          y)zb Create Unity quota tree object and perform action on it
        based on user input from playbookN)r
   rt   )objs    r   mainr     s     +C  "r    __main__N)rx   
__future__r   r   r   r{   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Cansible_collections.dellemc.unity.plugins.module_utils.storage.dellr   
get_loggerr+   r   objectr
   r   r   ru   rh   r    r   <module>r      s    6 @ @Yv\|Q
f 5 e|$" D( D(N$# zF r    