
    Vh1                         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
 ddlmZ d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 Y Fw xY w)a(  
---
module: lightsail
version_added: 1.0.0
short_description: Manage instances in AWS Lightsail
description:
  - Manage instances in AWS Lightsail.
  - Instance tagging is not yet supported in this module.
author:
  - "Nick Ball (@nickball)"
  - "Prasad Katti (@prasadkatti)"
options:
  state:
    description:
      - Indicate desired state of the target.
      - I(rebooted) and I(restarted) are aliases.
    default: present
    choices: ['present', 'absent', 'running', 'restarted', 'rebooted', 'stopped']
    type: str
  name:
    description: Name of the instance.
    required: true
    type: str
  zone:
    description:
      - AWS availability zone in which to launch the instance.
      - Required when I(state=present)
    type: str
  blueprint_id:
    description:
      - ID of the instance blueprint image.
      - Required when I(state=present)
    type: str
  bundle_id:
    description:
      - Bundle of specification info for the instance.
      - Required when I(state=present).
    type: str
  user_data:
    description:
      - Launch script that can configure the instance with additional data.
    type: str
    default: ''
  public_ports:
    description:
      - A list of dictionaries to describe the ports to open for the specified instance.
    type: list
    elements: dict
    suboptions:
      from_port:
        description: The first port in a range of open ports on the instance.
        type: int
        required: true
      to_port:
        description: The last port in a range of open ports on the instance.
        type: int
        required: true
      protocol:
        description: The IP protocol name accepted for the defined range of open ports.
        type: str
        choices: ['tcp', 'all', 'udp', 'icmp']
        required: true
      cidrs:
        description:
          - The IPv4 address, or range of IPv4 addresses (in CIDR notation) that are allowed to connect to the instance through the ports, and the protocol.
          - One of I(cidrs) or I(ipv6_cidrs) must be specified.
        type: list
        elements: str
      ipv6_cidrs:
        description:
          - The IPv6 address, or range of IPv6 addresses (in CIDR notation) that are allowed to connect to the instance through the ports, and the protocol.
          - One of I(cidrs) or I(ipv6_cidrs) must be specified.
        type: list
        elements: str
    version_added: 6.0.0
  key_pair_name:
    description:
      - Name of the key pair to use with the instance.
      - If I(state=present) and a key_pair_name is not provided, the default keypair from the region will be used.
    type: str
  wait:
    description:
      - Wait for the instance to be in state 'running' before returning.
      - If I(wait=false) an ip_address may not be returned.
      - Has no effect when I(state=rebooted) or I(state=absent).
    type: bool
    default: true
  wait_timeout:
    description:
      - How long before I(wait) gives up, in seconds.
    default: 300
    type: int

extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
aA  
- name: Create a new Lightsail instance
  community.aws.lightsail:
    state: present
    name: my_instance
    region: us-east-1
    zone: us-east-1a
    blueprint_id: ubuntu_16_04
    bundle_id: nano_1_0
    key_pair_name: id_rsa
    user_data: " echo 'hello world' > /home/ubuntu/test.txt"
    public_ports:
      - from_port: 22
        to_port: 22
        protocol: "tcp"
        cidrs: ["0.0.0.0/0"]
        ipv6_cidrs: ["::/0"]
  register: my_instance

- name: Delete an instance
  community.aws.lightsail:
    state: absent
    region: us-east-1
    name: my_instance
a}  
changed:
  description: if a snapshot has been modified/created
  returned: always
  type: bool
  sample:
    changed: true
instance:
  description: instance data
  returned: always
  type: dict
  sample:
    arn: "arn:aws:lightsail:us-east-1:123456789012:Instance/1fef0175-d6c8-480e-84fa-214f969cda87"
    blueprint_id: "ubuntu_16_04"
    blueprint_name: "Ubuntu"
    bundle_id: "nano_1_0"
    created_at: "2017-03-27T08:38:59.714000-04:00"
    hardware:
      cpu_count: 1
      ram_size_in_gb: 0.5
    is_static_ip: false
    location:
      availability_zone: "us-east-1a"
      region_name: "us-east-1"
    name: "my_instance"
    networking:
      monthly_transfer:
        gb_per_month_allocated: 1024
      ports:
        - access_direction: "inbound"
          access_from: "Anywhere (0.0.0.0/0)"
          access_type: "public"
          common_name: ""
          from_port: 80
          protocol: tcp
          to_port: 80
        - access_direction: "inbound"
          access_from: "Anywhere (0.0.0.0/0)"
          access_type: "public"
          common_name: ""
          from_port: 22
          protocol: tcp
          to_port: 22
    private_ip_address: "172.26.8.14"
    public_ip_address: "34.207.152.202"
    resource_type: "Instance"
    ssh_key_name: "keypair"
    state:
      code: 16
      name: running
    support_code: "123456789012/i-0997c97831ee21e33"
    username: "ubuntu"
    N)camel_dict_to_snake_dict)snake_dict_to_camel_dict)is_boto3_error_code)AnsibleCommunityAWSModulec                    	 |j                  |      }|d   S # t        d      $ r}|r| j                  |       Y d }~y d }~wt        j                  j
                  $ r}| j                  |       Y d }~d   S d }~ww xY w)NinstanceNameNotFoundExceptioninstance)get_instancer   fail_json_awsbotocore
exceptionsClientError)moduleclientinstance_namefail_if_not_foundreses         k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/lightsail.pyfind_instance_infor      s     !!}!= z? 23   #**  Qz? s    A>? A>A99A>c                    | j                   j                  d      }t        j                         |z   }|t        j                         kD  rF	 t        | ||      }|d   d   |v ryt        j                  d       |t        j                         kD  rF| j                  d| d|        y# t
        j                  j                  $ r}| j                  |       Y d}~dd}~ww xY w)	zH
    `states` is a list of instance states that we are waiting for.
    wait_timeoutstatename   Nz Timed out waiting for instance "z*" to get to one of the following states - )msg)
paramsgettimer   sleepr   r   r   r   	fail_json)r   r   r   statesr   wait_maxr   r   s           r   wait_for_instance_stater&      s    
 ==$$^4Lyy{\)H
TYY[
 	$)&&-HH (F2JJqM TYY[
  	2=/Aklrkst 	 	
 "".. 	$  ##	$s   B) #B) )C!CC!c                     	 |j                  t        | j                  j                  d            |       y # t        j
                  j                  $ r}| j                  |       Y d }~y d }~ww xY w)Npublic_ports)	portInfosr	   )put_instance_public_portsr   r   r    r   r   r   r   )r   r   r   r   s       r   update_public_portsr+      sc     ((.v}}/@/@/PQ& 	) 	
 **  Q s   58 A0A++A0c                    t        | ||      }|s|g| j                  j                  d      | j                  j                  d      | j                  j                  d      | j                  j                  d      d}| j                  j                  d      }|r||d<   	  |j                  di | | j                  j                  d      }|rd	g}t        | |||       | j                  j                  d
      t        | ||       t        | ||d      }	| j                  |	|k7  t        |	             y # t        j
                  j                  $ r}| j                  |       Y d }~d }~ww xY w)Nzoneblueprint_id	bundle_id	user_data)instanceNamesavailabilityZoneblueprintIdbundleIduserDatakey_pair_namekeyPairNamewaitrunningr(   Tr   changedr    )r   r   r    create_instancesr   r   r   r   r&   r+   	exit_jsonr   )
r   r   r   instcreate_paramsr6   r   r8   desired_statesafter_update_insts
             r   create_or_update_instancerD      sV   ffm<D+_ & 1 1& 9!==,,^<))+6))+6
 ))/:+8M-(	$#F##4m4 }}  ('[N#FFM>R}}(4FFM:*66=\`a
!T))*;<   "".. 	$  ##	$s    D7 7E/E**E/c                 H   d}t        | ||      }|| j                  |i        ddg}t        | |||       	 |j                  |       d}| j                  |t        |             y # t        j
                  j                  $ r}| j                  |       Y d }~Pd }~ww xY w)NFr;   r9   stoppedr   T)	r   r?   r&   delete_instancer   r   r   r   r   )r   r   r   r<   r@   rB   r   s          r   rG   rG     s    Gffm<D|26  +NFFM>J M: W/G/MN **  Q s   A) )B!BB!c                     d}t        | ||d      }	 |j                  |       d}| j                  |t        |             y# t        j                  j                  $ r}| j                  |       Y d}~Pd}~ww xY w)zc
    Reboot an existing instance
    Wait will not apply here as this is an OS-level operation
    FTr:   r   Nr;   )r   reboot_instancer   r   r   r   r?   r   )r   r   r   r<   r@   r   s         r   restart_instancerJ   .  s~     GffmtTD M: W/G/MN **  Q s   A A<!A77A<c                     d}t        | ||d      }ddg}t        | |||       |rE|d   d   |k7  r:	 |dk(  r|j                  |       n|j                  |       d}t        | ||      }| j                  j                  d
      }|r |g}t        | |||       t        | ||d      }| j                  |t        |             y	# t        j
                  j                  $ r}| j                  |       Y d	}~d	}~ww xY w)z,
    Start or stop an existing instance
    FTr:   r9   rF   r   r   r   Nr8   r;   )r   r&   start_instancestop_instancer   r   r   r   r   r    r?   r   )	r   r   r   r   r<   r@   rB   r   r8   s	            r   start_or_stop_instancerN   A  s   
 GffmtTD  +NFFM>J Wf%.	$	!%%=%A$$-$@ !&&-@==V$D~N!&&-SWX
W/G/MN "".. 	$  ##	$s   *C C="C88C=c                     t        t        dd      t        ddg d      t        d      t        d      t        d      t        d      t        dd	      t        d
d	      t        dd      t        ddt        t        dd      t        dd      t        dg dd      t        dd      t        dd            dg      
      } t        | g dg      }|j                  d      }|j                  j	                  d      }|j                  j	                  d      }|dk(  rt        |||       y |dk(  rt        |||       y |dv rt        ||||       y |dv rt        |||       y y )NstrT)typerequiredpresent)rS   absentrF   r9   	restartedrebooted)rQ   defaultchoices)rQ    )rQ   rW   booli,  int)rW   rQ   listdict)tcpalludpicmp)rQ   rX   rR   )rQ   elements)	from_portto_portprotocolcidrs
ipv6_cidrs)rf   rg   )rQ   rb   optionsrequired_one_of)
r   r   r-   r.   r/   r6   r0   r8   r   r(   )r   rS   )r-   r.   r/   )argument_specrequired_if	lightsailr   r   rT   )r9   rF   )rU   rV   )	r]   AnsibleAWSModuler   r   r    rD   rG   rN   rJ   )rj   r   r   r   r   s        r   mainrn   d  s`   ut,	3w
 uu%E"&E2.vt,#E2ED9%$752OZ^_7Ve< 55
M4 #2m1nF ]];'F==V$DMMg&E	!&&$7	(	-	(	(vvtU;	+	+. 
,    __main__)F)DOCUMENTATIONEXAMPLESRETURNr!   r   ImportError0ansible.module_utils.common.dict_transformationsr   r   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   >ansible_collections.community.aws.plugins.module_utils.modulesr   rm   r   r&   r+   rD   rG   rJ   rN   rn   __name__r=   ro   r   <module>ry      s   aH44
l 	
 V U \ x	
* !HO(O& OF+/\ zF g  		s   A AA