
    Vhl2                     $   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Zd dlmZ dZ e ej                               Zd d	lmZ d d
lmZ ddgZg dZg dZg dZddgZg dZg dZg dZd Zd Zd Zd Z e!dk(  r e         yy# e$ r dZY Ow xY w)    )absolute_importdivisionprint_functionaR  
module: sl_vm
short_description: Create or cancel a virtual instance in SoftLayer
description:
  - Creates or cancels SoftLayer instances.
  - When created, optionally waits for it to be 'running'.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  instance_id:
    description:
      - Instance ID of the virtual instance to perform action option.
    type: str
  hostname:
    description:
      - Hostname to be provided to a virtual instance.
    type: str
  domain:
    description:
      - Domain name to be provided to a virtual instance.
    type: str
  datacenter:
    description:
      - Datacenter for the virtual instance to be deployed.
    type: str
    choices:
      - ams01
      - ams03
      - che01
      - dal01
      - dal05
      - dal06
      - dal09
      - dal10
      - dal12
      - dal13
      - fra02
      - fra04
      - fra05
      - hkg02
      - hou02
      - lon02
      - lon04
      - lon06
      - mel01
      - mex01
      - mil01
      - mon01
      - osl01
      - par01
      - sao01
      - sea01
      - seo01
      - sjc01
      - sjc03
      - sjc04
      - sng01
      - syd01
      - syd04
      - tok02
      - tor01
      - wdc01
      - wdc04
      - wdc06
      - wdc07
  tags:
    description:
      - Tag or list of tags to be provided to a virtual instance.
    type: str
  hourly:
    description:
      - Flag to determine if the instance should be hourly billed.
    type: bool
    default: true
  private:
    description:
      - Flag to determine if the instance should be private only.
    type: bool
    default: false
  dedicated:
    description:
      - Flag to determine if the instance should be deployed in dedicated space.
    type: bool
    default: false
  local_disk:
    description:
      - Flag to determine if local disk should be used for the new instance.
    type: bool
    default: true
  cpus:
    description:
      - Count of cpus to be assigned to new virtual instance.
    type: int
    choices: [1, 2, 4, 8, 16, 32, 56]
  memory:
    description:
      - Amount of memory to be assigned to new virtual instance.
    type: int
    choices: [1024, 2048, 4096, 6144, 8192, 12288, 16384, 32768, 49152, 65536, 131072, 247808]
  flavor:
    description:
      - Specify which SoftLayer flavor template to use instead of cpus and memory.
    version_added: '0.2.0'
    type: str
  disks:
    description:
      - List of disk sizes to be assigned to new virtual instance.
    default: [25]
    type: list
    elements: int
  os_code:
    description:
      - OS Code to be used for new virtual instance.
    type: str
  image_id:
    description:
      - Image Template to be used for new virtual instance.
    type: str
  nic_speed:
    description:
      - NIC Speed to be assigned to new virtual instance.
    choices: [10, 100, 1000]
    type: int
  public_vlan:
    description:
      - VLAN by its ID to be assigned to the public NIC.
    type: str
  private_vlan:
    description:
      - VLAN by its ID to be assigned to the private NIC.
    type: str
  ssh_keys:
    description:
      - List of ssh keys by their ID to be assigned to a virtual instance.
    type: list
    elements: str
    default: []
  post_uri:
    description:
      - URL of a post provisioning script to be loaded and executed on virtual instance.
    type: str
  state:
    description:
      - Create, or cancel a virtual instance.
      - Specify V(present) for create, V(absent) to cancel.
    choices: [absent, present]
    default: present
    type: str
  wait:
    description:
      - Flag used to wait for active status before returning.
    type: bool
    default: true
  wait_time:
    description:
      - Time in seconds before wait returns.
    default: 600
    type: int
requirements:
  - softlayer >= 4.1.1
author:
  - Matt Colton (@mcltn)
a  
- name: Build instance
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Build instance request
      community.general.sl_vm:
        hostname: instance-1
        domain: anydomain.com
        datacenter: dal09
        tags: ansible-module-test
        hourly: true
        private: false
        dedicated: false
        local_disk: true
        cpus: 1
        memory: 1024
        disks: [25]
        os_code: UBUNTU_LATEST
        wait: false

- name: Build additional instances
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Build instances request
      community.general.sl_vm:
        hostname: "{{ item.hostname }}"
        domain: "{{ item.domain }}"
        datacenter: "{{ item.datacenter }}"
        tags: "{{ item.tags }}"
        hourly: "{{ item.hourly }}"
        private: "{{ item.private }}"
        dedicated: "{{ item.dedicated }}"
        local_disk: "{{ item.local_disk }}"
        cpus: "{{ item.cpus }}"
        memory: "{{ item.memory }}"
        disks: "{{ item.disks }}"
        os_code: "{{ item.os_code }}"
        ssh_keys: "{{ item.ssh_keys }}"
        wait: "{{ item.wait }}"
      with_items:
        - hostname: instance-2
          domain: anydomain.com
          datacenter: dal09
          tags:
            - ansible-module-test
            - ansible-module-test-replicas
          hourly: true
          private: false
          dedicated: false
          local_disk: true
          cpus: 1
          memory: 1024
          disks:
            - 25
            - 100
          os_code: UBUNTU_LATEST
          ssh_keys: []
          wait: true
        - hostname: instance-3
          domain: anydomain.com
          datacenter: dal09
          tags:
            - ansible-module-test
            - ansible-module-test-replicas
          hourly: true
          private: false
          dedicated: false
          local_disk: true
          cpus: 1
          memory: 1024
          disks:
            - 25
            - 100
          os_code: UBUNTU_LATEST
          ssh_keys: []
          wait: true

- name: Cancel instances
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Cancel by tag
      community.general.sl_vm:
        state: absent
        tags: ansible-module-test
#N)	VSManagerTF)AnsibleModule)string_typespresentabsent)'ams01ams03che01dal01dal05dal06dal09dal10dal12dal13fra02fra04fra05hkg02hou02lon02lon04lon06mel01mex01mil01mon01osl01par01sao01sea01seo01sjc01sjc03sjc04sng01syd01syd04tok02tor01wdc01wdc04wdc06wdc07)                   8   )i   i   i   i   i    i 0  i @  i   i   i   i   i     d   )r:   r;         ,  )
      r:      (   2   K   r;   }   r<      r=      r>   i^  i  i  i    i  i  )r?   r;   rH   c                    t         j                  | j                  j                  d      | j                  j                  d      | j                  j                  d            }|ry| j                  j                  d      .| j                  j                  d      dk7  rd| j                  d<   nY| j                  j                  d      =| j                  j                  d      dk7  rd| j                  d<   g | j                  d	<   ny| j                  j                  d
      }t	        |t
              r8dj                  t        t        | j                  j                  d
                  }t         j                  | 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      |      }||d   dkD  rd|fS y)Nhostnamedomain
