
    VhS#                         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mZ d dlmZ  G d	 d
e      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionu  
---
module: volume
short_description: Manages volumes on the cloudscale.ch IaaS service.
description:
  - Create, attach/detach, update, delete and revert volumes on the cloudscale.ch IaaS service.
notes:
  - To create a new volume at least the I(name) and I(size_gb) options
    are required.
  - A volume can be created and attached to a server in the same task.
author:
  - Gaudenz Steinlin (@gaudenz)
  - René Moser (@resmo)
  - Denis Krienbühl (@href)
version_added: "1.0.0"
options:
  state:
    description:
      - State of the volume.
    default: present
    choices: [ present, absent ]
    type: str
  name:
    description:
      - Name of the volume. Either name or UUID must be present to change an
        existing volume.
    type: str
  uuid:
    description:
      - UUID of the volume. Either name or UUID must be present to change an
        existing volume.
    type: str
  size_gb:
    description:
      - Size of the volume in GB.
    type: int
  type:
    description:
      - Type of the volume. Cannot be changed after creating the volume.
        Defaults to C(ssd) on volume creation.
    choices: [ ssd, bulk ]
    type: str
  zone:
    description:
      - Zone in which the volume resides (e.g. C(lpg1) or C(rma1)). Cannot be
        changed after creating the volume. Defaults to the project default zone.
    type: str
  servers:
    description:
      - UUIDs of the servers this volume is attached to. Set this to C([]) to
        detach the volume. Currently a volume can only be attached to a
        single server.
      - The aliases C(server_uuids) and C(server_uuid) are deprecated and will
        be removed in version 3.0.0 of this collection.
    aliases: [ server_uuids, server_uuid ]
    type: list
    elements: str
  revert:
    description:
      - 'UUID of the snapshot to revert the volume to. This must be the most recent
        snapshot of the volume.
        For root volumes: the respective server must be shut down.
        For non-root volumes: the volume to be reverted must be detached'
    type: str
  tags:
    description:
      - Tags associated with the volume. Set this to C({}) to clear any tags.
    type: dict
extends_documentation_fragment: cloudscale_ch.cloud.api_parameters
a  
# Create a new SSD volume
- name: Create an SSD volume
  cloudscale_ch.cloud.volume:
    name: my_ssd_volume
    zone: 'lpg1'
    size_gb: 50
    api_token: xxxxxx
  register: my_ssd_volume

# Attach an existing volume to a server
- name: Attach volume to server
  cloudscale_ch.cloud.volume:
    uuid: "{{ my_ssd_volume.uuid }}"
    servers:
      - ea3b39a3-77a8-4d0b-881d-0bb00a1e7f48
    api_token: xxxxxx

# Create and attach a volume to a server
- name: Create and attach volume to server
  cloudscale_ch.cloud.volume:
    name: my_ssd_volume
    zone: 'lpg1'
    size_gb: 50
    servers:
      - ea3b39a3-77a8-4d0b-881d-0bb00a1e7f48
    api_token: xxxxxx

# Detach volume from server
- name: Detach volume from server
  cloudscale_ch.cloud.volume:
    uuid: "{{ my_ssd_volume.uuid }}"
    servers: []
    api_token: xxxxxx

# Revert a volume to the most recent snapshot
- name: Revert volume to snapshot
  cloudscale_ch.cloud.volume:
    uuid: "{{ my_ssd_volume.uuid }}"
    revert: "e504dc99-ff01-4e89-ad89-7df080f97b4b"
    api_token: xxxxxx

# Delete a volume
- name: Delete volume
  cloudscale_ch.cloud.volume:
    name: my_ssd_volume
    state: absent
    api_token: xxxxxx
a  
href:
  description: The API URL to get details about this volume.
  returned: state == present
  type: str
  sample: https://api.cloudscale.ch/v1/volumes/2db69ba3-1864-4608-853a-0771b6885a3a
uuid:
  description: The unique identifier for this volume.
  returned: state == present
  type: str
  sample: 2db69ba3-1864-4608-853a-0771b6885a3a
name:
  description: The display name of the volume.
  returned: state == present
  type: str
  sample: my_ssd_volume
size_gb:
  description: The size of the volume in GB.
  returned: state == present
  type: str
  sample: 50
type:
  description: The type of the volume.
  returned: state == present
  type: str
  sample: bulk
zone:
  description: The zone of the volume.
  returned: state == present
  type: dict
  sample: {'slug': 'lpg1'}
server_uuids:
  description: The UUIDs of the servers this volume is attached to. This return
    value is deprecated and will disappear in the future when the field is
    removed from the API.
  returned: state == present
  type: list
  sample: ['47cec963-fcd2-482f-bdb6-24461b2d47b1']
servers:
  description: The list of servers this volume is attached to.
  returned: state == present
  type: list
  sample: [
            {
                "href": "https://api.cloudscale.ch/v1/servers/47cec963-fcd2-482f-bdb6-24461b2d47b1",
                "name": "my_server",
                "uuid": "47cec963-fcd2-482f-bdb6-24461b2d47b1"
            }
          ]
state:
  description: The current status of the volume.
  returned: success
  type: str
  sample: present
tags:
  description: Tags associated with the volume.
  returned: state == present
  type: dict
  sample: { 'project': 'my project' }
)AnsibleModule   )AnsibleCloudscaleBasecloudscale_argument_spec)deepcopyc                   .     e Zd Z fdZ fdZd Z xZS )AnsibleCloudscaleVolumec                 d    | j                   j                  ddg       t        t        |   |      S )Nnamesize_gb)_modulefail_on_missing_paramssuperr   create)selfresource	__class__s     n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cloudscale_ch/cloud/plugins/modules/volume.pyr   zAnsibleCloudscaleVolume.create   s-    ++VY,?@,d:8DD    c                     d}|dk7  rt         t        |   |||      S ||   }|}t        |      t        |      k7  rd}|S |D ]  }|d   |vsd} |S )NFserversTuuid)r   r   find_differencelen)	r   keyr   paramis_different
server_hasserver_wantedhasr   s	           r   r   z'AnsibleCloudscaleVolume.find_difference   s~    )0$GXW\]]c]
}Z0L 	 " (v;m3#'L( r   c                    | j                         }t        |d         dkD  r| j                  j                  d       |d   dz   }d| j                  j                  d   i}| j                  ||      }| j                  d	d
      }d|d<   | j                  j                  d   |d<   t               |d<   t        |      |d   d<   t        |      |d   d<   |d   d   j                  d| j                  j                  d   i       |S )Nr   r   z1Cannot revert a volume that is attached to server)msghrefz/revertsnapshotrevertcurrent_operationFTchangeddiffbeforeafter)
queryr   r   	fail_jsonparams_postwait_for_statedictr
   update)r   r   
revert_urlrevert_paramr(   results         r   r(   zAnsibleCloudscaleVolume.revert   s   ::<x	"#a'LL""'Z"[f%	1
"DLL$7$7$ABJ5$$%8%@ y<<..x8xv#+H#5vx "*8"4vwvw&&dll))(3(
 	 r   )__name__
__module____qualname__r   r   r(   __classcell__)r   s   @r   r   r      s    E
"r   r   c                     t               } | j                  t        t        ddd      t        d      t        d      t        d      t        d      t        dd      t        d	dd
dg      t        d      t        d      	             t        | dd      }|j                  j                  d      s|j                  j                  d
      r|j                  ddd       t        |dg dg d      }|j                  d   dk(  r|j                         }n0|j                  d   r|j                         }n|j                         } |j                  di | y )Nstrpresent)r>   absent)typedefaultchoices)r@   int)ssdbulk)r@   rB   listserver_uuidsserver_uuid)r@   elementsaliasesr3   )	stater   r   zoner   r@   r   r(   tags))r   r   T)argument_specrequired_one_ofsupports_check_modezmThe aliases "server_uuid" and "server_uuids" have been deprecated and will be removed, use "servers" instead.z3.0.0zcloudscale_ch.cloud)versioncollection_namevolumes)r   r@   rL   r   r   rM   )r   r   r   rM   )resource_nameresource_create_param_keysresource_update_param_keysrK   r?   r(    )r	   r4   r3   r   r0   get	deprecater   r?   r(   r>   	exit_json)rN   modulecloudscale_volumer7   s       r   mainr]      sK   ,.My:OPuuu% uo6&5>=:YZv
 
 #+ F }}'6==+<+<^+L $ "):O 	 	Q
 0$
$
& }}W)"))+	x	 "))+"**,Fvr   __main__N)
__future__r   r   r   r@   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   module_utils.apir   r	   copyr
   r   r]   r8   rW   r   r   <module>rg      sc    A @EN0d;
z 5 (3 (V4n zF r   