
    VhM                        d Z ddlmZmZmZ eZdZdZddl	m
Z
mZ dZ	 ddlmZmZmZmZmZmZmZmZ 	 dd
lmZ ddlmZ dZ	 ddlmZmZ dZ ed        Z!ed        Z"ed        Z#ed        Z$ed        Z%ed        Z&ed        Z'd Z(d Z)d Z*d Z+d Z,d Z-e.dk(  r e-        yy# e$ r ddlmZmZmZmZmZmZmZ Y e$ r d	ZY w xY w# e$ r d	ZY w xY w# e$ r d	Z Y w xY w)z.This module manages file systems on Infinibox.    )absolute_importdivisionprint_functiona	  
---
module: infini_fs
version_added: 2.3.0
short_description: Create, Delete or Modify filesystems on Infinibox
description:
    - This module creates, deletes or modifies filesystems on Infinibox.
author: David Ohlemacher (@ohlemacher)
options:
  fs_type:
    description:
      - Specifies the file system type, regular or snapshot.
    type: str
    required: false
    default: master
    choices: [ "master", "snapshot" ]
  name:
    description:
      - File system name.
    required: false
    type: str
  parent_fs_name:
    description:
      - Specify a fs name. This is the fs parent for creating a snapshot. Required if fs_type is snapshot.
    type: str
    required: false
  pool:
    description:
      - Pool that will host file system.
    required: true
    type: str
  restore_fs_from_snapshot:
    description:
      - Specify true to restore a file system (parent_fs_name) from an existing snapshot specified by the name field.
      - State must be set to present and fs_type must be 'snapshot'.
    type: bool
    required: false
    default: false
  serial:
    description:
      - Serial number matching an existing file system.
    required: false
    type: str
  size:
    description:
      - File system size in MB, GB or TB units. See examples.
    required: false
    type: str
  snapshot_lock_expires_at:
    description:
      - This will cause a snapshot to be locked at the specified date-time.
        Uses python's datetime format YYYY-mm-dd HH:MM:SS.ffffff, e.g. 2020-02-13 16:21:59.699700
    type: str
    required: false
  snapshot_lock_only:
    description:
      - This will lock an existing snapshot but will suppress refreshing the snapshot.
    type: bool
    required: false
    default: false
  state:
    description:
      - Creates/Modifies file system when present or removes when absent.
    required: false
    default: present
    choices: [ "stat", "present", "absent" ]
    type: str
  thin_provision:
    description:
      - Whether the master file system should be thin or thick provisioned.
    required: false
    default: true
    type: bool
  write_protected:
    description:
      - Specifies if the file system should be write protected. Default will be True for snapshots, False for master file systems.
    type: str
    required: false
    default: "Default"
    choices: ["Default", "True", "False"]
extends_documentation_fragment:
    - infinibox
requirements:
    - capacity
a  
- name: Create new file system named foo under pool named bar
  infini_fs:
    name: foo
    size: 1GB
    pool: bar
    thin_provision: true
    state: present
    user: admin
    password: secret
    system: ibox001
- name: Create snapshot named foo_snap from fs named foo
  infini_fs:
    name: foo_snap
    pool: bar
    fs_type: snapshot
    parent_fs_name: foo
    state: present
    user: admin
    password: secret
    system: ibox001
- name: Stat snapshot, also a fs, named foo_snap
  infini_fs:
    name: foo_snap
    pool: bar
    state: present
    user: admin
    password: secret
    system: ibox001
