
    Vh~,                        d dl mZ dZdZdZd dlmZ ddlmZ ddl	m
Z
 dd	lmZ  G d
 de      Zd Zedk(  r e        yy)    )annotationsaS  
---
module: volume

short_description: Create and manage block Volume on the Hetzner Cloud.


description:
    - Create, update and attach/detach block Volume on the Hetzner Cloud.

author:
    - Christopher Schmitt (@cschmitt-hcloud)

options:
    id:
        description:
            - The ID of the Hetzner Cloud Block Volume to manage.
            - Only required if no volume I(name) is given
        type: int
    name:
        description:
            - The Name of the Hetzner Cloud Block Volume to manage.
            - Only required if no volume I(id) is given or a volume does not exist.
        type: str
    size:
        description:
            - The size of the Block Volume in GB.
            - Required if volume does not yet exists.
        type: int
    automount:
        description:
            - Automatically mount the Volume.
        type: bool
        default: False
    format:
        description:
            - Automatically Format the volume on creation
            - Can only be used in case the Volume does not exist.
        type: str
        choices: [xfs, ext4]
    location:
        description:
            - Location of the Hetzner Cloud Volume.
            - Required if no I(server) is given and Volume does not exist.
        type: str
    server:
        description:
            - Server Name the Volume should be assigned to.
            - Required if no I(location) is given and Volume does not exist.
        type: str
    delete_protection:
        description:
            - Protect the Volume for deletion.
        type: bool
    labels:
        description:
            - User-defined key-value pairs.
        type: dict
    state:
        description:
            - State of the Volume.
        default: present
        choices: [absent, present]
        type: str
extends_documentation_fragment:
- hetzner.hcloud.hcloud

a  
- name: Create a Volume
  hetzner.hcloud.volume:
    name: my-volume
    location: fsn1
    size: 100
    state: present
- name: Create a Volume and format it with ext4
  hetzner.hcloud.volume:
    name: my-volume
    location: fsn
    format: ext4
    size: 100
    state: present
- name: Mount a existing Volume and automount
  hetzner.hcloud.volume:
    name: my-volume
    server: my-server
    automount: true
    state: present
- name: Mount a existing Volume and automount
  hetzner.hcloud.volume:
    name: my-volume
    server: my-server
    automount: true
    state: present
- name: Ensure the Volume is absent (remove if needed)
  hetzner.hcloud.volume:
    name: my-volume
    state: absent
a  
hcloud_volume:
    description: The block Volume
    returned: Always
    type: complex
    contains:
        id:
            description: ID of the Volume
            type: int
            returned: Always
            sample: 12345
        name:
            description: Name of the Volume
            type: str
            returned: Always
            sample: my-volume
        size:
            description: Size in GB of the Volume
            type: int
            returned: Always
            sample: 1337
        linux_device:
            description: Path to the device that contains the Volume.
            returned: always
            type: str
            sample: /dev/disk/by-id/scsi-0HC_Volume_12345
            version_added: "0.1.0"
        location:
            description: Location name where the Volume is located at
            type: str
            returned: Always
            sample: "fsn1"
        labels:
            description: User-defined labels (key-value pairs)
            type: dict
            returned: Always
            sample:
                key: value
                mylabel: 123
        server:
            description: Server name where the Volume is attached to
            type: str
            returned: Always
            sample: "my-server"
        delete_protection:
            description: True if Volume is protected for deletion
            type: bool
            returned: always
            sample: false
            version_added: "0.1.0"
)AnsibleModule   )AnsibleHCloud)HCloudException)BoundVolumec                  `     e Zd ZU dZdZded<   d Zd Zd Zd Z	d Z
d	 Ze fd
       Z xZS )AnsibleHCloudVolumehcloud_volumeNzBoundVolume | Nonec           	        t        | j                  j                        | j                  j                  | j                  j                  | j                  j
                  j                  | j                  j                  | j                  j                   | j                  j                  j                  nd | j                  j                  | j                  j                  d   dS )Ndelete)idnamesizelocationlabelsserverlinux_devicedelete_protection)
strr   r   r   r   r   r   r   r   
protectionselfs    i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/hetzner/hcloud/plugins/modules/volume.py_prepare_resultz#AnsibleHCloudVolume._prepare_result   s    d((++,&&++&&++**3388((//8<8J8J8Q8Q8]d((//44cg ..;;!%!3!3!>!>x!H	
 		
    c                   	 | j                   j                  j                  d      N| j                  j                  j                  | j                   j                  j                  d            | _        y | j                  j                  j                  | j                   j                  j                  d            | _        y # t        $ r}| j                  |       Y d }~y d }~ww xY w)Nr   r   )
moduleparamsgetclientvolumes	get_by_idr   get_by_namer   fail_json_hcloud)r   	exceptions     r   _get_volumezAnsibleHCloudVolume._get_volume   s    	-{{!!%%d+7%)[[%8%8%B%B4;;CUCUCYCYZ^C_%`"%)[[%8%8%D%DT[[EWEWE[E[\bEc%d" 	-!!),,	-s   A2C 5AC 	C'C""C'c                   | j                   j                  ddg       | j                   j                  j                  d      | j                   j                  j                  d      | j                   j                  j                  d      | j                   j                  j                  d      | j                   j                  j                  d      d}| j                   j                  j                  d      L| j                  j
                  j                  | j                   j                  j                  d            |d<   n| j                   j                  j                  d	      L| j                  j                  j                  | j                   j                  j                  d	            |d	<   n| j                   j                  d
       | j                   j                  s	  | j                  j                  j                  di |}|j                  j                          |j                  D cg c]  }|j                          c} | j                   j                  j                  d      }|<| j                          | j                   j#                  |      }|j                          | j)                          | j                          y c c}w # t$        $ r}| j'                  |       Y d }~Ed }~ww xY w)Nr   r   required_params	automountformatr   )r   r   r+   r,   r   r   r   zserver or location is required)msgr   r    )r   fail_on_missing_paramsr   r    r!   serversr$   	locations	fail_json
