
    Vh+                     4   d Z ddlmZmZmZ eZ	 ddlmZm	Z	 ddl
mZ dZdZdZ	 ddlZddlmZ dd	lmZ dd
lmZ ddlmZ dZ	 ddlZ ej6                  ej8                  j:                         dad Zd Z d Z!d Z"d Z#e d        Z$e d        Z%e d        Z&e d        Z'e d        Z(e d        Z)e d        Z*e d        Z+e d        Z,e d        Z-e dd       Z.d Z/d Z0d Z1y# e$ rZdZeZY dZ[dZ[ww xY w# e$ r dZY e$ r dZY w xY w# e$ r dZY w xY w) z Infinidat utilities     )absolute_importdivisionprint_function)	InfiniBoxcore)ObjectNotFoundTNFwraps)environ)path)datetimec                 6    t        j                  | dz        dfS )z2 Convert unix time with ms to a datetime UTC time g     @@UTC)r   utcfromtimestamp)unix_mss    v/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/infinidat/infinibox/plugins/module_utils/infinibox.pyunixMillisecondsToDater   6   s    %%go6>>    c                 .     t                fd       }|S )z Catch API Errors Decoratorc                     | d   }	  | i |S # t         j                  j                  $ r%}|j                  t	        |             Y d }~y d }~wt         j                  j
                  $ r%}|j                  t	        |             Y d }~y d }~wt        $ r%}|j                  t	        |             Y d }~y d }~ww xY w)Nr   msg)r   
exceptionsSystemNotFoundException	fail_jsonstrAPICommandException	Exception)argskwargsmoduleerrfuncs       r   	__wrapperzapi_wrapper.<locals>.__wrapper=   s    a	+(((66 	+S**
 	 22 	+S**   	+S**	+s,    B>A B>-BB>B99B>r	   )r#   r$   s   ` r   api_wrapperr%   ;   s!    
4[
 
 r   c                  \    t        t        d      t        d      t        dd            S )zTReturn standard base dictionary used for the argument_spec argument in AnsibleModuleT)required)r'   no_log)systemuserpassword)dict r   r   infinibox_argument_specr.   L   s)    T"4 tD1 r   c                      ddggS )zPReturn the default list used for the required_together argument to AnsibleModuler*   r+   r-   r-   r   r   infinibox_required_togetherr0   U   s    Z !!r   c                 H    | j                         }|j                  |       |S )zd
    Merge two dicts into one and return.
    result = {**dict1, **dict2} only works in py3.5+.
    )copyupdate)dict1dict2results      r   merge_two_dictsr7   Z   s     
 ZZ\F
MM%Mr   c                    t         s#| j                  d   }| j                  j                  dd      }| j                  j                  dd      }|r|rt        |||fd      a nt	        j                  d      rLt	        j                  d      r7t        |t	        j                  d      t	        j                  d      fd      a nKt        j                  t        j                  d	      d
z         rt        |d      a n| j                  d       	 t         j                          t         S t         S # t        $ r | j                  d       Y t         S w xY w)z
    Return System Object if it does not exist or Fail.
    Use a global system Infinibox object so that there will only be one
    system session used for this module instance.
    Enables execute_state() to log out of the only session properly.
    r)   r*   Nr+   T)authuse_sslINFINIBOX_USERINFINIBOX_PASSWORD~z/.infinidat/infinisdk.ini)r:   zrYou must set INFINIBOX_USER and INFINIBOX_PASSWORD environment variables or set username/password module argumentsr   z7Infinibox authentication failed. Check your credentials)INFINIBOX_SYSTEMparamsgetr   r   r   isfile