- name: Remove snapshot, also a fs, named foo_snap
  infini_fs:
    name: foo_snap
    state: absent
    user: admin
    password: secret
    system: ibox001
)AnsibleModulemissing_required_libT)api_wrappercheck_snapshot_lock_optionsget_filesystemget_fs_by_snget_pool
get_systeminfinibox_argument_specmanage_snapshot_locks)r   r	   r
   r   r   r   r   F)APICommandFailed)ObjectNotFoundN)KiBCapacityc                    d}| j                   s| j                  d   rd}nd}|j                  j                  | j                  d   |t	        | |            }| j                  d   r?t        | j                  d         j                  dt        z        }|j                  |       |j                         }| j                  d	   }||k7  r|j                  d	|       d
}|S )z Create Filesystem Fthin_provisionTHINTHICKname)r   provtypepoolsize@   write_protectedT)
check_modeparamsfilesystemscreater   r   roundupr   update_sizeis_write_protectedupdate_field)modulesystemchangedprovisioning
filesystemr   is_write_protdesired_is_write_prots           q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/infinidat/infinibox/plugins/modules/infini_fs.pycreate_filesystemr.      s     G==)*!L"L''..v&!&&) / 

 == FMM&12::28DD""4("557 &.? @11##$57LMN    c                 H   d}| j                   r|S | j                  d   rTt        | j                  d         j                  dt        z        }|j                         |k7  r|j                  |       d}| j                  d   gt        |j                               }|dk(  r"| j                  d   r|j                  d       d}|dk(  r"| j                  d   s|j                  d       d}|j                         }| j                  d   }||k7  r|j                  d|       d}|S )	z Update Filesystem Fr   r   Tr   r   r   r   )r   r   r   r"   r   get_sizer#   strget_provisioningupdate_provisioningr$   r%   )r&   r*   r(   r   r)   r+   r,   s          r-   update_filesystemr5      s    G}}Vf-.66rCx@ D(""4(G}}%&2:66897"v}}5E'F**62G6!&--8H*I**73G113M"MM*;<-- 13HINr/   c                 F    d}| j                   s|j                          d}|S )z Delete Filesystem FT)r   delete)r&   r*   r(   s      r-   delete_filesystemr8      s(     GNr/   c                 >   | j                   d   }| j                   d   }d}| j                  s	 |j                  j                  |      }sd	| }| j                  |       | j                  sv| j                   d
   rd}| j                  |       t        |        |j                  |      }|j                         }| j                   d   }	||	k7  r|j                  d|	       t        |        d}|S # t        $ r d| d| d}| j                  |       Y w xY w)z  Create Snapshot from parent fs r   parent_fs_nameF)r   zCannot create snapshot z. Parent file system 
 not foundmsgz4Cannot find new snapshot's parent file system named snapshot_lock_onlyzGSnapshot does not exist. Cannot comply with 'snapshot_lock_only: true'.r   T)r   r   r    getr   	fail_jsonr	   create_snapshotr$   r%   r   )
r&   r'   snapshot_namer:   r(   	parent_fsr=   snapshotr+   r,   s
             r-   create_fs_snapshotrE      s>    MM&)M]]#34NG	&**..N.CI HHXYC%  }}12_  S )'/ 00m0DH$779M$*MM2C$D! 55%%&79NOfh/N)  	&+M?:OP^O__ijC%	&s   C5 5$DDc                    d}d}| j                   s| j                  d   sH|j                         dk(  }|s| j                   s|j                          d}nd}| j	                  |       t        |        t        | |      }| j                  d   6|j                         }| j                  d   }||k7  r|j                  d|       |xs |S )z/ Update/refresh fs snapshot. May also lock it. Fr>   LOCKEDTz7File system snapshot is locked and may not be refreshedr<   r   )	r   r   get_lock_staterefresh_snapshotr@   r	   r   r$   r%   )r&   rD   refresh_changedlock_changedsnap_is_lockedr=   r+   r,   s           r-   update_fs_snapshotrM     s     OL}}12%446(BN!((--/"&O  S )#F+,VX>==*+7$779M$*MM2C$D! 55%%&79NO*l*r/   c                     d| d}|j                   j                  |      }|j                         d   }t        |      dk7  r| j	                  d| d       |d   d	   }|S )
z Find the ID of this fs zfilesystems?name=z
&fields=id)pathresult   z%Cannot find a file ststem with name ''r   id)apir?   get_jsonlenr@   )r&   r'   fs_namefs_urlfsrP   fs_ids          r-   
find_fs_idr[   9  sl     !	4F	V	$B[[]8$F
6{a@	KL1IdOELr/   c                    d}| j                   d   }| j                   d   }| j                   d   }t        | ||      }| j                   d   }t        | ||      }|st        d      |dk7  r| j                  d	       |s| j                  d
	       | j                  s,d| d}	d|i}
	 |j
                  j                  |	|
       d}|S |S # t        $ r/}| j                  d| d| dt        |       	       Y d}~|S d}~ww xY w)z' Use snapshot to restore a file system Frestore_fs_from_snapshotfs_typer   r:   z6A programming error occurred. is_restoring is not TruerD   z[Cannot restore a parent file system from snapshot unless the file system type is 'snapshot'r<   zaCannot restore a parent file system from snapshot unless the parent file system name is specifiedzfilesystems/z/restore?approved=true	source_id)rO   dataTzCannot restore file system z from snapshot z: N)
