
    VhL                     z   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dlmZmZmZmZmZmZmZmZmZ dZ	 ddlmZ dd	lmZ dZ	 ddlmZm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
ZY lw xY w# e$ r d
ZY nw xY w)z) A module for managing Infinibox volumes     )absolute_importdivisionprint_functiona 
  
---
module: infini_vol
version_added: '2.3.0'
short_description:  Create, Delete or Modify volumes on Infinibox
description:
    - This module creates, deletes or modifies a volume on Infinibox.
author: David Ohlemacher (@ohlemacher)
options:
  name:
    description:
      - Volume name.
    type: str
    required: false
  serial:
    description:
      - Volume serial number.
    type: str
    required: false
  parent_volume_name:
    description:
      - Specify a volume name. This is the volume parent for creating a snapshot. Required if volume_type is snapshot.
    type: str
    required: false
  pool:
    description:
      - Pool that master volume will reside within.  Required for creating a master volume, but not a snapshot.
    type: str
    required: false
  size:
    description:
      - Volume size in MB, GB or TB units.  Required for creating a master volume, but not a snapshot
    type: str
    required: false
  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 master volume or snapshot when present or removes when absent.
    type: str
    required: false
    default: present
    choices: [ "stat", "present", "absent" ]
  thin_provision:
    description:
      - Whether the master volume should be thin or thick provisioned.
    type: bool
    required: false
    default: true
  write_protected:
    description:
      - Specifies if the volume should be write protected. Default will be True for snapshots, False for regular volumes.
    type: str
    required: false
    default: "Default"
    choices: ["Default", "True", "False"]
  volume_type:
    description:
      - Specifies the volume type, regular volume or snapshot.
    type: str
    required: false
    default: master
    choices: [ "master", "snapshot" ]
  restore_volume_from_snapshot:
    description:
      - Specify true to restore a volume (parent_volume_name) from an existing snapshot specified by the name field.
      - State must be set to present and volume_type must be 'snapshot'.
    type: bool
    required: false
    default: false

extends_documentation_fragment:
    - infinibox
requirements:
    - capacity
a!  
- name: Create new volume named foo under pool named bar
  infini_vol:
    name: foo
    # volume_type: master  # Default
    size: 1TB
    thin_provision: true
    pool: bar
    state: present
    user: admin
    password: secret
    system: ibox001
- name: Create snapshot named foo_snap from volume named foo
  infini_vol:
    name: foo_snap
    volume_type: snapshot
    parent_volume_name: foo
    state: present
    user: admin
    password: secret
    system: ibox001
- name: Stat snapshot, also a volume, named foo_snap
  infini_vol:
    name: foo_snap
    state: present
    user: admin
    password: secret
    system: ibox001
- name: Remove snapshot, also a volume, named foo_snap
  infini_vol:
    name: foo_snap
    state: absent
    user: admin
    password: secret
    system: ibox001
)AnsibleModulemissing_required_lib)	HAS_INFINISDKapi_wrappercheck_snapshot_lock_optionsget_pool
get_systemget_vol_by_sn
get_volumeinfinibox_argument_specmanage_snapshot_locksT)APICommandFailed)ObjectNotFoundF)KiBCapacityc                    d}| j                   s| j                  d   rd}nd}t        | |      }|j                  j	                  | j                  d   ||      }| j                  d   r?t        | j                  d         j                  dt        z        }|j                  |       | j                  d	   6|j                         }| j                  d	   }||k7  r|j                  d	|       d
}|S )z Create Volume Fthin_provisionTHINTHICKname)r   provtypepoolsize@   write_protectedT)
check_modeparamsr   volumescreater   roundupr   update_sizeis_write_protectedupdate_field)	modulesystemchanged	prov_typer   volumer   is_write_protdesired_is_write_prots	            r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/infinidat/infinibox/plugins/modules/infini_vol.pycreate_volumer/      s     G==)*II'&&v& ' 
 == FMM&12::28DDt$==*+7"557M$*MM2C$D! 55##$57LMN    c                     d| d}|j                   j                  |      }|j                         d   }t        |      dk7  r| j	                  d| d       |d   d	   }|S )