expanduserr   loginr   )r!   boxr*   r+   s       r   
get_systemrE   d   s0    mmH%}}  .==$$Z6H(D(3CTR[[)*w{{;O/P(/6{{;K/L/6{{;O/P/R15 7 [[-0KKL(d;  "V  W	\""$   	\!Z[	\s   D1 1EEc                     	 	 | j                   d   }|j                  j                  |      S # t        $ r2 	 | j                   d   }n# t        $ r | j                   d   }Y nw xY wY Vw xY w# t        $ r Y yw xY w)zy
    Return Pool. Try key look up using 'pool', or if that fails, 'name'.
    If the pool is not found, return None.
    poolnameobject_namerH   N)r?   KeyErrorpoolsr@   r   )r!   r)   rH   s      r   get_poolrM      s    
	4==(D ||T**  	44}}V, 4}}]34	4  sO   . A, 	A)AA)A# A)"A##A)&A, (A))A, ,	A87A8c                 `   	 	 |j                   j                  | j                  d         }|S # t        $ rg 	 |j                   j                  | j                  d         }n8# t        $ r, |j                   j                  | j                  d         }Y nw xY wY |S w xY w# t        $ r Y yw xY w)zReturn Filesystem or None
filesystemrJ   rH   rI   N)filesystemsr@   r?   rK   r   r!   r)   rO   s      r   get_filesystemrR      s    
	W++//V]]<5P/QJ   	WW#//33v9N3O
 W#//33}9U3V
W	W  O   ). B! 	B)A"!B"2BBBBB! BB! !	B-,B-c                     	 	 | j                   d   }|j                  j                  |      }|S # t        $ r | j                   d   }Y 8w xY w# t        $ r Y yw xY w)z+Return export if found or None if not foundexportrH   )export_pathN)r?   rK   exportsr@   r   )r!   r)   export_namerU   s       r   
get_exportrY      sl    	0 --1K ###< M  	0 --/K	0  s,   0 A AA 
AA 	AAc                 `   	 	 |j                   j                  | j                  d         }|S # t        $ rg 	 |j                   j                  | j                  d         }n8# t        $ r, |j                   j                  | j                  d         }Y nw xY wY |S w xY w# t        $ r Y yw xY w)zReturn Volume or NonerH   rJ   volumerI   N)volumesr@   r?   rK   r   r!   r)   r[   s      r   
get_volumer^      s    
	O^^''V]]6-B'CF   	OO++x1H+I O++}1M+NO	O  rS   c                     	 |j                   j                  | j                  d         }|S # t        t        f$ r Y yw xY w)zReturn network space or NonerH   rJ   N)network_spacesr@   r?   rK   r   )r!   r)   	net_spaces      r   get_net_spacerb      sJ    ))--6==3H-I	  n% s   )- ??c                 x    	 |j                   j                  | j                  d         }|S # t        $ r Y yw xY w)z-Return volume that matches the serial or Noneserialrd   N)r\   r@   r?   r   r]   s      r   get_vol_by_snrf      sB    ##6==+B#C M     )- 	99c                 x    	 |j                   j                  | j                  d         }|S # t        $ r Y yw xY w)z1Return filesystem that matches the serial or Nonerd   re   N)rP   r@   r?   r   rQ   s      r   get_fs_by_snri      sE    ''++6==3J+K
   rg   c                    d}|j                   j                         D ],  }|j                         }	 | j                  d   }||k(  s)|} |S  |S # t        $ r2 	 | j                  d   }n# t        $ r | j                  d   }Y nw xY wY Hw xY w)z/Find a host by the name specified in the moduleNrH   hostrI   )hoststo_listget_namer?   rK   )r!   r)   rk   a_hosta_host_name
host_params         r   get_hostrr      s     D,,&&( oo'	:v.J *$DK K  	::#]]62
 :#]]=9
:	:5   A	B	A('B	(B B	BB	B	c                    d}|j                   j                         D ],  }|j                         }	 | j                  d   }||k(  s)|} |S  |S # t        $ r2 	 | j                  d   }n# t        $ r | j                  d   }Y nw xY wY Hw xY w)z2Find a cluster by the name specified in the moduleNrH   clusterrI   )host_clustersrm   rn   r?   rK   )r!   r)   ru   	a_clustera_cluster_namecluster_params         r   get_clusterrz      s     G ))113 	"++-	="MM&1M ]*GN N  	== &i 8 = &m <=	=rs   c                     d}|s| j                   d   }n|}	 |j                  j                  |      }|S # t        $ r Y |S w xY w)z4Find a user by the user_name specified in the moduleN	user_namerJ   )r?   usersr@   r   )r!   r)   user_name_to_findr*   r|   s        r   get_userr     s[     DMM+.	%	||Y/ K  Ks   6 	AAc                 .   | j                   d   }|rt        j                  |      }t        j                         }||k  rd}|d| dz  }| j	                  |       d}|j                  |      }||k\  rd|d	z  d
z   d}| j	                  |       yyy)aE  
    Check if specified options are feasible for a snapshot.

    Prevent very long lock times.
    max_delta_minutes limits locks to 30 days (43200 minutes).

    This functionality is broken out from manage_snapshot_locks() to allow
    it to be called by create_snapshot() before the snapshot is actually
    created.
    snapshot_lock_expires_atz5Cannot lock snapshot with a snapshot_lock_expires_at zof 'z' from the pastr   i  )minutesz!snapshot_lock_expires_at exceeds    <   z days in the futureN)r?   arrowr@   utcnowr   shift)r!   r   lock_expires_atnowr   max_delta_minutesmax_lock_expires_ats          r   check_snapshot_lock_optionsr   "  s      &}}-GH))$<= llnc!ICT23?CCC% "!ii0AiB1156G26MQS6S5TTghC% 2  r   c                 L   | j                   d   }|j                         dk(  }|j                         }d}t        |        |rbt	        j
                  |      }|r"||k  rd| d| d}| j                  |       |S |r||k(  r	 |S | j                  s|j                  |       d}|S )	z
    Manage the locking of a snapshot. Check for bad lock times.
    See check_snapshot_lock_options() which has additional checks.
    r   LOCKEDFzsnapshot_lock_expires_at 'z%' preceeds the current lock time of ''r   T)	r?   get_lock_stateget_lock_expires_atr   r   r@   r   
check_modeupdate_lock_expires_at)r!   snapshotr   snap_is_lockedcurrent_lock_expires_atchangedr   r   s           r   manage_snapshot_locksr   A  s    
  &}}-GH,,.(:N&::<G'))$<=o0GG..??de|d}}~C% N 3J J N $$//@GNr   c                 n    d}t         s|dz  }t        s|dz  }t        s|dz  }| j                  |       y )N zFailed to import arrow module. z#Failed to import infinisdk module. z!Failed to import urllib3 module. r   )	HAS_ARROWHAS_INFINISDKHAS_URLLIB3r   )r!   r   s     r   !catch_failed_module_utils_importsr   ^  sB    
C004422
r   )N)2__doc__
__future__r   r   r   type__metaclass__	infinisdkr   r   infinisdk.core.exceptionsr   r   INFINISDK_IMPORT_ERRORImportErrorimp_excr   r   r   	functoolsr
   osr   r   r   r   urllib3disable_warningsr   InsecureRequestWarningr>   r   r%   r.   r0   r7   rE   rM   rR   rY   r^   rb   rf   ri   rr   rz   r   r   r   r   r-   r   r   <module>r      s    B B")8
 M!	    GW//FFG
  ?
""
  D  $              (  *  &>:O
  %M$%  I M  Ks@   C" C8 *D "C5'C00C58D
D
	D
DD