
    Vh	D                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 ddlmZ ddlmZ  G d	 d
e      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionu  
---
module: instance
short_description: Manages server instances on Vultr.
description:
  - Manage server instances on Vultr.
version_added: "1.1.0"
author:
  - "René Moser (@resmo)"
options:
  label:
    description:
      - Name of the instance.
    required: true
    aliases: [ name ]
    type: str
  hostname:
    description:
      - The hostname to assign to this instance.
    type: str
  os:
    description:
      - The operating system name.
      - Mutually exclusive with I(image) and I(app).
    type: str
  app:
    description:
      - The app deploy name of Vultr OneClick apps.
      - Mutually exclusive with I(image) and I(os).
    type: str
  image:
    description:
      - The image deploy name of Vultr Marketplace apps.
      - Mutually exclusive with I(os) and I(app).
    type: str
  firewall_group:
    description:
      - The firewall group description to assign this instance to.
    type: str
  plan:
    description:
      - The plan name to use for the instance.
      - Required if the instance does not yet exist.
    type: str
  activation_email:
    description:
      - Whether to send an activation email when the instance is ready or not.
      - Only considered on creation.
    type: bool
    default: false
  backups:
    description:
      - Whether to enable automatic backups or not.
    type: bool
  ddos_protection:
    description:
      - Whether to enable ddos_protection or not.
    type: bool
  enable_ipv6:
    description:
      - Whether to enable IPv6 or not.
    type: bool
  tags:
    description:
      - Tags for the instance.
    type: list
    elements: str
  user_data:
    description:
      - User data to be passed to the instance.
    type: str
  user_scheme:
    description:
      - The user scheme used as login user (Linux-only).
      - By default, the I(root) user is configured.
      - Only considered while creating the instance.
    type: str
    choices: [ root, limited ]
    version_added: "1.11.0"
  startup_script:
    description:
      - Name or ID of the startup script to execute on boot.
      - Only considered while creating the instance.
    type: str
  ssh_keys:
    description:
      - List of SSH key names passed to the instance on creation.
    type: list
    elements: str
  snapshot:
    description:
      - Description or ID of the snapshot.
      - Only considered while creating the instance.
    type: str
    version_added: "1.7.0"
  reserved_ipv4:
    description:
      - IP address of the floating IP to use as the main IP of this instance.
      - Only considered on creation.
    type: str
  region:
    description:
      - Region the instance is deployed into.
    type: str
    required: true
  vpcs:
    description:
      - A list of VPCs identified by their description to be assigned to the instance.
    type: list
    elements: str
    version_added: "1.5.0"
  skip_wait:
    description:
      - Whether to skip the wait for the instance to be completely ready for access.
    type: bool
    default: false
    version_added: "1.13.0"
  state:
    description:
      - State of the instance.
      - The state I(reinstalled) was added in version 1.8.0.
    default: present
    choices: [ present, absent, started, stopped, restarted, reinstalled ]
    type: str
extends_documentation_fragment:
  - vultr.cloud.vultr_v2
a  
---
- name: Create an instance using OS
  vultr.cloud.instance:
    label: my web server
    hostname: my-hostname
    user_data: |
      #cloud-config
      packages:
        - nginx
    firewall_group: my firewall group
    plan: vc2-1c-2gb
    ddos_protection: true
    backups: true
    enable_ipv6: true
    ssh_keys:
      - my ssh key
    vpcs:
      - my vpc description
    tags:
      - web
      - project-genesis
    region: ams
    os: Debian 12 x64 (bookworm)

- name: Deploy an instance of a marketplace app
  vultr.cloud.instance:
    label: git-server
    hostname: git
    firewall_group: my firewall group
    plan: vc2-1c-2gb
    ddos_protection: true
    backups: true
    enable_ipv6: true
    region: ams
    image: Gitea on Ubuntu 20.04

- name: Stop an existing instance
  vultr.cloud.instance:
    label: my web server
    region: ams
    state: stopped

- name: Start an existing instance
  vultr.cloud.instance:
    label: my web server
    region: ams
    state: started

- name: Reinstall an instance
  vultr.cloud.instance:
    label: my web server
    region: ams
    state: reinstalled

- name: Delete an instance
  vultr.cloud.instance:
    label: my web server
    region: ams
    state: absent
aY  
---
vultr_api:
  description: Response from Vultr API with a few additions/modification.
  returned: success
  type: dict
  contains:
    api_timeout:
      description: Timeout used for the API requests.
      returned: success
      type: int
      sample: 60
    api_retries:
      description: Amount of max retries for the API requests.
      returned: success
      type: int
      sample: 5
    api_retry_max_delay:
      description: Exponential backoff delay in seconds between retries up to this max delay value.
      returned: success
      type: int
      sample: 12
    api_endpoint:
      description: Endpoint used for the API requests.
      returned: success
      type: str
      sample: "https://api.vultr.com/v2"
