
    Vh&Y                        d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZ d	Z	 d dlZg 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 8w xY w)    )absolute_importdivisionprint_functiona  
module: oneandone_server
short_description: Create, destroy, start, stop, and reboot a 1&1 Host server
description:
  - Create, destroy, update, start, stop, and reboot a 1&1 Host server. When the server is created it can optionally wait
    for it to be 'running' before returning.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  state:
    description:
      - Define a server's state to create, remove, start or stop it.
    type: str
    default: present
    choices: ["present", "absent", "running", "stopped"]
  auth_token:
    description:
      - Authenticating API token provided by 1&1. Overrides the E(ONEANDONE_AUTH_TOKEN) environment variable.
    type: str
  api_url:
    description:
      - Custom API URL. Overrides the E(ONEANDONE_API_URL) environment variable.
    type: str
  datacenter:
    description:
      - The datacenter location.
    type: str
    default: US
    choices: ["US", "ES", "DE", "GB"]
  hostname:
    description:
      - The hostname or ID of the server. Only used when state is 'present'.
    type: str
  description:
    description:
      - The description of the server.
    type: str
  appliance:
    description:
      - The operating system name or ID for the server. It is required only for 'present' state.
    type: str
  fixed_instance_size:
    description:
      - The instance size name or ID of the server. It is required only for 'present' state, and it is mutually exclusive
        with vcore, cores_per_processor, ram, and hdds parameters.
      - 'The available choices are: V(S), V(M), V(L), V(XL), V(XXL), V(3XL), V(4XL), V(5XL).'
    type: str
  vcore:
    description:
      - The total number of processors. It must be provided with O(cores_per_processor), O(ram), and O(hdds) parameters.
    type: int
  cores_per_processor:
    description:
      - The number of cores per processor. It must be provided with O(vcore), O(ram), and O(hdds) parameters.
    type: int
  ram:
    description:
      - The amount of RAM memory. It must be provided with with O(vcore), O(cores_per_processor), and O(hdds) parameters.
    type: float
  hdds:
    description:
      - A list of hard disks with nested O(ignore:hdds[].size) and O(ignore:hdds[].is_main) properties. It must be provided with O(vcore),
        O(cores_per_processor), and O(ram) parameters.
    type: list
    elements: dict
  private_network:
    description:
      - The private network name or ID.
    type: str
  firewall_policy:
    description:
      - The firewall policy name or ID.
    type: str
  load_balancer:
    description:
      - The load balancer name or ID.
    type: str
  monitoring_policy:
    description:
      - The monitoring policy name or ID.
    type: str
  server:
    description:
      - Server identifier (ID or hostname). It is required for all states except 'running' and 'present'.
    type: str
  count:
    description:
      - The number of servers to create.
    type: int
    default: 1
  ssh_key:
    description:
      - User's public SSH key (contents, not path).
    type: raw
  server_type:
    description:
      - The type of server to be built.
    type: str
    default: "cloud"
    choices: ["cloud", "baremetal", "k8s_node"]
  wait:
    description:
      - Wait for the server to be in state 'running' before returning. Also used for delete operation (set to V(false) if
        you do not want to wait for each individual server to be deleted before moving on with other tasks).
    type: bool
    default: true
  wait_timeout:
    description:
      - How long before wait gives up, in seconds.
    type: int
    default: 600
  wait_interval:
    description:
      - Defines the number of seconds to wait when using the wait_for methods.
    type: int
    default: 5
  auto_increment:
    description:
      - When creating multiple servers at once, whether to differentiate hostnames by appending a count after them or substituting
        the count where there is a %02d or %03d in the hostname string.
    type: bool
    default: true

requirements:
  - "1and1"

author:
  - "Amel Ajdinovic (@aajdinov)"
  - "Ethan Devenport (@edevenport)"
a  
- name: Create three servers and enumerate their names
  community.general.oneandone_server:
    auth_token: oneandone_private_api_key
    hostname: node%02d
    fixed_instance_size: XL
    datacenter: US
    appliance: C5A349786169F140BCBC335675014C08
    auto_increment: true
    count: 3

- name: Create three servers, passing in an ssh_key
  community.general.oneandone_server:
    auth_token: oneandone_private_api_key
    hostname: node%02d
    vcore: 2
    cores_per_processor: 4
    ram: 8.0
    hdds:
      - size: 50
        is_main: false
    datacenter: ES
    appliance: C5A349786169F140BCBC335675014C08
    count: 3
    wait: true
    wait_timeout: 600
    wait_interval: 10
    ssh_key: SSH_PUBLIC_KEY