r   r[   AssertionError	exit_jsonr   rT   postr   r@   r2   )r&   r'   r(   is_restoringr^   	snap_namesnap_idr:   parent_fs_idrestore_urlrestore_dataerrs               r-   r]   r]   G  s@    G==!;<LmmI&Gf%I3G]]#34Nffn=L UVV*z{  A  	B$\N2HI
	wJJOO<O@G N7N   	w#>~>No^g]hhjknorksjt!uvvN	ws   (C 	D$C>>Dc                    t        |       }t        | |      }| j                  d   rt        | |      }nt	        | |      }| j                  d   }|dk(  r%|s#| j                  d| j                  d    d       |s#| j                  d| j                  d    d       |j                         }t        |j                  d	d
            }|j                  dd
      }|j                  dd
      }|j                  dd
      }	t        |j                               }
|j                         }t        |j                  dd
            }|j                  dd
      }|j                  dd
      }|j                  dd
      }|j                  dd
      }t        |j                               }t        |j                  dd
            }t        |j                  dd
            }|j                  dd
      }|dk(  rd}nd}t        d!i ddd	|d|d|d|	d|d|
d|d|d|d|d|d|d|d|d |d|} | j                  d!i | y
)"z Handle the stat state r   r^   masterPool r   r;   r<   File system 
created_atNrS   typehas_childrenmapped	parent_idr)   serial
updated_at	used_sizer   SNAPSHOTzFile system snapshot stat foundzFile system stat foundr(   Ffilesystem_idfilesystem_type
lock_statelock_expires_atr=   r   used )r   r   r   r
   r   r@   
get_fieldsr2   r?   get_lock_expires_atrH   r1   dictrb   )r&   r'   r   r*   r^   fieldsro   rx   ry   rq   r{   rz   rr   r   rs   r)   rt   r   ru   r|   r   r=   rP   s                          r-   handle_statr   g  s   FFF#D}}V#FF3
!&&1
mmI&G(5v)>(?z!JK|FMM&,A+B*MN""$FVZZd34JJJtT*Mjj.O::nd3L*88:;O**,JHd+,F::fd#D

;-I::nd3LZZ$'Fz""$%DVZZd34Jvzz+t,-Djj!2D9O*$/&  $ (	
 "  (     "     !" (#F& Fvr/   c                    t        |       }t        | |      }| j                  d   rt        | |      }nt	        | |      }| j                  d   }| j                  d   }|dk(  rg|s#| j                  d| j                  d    d       |s t        | |      }| j                  |d	
       yt        | |      }| j                  |d
       y|dk(  r|}|rFt        | |      }| j                  d   }| j                  d   }	d|	 d| }
| j                  ||

       y|s t        | |      }| j                  |d
       yt        | |      }| j                  |d
       yy)z Handle the present state r   r^   r]   rl   rm   r   r;   r<   zFile system createdr(   r=   zFile system updatedrD   r:   rn   z restored from snapshot zFile system snapshot createdzFile system snapshot updatedN)r   r   r   r
   r   r@   r.   rb   r5   r]   rE   rM   )r&   r'   r   r*   r^   rd   r(   rD   snap_fs_namer:   r=   s              r-   handle_presentr     ss   FFF#D}}V#FF3
!&&1
mmI&G==!;<L(5v)>(?z!JK'7GW2GH'
;GW2GH	J	.vv>G!==0L#]]+;<N  00HWCW#6,VV<  6T U,VZ@  6T U 
r/   c                    t        |       }t        | |      }| j                  d   rt        | |      }nt	        | |      }|r(|j                         dk(  rd}| j                  d|       |r|s| j                  dd       |j                         }|dk(  r t        | |      }| j                  |d       y|d	k(  r"|}t        | |      }| j                  |d
       y| j                  d       y)z Handle the absent state r   rG   zCannot delete snapshot. Locked.Fr   zFile system already absentMASTERzFile system removedrw   zSnapshot removedzA programming error has occuredr<   N)
