
    Vhe$                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZmZ d dlmZ dZ	 d dlZd
ZddgZddgZddgZd Zd Zd Zd Zd Zedk(  r e        yy# e$ r d	ZY 4w xY w)    )absolute_importdivisionprint_functiona	  
module: packet_volume

short_description: Create/delete a volume in Packet host

description:
  - Create/delete a volume in Packet host.
  - API is documented at U(https://www.packet.com/developers/api/#volumes).
version_added: '0.2.0'

author:
  - Tomas Karasek (@t0mk) <tom.to.the.k@gmail.com>
  - Nurfet Becirevic (@nurfet-becirevic) <nurfet.becirevic@gmail.com>

extends_documentation_fragment:
  - community.general.attributes

attributes:
  check_mode:
    support: full
  diff_mode:
    support: none

options:
  state:
    description:
      - Desired state of the volume.
    default: present
    choices: ['present', 'absent']
    type: str

  project_id:
    description:
      - ID of project of the device.
    required: true
    type: str

  auth_token:
    description:
      - Packet API token. You can also supply it in environment variable E(PACKET_API_TOKEN).
    type: str

  name:
    description:
      - Selector for API-generated name of the volume.
    type: str

  description:
    description:
      - User-defined description attribute for Packet volume.
      - It is used used as idempotent identifier - if volume with given description exists, new one is not created.
    type: str

  id:
    description:
      - UUID of a volume.
    type: str

  plan:
    description:
      - V(storage_1) for standard tier, V(storage_2) for premium (performance) tier.
      - Tiers are described at U(https://www.packet.com/cloud/storage/).
    choices: ['storage_1', 'storage_2']
    default: 'storage_1'
    type: str

  facility:
    description:
      - Location of the volume.
      - Volumes can only be attached to device in the same location.
    type: str

  size:
    description:
      - Size of the volume in gigabytes.
    type: int

  locked:
    description:
      - Create new volume locked.
    type: bool
    default: false

  billing_cycle:
    description:
      - Billing cycle for new volume.
    choices: ['hourly', 'monthly']
    default: 'hourly'
    type: str

  snapshot_policy:
    description:
      - Snapshot policy for new volume.
    type: dict

    suboptions:
      snapshot_count:
        description:
          - How many snapshots to keep, a positive integer.
        required: true
        type: int

      snapshot_frequency:
        description:
          - Frequency of snapshots.
        required: true
        choices: ["15min", "1hour", "1day", "1week", "1month", "1year"]
        type: str

requirements:
  - "packet-python >= 1.35"
a  
# All the examples assume that you have your Packet API token in env var PACKET_API_TOKEN.
# You can also pass the api token in module param auth_token.

- hosts: localhost
  vars:
    volname: testvol123
    project_id: 53000fb2-ee46-4673-93a8-de2c2bdba33b

  tasks:
    - name: Create volume
      community.general.packet_volume:
        description: "{{ volname }}"
        project_id: "{{ project_id }}"
        facility: 'ewr1'
        plan: 'storage_1'
        state: present
        size: 10
        snapshot_policy:
          snapshot_count: 10
          snapshot_frequency: 1day
      register: result_create

    - name: Delete volume
      community.general.packet_volume:
        id: "{{ result_create.id }}"
        project_id: "{{ project_id }}"
        state: absent
a  
id:
  description: UUID of specified volume.
  type: str
  returned: success
  sample: 53000fb2-ee46-4673-93a8-de2c2bdba33c
name:
  description: The API-generated name of the volume resource.
  type: str
  returned: if volume is attached/detached to/from some device
  sample: "volume-a91dc506"
description:
  description: The user-defined description of the volume resource.
  type: str
  returned: success
  sample: "Just another volume"
N)AnsibleModuleenv_fallback)	to_nativeTFPACKET_API_TOKEN	storage_1	storage_2presentabsenthourlymonthlyc                 l    	 t        j                  | d      }t        |      | k(  S # t        $ r Y yw xY w)N   )versionF)uuidUUID
ValueErrorstr)myuuidvals     s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/packet_volume.pyis_valid_uuidr      s<    ii* s8v  s   ' 	33c                    | j                   j                  d      rE| j                   j                  d      t              st        dj	                              fdS | j                   j                  d      r | j                   j                  d      fdS | j                   j                  d      r | j                   j                  d      fdS y )Nidz#Volume ID '{0}' is not a valid UUIDc                     | d   k(  S )Nr    )vis    r   <lambda>z%get_volume_selector.<locals>.<lambda>   s    4A     namec                     | d   k(  S )Nr#   r   )r   ns    r   r!   z%get_volume_selector.<locals>.<lambda>   s    6a r"   descriptionc                     | d   k(  S )Nr&   r   )r   ds    r   r!   z%get_volume_selector.<locals>.<lambda>   s    =)Q. r"   )paramsgetr   	Exceptionformat)moduler(   r    r%   s    @@@r   get_volume_selectorr.      s    }}MMd#QAHHKLL%%			6	"MMf%''			=	)MMm,.. 
*r"   c                 `    | j                  |      }|t        dj                  |            |S )Nz${0} must be specified for new volume)r*   r+   r,   )r)   keyitems      r   get_or_failr2      s0    ::c?D|>EEcJKKKr"   c           	         ddi}t        |      }|j                  j                  d      }dj                  |      }|j	                  |d      d   }|D cg c]  } ||      s| }	}| dk(  rt        |	      dk(  rt        |j                  d	      t        |j                  d
      t        |j                  d      t        |j                  d      t        |j                  d      t        |j                  d      |j                  j                  d      d}
|j	                  |d|
      }d|d<   dD ]
  }||   ||<    |S dD ]  }|	d   |   ||<    |S t        |	      dkD  r,dj                  t        |	            }|j                  |       t        |	      dk(  r=|	d   }|j	                  dj                  |d         d       d|d<   dD ]
  }||   ||<    |S c c}w )NchangedF