- name: Removing server
  community.general.oneandone_server:
    auth_token: oneandone_private_api_key
    state: absent
    server: 'node01'

- name: Starting server
  community.general.oneandone_server:
    auth_token: oneandone_private_api_key
    state: running
    server: 'node01'

- name: Stopping server
  community.general.oneandone_server:
    auth_token: oneandone_private_api_key
    state: stopped
    server: 'node01'
z
servers:
  description: Information about each server that was processed.
  type: list
  sample:
    - {"hostname": "my-server", "id": "server-id"}
  returned: always
Nxrange)AnsibleModule)get_datacenterget_fixed_instance_sizeget_applianceget_private_networkget_monitoring_policyget_firewall_policyget_load_balancer
get_serverOneAndOneResources%wait_for_resource_creation_completion%wait_for_resource_deletion_completionTF)USESDEGB)	DEPLOYINGPOWERED_OFF
POWERED_ONPOWERING_ONPOWERING_OFFc                 B    | j                   r| j                  |       y y )N)changed)
check_mode	exit_json)moduleresults     v/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/oneandone_server.py_check_moder$      s&     	 	
     c                    	 t        ||      }|r| j                  ryy | j                  ry|j                  t        j                  j                  |||||||
|	||||||      |      }|r4t        |t        j                  |d   ||       |j                  |d         }|S # t        $ r%}| j                  t        |             Y d }~y d }~ww xY w)NFT)namedescriptionfixed_instance_size_idvcorecores_per_processorramappliance_iddatacenter_idrsa_keyprivate_network_idfirewall_policy_idload_balancer_idmonitoring_policy_idserver_typeidmsg)r   r   create_server	oneandoneclientServerr   r   server	Exception	fail_jsonstr)r!   oneandone_connhostnamer(   r)   r*   r+   r,   hddsr.   r-   ssh_keyr0   r1   r2   r3   r4   waitwait_timeoutwait_intervalexisting_serverr<   exs                          r#   _create_serverrI      s    '&$^X>  --##''=$7)+#5#5!1%9' $ * ,01" 1"))t $..vd|<F &SW%%&s"   B  B  A4B   	C)C		Cc                 ^    | d   D ]$  }|d   dk(  r	|d   | d<   |d   dk(  s|d   | d<   & | S )NipstypeIPV6ippublic_ipv6IPV4public_ipv4 )r<   	addr_datas     r#   _insert_network_datarT   '  sR    E] 4	V&$-dOF=!v&($-dOF=!	4
 Mr%   c                 P	   | 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                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        ||      }|!t        | d       | j	                  d|z         d}|r/t        ||      }|!t        | d       | j	                  d|z         t        ||      }|!t        | d       | j	                  d|z         d}|r/t        ||      }|!t        | d       | j	                  d|z         d}|r/t        ||      }|!t        | d       | j	                  d|z         d}|r/t        ||      }|!t        | d       | j	                  d|z         d}|r/t        ||      }|!t        | d       | j	                  d|z         |rt        ||      }t        ||      }n|g|z  }|g|z  }g }|