z Find the ID of this vol zvolumes?name=z
&fields=id)pathresult   z Cannot find a volume with name ''r   id)apigetget_jsonlen	fail_json)r'   r(   vol_namevol_urlvolr3   vol_ids          r.   find_vol_idr@      sl     hZz2G
**..g.
&C\\^H%F
6{a;H:QGHAYt_FMr0   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| 	       Y d}~|S d}~ww xY w)z" Use snapshot to restore a volume Frestore_volume_from_snapshotvolume_typer   parent_volume_namez6A programming error occurred. is_restoring is not TruesnapshotzQCannot restore a parent volume from snapshot unless the volume type is 'snapshot'msgzWCannot restore a parent volume from snapshot unless the parent volume name is specifiedzvolumes/z/restore?approved=true	source_id)r2   dataTzCannot restore volume z from z: N)	r    r@   AssertionError	exit_jsonr   r7   postr   r;   )r'   r(   r)   is_restoringrC   	snap_namesnap_idrD   parent_volume_idrestore_urlrestore_dataerrs               r.   rB   rB      s:    G==!?@L--.Kf%I&&)4G';<"663EF UVVj pqvw !1 22HI
	hJJOO<O@G N7N   	h#9:L9MVT]S^^`ad`e!fggN	hs   (C 	C:C55C:c                 f   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                  d   8|j                         }| j                  d   }||k7  r|j                  d|       d}|S )	z Update Volume Fr   r   Tr   r   r   r   )r   r    r   r#   r   get_sizer$   strget_provisioningupdate_provisioningr%   r&   )r'   r+   r)   r   provisioningr,   r-   s          r.   update_volumerZ      s+    G}}Vf-.66rCx@??$t$G}}%&2622457"v}}5E'F&&v.G6!&--8H*I&&w/G}}&'3113 &.? @11 13HIGNr0   c                 F    d}| j                   s|j                          d}|S )z, Delete Volume. Volume could be a snapshot. FT)r   delete)r'   r+   r)   s      r.   delete_volumer]     s%     GNr0   c                 @   | j                   d   }| j                   d   }	 |j                  j                  |      }sd| }| j	                  |       | j
                  s| j                   d	   rd
}| j	                  |       t        |        |j                  |      }| j                   d   6|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 volumer   rD   )r   zCannot create snapshot z. Parent volume z
 not foundrF   z/Cannot find new snapshot's parent volume named snapshot_lock_onlyzGSnapshot does not exist. Cannot comply with 'snapshot_lock_only: true'.r   T)r    r!   r8   r   r;   r   r
   create_snapshotr%   r&   r   )
r'   r(   snapshot_namerD   parent_volumerG   rE   r,   r-   r)   s
             r.   r`   r`     sA    MM&)M';<"**0B*C ?@R?STS!==-.[C%#F+ 00m0D==*+7$779M$*MM2C$D! 55%%&79NO&(+GN+  "'6FGYFZZdeS!"s   C6 6$DDc                    d}| j                   d   sH|j                         dk(  }|s| j                  s|j                          d}nd}| j	                  |       t        |        t        | |      }| j                  sE| j                   d   6|j                         }| j                   d   }||k7  r|j                  d|       |xs |S )z- Update/refresh snapshot. May also lock it.  Fr_   LOCKEDTz+Snapshot is locked and may not be refreshedrF   r   )	r    get_lock_stater   refresh_snapshotr;   r
   r   r%   r&   )r'   rE   refresh_changedsnap_is_lockedrG   lock_changedr,   r-   s           r.   update_snapshotrj   9  s     O==-.!002h>$$))+"O?C%'(:L==*+7$779M$*MM2C$D! 55%%&79NO*l*r0   c                    t        |       }| j                  d   rt        | |      }nt        | |      }|s%d| j                  d    d}| j	                  |       |j                         }t        |j                  dd            }|j                  dd      }t        |j                               }|j                         }t        |j                  dd            }	|j                  dd      }
|j                  d	d      }|j                  d
d      }t        |j                               }t        |j                               }t        |j                  dd            }t        |j                  dd            }|j                  dd      }|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   zVolume z not found. Cannot stat.rF   
created_atNhas_childrenmapped	parent_idrY   
updated_at	used_sizer6   typer   SNAPSHOTzVolume snapshot stat foundzVolume stat foundr)   Flock_expires_at
lock_staterG   serialr   used	volume_idrC    )r   r    r   r   r;   
get_fieldsrV   r8   get_lock_expires_atre   
get_serialrU   dictrK   )r'   r(   r+   rG   fieldsrl   rm   rt   ru   rn   r   ro   rY   rv   r   rp   rw   rx   rC   r   r3   s                        r.   handle_statr   T  sL   F}}VFF+vv.f-..FGS! FVZZd34J::nd3L&4467O&&(JHd+,F::fd#D

;-I::nd3L""$%Fv !DVZZd34Jvzz+t,-D

