
    Vh                         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mZ d dlmZmZmZmZ d Zd	 Zd
 ZddZd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: ali_instance
short_description: Create, Start, Stop, Restart or Terminate an Instance in ECS; Add or Remove Instance to/from a Security
  Group
description:
  - Create, start, stop, restart, modify or terminate ECS instances.
  - Add or remove ecs instances to/from security group.
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  state:
    description:
      - The state of the instance after operating.
    default: 'present'
    choices: ['present', 'running', 'stopped', 'restarted', 'absent']
    type: str
  availability_zone:
    description:
      - Aliyun availability zone ID in which to launch the instance. If it is not specified, it will be allocated by system
        automatically.
    aliases: ['alicloud_zone', 'zone_id']
    type: str
  image_id:
    description:
      - Image ID used to launch instances. Required when O(state=present) and creating new ECS instances.
    aliases: ['image']
    type: str
  instance_type:
    description:
      - Instance type used to launch instances. Required when O(state=present) and creating new ECS instances.
    aliases: ['type']
    type: str
  security_groups:
    description:
      - A list of security group IDs.
    aliases: ['group_ids']
    type: list
    elements: str
  vswitch_id:
    description:
      - The subnet ID in which to launch the instances (VPC).
    aliases: ['subnet_id']
    type: str
  instance_name:
    description:
      - The name of ECS instance, which is a string of 2 to 128 Chinese or English characters. It must begin with an uppercase/lowercase
        letter or a Chinese character and can contain numerals, V(.), V(_) or V(-). It cannot begin with V(http://) or V(https://).
    aliases: ['name']
    type: str
  description:
    description:
      - The description of ECS instance, which is a string of 2 to 256 characters. It cannot begin with V(http://) or V(https://).
    type: str
  internet_charge_type:
    description:
      - Internet charge type of ECS instance.
    default: 'PayByBandwidth'
    choices: ['PayByBandwidth', 'PayByTraffic']
    type: str
  max_bandwidth_in:
    description:
      - Maximum incoming bandwidth from the public network, measured in Mbps (Megabits per second).
    default: 200
    type: int
  max_bandwidth_out:
    description:
      - Maximum outgoing bandwidth to the public network, measured in Mbps (Megabits per second). Required when O(allocate_public_ip=true).
        Ignored when O(allocate_public_ip=false).
    default: 0
    type: int
  host_name:
    description:
      - Instance host name. Ordered hostname is not supported.
    type: str
  unique_suffix:
    description:
      - Specifies whether to add sequential suffixes to the host_name. The sequential suffix ranges from 001 to 999.
    default: false
    type: bool
    version_added: '0.2.0'
  password:
    description:
      - The password to login instance. After rebooting instances, modified password will take effect.
    type: str
  system_disk_category:
    description:
      - Category of the system disk.
    default: 'cloud_efficiency'
    choices: ['cloud_efficiency', 'cloud_ssd']
    type: str
  system_disk_size:
    description:
      - Size of the system disk, in GB. The valid values are V(40)~V(500).
    default: 40
    type: int
  system_disk_name:
    description:
      - Name of the system disk.
    type: str
  system_disk_description:
    description:
      - Description of the system disk.
    type: str
  count:
    description:
      - The number of the new instance. An integer value which indicates how many instances that match O(count_tag) should
        be running. Instances are either created or terminated based on this value.
    default: 1
    type: int
  count_tag:
    description:
      - O(count) determines how many instances based on a specific tag criteria should be present. This can be expressed in
        multiple ways and is shown in the EXAMPLES section. The specified count_tag must already exist or be passed in as
        the O(tags) option. If it is not specified, it will be replaced by O(instance_name).
    type: str
  allocate_public_ip:
    description:
      - Whether allocate a public IP for the new instance.
    default: false
    aliases: ['assign_public_ip']
    type: bool
  instance_charge_type:
    description:
      - The charge type of the instance.
    choices: ['PrePaid', 'PostPaid']
    default: 'PostPaid'
    type: str
  period:
    description:
      - The charge duration of the instance, in months. Required when O(instance_charge_type=PrePaid).
      - The valid value are [V(1-9), V(12), V(24), V(36)].
    default: 1
    type: int
  auto_renew:
    description:
      - Whether automate renew the charge of the instance.
    type: bool
    default: false
  auto_renew_period:
    description:
      - The duration of the automatic renew the charge of the instance. Required when O(auto_renew=true).
    choices: [1, 2, 3, 6, 12]
    type: int
  instance_ids:
    description:
      - A list of instance IDs. It is required when need to operate existing instances. If it is specified, O(count) will
        lose efficacy.
    type: list
    elements: str
  force:
    description:
      - Whether the current operation needs to be execute forcibly.
    default: false
    type: bool
  tags:
    description:
      - A hash/dictionaries of instance tags, to add to the new instance or for starting/stopping instance by tag. V({"key":"value"}).
    aliases: ["instance_tags"]
    type: dict
    version_added: '0.2.0'
  purge_tags:
    description:
      - Delete any tags not specified in the task that are on the instance. If V(true), it means you have to specify all the
        desired tags on each task affecting an instance.
    default: false
    type: bool
    version_added: '0.2.0'
  key_name:
    description:
      - The name of key pair which is used to access ECS instance in SSH.
    required: false
    type: str
    aliases: ['keypair']
  user_data:
    description:
      - User-defined data to customize the startup behaviors of an ECS instance and to pass data into an ECS instance. It
        only will take effect when launching the new ECS instances.
    required: false
    type: str
  ram_role_name:
    description:
      - The name of the instance RAM role.
    type: str
    version_added: '0.2.0'
  spot_price_limit:
    description:
      - The maximum hourly price for the preemptible instance. This parameter supports a maximum of three decimal places and
        takes effect when the SpotStrategy parameter is set to SpotWithPriceLimit.
    type: float
    version_added: '0.2.0'
  spot_strategy:
    description:
      - The bidding mode of the pay-as-you-go instance. This parameter is valid when O(instance_charge_type=PostPaid).
    choices: ['NoSpot', 'SpotWithPriceLimit', 'SpotAsPriceGo']
    default: 'NoSpot'
    type: str
    version_added: '0.2.0'
  period_unit:
    description:
      - The duration unit that you will buy the resource. It is valid when O(instance_charge_type=PrePaid).
    choices: ['Month', 'Week']
    default: 'Month'
    type: str
    version_added: '0.2.0'
  dry_run:
    description:
      - Specifies whether to send a dry-run request.
      - If O(dry_run=true), Only a dry-run request is sent and no instance is created. The system checks whether the required
        parameters are set, and validates the request format, service permissions, and available ECS instances. If the validation
        fails, the corresponding error code is returned. If the validation succeeds, the DryRunOperation error code is returned.
      - If O(dry_run=false), a request is sent. If the validation succeeds, the instance is created.
    default: false
    type: bool
    version_added: '0.2.0'
  include_data_disks:
    description:
      - Whether to change instance disks charge type when changing instance charge type.
    default: true
    type: bool
    version_added: '0.2.0'
author:
  - "He Guimin (@xiaozhu36)"
requirements:
  - "Python >= 3.6"
  - "footmark >= 1.19.0"
extends_documentation_fragment:
  - community.general.alicloud
  - community.general.attributes
a  
# basic provisioning example vpc network
- name: Basic provisioning example
  hosts: localhost
  vars:
    alicloud_access_key: <your-alicloud-access-key-id>
    alicloud_secret_key: <your-alicloud-access-secret-key>
    alicloud_region: cn-beijing
    image: ubuntu1404_64_40G_cloudinit_20160727.raw
    instance_type: ecs.n4.small
    vswitch_id: vsw-abcd1234
    assign_public_ip: true
    max_bandwidth_out: 10
    host_name: myhost
    password: mypassword
    system_disk_category: cloud_efficiency
    system_disk_size: 100
    internet_charge_type: PayByBandwidth
    security_groups: ["sg-f2rwnfh23r"]

    instance_ids: ["i-abcd12346", "i-abcd12345"]
    force: true

  tasks:
    - name: Launch ECS instance in VPC network
      community.general.ali_instance:
        alicloud_access_key: '{{ alicloud_access_key }}'
        alicloud_secret_key: '{{ alicloud_secret_key }}'
        alicloud_region: '{{ alicloud_region }}'
        image: '{{ image }}'
        system_disk_category: '{{ system_disk_category }}'
        system_disk_size: '{{ system_disk_size }}'
        instance_type: '{{ instance_type }}'
        vswitch_id: '{{ vswitch_id }}'
        assign_public_ip: '{{ assign_public_ip }}'
        internet_charge_type: '{{ internet_charge_type }}'
        max_bandwidth_out: '{{ max_bandwidth_out }}'
        tags:
          Name: created_one
        host_name: '{{ host_name }}'
        password: '{{ password }}'

    - name: With count and count_tag to create a number of instances
      community.general.ali_instance:
        alicloud_access_key: '{{ alicloud_access_key }}'
        alicloud_secret_key: '{{ alicloud_secret_key }}'
        alicloud_region: '{{ alicloud_region }}'
        image: '{{ image }}'
        system_disk_category: '{{ system_disk_category }}'
        system_disk_size: '{{ system_disk_size }}'
        instance_type: '{{ instance_type }}'
        assign_public_ip: '{{ assign_public_ip }}'
        security_groups: '{{ security_groups }}'
        internet_charge_type: '{{ internet_charge_type }}'
        max_bandwidth_out: '{{ max_bandwidth_out }}'
        tags:
          Name: created_one
          Version: 0.1
        count: 2
        count_tag:
          Name: created_one
        host_name: '{{ host_name }}'
        password: '{{ password }}'

    - name: Start instance
      community.general.ali_instance:
        alicloud_access_key: '{{ alicloud_access_key }}'
        alicloud_secret_key: '{{ alicloud_secret_key }}'
        alicloud_region: '{{ alicloud_region }}'
        instance_ids: '{{ instance_ids }}'
        state: 'running'

    - name: Reboot instance forcibly
      ecs:
        alicloud_access_key: '{{ alicloud_access_key }}'
        alicloud_secret_key: '{{ alicloud_secret_key }}'
        alicloud_region: '{{ alicloud_region }}'
        instance_ids: '{{ instance_ids }}'
        state: 'restarted'
        force: '{{ force }}'

    - name: Add instances to an security group
      ecs:
        alicloud_access_key: '{{ alicloud_access_key }}'
        alicloud_secret_key: '{{ alicloud_secret_key }}'
        alicloud_region: '{{ alicloud_region }}'
        instance_ids: '{{ instance_ids }}'
        security_groups: '{{ security_groups }}'
a  
instances:
  description: List of ECS instances.
  returned: always
  type: complex
  contains:
    availability_zone:
      description: The availability zone of the instance is in.
      returned: always
      type: str
      sample: cn-beijing-a
    block_device_mappings:
      description: Any block device mapping entries for the instance.
      returned: always
      type: complex
      contains:
        device_name:
          description: The device name exposed to the instance.
          returned: always
          type: str
          sample: /dev/xvda
        attach_time:
          description: The time stamp when the attachment initiated.
          returned: always
          type: str
          sample: "2018-06-25T04:08:26Z"
        delete_on_termination:
          description: Indicates whether the volume is deleted on instance termination.
          returned: always
          type: bool
          sample: true
        status:
          description: The attachment state.
          returned: always
          type: str
          sample: in_use
        volume_id:
          description: The ID of the cloud disk.
          returned: always
          type: str
          sample: d-2zei53pjsi117y6gf9t6
    cpu:
      description: The CPU core count of the instance.
      returned: always
      type: int
      sample: 4
    creation_time:
      description: The time the instance was created.
      returned: always
      type: str
      sample: "2018-06-25T04:08Z"
    description:
      description: The instance description.
      returned: always
      type: str
      sample: "my ansible instance"
    eip:
      description: The attribution of EIP associated with the instance.
      returned: always
      type: complex
      contains:
        allocation_id:
          description: The ID of the EIP.
          returned: always
          type: str
          sample: eip-12345
        internet_charge_type:
          description: The internet charge type of the EIP.
          returned: always
          type: str
          sample: "paybybandwidth"
        ip_address:
          description: EIP address.
          returned: always
          type: str
          sample: 42.10.2.2
    expired_time:
      description: The time the instance will expire.
      returned: always
      type: str
      sample: "2099-12-31T15:59Z"
    gpu:
      description: The attribution of instance GPU.
      returned: always
      type: complex
      contains:
        amount:
          description: The count of the GPU.
          returned: always
          type: int
          sample: 0
        spec:
          description: The specification of the GPU.
          returned: always
          type: str
          sample: ""
    host_name:
      description: The host name of the instance.
      returned: always
      type: str
      sample: iZ2zewaoZ
    id:
      description: Alias of instance_id.
      returned: always
      type: str
      sample: i-abc12345
    instance_id:
      description: ECS instance resource ID.
      returned: always
      type: str
      sample: i-abc12345
    image_id:
      description: The ID of the image used to launch the instance.
      returned: always
      type: str
      sample: m-0011223344
    inner_ip_address:
      description: The inner IPv4 address of the classic instance.
      returned: always
      type: str
      sample: 10.0.0.2
    instance_charge_type:
      description: The instance charge type.
      returned: always
      type: str
      sample: PostPaid
    instance_name:
      description: The name of the instance.
      returned: always
      type: str
      sample: my-ecs
    instance_type:
      description: The instance type of the running instance.
      returned: always
      type: str
      sample: ecs.sn1ne.xlarge
    instance_type_family:
      description: The instance type family of the instance belongs.
      returned: always
      type: str
      sample: ecs.sn1ne
    internet_charge_type:
      description: The billing method of the network bandwidth.
      returned: always
      type: str
      sample: PayByBandwidth
    internet_max_bandwidth_in:
      description: Maximum incoming bandwidth from the internet network.
      returned: always
      type: int
      sample: 200
    internet_max_bandwidth_out:
      description: Maximum incoming bandwidth from the internet network.
      returned: always
      type: int
      sample: 20
    io_optimized:
      description: Indicates whether the instance is optimized for EBS I/O.
      returned: always
      type: bool
      sample: false
    memory:
      description: Memory size of the instance.
      returned: always
      type: int
      sample: 8192
    network_interfaces:
      description: One or more network interfaces for the instance.
      returned: always
      type: complex
      contains:
        mac_address:
          description: The MAC address.
          returned: always
          type: str
          sample: "00:11:22:33:44:55"
        network_interface_id:
          description: The ID of the network interface.
          returned: always
          type: str
          sample: eni-01234567
        primary_ip_address:
          description: The primary IPv4 address of the network interface within the vswitch.
          returned: always
          type: str
          sample: 10.0.0.1
    osname:
      description: The operation system name of the instance owned.
      returned: always
      type: str
      sample: CentOS
    ostype:
      description: The operation system type of the instance owned.
      returned: always
      type: str
      sample: linux
    private_ip_address:
      description: The IPv4 address of the network interface within the subnet.
      returned: always
      type: str
      sample: 10.0.0.1
    public_ip_address:
      description: The public IPv4 address assigned to the instance or eip address.
      returned: always
      type: str
      sample: 43.0.0.1
    resource_group_id:
      description: The ID of the resource group to which the instance belongs.
      returned: always
      type: str
      sample: my-ecs-group
    security_groups:
      description: One or more security groups for the instance.
      returned: always
      type: list
      elements: dict
      contains:
        group_id:
          description: The ID of the security group.
          returned: always
          type: str
          sample: sg-0123456
        group_name:
          description: The name of the security group.
          returned: always
          type: str
          sample: my-security-group
    status:
      description: The current status of the instance.
      returned: always
      type: str
      sample: running
    tags:
      description: Any tags assigned to the instance.
      returned: always
      type: dict
      sample:
    user_data:
      description: User-defined data.
      returned: always
      type: dict
      sample:
    vswitch_id:
      description: The ID of the vswitch in which the instance is running.
      returned: always
      type: str
      sample: vsw-dew00abcdef
    vpc_id:
      description: The ID of the VPC the instance is in.
      returned: always
      type: str
      sample: vpc-0011223344
    spot_price_limit:
      description:
        - The maximum hourly price for the preemptible instance.
      returned: always
      type: float
      sample: 0.97
    spot_strategy:
      description:
        - The bidding mode of the pay-as-you-go instance.
      returned: always
      type: str
      sample: NoSpot
ids:
  description: List of ECS instance IDs.
  returned: always
  type: list
  sample: [i-12345er, i-3245fs]
N)AnsibleModulemissing_required_lib)ecs_argument_spececs_connectFOOTMARK_IMP_ERRHAS_FOOTMARKc                    g }| j                  |      }t        |      dkD  rj|D ]e  }| j                  |j                        }t	        |d|       t	        |d|j                                |j                  |j                                g |S )Ninstance_idsr   )instance_idblock_device_mappings	user_data)describe_instanceslendescribe_disksidsetattrdescribe_user_dataappendread)
connectionidsresult	instancesinstvolumess         r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/ali_instance.pyget_instances_infor!   u  s    F--3-?I
9~ 	'D //DGG/DGD17;D+t'>'>'@AMM$))+&		'
 M    c                    |dk  ry | j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }	| j                   d	   }