r=|
D ]8  } |j                  t        j                  j!                  | d   | d    !             : g }!t#        |      D ]a  \  }"}#t%        d,i d"| d#|d|#d||"   d$|d|d|d|	d	|d%|d&|d|d'|d(|d)|d*|d|d|d|d|}$|$sQ|!j                  |$       c d}%|!r<|!D ]  }$|$st        | d+        t        | d       |!D &cg c]  }&t'        |&       }!}&d+}%t        | d       |%|!fS c c}&w )-a<  
    Create new server

    module : AnsibleModule object
    oneandone_conn: authenticated oneandone object

    Returns a dictionary containing a 'changed' attribute indicating whether
    any server was added, and a 'servers' attribute with the list of the
    created servers' hostname, id and ip addresses.
    rA   r(   auto_incrementcountfixed_instance_sizer*   r+   r,   rB   
datacenter	appliancerC   private_networkmonitoring_policyfirewall_policyload_balancerr4   rD   rE   rF   NFzdatacenter %s not found.r6   z!fixed_instance_size %s not found.zappliance %s not found.zprivate network %s not found.zmonitoring policy %s not found.zfirewall policy %s not found.zload balancer %s not found.sizeis_main)r_   r`   r!   r@   r)   r.   r-   r0   r3   r1   r2   TrR   )paramsgetr	   r$   r>   r
   r   r   r   r   r   _auto_increment_hostname_auto_increment_descriptionappendr9   r:   Hdd	enumeraterI   rT   )'r!   r@   rA   r(   rV   rW   rX   r*   r+   r,   rB   rY   rZ   rC   r[   r\   r]   r^   r4   rD   rE   rF   r.   r)   r-   r0   r3   r1   r2   	hostnamesdescriptionshdd_objshddserversindexr'   r<   r   _servers'                                          r#   r8   r8   0  sm    }}  ,H--##M2K]]&&'78NMMg&E --++,ABMMg&E --++,AB
--

E
"C==V$D""<0J!!+.Imm	*Gmm''(9:O))*=>mm''(9:OMM%%o6M--##M2K==V$D==$$^4LMM%%o6M">:>MFE"*Z7 	 	9 "!8"! ")&7:MM  O !;LFE")I5 	 	7 0 %&3oE  G  4   '&58II  K 0 %&3oE  G , #&1MA  C ,UH=	25+FJ&	#}u,H 	COOI,,00[I 1  	 G + #t )))) ) %U+	)
 $:) ) !4) ) ) () &) )  2) "6)  2)  .!)" $#)$ %)& &')( ())* NN6"/#2 G 	*FFD)	* 	FE"@GHW'0HHW Is   =R#c                 *   | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }d}d}t        ||d      }|rQt        | d       	 |j	                  |d   	       |r t        |t        j                  |d   ||       d}|d   |d   d}t        | d       ||fS # t        $ r(}	| j                  d
t        |	      z         Y d}	~	Gd}	~	ww xY w)a,  
    Removes a server.

    module : AnsibleModule object
    oneandone_conn: authenticated oneandone object.

    Returns a dictionary containing a 'changed' attribute indicating whether
    the server was removed, and a 'removed_server' attribute with
    the removed server's hostname and id.
    r<   rD   rE   rF   FNTr5   )	server_idz"failed to terminate the server: %sr6   r'   )r5   rA   )ra   rb   r   r$   delete_serverr   r   r<   r=   r>   r?   )
r!   r@   rp   rD   rE   rF   r   removed_serverr<   rH   s
             r#   remove_serverrs     s'    !!(+I==V$D==$$^4LMM%%o6MGN	48FFD!	D((6$<(@5n6H6O6O6<Tl6B6C	E
 G ,v
 ^$$  	D83r7B  D D	Ds   9C! !	D*DDc                 *   | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }d}t        ||d      }|r0	 |dk(  r/|d	   d   d
k(  r$t        | d       |j	                  |d   dd       n3|dk(  r.|d	   d   dk(  r#t        | d       |j	                  |d   dd       t        | d       |rd}
t        j                         |z   }|t        j                         kD  rct        j                  |       |j                  |d         }|d	   d   }|dk(  r|dk(  rd}
n%|dk(  r|d
k(  rd}
n|t        j                         kD  rc|
s| j                  d|d|       d}t        |      }t        | d       ||fS # t
        $ r/}	| j                  d|d|dt        |	             Y d}	~	d}	~	ww xY w)aZ  
    Starts or Stops a server.

    module : AnsibleModule object
    oneandone_conn: authenticated oneandone object.

    Returns a dictionary with a 'changed' attribute indicating whether
    anything has changed for the server as a result of this function
    being run, and a 'server' attribute with basic information for
    the server.
    stater<   rD   rE   rF   FTstoppedstatusr   r5   	POWER_OFFSOFTWARE)rp   actionmethodrunningr   POWER_ONzfailed to set server z
 to state z: r6   NzTimeout waiting for server z to get to state )ra   rb   r   r$   modify_server_statusr=   r>   r?   timesleeprT   )r!   r@   ru   rp   rD   rE   rF   r   r<   rH   operation_completedserver_states               r#   startstop_serverr     s3    MMg&E!!(+I==V$D==$$^4LMM%%o6MG 	48F	0	!fX&6w&?<&OFD)33$Tl&% 4 ' )#x(8(A](RFD)33$Tl%% 4 ' 	FE" "'99;5L,

=)'226$<@%h/8I%,-*G*.'I%,,*F*.' , '  !5* ! + %f-V?  	0uc"g/  0 0	0s   A'G 	H#$HHc                 ^    d|vrd|z  }t        d| dz         D cg c]  }||z  	 c}S c c}w )z
    Allow a custom incremental count in the hostname when defined with the
    string formatting (%) operator. Otherwise, increment using name-01,
    name-02, name-03, and so forth.
    %z%s-%%01d   r   )rW   rA   is      r#   rc   rc   :  sG     (( 519% 	1  s   *c                 `    d|v r t        d| dz         D cg c]  }||z  	 c}S |g| z  S c c}w )z
    Allow the incremental count in the description when defined with the
    string formatting (%) operator. Otherwise, repeat the same description.
    r   r   r   )rW   r(   r   s      r#   rd   rd   I  sL    
 k Auqy)
 !O
 	

 }u$$