vultr_instance:
  description: Response from Vultr API.
  returned: success
  type: dict
  contains:
    id:
      description: ID of the instance.
      returned: success
      type: str
      sample: cb676a46-66fd-4dfb-b839-443f2e6c0b60
    v6_main_ip:
      description: IPv6 of the instance.
      returned: success
      type: str
      sample: ""
    v6_network:
      description: IPv6 network of the instance.
      returned: success
      type: str
      sample: ""
    v6_network_size:
      description: IPv6 network size of the instance.
      returned: success
      type: int
      sample: 0
    main_ip:
      description: IPv4 of the instance.
      returned: success
      type: str
      sample: 95.179.189.95
    netmask_v4:
      description: Netmask IPv4 of the instance.
      returned: success
      type: str
      sample: 255.255.254.0
    hostname:
      description: Hostname of the instance.
      returned: success
      type: str
      sample: vultr.guest
    internal_ip:
      description: Internal IP of the instance.
      returned: success
      type: str
      sample: ""
    gateway_v4:
      description: Gateway IPv4.
      returned: success
      type: str
      sample: 95.179.188.1
    kvm:
      description: KVM of the instance.
      returned: success
      type: str
      sample: "https://my.vultr.com/subs/vps/novnc/api.php?data=..."
    disk:
      description: Disk size of the instance.
      returned: success
      type: int
      sample: 25
    allowed_bandwidth:
      description: Allowed bandwidth of the instance.
      returned: success
      type: int
      sample: 1000
    vcpu_count:
      description: vCPUs of the instance.
      returned: success
      type: int
      sample: 1
    firewall_group_id:
      description: Firewall group ID of the instance.
      returned: success
      type: str
      sample: ""
    plan:
      description: Plan of the instance.
      returned: success
      type: str
      sample: vc2-1c-1gb
    image_id:
      description: Image ID of the instance.
      returned: success
      type: str
      sample: ""
    os_id:
      description: OS ID of the instance.
      returned: success
      type: int
      sample: 186
    app_id:
      description: App ID of the instance.
      returned: success
      type: int
      sample: 37
    date_created:
      description: Date when the instance was created.
      returned: success
      type: str
      sample: "2020-10-10T01:56:20+00:00"
    label:
      description: Label of the instance.
      returned: success
      type: str
      sample: my instance
    region:
      description: Region the instance was deployed into.
      returned: success
      type: str
      sample: ews
    status:
      description: Status about the deployment of the instance.
      returned: success
      type: str
      sample: active
    server_status:
      description: Server status of the instance.
      returned: success
      type: str
      sample: installingbooting
    power_status:
      description: Power status of the instance.
      returned: success
      type: str
      sample: running
    ram:
      description: RAM in MB of the instance.
      returned: success
      type: int
      sample: 1024
    os:
      description: OS of the instance.
      returned: success
      type: str
      sample: Application
    tags:
      description: Tags of the instance.
      returned: success
      type: list
      sample: [ my-tag ]
    features:
      description: Features of the instance.
      returned: success
      type: list
      sample: [ ddos_protection, ipv6, auto_backups ]
    user_data:
      description: Base64 encoded user data (cloud init) of the instance.
      returned: success
      type: str
      sample: I2Nsb3VkLWNvbmZpZwpwYWNrYWdlczoKICAtIGh0b3AK
    user_scheme:
      description: The user scheme to login into this instance
      returned: success
      type: str
      sample: root
      version_added: "1.11.0"
    backups:
      description: Whether backups are enabled or disabled.
      returned: success
      type: str
      sample: enabled
      version_added: "1.3.0"
    ddos_protection:
      description: Whether DDOS protections is enabled or not.
      returned: success
      type: bool
      sample: true
      version_added: "1.3.0"
    enable_ipv6:
      description: Whether IPv6 is enabled or not.
      returned: success
      type: bool
      sample: true
      version_added: "1.3.0"
    vpcs:
      description: List of VPCs attached.
      returned: success
      type: list
      version_added: "1.5.0"
      contains:
        id:
          description: ID of the VPC.
          returned: success
          type: str
          sample: 5536d2a4-66fd-4dfb-b839-7672fd5bc116
        description:
          description: Description of the VPC.
          returned: success
          type: str
          sample: my vpc
        ip_address:
          description: IP assigned from the VPC.
          returned: success
          type: str
          sample: "192.168.23.3"
        mac_address:
          description: MAC address of the network interface.
          returned: success
          type: str
          sample: "5a:01:04:3d:5e:72"
)AnsibleModule   )AnsibleVultrCommonInstance)vultr_argument_specc                   :     e Zd ZddZ fdZ fdZ fdZ xZS )AnsibleVultrInstancec                 \   || j                   j                  d   k(  r|d   |k7  s|rd| j                  d<   | j                   j                  s`| j	                  |dddgd	      }| j                  | j                  d
|| j                     d
|d       | j	                  |d|g|       }|S )Nstatepower_statusTchangedserver_statusnonelocked!=resourcekeystatescmp/POST)pathmethod)r   r   r   	skip_wait)moduleparamsresult
check_modewait_for_state	api_queryresource_pathresource_key_id)selfr   r   actionr   forcer"   s          h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/vultr/cloud/plugins/modules/instance.pyhandle_power_statusz(AnsibleVultrInstance.handle_power_status  s    DKK&&w//Xn5MQ]5]af%)DKK	";;))..o_ego^pvz.{'+'9'98DDXDX;Y[ab!    ..%&(>"00	 /      c                    t         t        |          }|rC| j                  j                  sd| j                  j
                  j                  d      dk(  r<| j                  |dddgd| j                  j
                  j                  dd      	      }| j                  |d
dd
| j                  j
                  j                  dd             }| j                  |ddd| j                  j
                  j                  dd             }| j                  |dddd| j                  j
                  j                  dd             }| j                  |ddddd      }|S )Nr   presentr   r   r   r   r   F)r   r   r   r   r   stoppedhalt)r   r   r'   r   r"   startedstartrunning	restartedrebootT)r   r   r'   r   r(   r"   reinstalled	reinstall)	superr   create_or_updater   r!   r   getr"   r*   r&   r   	__class__s     r)   r8   z%AnsibleVultrInstance.create_or_update  ss   -tEG;;))dkk.@.@.D.DW.MQZ.Z..%'"H-"kk0044[%H /  //!&#';;#5#5#9#9+u#MM 0 H //!&#';;#5#5#9#9+u#MM 0 H //!!&#';;#5#5#9#9+u#MM 0 H //!#"&$ 0 H r+   c                    t         t        |           | j                  j                  d   dk7  r| j                  j                  j                  d      *| j                         d   | j                  j                  d<   | j                  j                  j                  d      5| j                  j                  d   rdnd| j                  j                  d<   y y y )	Nr   absentfirewall_groupidfirewall_group_idbackupsenableddisabled)r7   r   	configurer   r   r9   get_firewall_group)r&   r;   s    r)   rD   zAnsibleVultrInstance.configure  s    "D35;;g&(2{{!!%%&67C:>:Q:Q:STX:Y""#67{{!!%%i0<=A[[=O=OPY=Z	`j""9- =	 3r+   c                     | j                         }|r-| j                  j                  s| j                  |dddgd      }t        t
        |   |      S )Nr   r   r   r   r   )r   )queryr   r!   r"   r7   r   r=   r:   s     r)   r=   zAnsibleVultrInstance.absent   sT    ::<DKK22**H/[ackZlrv*wH)477JJr+   )FT)__name__