project_idzprojects/{0}/storageGETvolumesr   r   r&   sizeplanfacilitylockedbilling_cyclesnapshot_policy)r&   r8   r9   r:   r;   r<   snapshot_policiesPOSTT)r   r#   r&      zAMore than one volume matches in module call for absent state: {0}msgzstorage/{0}r   DELETE)	r.   r)   r*   r,   call_apilenr2   r   	fail_json)target_stater-   packet_connreturn_dictsr5   
api_methodall_volumesr   matching_volumesr)   new_volume_datak_msgvolumes                  r   act_on_volumerR      s   e$KF#A""<0J'..z:J&&z59)DK#.7a!A$77y  A%*6==-H#FMM6:#FMM6:'zB%fmmX>!,V]]O!L%+]]%6%67H%IF *22:vvNO%)K	"2 4!0!3A4( ! 3 8!1!!4Q!7A8    1$W^^./1D& A%%a(F  !5!5fTl!CXN%)K	"2 +!'A+ I 8s   G#Gc                     t        t        t        d      t        d      t        d      t        t        d      t        dt        t        gfd      t        d      t        t
        d      t        d      t        d	      t        d
d      t        d      t        dt        d            ddgg d      } t        s| j                  d       | j                  j                  d      s'dj                  t              }| j                  |       | j                  j                  d      }t        j                  |      }| j                  j                  d      }|t        v r=| j                  r| j                  d       	  | j                  di t!        || |       y | j                  dj                  |             y # t"        $ r5}| j                  dj                  |t%        |                   Y d }~y d }~ww xY w)Nr   )typer   )choicesdefaultT)rT   fallbackno_log)requiredr
   intboolF)rT   rV   dictr   )rT   rU   rV   )r   r&   r#   state
auth_tokenr5   r9   r:   r8   r;   r=   r<   )r#   r   r&   ))r#   r   )r   r&   )r#   r&   )argument_specsupports_check_moderequired_one_ofmutually_exclusivezpacket required for this modulerA   r^   z\if Packet API token is not in environment variable {0}, the auth_token parameter is required)r^   r]   )r4   z#failed to set volume state {0}: {1}z({0} is not a valid state for this moduler   )r   r\   VOLUME_STATESr   PACKET_API_TOKEN_ENV_VARVOLUME_PLANSBILLINGHAS_PACKET_SDKrF   r)   r*   r,   packetManager
check_mode	exit_jsonrR   r+   r   )r-   	_fail_msgr^   rH   r]   es         r   mainrn     s   %(5!}i@&)A(BC
 T*lK@u%5!VU3 f-E7HM!
$ !67
+F8 >?==\*<<BFC[<\ 	Y'""<0J..J7KMMg&EU+	*FI}UFKHI 	GNNuUV  	*9@@9Q<)  * *	*s   G 	H	+HH	__main__)
__future__r   r   r   rT   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   rg   rh   ImportErrorrd   re   rc   rf   r   r.   r2   rR   rn   __name__r   r"   r   <module>ry      s    C Bob<
$  B A
 . [)H%Y
/*Z6Wr zF ]  Ns   A A! A!