s   +c                  `   t        t        dAi dt        dt        j                  j	                  d      d      dt        dt        j                  j	                  d            d	t        d
      dt        d
      dt        d
      dt        d
      dt        d
      dt        d
      dt        d
      dt        dd      dt        dd      dt        dd      dt        dd      dt        d
      d t        t
        d!"      d#t        d
      d$t        d
      d%t        d
      d&t        d
      d't        dd(g d)*      d+t        dd      d,t        dd-      d.t        dd/      d0t        dd1g d2*      dddgddgddgddgfg d3f4      } t        s| j                  d56       | j                  j	                  d      s| j                  d76       | j                  j	                  d      s:t        j                  j                  | j                  j	                  d      8      }nSt        j                  j                  | j                  j	                  d      | j                  j	                  d      9      }| j                  j	                  d0      }|d:k(  r>| j                  j	                  d      s| j                  d;6       	 t        | |      \  }}n|d<v r>| j                  j	                  d      s| j                  d=6       	 t        | |      \  }}nM|d1k(  rHd>D ]3  }| j                  j	                  |      r| j                  d?|z  6       5 	 t!        | |      \  }}| j#                  @       y # t        $ r%}| j                  t        |      6       Y d }~=d }~ww xY w# t        $ r%}| j                  t        |      6       Y d }~nd }~ww xY w# t        $ r%}| j                  t        |      6       Y d }~d }~ww xY w)BN
auth_tokenr?   ONEANDONE_AUTH_TOKENT)rL   defaultno_logapi_urlONEANDONE_API_URL)rL   r   rA   )rL   r(   rZ   rX   r*   intr+   r,   floatrB   listdict)rL   elementsrW   r   rC   rawF)rL   r   rV   boolr<   rY   r   )choicesr   r[   r]   r^   r\   r4   cloud)r   	baremetalk8s_node)rL   r   r   rD   rE   iX  rF      ru   present)r   absentr|   rv   )r*   r+   r,   rB   )argument_specsupports_check_modemutually_exclusiverequired_togetherz1and1 required for this moduler6   zTThe "auth_token" parameter or ONEANDONE_AUTH_TOKEN environment variable is required.)	api_token)r   r   r   z5'server' parameter is required for deleting a server.)r|   rv   z>'server' parameter is required for starting/stopping a server.)rA   rZ   rY   z(%s parameter is required for new server.)r   rl   rR   )r   r   osenvironrb   DATACENTERSHAS_ONEANDONE_SDKr>   ra   r9   r:   OneAndOneServicers   r=   r?   r   r8   r    )r!   r@   ru   r   rl   rH   params          r#   mainr   W  sZ     


'=> 

 

':;= 
 u% 
 %( 
 & 
 !%% 0 
 E" 
 !%% 0 
 '" 
 6F3 
  E1-! 
" eE2# 
$  VT:% 
& U#' 
( #) 
. !e,/ 
0 !e,1 
2 E*3 
4 #.5 
6 %Bde7 
8 6409 
: 5#6; 
< E15= 
> E9>ij? 
B !2G<?TVk>l2E:=RTZ<[^JLK&FP =>==\*E 	 	F ==Y'"))::mm''5 ; 7 #))::mm''5v}}?P?PQZ?[ ; ] MMg&E}}  *K  M	*!.v~!FWg 
(	(}}  *T  V	*!1&.!IWg 
)	$ 	LE ==$$U+  BUJ ! L		L	*!.v~!FWg Wg61  	*R))	*  	*R))	*  	*R))	*sH   *N ,O :O? 	O&OO	O<O77O<?	P-P((P-__main__)+
__future__r   r   r   rL   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   r   ansible.module_utils.six.movesr   ansible.module_utils.basicr   Dansible_collections.community.general.plugins.module_utils.oneandoner	   r
   r   r   r   r   r   r   r   r   r   r   oneandone.clientr9   ImportErrorr   ONEANDONE_SERVER_STATESr$   rI   rT   r8   rs   r   rc   rd   r   __name__rR   r%   r#   <module>r      s    A @FP.`
 
  1 4      ' 
.&bSl)%XEP%X7v zF c  s   A7 7B B