
    Vh5                        d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
Z
dZ	 d dlmZmZ d dlmZ d d	lmZ d d
lmZ  ej,                  dej.                        Z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 [w xY w)    )absolute_importdivisionprint_functiona  
module: profitbricks_volume
short_description: Create or destroy a volume
description:
  - Allows you to create or remove a volume from a ProfitBricks datacenter. This module has a dependency on profitbricks >=
    1.0.0.
deprecated:
  removed_in: 11.0.0
  why: Module relies on library unsupported since 2021.
  alternative: >
    Profitbricks has rebranded as Ionos Cloud and they provide a collection named ionoscloudsdk.ionoscloud.
    Whilst it is likely it will provide the features of this module, that has not been verified.
    Please refer to that collection's documentation for more details.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  datacenter:
    description:
      - The datacenter in which to create the volumes.
    type: str
  name:
    description:
      - The name of the volumes. You can enumerate the names using auto_increment.
    type: str
  size:
    description:
      - The size of the volume.
    type: int
    required: false
    default: 10
  bus:
    description:
      - The bus type.
    type: str
    required: false
    default: VIRTIO
    choices: ["IDE", "VIRTIO"]
  image:
    description:
      - The system image ID for the volume, for example V(a3eae284-a2fe-11e4-b187-5f1f641608c8). This can also be a snapshot
        image ID.
    type: str
  image_password:
    description:
      - Password set for the administrative user.
    type: str
    required: false
  ssh_keys:
    description:
      - Public SSH keys allowing access to the virtual machine.
    type: list
    elements: str
    default: []
  disk_type:
    description:
      - The disk type of the volume.
    type: str
    required: false
    default: HDD
    choices: ["HDD", "SSD"]
  licence_type:
    description:
      - The licence type for the volume. This is used when the image is non-standard.
      - 'The available choices are: V(LINUX), V(WINDOWS), V(UNKNOWN), V(OTHER).'
    type: str
    required: false
    default: UNKNOWN
  count:
    description:
      - The number of volumes you wish to create.
    type: int
    required: false
    default: 1
  auto_increment:
    description:
      - Whether or not to increment a single number in the name for created virtual machines.
    default: true
    type: bool
  instance_ids:
    description:
      - List of instance IDs, currently only used when O(state=absent) to remove instances.
    type: list
    elements: str
    default: []
  subscription_user:
    description:
      - The ProfitBricks username. Overrides the E(PB_SUBSCRIPTION_ID) environment variable.
    type: str
    required: false
  subscription_password:
    description:
      - THe ProfitBricks password. Overrides the E(PB_PASSWORD) environment variable.
    type: str
    required: false
  wait:
    description:
      - Wait for the datacenter to be created before returning.
    required: false
    default: true
    type: bool
  wait_timeout:
    description:
      - How long before wait gives up, in seconds.
    type: int
    default: 600
  state:
    description:
      - Create or terminate datacenters.
      - 'The available choices are: V(present), V(absent).'
    type: str
    required: false
    default: 'present'
  server:
    description:
      - Server name to attach the volume to.
    type: str

requirements: ["profitbricks"]
author: Matt Baldwin (@baldwinSPC) <baldwin@stackpointcloud.com>
a|  
- name: Create multiple volumes
  community.general.profitbricks_volume:
    datacenter: Tardis One
    name: vol%02d
    count: 5
    auto_increment: true
    wait_timeout: 500
    state: present

- name: Remove Volumes
  community.general.profitbricks_volume:
    datacenter: Tardis One
    instance_ids:
      - 'vol01'
      - 'vol02'
    wait_timeout: 500
    state: absent
NT)ProfitBricksServiceVolumeF)AnsibleModule)xrange)	to_nativez([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}c                    |sy t        j                          |z   }|t        j                          kD  r}t        j                  d       | j                  |d   d      }|d   d   dk(  ry |d   d   dk(  r#t        d	|z   d
z   t	        |d         z   dz         |t        j                          kD  r}t        d|z   d
z   t	        |d         z   dz         )N   	requestIdT)
request_idstatusmetadatar   DONEFAILEDzRequest failed to complete z "z" to complete.z&Timed out waiting for async operation )timesleepget_request	Exceptionstr)profitbrickspromisewait_timeoutmsgoperation_results        y/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/profitbricks_volume.py_wait_for_completionr      s    99;-L

$

1'33{+ 4  J'1V;j)(3x?-3d:SK(>* *,<=> > 
$ 036=K A
 	
	     c           
         | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }	| j                   j                  d      }
| j                   j                  d      }| j                   j                  d	      }	 t        |||||||	|

      }|j                  ||      }|rt	        |||d       |S # t
        $ r)}| j                  dt        |      z         Y d }~S d }~ww xY w)Nsizebusimageimage_passwordssh_keys	disk_typelicence_typer   wait)namer!   r"   r#   r$   r%   r&   r'   _create_volumezfailed to create the volume: %sr   )paramsgetr   create_volumer   r   	fail_jsonr   )moduler   
datacenterr)   r!   r"   r#   r$   r%   r&   r'   r   r(   vvolume_responsees                   r   r*   r*      sI   ==V$D
--

E
"CMMg&E]]&&'78N}}  ,H!!+.I==$$^4L==$$^4L==V$DI)%	
 '44ZC !-/?A   I>QGHHIs   55D, ,	E5EEc                     	 |j                  ||       y # t        $ r(}| j                  dt        |      z         Y d }~y d }~ww xY w)Nzfailed to remove the volume: %sr+   )delete_volumer   r/   r   )r0   r   r1   volumer4   s        r   _delete_volumer8      sH    I"":v6 I>QGHHIs    	AAAc           	      F   | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }d}d}g }t        j                  |      sB|j	                         }	|	d   D ]*  }
|j                  |
d         }||d	   d   k(  s#|
d   }d} n |s| j                  d
       |rst               }d}	 |dz   t        |||z   t        |      z         }t        t        |      j!                  |            }g }|d| }|D ]  }|j#                  ||z          n|g|z  }|D ]=  }t%        | |t'        |      |      }|j#                  |       t)        | |||d          d}? ||dd|D cg c]  }|d   	 c}id}|S # t        $ r[}|j                  j                  d      rd|z  }n/| j                  |j                  t        j                                Y d}~$d}~ww xY wc c}w )z
    Creates a volume.

    This will create a volume in a datacenter.

    module : AnsibleModule object
    profitbricks: authenticated profitbricks object.

    Returns:
        True if the volume was created, false otherwise
    r1   r)   auto_incrementcountFTitemsid
propertieszdatacenter could not be found.r+      r   znot allz%s%%dr   	exceptionNcreate	instances)failedvolumesactioninstance_ids)r,   r-   
uuid_matchmatchlist_datacentersget_datacenterr/   set	TypeErrormessage
startswith	traceback
format_excr	   lenlist
differenceappendr*   r   _attach_volume)r0   r   r1   r)   r:   r;   datacenter_foundrD   rE   datacenter_listddcnumberscount_offsetr4   number_rangeavailable_numbersnamesnumbers_to_usenumbercreate_responseiresultss                          r   r.   r.      sD    ""<0J==V$D]]&&'78NMMg&EFG Z(&779 ) 	A,,QtW5BR-f55tW
#' 	 =>%	R1H lL5,@3w<,OP \!2!=!=g!FG*6E2$ 	(FLL'	(  (s:PTU'v|Z9NO	 73a!D'3
	G N=  	Ryy##I.~  QYY):N:N:P Q		R4 4s   -F7 #H7	H AHHc                    t        | j                  j                  d      t              r't	        | j                  j                  d            dk  r| j                  d       | j                  j                  d      }d}| j                  j                  d      }t        j                  |      s@|j                         }|d   D ](  }|j                  |d         }||d	   d
   k(  s#|d   } n |D ]d  }t        j                  |      rt        | |||       d})|j                  |      }	|	d   D ]#  }
||
d	   d
   k(  s|
d   }t        | |||       d}% f |S )z
    Removes a volume.

    This will create a volume in a datacenter.

    module : AnsibleModule object
    profitbricks: authenticated profitbricks object.

    Returns:
        True if the volume was removed, false otherwise
    rG   r?   zGinstance_ids should be a list of virtual machine ids or names, abortingr+   r1   Fr<   r=   r>   r)   T)
isinstancer,   r-   rS   rR   r/   rH   rI   rJ   rK   r8   list_volumes)r0   r   r1   changedrG   rX   rY   rZ   nrE   r2   	volume_ids               r   r6   r6   :  sg    fmm''7>#fmmFWFWXfFgBhklBlfg""<0JG==$$^4L Z(&779 ) 	A,,QtW5BR-f55tW
		  
#A6<Q?G"//
;GW% #,// !$I"6<YO"G	#
# Nr   c                 x   | j                   j                  d      }|rVt        j                  |      s-|j	                  |      }|d   D ]  }||d   d   k(  s|d   } n 	 |j                  |||      S y# t        $ r;}| j                  dt        |      z  t        j                                Y d}~yd}~ww xY w)	z
    Attaches a volume.

    This will attach a volume to the server.

    module : AnsibleModule object
    profitbricks: authenticated profitbricks object.

    Returns:
        True if the volume was attached, false otherwise
    serverr<   r>   r)   r=   zfailed to attach volume: %sr@   N)r,   r-   rH   rI   list_serversattach_volumer   r/   r
   rP   rQ   )r0   r   r1   r7   rl   server_listsr4   s           r   rV   rV   e  s     ]]x(F   (&33J?K ) Q|_V44tWF
	q--j&&II   	q!>1!MYbYmYmYopp	qs   !A5 5	B9>1B44B9c                  8   t        t        d4i dt               dt               dt               dt        dd      dt        d	d
gd	      dt               dt        d      dt        ddg d      dt        ddgd      dt        d      dt        dd      dt        dd      dt        ddg        d!t               d"t        d      d#t        dd      d$t        dd%      d&t        d'      (      } | j                  j                  d!      s| j	                  d)*       | j                  j                  d"      s| j	                  d+*       | j                  j                  d!      }| j                  j                  d"      }t        ||,      }| j                  j                  d&      }|d-k(  rM| j                  j                  d      s| j	                  d.*       	 t        | |      }| j                  |/       y |d'k(  rz| j                  j                  d      s| j	                  d2*       | j                  j                  d      s| j	                  d3*       	 t        | |      } | j                  d4i | y y # t        $ r;}| j	                  d0t        |      z  t        j                         1       Y d }~y d }~ww xY w# t        $ r;}| j	                  d0t        |      z  t        j                         1       Y d }~y d }~ww xY w)5Nr1   rl   r)   r!   int
   )typedefaultr"   VIRTIOIDE)choicesru   r#   r$   T)no_logr%   rS   r   F)rt   elementsru   ry   r&   HDDSSDr'   UNKNOWN)ru   r;   r?   r:   boolrG   )rt   rz   ru   subscription_usersubscription_passwordr(   r   iX  statepresent)argument_specz'subscription_user parameter is requiredr+   z+subscription_password parameter is required)usernamepasswordabsentzBdatacenter parameter is required for running or stopping machines.)rh   zfailed to set volume state: %sr@   z1datacenter parameter is required for new instancez+name parameter is required for new instance )r   dictr,   r-   r/   r   r6   	exit_jsonr   r
   rP   rQ   r.   )r0   r   r   r   r   rh   r4   volume_dict_arrays           r   mainr     s    
v
6
 
 5"-	

 h.A
 &
  t,
 vr%P
 E5>5A
 i0
 E1-
  VT:
 6E2F
 #f
 #'d"3
  640!
" 5#6#
$ y)%
F. ==01FG==45JK))*=>"MM--.EF&"&(L MMg&E}}  .!ef	t%fl;WW- 
)	}}  .!TU}}  (!NO	t"/"EF101 
  	t!AIaL!P\e\p\p\rss	t  	t!AIaL!P\e\p\p\rss	ts0   /J .K 	K1KK	L1LL__main__)!
__future__r   r   r   rt   __metaclass__DOCUMENTATIONEXAMPLESrer   rP   
HAS_PB_SDKprofitbricks.clientr   r   ImportErroransible.module_utils.basicr   ansible.module_utils.six.movesr	   +ansible.module_utils.common.text.convertersr
   compileIrH   r   r*   r8   r.   r6   rV   r   __name__r   r   r   <module>r      s    A @||( 
  
? 5 1 A RZZ/7
. FIFR(Vq::tz zF w  Js   A: :BB