__module____qualname__r*   r8   rD   r=   __classcell__)r;   s   @r)   r   r     s     ",\kK Kr+   r   c                  p   t               } | j                  t        d3i dt        dddg      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t        d      dt        d      dt        dd      dt        dd      dt        d      dt        d      dt        d      dt        d      dt        ddd      dt        dd      d t        dd!d"g#      d$t        dd      d%t        g d&d'(             t        | d)d*d+      }t	        |d,d-d.g d/g d0d1      }|j
                  j                  d%      }|d2k(  r|j                          y |j                          y )4NlabelstrTname)typerequiredaliaseshostname)rP   appimagesnapshotosplanactivation_emailboolF)rP   defaultddos_protectionrA   enable_ipv6tagslist)rP   elementsvpcsreserved_ipv4r>   startup_script	user_datassh_keys)rP   r`   no_logregion)rP   rQ   user_schemerootlimited)rP   choicesr   r   )r-   r=   r0   r.   r3   r5   r-   )rk   r[   ))r   r-   )rX   ))rW   rT   rU   rV   )argument_specrequired_ifmutually_exclusivesupports_check_modevultr_instancez
/instancesinstance)rM   rS   rX   app_idos_idiso_idimage_idsnapshot_id	script_idrg   r]   rb   r@   rd   r^   rY   r\   	sshkey_idrA   
attach_vpcrh   )	rX   r^   r@   r]   r\   rA   rd   ry   
detach_vpc)r   	namespacer$   ressource_result_key_singularresource_create_param_keysresource_update_param_keysresource_key_namer=    )	r	   updatedictr   r   r   r9   r=   r-   )rl   r   vultrr   s       r)   mainr     s   ')M !	
ED6(C!	
u%!	
 % !	
 E"	!	

 u%!	
 !	
 5!!	
 "vu=!	
 !f-!	
 f%!	
 &)!	
 6E2!	
 6E2!	
 E*!	
  U+!	
   U+!!	
" &#!	
$ veD%!	
& UT2'!	
( %&)1DE)!	
* 6+!	
,  "
-!	
#J #6@ 	F !""&0$
.
$
 "O(ET MMg&Er+   __main__N)
__future__r   r   r   rP   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   module_utils.common_instancer   module_utils.vultr_v2r	   r   r   rH   r   r+   r)   <module>r      se    A @~@<|c
L 5 E 7OK5 OKd\~ zF r+   