check_moder"   createactionwait_until_finishednext_actionsr'   r   change_protectionr   r%   _mark_as_changed)r   r   respr6   r   r&   s         r   _create_volumez"AnsibleHCloudVolume._create_volume   sH   **FF;K*LKK&&**62KK&&**62++//<kk((,,X6kk((,,X6
 ;;!!(+7#{{22>>t{{?Q?Q?U?UV^?_`F8[[##J/;!%!6!6!B!B4;;CUCUCYCYZdCe!fF:KK!!&F!G{{%%
11t{{**11;F;//1<@<M<MN&++-N$(KK$6$6$:$:;N$O!$0$$&!//AAIZA[F..0 	 O # 1%%i001s,   +AK 9KA&K K 	L %K;;L c                   	 | j                   j                  j                  d      }|r| j                  j                  |k  rR| j                   j
                  s+| j                  j                  |      }|j                          | j                          n4| j                  j                  |kD  r| j                   j                  d       | j                   j                  j                  d      }|r| j                  j                  j                  |      }| j                  j                  -| j                  j                  j                  |j                  k7  r| j                   j
                  sS| j                   j                  j                  dd      }| j                  j                  ||      }|j                          | j                          nf| j                  j                  P| j                   j
                  s*| j                  j!                         }|j                          | j                          | j                   j                  j                  d      }|[|| j                  j"                  k7  rB| j                   j
                  s| j                  j%                  |       | j                          | j                   j                  j                  d	      }|n|| j                  j&                  d
   k7  rR| j                   j
                  s,| j                  j)                  |      }|j                          | j                          | j+                          y # t,        $ r}| j/                  |       Y d }~y d }~ww xY w)Nr   z%Shrinking of volumes is not supportedr   r+   F)r+   r   )r   r   r   r.   )r   r   r    r   r   r4   resizer7   r:   warnr!   r1   r$   r   r   attachdetachr   updater   r9   r'   r   r%   )	r   r   r6   server_namer   r+   r   r   r&   s	            r   _update_volumez"AnsibleHCloudVolume._update_volume   s   *	-;;%%))&1D%%**T1;;11!%!3!3!:!:4!@224))+'',,t3KK$$%LM++,,00:K,,88E%%,,48J8J8Q8Q8V8VZ`ZeZe8e;;11$(KK$6$6$:$:;$N	!%!3!3!:!:6Y!:!W224))+%%,,8;;11!%!3!3!:!:!<224))+[[''++H5F!f0B0B0I0I&I{{--&&--V-<%%' $ 2 2 6 67J K ,1BdFXFXFcFcdlFm1m{{--!//AAIZA[F..0%%' 	-!!),,	-s   MM 	N(M>>Nc                ~    | j                          | j                  | j                          y | j                          y N)r'   r   r<   rD   r   s    r   present_volumez"AnsibleHCloudVolume.present_volume  s2    %!!r   c                   	 | j                          | j                  | j                  j                  so| j                  j                  *| j                  j                         }|j                          | j                  j                  j                  | j                         | j                          d | _        y # t        $ r}| j                  |       Y d }~y d }~ww xY wrF   )r'   r   r   r4   r   rA   r7   r!   r"   r   r:   r   r%   )r   r6   r&   s      r   delete_volumez!AnsibleHCloudVolume.delete_volume  s    	-!!-{{--))00<!%!3!3!:!:!<224KK''..t/A/AB%%'!%D 	-!!),,	-s   B8B; ;	CCCc                    t        t        dddiddiddiddiddiddidddddd	gd
ddiddgddd
t        |          ddggddggd      S )Ntypeintr   dictboolF)rK   defaultxfsext4)rK   choicesabsentpresent)rR   rO   )
r   r   r   r   r   r   r+   r,   r   stater   r   r   r   T)argument_specrequired_one_ofmutually_exclusivesupports_check_moder/   )r   rM   superbase_module_arguments)cls	__class__s    r   define_modulez!AnsibleHCloudVolume.define_module$  s     E?e_e_ %'#)e< %5&/B#)6"2 ()4( '/1  #F^,!+X 67 $'
 	
r   )__name__
__module____qualname__	representr   __annotations__r   r'   r<   rD   rG   rI   classmethodr^   __classcell__)r]   s   @r   r
   r
      sF    I(,M%,

->+-Z"- 
 
r   r
   c                 *   t         j                         } t        |       }| j                  j                  d      }|dk(  r$| j	                  dg       |j                          n|j                           | j                  di |j                          y )NrU   rS   r   r)   r/   )	r
   r^   r   r    r0   rI   rG   	exit_json
get_result)r   hcloudrU   s      r   mainrj   =  s}     ..0F (FMMg&E%%vh%?F+v((*+r   __main__N)
__future__r   DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   module_utils.hcloudr   module_utils.vendor.hcloudr   "module_utils.vendor.hcloud.volumesr   r
   rj   r_   r/   r   r   <module>rt      sY    #CJ@2
h 5 / 8 <Q
- Q
h, zF r   