4&I**VT*Kjj!2D9Oj *!   "	
 (     "        !" (#F& Fvr0   c                 <   t        |       }| j                  d   rt        | |      }nt        | |      }| j                  d   }| j                  d   }|dk(  rI|s t	        | |      }| j                  |d       yt        | |      }|rd}nd}| j                  ||       y|d	k(  rf|}|r t        | |      }| j                  |d
       y|s t        | |      }| j                  |d       yt        | |      }| j                  |d       y| j                  d       y)z Handle the present state r   rC   rB   masterzVolume createdr)   rG   zVolume updatedz(Volume present. No changes were requiredrE   zVolume restored from snapshotzSnapshot createdzSnapshot updated A programming error has occurredrF   N)r   r    r   r   r/   rK   rZ   rB   r`   rj   r;   )r'   r(   r+   rC   rM   r)   rG   rE   s           r.   handle_presentr     s)   F}}VFF+vv.--.K==!?@Lh#FF3GW2BC#FF3G&@W#6	
	"266BGW2QR)&&9  6H I)&(;  6H I?@r0   c                    t        |       }| j                  d   rt        | |      }nt        | |      }| j                  d   }|r'|j	                         dk(  rd}| j                  |       |dk(  r6|s| j                  dd	       yt        | |      }| j                  |d
	       y|dk(  r8|}|s| j                  dd	       yt        | |      }| j                  |d	       y| j                  d       y)z Handle the absent state r   rC   rd   zCannot delete snapshot. Locked.rF   r   FzVolume already absentr   zVolume removedrE   zSnapshot already absentzSnapshot removedzA programming error has occuredN)r   r    r   r   re   r;   rK   r]   )r'   r(   r+   rC   rG   r)   rE   s          r.   handle_absentr     s    F}}VFF+vv.--.K&'')X5/S!hU0GH#FF3GW2BC	
	"U0IJ#FH5GW2DE>?r0   c                    | j                   d   dk(  r1| j                   d   dv rd| j                   d<   nd| j                   d<   nw| j                   d   dk(  r1| j                   d   dv rd| j                   d<   nDd| j                   d<   n4d	| j                   d    d
| j                   d    d}| j                  |       | 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)zX Handle each state. Handle different write_protected defaults depending on volume_type. rC   rE   r   )TruetrueDefaultTFr   )Falsefalser   z+An error has occurred handling volume_type z or write_protected z valuesstatestatpresentabsentz'Internal handler error. Invalid state: rF   N)r    r;   r   r   r   r   logout)r'   rG   r   r(   s       r.   execute_stater     sZ   }}]#z1==*+/JJ/3FMM+,/4FMM+,	}	%	1==*+/LL/4FMM+,/3FMM+,;FMM-<X;YYmntn{n{  }N  oO  nP  PW  XMM'"EF?i6"h&!#J5'!RSF# F#s   AD/ /E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(  r2|rd}|dz  }| j                  |       |sld}| j                  |       nW|dk(  r>|s|rd}|dz  }| j                  |       |dk(  r0|s.d}|dz  }| j                  |       nd}| j                  |       |s|dk(  rd}| j                  |       yyyy)zVerify module options are saner   rv   r   r   r   rC   rD   r   z)Name or serial parameter must be provided)r   r   zName parameter must be providedrF   r   r   z+parent_volume_name should not be specified z4if volume_type is 'master'. Used for snapshots only.z#Size is required to create a volumerE   z.Neither pool nor size should not be specified zfor volume_type snapshotz0For state 'present' and volume_type 'snapshot', zparent_volume_name is requiredr   z%For state 'present', pool is requiredN)r    r;   )	r'   r   rv   r   r   r   rC   rD   rG   s	            r.   check_optionsr     s   == D]]8$FMM'"E== D== D--.K';<F=CS!%%3C%	("!CMM  S );  S )J&tF11  S )	!)LC;;C$$$-4C%x/9C% 0t- r0   c                     t               } | j                  t        t        dd      t        ddd      t        d      t        dd      t        dd      t        dd      t               t        dd      t        d	g d
      t        dd      t        dddg      t        dg d                   t        | 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 FN)requireddefaultrV   )r   r   rr   )r   bool)r   rr   r   )r   r   r   )r   choicesT)rr   r   r   rE   r   )r   r   r   )r   rD   r   rB   rv   r   snapshot_lock_expires_atr_   r   r   rC   r   )supports_check_mode	infinisdkrF   capacityr   zDsize (Physical Capacity) should be defined in MB, GB, TB or PB units)r   updater}   r   r   r;   r   HAS_CAPACITYr    r   	Exceptionr   r   )argument_specr'   s     r.   mainr     s6   +-Mud3#D5uMu%)-e&)I5ud3%)V#E?y2OPVT:X*7MN <XY	
" =dCF1+>?1*=>}}V	iV]]6*+ &&	  	i!gh	is   E E$#E$__main__N),__doc__
__future__r   r   r   rr   __metaclass__DOCUMENTATIONEXAMPLESansible.module_utils.basicr   r   Fansible_collections.infinidat.infinibox.plugins.module_utils.infiniboxr   r	   r
   r   r   r   r   r   r   infinisdk.core.exceptionsr   r   ImportErrorr   r   r   r   r/   r@   rB   rZ   r]   r`   rj   r   r   r   r   r   r   __name__ry   r0   r.   <module>r      sO   0
 A @Un#N K
 
 
 :8 &
  2 
 
  >  <    < + +43l"AJ@<>+&\#L zF C  M  Ls#   B# B0 #B-,B-0B:9B: