
    VhW                     F   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 dlZdZ	 d dlmZmZmZmZmZmZ d dlmZ d d	lmZ d d
lmZ g dZ ej8                  dej:                        Zd Zd Z d Z!d 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 kw xY w)    )absolute_importdivisionprint_functionaP  
module: profitbricks
short_description: Create, destroy, start, stop, and reboot a ProfitBricks virtual machine
description:
  - Create, destroy, update, start, stop, and reboot a ProfitBricks virtual machine. When the virtual machine is created it
    can optionally wait for it to be 'running' before returning. 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:
  auto_increment:
    description:
      - Whether or not to increment a single number in the name for created virtual machines.
    type: bool
    default: true
  name:
    description:
      - The name of the virtual machine.
    type: str
  image:
    description:
      - The system image ID for creating the virtual machine, for example V(a3eae284-a2fe-11e4-b187-5f1f641608c8).
    type: str
  image_password:
    description:
      - Password set for the administrative user.
    type: str
  ssh_keys:
    description:
      - Public SSH keys allowing access to the virtual machine.
    type: list
    elements: str
    default: []
  datacenter:
    description:
      - The datacenter to provision this virtual machine.
    type: str
  cores:
    description:
      - The number of CPU cores to allocate to the virtual machine.
    default: 2
    type: int
  ram:
    description:
      - The amount of memory to allocate to the virtual machine.
    default: 2048
    type: int
  cpu_family:
    description:
      - The CPU family type to allocate to the virtual machine.
    type: str
    default: AMD_OPTERON
    choices: ["AMD_OPTERON", "INTEL_XEON"]
  volume_size:
    description:
      - The size in GB of the boot volume.
    type: int
    default: 10
  bus:
    description:
      - The bus type for the volume.
    type: str
    default: VIRTIO
    choices: ["IDE", "VIRTIO"]
  instance_ids:
    description:
      - List of instance IDs, currently only used when state='absent' to remove instances.
    type: list
    elements: str
    default: []
  count:
    description:
      - The number of virtual machines to create.
    type: int
    default: 1
  location:
    description:
      - The datacenter location. Use only if you want to create the Datacenter or else this value is ignored.
    type: str
    default: us/las
    choices: ["us/las", "de/fra", "de/fkb"]
  assign_public_ip:
    description:
      - This will assign the machine to the public LAN. If no LAN exists with public Internet access it is created.
    type: bool
    default: false
  lan:
    description:
      - The ID of the LAN you wish to add the servers to.
    type: int
    default: 1
  subscription_user:
    description:
      - The ProfitBricks username. Overrides the E(PB_SUBSCRIPTION_ID) environment variable.
    type: str
  subscription_password:
    description:
      - THe ProfitBricks password. Overrides the E(PB_PASSWORD) environment variable.
    type: str
  wait:
    description:
      - Wait for the instance to be in state 'running' before returning.
    type: bool
    default: true
  wait_timeout:
    description:
      - How long before wait gives up, in seconds.
    type: int
    default: 600
  remove_boot_volume:
    description:
      - Remove the bootVolume of the virtual machine you are destroying.
    type: bool
    default: true
  state:
    description:
      - Create or terminate instances.
      - 'The choices available are: V(running), V(stopped), V(absent), V(present).'
    type: str
    default: 'present'
  disk_type:
    description:
      - The type of disk to be allocated.
    type: str
    choices: [SSD, HDD]
    default: HDD

requirements:
  - "profitbricks"
author: Matt Baldwin (@baldwinSPC) <baldwin@stackpointcloud.com>
a  
# Note: These examples do not set authentication details, see the AWS Guide for details.

# Provisioning example
- name: Create three servers and enumerate their names
  community.general.profitbricks:
    datacenter: Tardis One
    name: web%02d.stackpointcloud.com
    cores: 4
    ram: 2048
    volume_size: 50
    cpu_family: INTEL_XEON
    image: a3eae284-a2fe-11e4-b187-5f1f641608c8
    location: us/las
    count: 3
    assign_public_ip: true

- name: Remove virtual machines
  community.general.profitbricks:
    datacenter: Tardis One
    instance_ids:
      - 'web001.stackpointcloud.com'
      - 'web002.stackpointcloud.com'
      - 'web003.stackpointcloud.com'
    wait_timeout: 500
    state: absent

- name: Start virtual machines
  community.general.profitbricks:
    datacenter: Tardis One
    instance_ids:
      - 'web001.stackpointcloud.com'
      - 'web002.stackpointcloud.com'
      - 'web003.stackpointcloud.com'
    wait_timeout: 500
    state: running

- name: Stop virtual machines
  community.general.profitbricks:
    datacenter: Tardis One
    instance_ids:
      - 'web001.stackpointcloud.com'
      - 'web002.stackpointcloud.com'
      - 'web003.stackpointcloud.com'
    wait_timeout: 500
    state: stopped
NT)ProfitBricksServiceVolumeServer
DatacenterNICLANF)AnsibleModule)xrange)	to_native)us/laszde/frazde/fkbz([\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        r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/profitbricks.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	      }| j                   j                  d
      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                  | j                   j                  d
            }| j                   j                  d      }| j                   j                  d      }|rad}|j                  |      }|d   D ]  }|d   d   sd}|d   } |s2t	        dd      }|j                  ||      }t        |||d       |d   }t        t        t        j                               j                  dd      d d |||	|
||      }t        t        |            }t        |||||g|g      }	 |j                  ||      }t        |||d        |j!                  ||d   d!"      }|S # t"        $ r(}| j%                  d#t        |      z  $       Y d }~y d }~ww xY w)%Ncoresram
cpu_familyvolume_size	disk_typeimage_passwordssh_keysbuslanassign_public_ipsubscription_usersubscription_passwordlocationimagewaitr   Fitems
propertiespublicTid)namer7   _create_machine- 
   )r9   sizer3   r+   r,   r*   r-   )r.   )r9   r'   r&   r(   create_volumesnics)datacenter_idservercreate_virtual_machine   )rA   	server_iddepthz#failed to create the new server: %sr    )paramsgetboolean	list_lansr   
create_lanr#   r   r   uuiduuid4replacer
   intr   create_server
get_serverr   	fail_json)moduler   
datacenterr9   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r   public_foundlansilan_responsevnscreate_server_responseserver_responsees                                 r"   r:   r:      s   MMg&E
--

E
"C""<0J--##M2K!!+.I]]&&'78N}}  ,H
--

E
"C
--

E
"C}}(();<))*=>"MM--.EF}}  ,HMMg&E~~fmm&7&78J&KL==V$D==$$^4L%%j1= 	 C< *#$i	 
 A (22:qAL |!-/@Bt$C&&sB/4%	A 	H	A 	sS	A!-!;!;$Q "< "0 	\+A)+C	E '11$,T2 2 
   MBSVKLLMs   '8K! !	L*LLc                    | j                   j                  d      }	 |dk(  r|j                  ||       y|j                  ||       y# t        $ r.}| j                  d|d|dt        |             Y d }~y d }~ww xY w)NstaterunningTz,failed to start or stop the virtual machine z at z: rG   )rH   rI   start_serverstop_serverr   rS   r   )rT   r   rA   rE   ra   r_   s         r"   _startstop_machinere   H  s    MMg&E~I%%mY?  $$]I> ~[dfsuxyzu{|}}~s   A A 	A?$A::A?c                 l   | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        ||      }	 |j                  |      }t	        |||d       |S # t
        $ r(}| j                  dt        |      z         Y d }~y d }~ww xY w)	NrU   r2   r   )r9   r2   )rU   _create_datacenterz&failed to create the new server(s): %srG   )rH   rI   r	   create_datacenterr#   r   rS   r   )rT   r   rU   r2   r   rX   datacenter_responser_   s           r"   rg   rg   V  s    ""<0J}}  ,H==$$^4L	A
P*<<<J\+>)+?	A #" PEANOOPs    !B 	B3B..B3c           	         | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }d}d}	g }
g }|j                         }t        ||      }|rd}	|	st	        | |      }|d	   }t        |||d
       |rst               }d}	 |dz   t        |||z   t        |      z         }t        t        |      j!                  |            }g }|d| }|D ]  }|j#                  ||z          n|g}|j%                  |      }|D ]~  }t'        ||      rt)        | |t+        |      |      }|j-                  ||d	         }|d   D ]*  }||d   d   k(  s|j/                  d|d   d   d   i       , |
j#                  |        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}~ud}~ww xY wc c}w )z
    Create new virtual machine

    module : AnsibleModule object
    community.general.profitbricks: authenticated profitbricks object

    Returns:
        True if a new virtual machine was created, false otherwise
    rU   r9   auto_incrementcountr.   r   TFr8   rC      r   znot allz%s%%dr    	exceptionNr5   r6   	public_ipipscreate	instances)failedmachinesactioninstance_ids)rH   rI   list_datacenters_get_datacenter_idrg   r#   set	TypeErrormessage
startswithrS   	traceback
format_excr   lenlist
differenceappendlist_servers_get_server_idr:   r   	list_nicsupdate)rT   r   rU   r9   rk   rl   r.   r   rt   datacenter_foundvirtual_machinesvirtual_machine_idsdatacenter_listrA   ri   numberscount_offsetr_   number_rangeavailable_numbersnamesnumbers_to_usenumberserver_listcreate_responser@   r[   rX   resultss                                r"   rC   rC   k  s    ""<0J==V$D]]&&'78NMMg&E
--

E
"C==$$^4LF #335O&
CM0F+D1\+>)+C	E %	R1H lL5,@3w<,OP \!2!=!=g!FG*6E2$ 	(FLL'	(  ++M:K 1+t,)&,M@RTXY%%m_T5JKg 	QAaoe,,&&Q|_U5KA5N'OP	Q 	01 F $+;<a!D'<
	G NS  	Ryy##I.~  QYY):N:N:P Q		RJ =s   ;H I=	I:AI55I:c                    | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }d}t        | j                   j                  d      t              r't	        | j                   j                  d            dk  r| j                  d       |j                         }t        ||      }|s| j                  dt        |      z         |j                  |      }|D ]6  }	t        ||	      }
|
s|rt        | |||
       	 |j                  ||
      }d	}8 |S # t        $ r;}| j                  d
t        |      z  t        j                                 Y d}~zd}~ww xY w)ac  
    Removes a virtual machine.

    This will remove the virtual machine along with the bootVolume.

    module : AnsibleModule object
    community.general.profitbricks: authenticated profitbricks object.

    Not yet supported: handle deletion of attached data disks.

    Returns:
        True if a new virtual server was deleted, false otherwise
    rU   rw   remove_boot_volumeFrm   Ginstance_ids should be a list of virtual machine ids or names, abortingrG   #Virtual data center '%s' not found.Tz*failed to terminate the virtual server: %srn   N)rH   rI   
isinstancer   r   rS   rx   ry   r   r   r   _remove_boot_volumedelete_serverr   r   r~   r   )rT   r   rU   rw   r   changedr   rA   r   instancerE   r^   r_   s                r"   remove_virtual_machiner     s    ""<0J==$$^4L**+?@Gfmm''7>#fmmFWFWXfFgBhklBlfg #335O&
CMDs:VW ++M:K  ";9	!#FL-S"."<"<]I"V   N  D  %QT]^_T`%`lu  mA  mA  mC   D  DDs   (E  	F	1E??Fc                     	 |j                  ||      }|d   d   d   }|j                  ||      }y# t        $ r;}| j                  dt	        |      z  t        j                                Y d}~yd}~ww xY w)z0
    Remove the boot volume from the server
    r6   
bootVolumer8   z-failed to remove the server's boot volume: %srn   N)rR   delete_volumer   rS   r   r~   r   )rT   r   rA   rE   rB   	volume_idvolume_responser_   s           r"   r   r     s}    ((	B<(6t<	&44]IN LyYZ|[gpg{g{g}~~s   /2 	A61A11A6c                    t        | j                  j                  d      t              r't	        | j                  j                  d            dk  r| j                  d       | j                  j                  d      }| j                  j                  d      }d}| j                  j                  d      }| j                  j                  d      }|j                         }t        ||      }	|	s| j                  d	t        |      z         |j                  |	      }
|D ]!  }t        |
|      }|st        | ||	|       d
}# |r/t        j                         |z   }|t        j                         kD  rg }|j                  |	      d   D ]d  }|dk(  r,|d   d   j                         |k(  s"|j                  |       4|dk(  s:|d   d   j                         dk(  sT|j                  |       f t	        |      t	        |      k  rt        j                  d       nn|t        j                         kD  r|t        j                         k  r'| j                  dt        j                          z         |S )z
    Starts or Stops a virtual machine.

    module : AnsibleModule object
    community.general.profitbricks: authenticated profitbricks object.

    Returns:
        True when the servers process the action successfully, false otherwise.
    rw   rm   r   rG   r4   r   FrU   r   Tr5   rb   r6   vmStatestoppedshutoffr   z,wait for virtual machine state timeout on %s)r   rH   rI   r   r   rS   rx   ry   r   r   r   re   r   lowerr   r   asctime)rT   r   ra   r4   r   r   rU   rw   r   rA   r   r   rE   matched_instancesress                  r"   startstop_machiner     s2    fmm''7>#fmmFWFWXfFgBhklBlfg==V$D==$$^4LG""<0J==$$^4L #335O&