datacenter)rJ   rK   rL   FNos_code image_iddiskstags,cpusmemoryflavorhourly
local_diskssh_keys	nic_speedprivatepublic_vlanprivate_vlan	dedicatedpost_uri)rJ   rK   rT   rU   rV   rW   rL   rN   rP   rX   rQ   rY   rZ   r[   r\   r]   r^   r_   rR   idr   T)
	vsManagerlist_instancesparamsget
isinstancelistjoinmapstrcreate_instance)module	instancesrR   instances       k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/sl_vm.pycreate_virtual_instancero   .  s   (("":.}}  *==$$\2 ) I  	)$0V]]5F5Fy5QUW5W$&j!
--

J
'
38I8I*8UY[8[#%i !#g==V$D$xxC!2!26!:;<(("":.}}  *]]v&}}  *}}  *}}  *==$$\2!!),"":.==$$\2mm("":.--##K0!!),MM%%m4]]&&~6--##K0"":.' ) H,  2X~    c                 X   d }d}t        j                          | j                  j                  d      z   }|s`|t        j                          kD  rI	 t        j	                  |dd      }|rt        j                  |      }|s|t        j                          kD  rI||fS # t        $ r d}Y +w xY w)NF	wait_timer?   r4   )timerc   rd   ra   wait_for_readyget_instance	Exception)rk   r`   rm   	completedwait_timeouts        rn   wait_for_instancery   b  s    HI99;!2!2;!??LL499;6	!00R;I$11"5	 L499;6 h  	I	s   .B B)(B)c                 B   d}| j                   j                  d      | j                   j                  d      s6| j                   j                  d      s| j                   j                  d      r| j                   j                  d      }t        |t              r| j                   j                  d      g}t        j                  || j                   j                  d      | j                   j                  d            }|D ]  }	 t        j                  |d           |d fS | j                   j                  d      r;| j                   j                  d      d	k7  r	 t        j                  d          |d fS y
# t        $ r d}Y w xY w# t        $ r d}Y |d fS w xY w)NTinstance_idrR   rJ   rK   )rR   rJ   rK   r`   Fr   rM   )rc   rd   re   r	   ra   rb   cancel_instancerv   )rk   canceledrR   rl   rm   s        rn   r|   r|   q  s   H}}'/V]]5F5Fv5NRXR_R_RcRcdnRosy  tA  tA  tE  tE  FN  tO}}  (dL)MM%%f-.D,,$ARARS]A^gmgtgtgxgx  zB  hC,  D	! 	!H!))(4.9	! T> 
		=	)fmm.?.?.NRS.S	%%htn5 T>   ! !
  	H T>	s$   E<F <F
	F
FFc                     t        t        d/i dt        d      dt        d      dt        d      dt        dt              dt        d      d	t        d
d      dt        d
d      dt        d
d      dt        d
d      dt        dt              dt        dt              dt        d      dt        dddg      dt        d      dt        d      dt        dt
              dt        d      dt        d      dt        ddg d      d t        d      d!t        dd"t        #      d$t        d
d      d%t        dd&      '      } t        s| j                  d()       | j                  j                  d!      d*k(  rt        |       \  }}n]| j                  j                  d!      d"k(  r?t        |       \  }}| j                  j                  d$      du r|rt        | |d+         \  }}| j                  t        j                   t        j"                  d, -            .       y )0Nr{   ri   )typerJ   rK   rL   )r   choicesrR   rW   boolT)r   defaultr[   Fr^   rX   rT   intrU   rV   rQ   rf   r:   )r   elementsr   rN   rP   rZ   r\   r]   rY   )r   r   r   no_logr_   stater
   )r   r   r   waitrr   iX  )argument_specz1softlayer python library required for this module)msgr   r`   c                     | j                   S )N)__dict__)os    rn   <lambda>zmain.<locals>.<lambda>  s    abakak rp   )r   )changedrm    )r   dictDATACENTERS	CPU_SIZESMEMORY_SIZES
NIC_SPEEDSSTATESHAS_SL	fail_jsonrc   rd   r|   ro   ry   	exit_jsonjsonloadsdumps)rk   r   rm   s      rn   mainr     sF    
%(
u%
 U#
 <	

 5!
 VT2
 fe4
 6
 6
 5)4
 UL9
 U#
 FURDA
 e$
 u%
  z:!
" %(#
$ 5)%
& vr%P'
( u%)
* E9fE+
, 640-
. s3/
F8 PQ}}!X--f5(			7	#y	05f=(==V$,"3FHTN"KWh
Wtzz$**XWk:l/mnrp   __main__)"
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   rs   	SoftLayerr   r   create_client_from_envra   ImportErroransible.module_utils.basicr   ansible.module_utils.sixr	   r   r   r   r   INITIALDISK_SIZESLOCALDISK_SIZESSANDISK_SIZESr   ro   ry   r|   r   __name__r   rp   rn   <module>r      s    A @gRWt 
  #F:)::<=I 5 1 X	E %	`I *u
1h.)oX zF w  Fs   #B BB