
    Vh.2                         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  G d d	      Zd
 Zedk(  r e        yy# e$ r Y 4w xY w)a?	  
---
module: ecs_cluster
version_added: 1.0.0
short_description: Create or terminate ECS clusters.
notes:
  - When deleting a cluster, the information returned is the state of the cluster prior to deletion.
  - It will also wait for a cluster to have instances registered to it.
description:
  - Creates or terminates ecs clusters.
author:
  - Mark Chance (@Java1Guy)
options:
    state:
        description:
            - The desired state of the cluster.
        required: true
        choices: ['present', 'absent', 'has_instances']
        type: str
    name:
        description:
            - The cluster name.
        required: true
        type: str
    delay:
        description:
            - Number of seconds to wait.
        required: false
        type: int
        default: 10
    repeat:
        description:
            - The number of times to wait for the cluster to have an instance.
        required: false
        type: int
        default: 10
    capacity_providers:
        version_added: 5.2.0
        description:
            - List of capacity providers to use for the cluster.
        required: false
        type: list
        elements: str
    capacity_provider_strategy:
        version_added: 5.2.0
        description:
            - List of capacity provider strategies to use for the cluster.
        required: false
        type: list
        elements: dict
        suboptions:
            capacity_provider:
                description:
                  - Name of capacity provider.
                type: str
            weight:
                description:
                  - The relative percentage of the total number of launched tasks that should use the specified provider.
                type: int
            base:
                description:
                  - How many tasks, at a minimum, should use the specified provider.
                type: int
                default: 0
    purge_capacity_providers:
        version_added: 5.2.0
        description:
            - Toggle overwriting of existing capacity providers or strategy. This is needed for backwards compatibility.
            - By default I(purge_capacity_providers=true).
        required: false
        type: bool
        default: true
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
a:  
# Note: These examples do not set authentication details, see the AWS Guide for details.

- name: Cluster creation
  community.aws.ecs_cluster:
    name: default
    state: present

- name: Cluster creation with capacity providers and strategies.
  community.aws.ecs_cluster:
    name: default
    state: present
    capacity_providers:
      - FARGATE
      - FARGATE_SPOT
    capacity_provider_strategy:
      - capacity_provider: FARGATE
        base: 1
        weight: 1
      - capacity_provider: FARGATE_SPOT
        weight: 100
    purge_capacity_providers: true

- name: Cluster deletion
  community.aws.ecs_cluster:
    name: default
    state: absent

- name: Wait for register
  community.aws.ecs_cluster:
    name: "{{ new_cluster }}"
    state: has_instances
    delay: 10
    repeat: 10
  register: task_output
a  
activeServicesCount:
    description: how many services are active in this cluster
    returned: 0 if a new cluster
    type: int
capacityProviders:
    version_added: 5.2.0
    description: list of capacity providers used in this cluster
    returned: always
    type: list
defaultCapacityProviderStrategy:
    version_added: 5.2.0
    description: list of capacity provider strategies used in this cluster
    returned: always
    type: list
clusterArn:
    description: the ARN of the cluster just created
    type: str
    returned: 0 if a new cluster
    sample: arn:aws:ecs:us-west-2:123456789012:cluster/test-cluster
clusterName:
    description: name of the cluster just created (should match the input argument)
    type: str
    returned: always
    sample: test-cluster
pendingTasksCount:
    description: how many tasks are waiting to run in this cluster
    returned: 0 if a new cluster
    type: int
registeredContainerInstancesCount:
    description: how many container instances are available in this cluster
    returned: 0 if a new cluster
    type: int
runningTasksCount:
    description: how many tasks are running in this cluster
    returned: 0 if a new cluster
    type: int
status:
    description: the status of the new cluster
    returned: always
    type: str
    sample: ACTIVE
    N)camel_dict_to_snake_dict)snake_dict_to_camel_dict)AnsibleCommunityAWSModulec                   6    e Zd ZdZd Zd	dZd Zd Zd Zd Z	y)
EcsClusterManagerzHandles ECS Clustersc                     || _         	 |j                  d      | _        y # t        j                  j
                  t        j                  j                  f$ r}|j                  |d       Y d }~y d }~ww xY w)NecszFailed to connect to AWSmsg)moduleclientr	   botocore
exceptionsClientErrorBotoCoreErrorfail_json_aws)selfr   es      m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/ecs_cluster.py__init__zEcsClusterManager.__init__   se    	D}}U+DH##//1D1D1R1RS 	D  (B CC	Ds     7A4A//A4c                 B    |D ]  }||   j                  |      s|c S  y )N)endswith)r   array_of_clusterscluster_name
field_namecs        r   find_in_arrayzEcsClusterManager.find_in_array   s-    " 	A}%%l3	     c                    | j                   j                  |g      }t        |d         dkD  r!| j                  |d   |d      }|r	|d   dk(  ry t        |d         dkD  r| j                  |d   |      }|r|S t	        d| d	      )
N)clustersfailuresr   arnreasonMISSINGr    z#Unknown problem describing cluster .)r	   describe_clusterslenr   	Exception)r   r   responser   s       r   describe_clusterz"EcsClusterManager.describe_cluster   s    88--~-Fx
#$q(""8J#7uMAQx[I-x
#$q(""8J#7FA=l^1MNNr   c                     t        |      }|rt        |      |d<   |rt        |      |d<    | j                  j                  di |}|d   S )N)clusterNamecapacityProvidersdefaultCapacityProviderStrategycluster )dictr   r	   create_clusterr   r   capacity_providerscapacity_provider_strategyparamsr)   s         r   r2   z EcsClusterManager.create_cluster   sX    ,/*BCU*VF&'%8PQk8lF45*488**4V4	""r   c                     t        |      }|rt        |      |d<   ng |d<   |rt        |      |d<   ng |d<    | j                  j                  di |}|d   S )Nr/   r-   r.   r/   r0   )r1   r   r	   put_cluster_capacity_providersr3   s         r   update_clusterz EcsClusterManager.update_cluster   sn    l+*BCU*VF&'*,F&'%8PQk8lF458:F45:488::DVD	""r   c                 :    | j                   j                  |      S )Nr8   )r	   delete_cluster)r   r,   s     r   r<   z EcsClusterManager.delete_cluster   s    xx&&{&;;r   N)
clusterArn)
__name__
__module____qualname____doc__r   r   r*   r2   r:   r<   r0   r   r   r   r      s%    DO##<r   r   c                  ^   t        t        dg d      t        dd      t        ddd	      t        ddd	      t        dd
d	      t        ddd      t        dddt        t        d      t        d      t        dd                        } ddgg}t        | d|      }t        |      }	 |j                  |j                  d         }t        d      }|j                  d   dk(  r	|j                  d   }|j                  d   }|j                  d   }	rd|v r|d   d k(  r|d!   }
|d"   }|g }d}|	/|	D ]  }t        |      |vsd} |D ]  }t        |      |	vsd} n	|	|g k7  rd}|sd}|
}|}	||
k7  s|r7|j                  s$|j                  |j                  d   ||	#      |d$<   d|d%<   n||d$<   n|j                  s$|j                  |j                  d   ||	#      |d$<   d|d%<   n\|j                  d   d&k(  rLsnF||d$<   d|v r|d   d'k(  rd|d%<   n.|j                  s|j                  |j                  d          d|d%<   n|j                  d   d(k(  rs%|j                  d)|j                  d   z   d*z          |j                  d+   }|j                  d,   }t        j                  |       d}t!        |      D ]F  }|j                  |j                  d         }|d-   }|dkD  rd|d%<    nt        j                  |       H |dk(  r7|d.z
  u r0|j                  d/t        |      z   d0z   t        |      z   d1z           |j"                  d2i | y # t
        $ r<}|j                  d|j                  d   z   dz   t        |      z          Y d }~d }~ww xY w)3NT)presentabsenthas_instances)requiredchoicesstr)rF   typeFint
   )rF   rI   defaultboollist)rF   rI   elementsr1   )rI   r   )rI   rL   )capacity_providerweightbase)rF   rI   rO   options)statenamedelayrepeatpurge_capacity_providersr4   r5   rT   rU   )argument_specsupports_check_moderequired_togetherzException describing cluster 'z': r
   )changedrC   rX   r4   r5   statusACTIVEr-   r.   )r   r4   r5   r/   r\   rD   INACTIVErE   z	Cluster 'z not found.rV   rW   !registeredContainerInstancesCount   z(Cluster instance count still zero after z
 tries of z seconds each.r0   )r1   AnsibleAWSModuler   r*   r6   r(   	fail_jsonrH   r   r   
check_moder:   r2   r<   timesleeprange	exit_json)rY   r[   r   cluster_mgrexistingr   resultsrX   requested_cprequested_cpsexisting_cpexisting_cpscps_update_neededstrategyrV   rW   countis                     r   mainrt      sL   D*PQ4e,Er:U;!%u64!PVeL#'"&E"2'ua0		$
M$ "6*+# +F $F+Kh//f0EF 5!G}}W*#)==1K#L }}%9:&BCH,(1Cx1O"#67K#$EFL#! !&( - 1H/9M,0)1 !- 1H/9N,0)1 &<2+=$(! ,$)!* , {*.?(()4)C)C%+]]6%:+73@ *D *GI&
 &*	"%-	"$$%0%?%?!'v!6'3/< &@ &	"
 "&GI 
w	8	+ "*GI8#(:j(H%*	"((..v}}V/DE%)	"	w	?	2v}}V/D!D}!TU g&x(

5v 	A"33FMM&4IJH@AEqy%)	"JJu	 A:!vz/>f+ e* #	#   Fw  h=f@UUX]]`cde`ffgghs   2M' '	N,01N''N,__main__)DOCUMENTATIONEXAMPLESRETURNre   r   ImportError0ansible.module_utils.common.dict_transformationsr   r   >ansible_collections.community.aws.plugins.module_utils.modulesr   rb   r   rt   r>   r0   r   r   <module>r|      sr   L\#J*
X 	 V U x4< 4<n} @ zF A  		s   = AA