CMDs:VW ++M:K  ";9	v|]ING yy{\1TYY[( "#00?H 6I%<(399;uD)005i'<(399;yH)0056 $%L(99

1 TYY[( 499;&!ORVR^R^R`!`ar$   c                 D    | d   D ]  }||d   d   |d   fv s|d   c S  y)zG
    Fetch and return datacenter UUID by datacenter name if found.
    r5   r6   r9   r8   N )datacentersidentityrU   s      r"   ry   ry   8  sD     "'* $

<08*T:JKKd##$ r$   c                 D    | d   D ]  }||d   d   |d   fv s|d   c S  y)z?
    Fetch and return server UUID by server name if found.
    r5   r6   r9   r8   Nr   )serversr   rB   s      r"   r   r   B  sB     '"  |,V4fTlCC$<  r$   c                  :   t        t        d?i dt               dt               dt               dt        dd      dt        dd	      d
t        ddgd      dt        dd      dt        ddgd      dt        d      dt        ddg d      dt        ddgd      dt        dd      d t        dd      d!t        d"d      d#t        ddg $      d%t               d&t        d      d't        t        d(      d)t        d"d      d*t        d"d      d+t        dd,      d-t        d"d      d.t        d/0      1      } t        s| j	                  d23       | j
                  j                  d%      }| j
                  j                  d&      }t        ||4      }| j
                  j                  d.      }|d5k(  rM| j
                  j                  d      s| j	                  d63       	 t        | |      }| j                  |7       y |d:v rN| j
                  j                  d      s| j	                  d63       	 t        | ||      }| j                  |7       y |d/k(  r| j
                  j                  d      s| j	                  d;3       | j
                  j                  d      s| j	                  d<3       | j
                  j                  d%      s| j	                  d=3       | j
                  j                  d&      s| j	                  d>3       	 t        | |      } | j                  d?i | y y # t        $ r;}| j	                  d8t        |      z  t        j                         9       Y d }~y d }~ww xY w# t        $ r;}| j	                  d8t        |      z  t        j                         9       Y d }~y d }~ww xY w# t        $ r;}| j	                  d8t        |      z  t        j                         9       Y d }~y d }~ww xY w)@NrU   r9   r3   r&   rP      )typedefaultr'   i   r(   AMD_OPTERON
INTEL_XEON)choicesr   r)   r=   r*   HDDSSDr+   T)no_logr,   r   r   F)r   elementsr   r   r-   VIRTIOIDEr.   rm   rl   rk   boolrw   )r   r   r   r0   r1   r2   r   r/   r4   r   iX  r   ra   present)r   )argument_specz%profitbricks required for this modulerG   )usernamepasswordabsentzBdatacenter parameter is required for running or stopping machines.)r   z failed to set instance state: %srn   )rb   r   z+name parameter is required for new instancez,image parameter is required for new instancez8subscription_user parameter is required for new instancez<subscription_password parameter is required for new instancer   )r   dict	LOCATIONS
HAS_PB_SDKrS   rH   rI   r   r   	exit_jsonr   r   r~   r   r   rC   )rT   r0   r1   r   ra   r   r_   machine_dict_arrays           r"   mainr   L  s5    
v

 &
 E1-	

 %.
 ]L$A$13
 %4
 E5>5A
  t,
 vr%P
 h.A
 %+
 E1-
  VT:
  6E2F!
" #f#
$ #'d"3%
& )X>'
( "vu=)
* 640+
, 5#6-
.  $>/
0 y)1
F: DE))*=>"MM--.EF&"&(L MMg&E}}  . "A B	v.v|DWW- 
(	(}}  . "= >	v)&,FWW- 
)	}}  (!NO}}  )!OP}}  !45 "9 :}}  !89 "9 :	v#9&,#OF212 
  	v!CiPQl!R^g^r^r^tuu	v  	v!CiPQl!R^g^r^r^tuu	v$  	v!CiPQl!R^g^r^r^tuu	vsH   =M N (O 	N1NN	O1OO	P1PP__main__)+
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESrerM   r   r~   r   profitbricks.clientr   r   r   r	   r
   r   ImportErroransible.module_utils.basicr   ansible.module_utils.six.movesr   +ansible.module_utils.common.text.convertersr   r   compileI
uuid_matchr#   r:   re   rg   rC   r   r   r   ry   r   r   __name__r   r$   r"   <module>r      s    A @M^.` 
   
]] 5 1 A	 RZZ/7
.K\~P*Rj.b	8vOvd zF W  Js   B B B 