r   r   r   r
   r   rH   r@   rb   get_typer8   )r&   r'   r   r*   r=   existing_fs_typer(   rD   s           r-   handle_absentr     s    FFF#D}}V#FF3
!&&1
j//1X=/C0z,HI!**,8##FJ7.CD	Z	'#FH5.@A>?r/   c                 *   | j                   d   }	 |dk(  rt        |        n7|dk(  rt        |        n&|dk(  rt        |        n| j	                  d|        t        |       }|j                          y# t        |       }|j                          w xY w)z Execute states statestatpresentabsentz'Internal handler error. Invalid state: r<   N)r   r   r   r   r@   r   logout)r&   r   r'   s      r-   execute_stater     s    MM'"EF?i6"h&!#J5'!RSF# F#s   AA5 5Bc                    | j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }|dk(  r|s|sd	}| j                  |
       |dv r|sd}| j                  |
       |dk(  r|dk(  rI|rd}|dz  }| j                  |
       |sd}| j                  |
       |sd}| j                  |
       yy|dk(  r8|rd}|dz  }| j                  |
       |sd}|dz  }| j                  |
       yyd}| j                  |
       yy)zVerify module options are saner   rt   r   r   r   r^   r:   r   z)Name or serial parameter must be providedr<   )r   r   zName parameter must be providedr   rl   z'parent_fs_name should not be specified z0if fs_type is 'master'. Used for snapshots only.z/Size is required to create a master file systemz%For state 'present', pool is requiredrD   zSize should not be specified zfor fs_type snapshotz,For state 'present' and fs_type 'snapshot', zparent_fs_name is requiredz A programming error has occurredN)r   r@   )	r&   r   rt   r   r   r   r^   r:   r=   s	            r-   check_optionsr     s   == D]]8$FMM'"E== D== DmmI&G]]#34NF=C%%%3C%	h?II  S )G  S )=  S )  
"5--  S )!D33  S ) "
 5C%/ r/   c                     t               } | j                  t        t        ddgd      t        dd      t        dd      t        d	      t        dd
      t        dd      t               t               t        dd
d      t        dg d      t        dd
      t        g dd                   t        | d      }|j                  d   dk(  r2|j                  d   dk(  rd|j                  d<   n/d|j                  d<   n|j                  d   dk(  |j                  d<   t
        s|j                  t        d             t        s|j                  t        d             |j                  d   r	 t        |j                  d          t        |       t        |       y# t        $ r |j                  d       Y 4w xY w)z Main rl   rD   )choicesdefaultFN)requiredr   )r   r   T)r   bool)r   rp   )r   rp   r   r   )r   r   r   )r   r   )TrueFalseDefaultr   )r^   r   r:   r   r]   rt   r   snapshot_lock_expires_atr>   r   r   r   )supports_check_moder   r^   r   	infinisdkr<   capacityr   zDsize (Physical Capacity) should be defined in MB, GB, TB or PB units)r   updater   r   r   HAS_INFINISDKr@   r   HAS_CAPACITYr   	Exceptionr   r   )argument_specr&   s     r-   mainr   "  s   +-M(J!7Jud3u=t$%)%f%E5%)V#UOy2OP6: )EyY	
" =dCF}}&'94==#x//4FMM+,/3FMM+,+1==9J+Kv+U'(1+>?1*=>}}V	V]]6*+ &&  	Z  	s   7F& &GG__main__)/__doc__
__future__r   r   r   rp   __metaclass__DOCUMENTATIONEXAMPLESansible.module_utils.basicr   r   r   Fansible_collections.infinidat.infinibox.plugins.module_utils.infiniboxr   r	   r
   r   r   r   r   r   ModuleNotFoundError	infiniboxImportErrorinfinisdk.core.exceptionsr   r   CAPACITY_IMP_ERRr   r   r   r   r.   r5   r8   rE   rM   r[   r]   r   r   r   r   r   r   __name__r}   r/   r-   <module>r      s   5
 A @Tl$P K	 	 	.:8  &L
  6  >    > + +6 
 
  >8v"VJ@:"*&Z-` zF o  	    M  M  Ls:   B$ C	 
C $C=CC	CCC C 