| j                   d
   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }| j                   d   }|s| j                  d       |s| j                  d       t        |t              s| j                  d        t	        |      dk  r| j                  d!       d"j                  t        t        | j                               t        t        j                                     }	  |j                  d0i d|d|d#|d   d$|d|d|	d	|
d%|d&|d|d|d'd(d|d|d)|d|d|d*|d|d|d+d,d|d|d-|d|d.|d|d|d|d|}|S # t        $ r,} | j                  d/j                  |              Y d } ~ S d } ~ ww xY w)1Nr   availability_zoneimage_idinstance_typesecurity_groups
vswitch_idinstance_namedescriptioninternet_charge_typemax_bandwidth_outmax_bandwidth_in	host_namepasswordsystem_disk_categorysystem_disk_sizesystem_disk_namesystem_disk_descriptionallocate_public_ipperiod
auto_renewinstance_charge_typeauto_renew_periodr   key_nameram_role_namespot_price_limitspot_strategyunique_suffixz%image_id is required for new instancemsgz*instance_type is required for new instance8The parameter security_groups should be a list, abortingz[Expected the parameter security_groups is non-empty when create new ECS instances, abortingzAnsible-Alicloud-{0}-{1}security_group_idzone_idinternet_max_bandwidth_outinternet_max_bandwidth_inio_optimized	optimizedsystem_disk_disk_nameamountperiod_unitMonthkey_pair_nameclient_tokenz%Unable to create instance, error: {0} )params	fail_json
isinstancelistr   formathashstrtimerun_instances	Exception)!moduleecsexact_countrB   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r   r9   r:   r;   r<   r=   rL   r   es!                                    r    run_instancer\     s   amm/0G}}Z(HMM/2Mmm$56O|,JMM/2M--.K!==)?@&9:}}%78k*I}}Z(H!==)?@}}%78}}%78$mm,EF';<]]8$F|,J!==)?@&9:k*I}}Z(HMM/2M}}%78MM/2MMM/2MDEIJot,WX
?q z{-44T#fmm:L5MsSWS\S\S^O_`LP%C%% 
Cx 
C} 
Chwxyhz 
C.5
CER
C`k
C;O
Cl}
C AQ
C ]f
C qy
C 4?	
C Vj	
C
 8H
C
 `p
C ?V
C cm
C .9
C Pd
C ms
C AH
C 2<
C Pa
C qy
C 1:
C IU
C er
C 8H
C Xe
C uB
C	   PDKKANOOPs   A-J? ?	K4!K//K4c                    | j                   d   }| j                   d   }| j                   d   }|s|j                  }| j                   d   }|s|j                  }| j                   d   }|r|r|j                  dd  }||z   }|s|j                  }d}|dk(  r| j                   d	   }t	        |d
|j                                |j                  }	|dk(  r| j                   d
   j                         }		 |j                  |||||	      S # t        $ r6}
| j                  dj                  |j                  |
             Y d }
~
y d }
~
ww xY w)Nstater)   r=   r*   r.    	restartedr/   r   stopped)namer*   r.   r/   r   z/Modify instance {0} attribute got an error: {1}r>   )rN   rc   r*   r.   r   r   r   encodemodifyrW   rO   rR   r   )rX   instancer^   rc   r=   r*   r.   suffixr/   r   r[   s              r    modify_instancerh     sL   MM'"E==)DMM/2M}}--.K**k*I##BC(&	&&	 H==, Hk8#>#>#@A""I	MM+.557	gDkYaiu~ gNUUV^VaVacdeffgs   .D 	E,D>>Ec                    	 	 | j                  |      }d}|D ]  }|s|j                  |k7  sd} |ry||z  }t        j                  |       |dk  rt	        dj                  |            m# t        $ r}|d}~ww xY w)z^
    To verify instance charge type has become expected after modify instance charge type
    Tr   FNr   z,Timeout Error: Waiting for instance to {0}. )r   r7   rU   sleeprW   rR   )	rY   r   charge_typedelaytimeoutr   flagr   r[   s	            r    wait_for_instance_modify_chargero     s    ..L.IID! !D55D D! uGJJu!| N U UVa bcc   s&   A0 A0 A0 :A0 0	B 9A;;B c                     t               } | j                  t        dxi dt        dddg      dt        dddg	      d
t        ddg	      dt        ddg	      dt        dd      dt        d      dt        ddg	      dt        ddg	      dt        d      dt        dd      dt        ddddg      d t        dd!      d"t        dd#      d$t        dd%d%d&g      d't        dd(      d)t        d      d*t        d      d+t        d,d-      d.t        d/d0g	      d1t        d,d-      d2t        d3g d45      d6t        d      d7t        d,d8gd-9      d:t        dd;d<d;g      d=t        dd      d>t        d,d-      d?t        dd@      dAt        dg dBC      dDt        ddEg	      dFt        d      dGt        d      dHt        dI      dJt        ddKg dL      dMt        d,d-      dNt        ddOdOdPg      dQt        d,d-      dRt        d,d             t        | S      }t        d-u r |j                  t        dT      t        U       t        |      }|j                  d   }|j                  d2   }|j                  d?   }|j                  d   }|j                  d   }|j                  d   }|j                  d+   }	|j                  d   }
|j                  dD   }|j                  d.   }|j                  d"   }|j                  d:   }|d<k(  rdV|j                  dJ<   d-}g }|rYt        |t              s|j                  dWX       |j                  |
|Y      }|sV|j                  dZj                  |      X       n4|r|j                  |
t        |      [      }n|r|j                  |
|\      }g }|d]k(  rt        |      dk  r|j                  d^X       	 g }|D ]@  }|j                   d_k7  r|	s|j                  d`X       |j#                  |j$                         B |j'                  ||	a      rd}|j)                  |       |j+                  ||g b       |j                  d7   r|d#k  r|j                  ddX       |j                  d7   sd#|j                  d"<   |d3k(  r&|st        |      |kD  rt/        d#t        |      |z
        D ]  }|t        |      dz
     }|j                   d_k7  r-|	s+|j                  dej                  |j$                        X       	 |j1                  |	f      rd}|j3                  t        |      dz
          nW	 t5        j6                  dh|      r|j                  diX       t9        |||t        |      z
        }|rd}|j)                  |       |j                  d   }|rt        |t              s|j                  dkX       |D ]  }|j:                  dl   }t        t=        |      j?                  t=        |                  }t        t=        |      j?                  t=        |                  }|D ]  }|jA                  |      sd} |D ]  }|jC                  |      sd}  g }|D ]I  }|||jD                  k7  s|dVk(  r|jG                         s,d}/|j#                  |j$                         K |r|jI                  ||m      }|D ]:  }tK        ||      rd}|j$                  |vs |j#                  |j$                         < g }|D ]-  }|jL                  |k7  s|j#                  |j$                         / |r|||j                  dR   |j                  dQ   ddn}|d<k(  r$|j                  d=   |d=<   |j                  dN   |dN<    |jN                  dxi |rd}tQ        |||       nt        |      dk  r|j                  d^X       |dok(  r	 g }|D ]U  }tK        ||      rd}|j                   dok7  r|j#                  |j$                         |j#                  |j$                         W |r%|jS                  |p      rd}|j)                  |       n|d_k(  rt	 g }|D ]-  }|j                   d_k7  s|j#                  |j$                         / |r&|jU                  ||	r      rd}|j)                  |       |D ]  }tK        ||      sd} nl|dtk(  rg	 g }|D ],  }tK        ||      sd}|j#                  |j$                         . |jW                  ||j                  d+   r      rd}|j)                  |       |j                  d.   }|j                  d1   rF|D ]$  }|s|jX                  }	 |j[                  |      rd}& |j+                  |t]        ||      w       |r|D ]  }	 |j_                  |      rd} |j+                  |t]        ||      w       y # t,        $ r,}|j                  dcj                  |      X       Y d }~d }~ww xY w# t,        $ r7}|j                  dgj                  |j$                  |      X       Y d }~9d }~ww xY w# t,        $ r,}|j                  djj                  |      X       Y d }~d }~ww xY w# t,        $ r,}|j                  dqj                  |      X       Y d }~d }~ww xY w# t,        $ r,}|j                  dsj                  |      X       Y d }~d }~ww xY w# t,        $ r,}|j                  duj                  |      X       Y d }~d }~ww xY w# t,        $ r,}|j                  dvj                  |      X       Y d }~
d }~ww xY w# t,        $ r,}|j                  dvj                  |      X       Y d }~d }~ww xY w)yNr'   rQ   rT   	group_ids)typeelementsaliasesr$   alicloud_zonerB   )rr   rt   r&   rr   r%   imagecountint   )rr   default	count_tag)rr   r(   	subnet_idr)   rc   r.   r/   T)rr   no_logr+   PayByBandwidthPayByTraffic)rr   rz   choicesr-      r,   r   r0   cloud_efficiency	cloud_ssdr1   (   r2   r3   forceboolFtagsdictinstance_tags
purge_tagsr^   present)r   runningrb   ra   absent)rz   r   r*   r4   assign_public_ip)rr   rt   rz   r7   PostPaidPrePaidr5   r6   r   )rr   rs   r8   )ry               )rr   r   r9   keypairr   r:   r;   floatr<   NoSpot)r   SpotWithPriceLimitSpotAsPriceGor=   rI   rJ   Weekdry_runinclude_data_disks)argument_specfootmark)r?   	exceptionr`   z5The parameter instance_ids should be a list, abortingr>   )rB   r   z^There are no instances in our record based on instance_ids {0}. Please check it and try again.)rB   r   )rB   r)   r   zwPlease specify ECS instances that you want to operate by using parameters instance_ids, tags or instance_name, abortingrb   z?Instance is running, and please stop it or set 'force' as True.)r   r   )changedr   r   z!Delete instance got an error: {0}zO'max_bandwidth_out' should be greater than 0 when 'allocate_public_ip' is True.zlThat to delete instance {0} is failed results from it is running, and please stop it or set 'force' as True.)r   z%Delete instance {0} got an error: {1}z-\[\d+,\d+\]-zzOrdered hostname is not supported, If you want to add an ordered suffix to the hostname, you can set unique_suffix to Truez&Create new instances got an error: {0}r@   rA   )r   rK   )r   r7   r   r   auto_payr   r   z!Start instances got an error: {0})r   
force_stopz Stop instances got an error: {0}ra   z"Reboot instances got an error: {0}z{0})r   r   rM   )0r   updater   r   r   rO   r   r
   r	   rN   rP   rQ   r   rR   evalr   statusr   r   delete_instancesextend	exit_jsonrW   range	terminatepopresearchr\   security_group_idsset
differenceleave_security_groupjoin_security_groupr9   detach_key_pairattach_key_pairrh   r7   modify_instance_charge_typero   start_instancesstop_instancesreboot_instancesr   remove_tagsr!   add_tags)r   rX   rY   r.   r^   r   r{   rw   r)   r   rB   r9   r   r,   r7   r   r   r   targetsr   r[   inew_instancesr'   existingremoveaddsginst_idsrN   s                                 r    mainr     s   %'M &&5;-P&EOY3OP& x8& 57)4	&
 q)& E"& U[M:& x8& E"& 5.& "u6FQacqPrs& 5#6& E15& "u6HSegrRst& 5"5&  5)!&" !%% 0#&$ .%&& v'89'&( VU3)&* 9.fg+&, e$-&.  V6H5ISXY/&0 "uj9V`Jab1&2 *3&4 VU35&6 v67&8 E3CD9&: 59+6;&< E"=&> &?&@ 7+A&B xArsC&D 6E&F eWw>OPG&H &%0I&J  VT:K& 'P 7Fu1*=IYZ
f
Ck*IMM'"E==0Lk*IMM'"EMM/2MMM'"Emm/0G}}Z(H== D&9:!==)?@y()+o&GI,-!XY**7*V	 "BBH&BV  X	**7i*Q		**7-*X	
Cy>A "\ ]	PG! (;;)+E$$)j$ktww'( ###F

7#W#D }})*/@1/Dno==-.-.)*	9~%q#i.5"89 
6A$S^a%78D{{i/(( .ZZ`Z`aeahahZi ) ki>>>6&*G MM#i.1"45
6	]yy!19=(( .i( j$0ec)n>T$UM$"&!((7
 !--(9:ot4  %_ `! 	'223FGc(m66s?7KLM3/::3x=IJ  'B004"&'  'B//3"&'	'  	-D#DMM(Ar>++-"&OODGG,	- ))xx)XG  	$Dvt,wwc!

477#		$  	$D((,@@

477#	$ &)CW,2MM:N,O\b\i\ijs\t"&(F $y0#)==#:x (.m(D}%.s..88/S:NO y>A "\ ]IT% (D&vt4"&{{i/tww/JJtww'( s222H"GJJw' iS% 0D{{i/tww/0 s11wSX1Y"GJJw'% 'D&vt4"&'
 k!
U% 0D&vt4"&tww/0 ''WW^I_'`"GJJw' == D}}\" 	6Dyy6##D)"G	6 	4FsC4PQ 	6D6==&"G	6 W0B30LMo  	P!D!K!KA!NOO	P& % i((-T-[-[\`\c\cef-g(hhi ! ]$$)Q)X)XYZ)[$\\]P  T  %H%O%OPQ%R SST  S  %G%N%Nq%Q RRS  U  %I%P%PQR%S TTU  6  U\\!_ 556  6  U\\!_ 556s   Bh! i<Aj Bk #l :Al l m 0Am 4m<.n4!	i*!ii	j",jj	k%!kk	l	!ll		m!l<<m	m9!m44m9<	n1!n,,n14	o)=!o$$o)__main__)
   i,  )
__future__r   r   r   rr   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   rU   ansible.module_utils.basicr   r   Gansible_collections.community.general.plugins.module_utils.alicloud_ecsr   r	   r
   r   r!   r\   rh   ro   r   __name__rM   r"   r    <module>r      sv   . C BgRXtM
^ 
  J 
	;|"gJ*tNn zF r"   