
    Vh&                         d Z dZdZ	 ddlZddlmZ ddlmZ ddlm	Z
  G d d	e      Z G d
 de      Zd Zedk(  r e        yy# e$ r Y 4w xY w)ay  
---
module: ecs_attribute
version_added: 1.0.0
short_description: manage ecs attributes
description:
  - Create, update or delete ECS container instance attributes.
author:
  - Andrej Svenke (@anryko)
options:
    cluster:
        description:
            - The short name or full Amazon Resource Name (ARN) of the cluster
              that contains the resource to apply attributes.
        required: true
        type: str
    state:
        description:
            - The desired state of the attributes.
        required: false
        default: present
        choices: ['present', 'absent']
        type: str
    attributes:
        description:
            - List of attributes.
        required: true
        type: list
        elements: dict
        suboptions:
            name:
                description:
                    - The name of the attribute. Up to 128 letters (uppercase and lowercase),
                      numbers, hyphens, underscores, and periods are allowed.
                required: true
                type: str
            value:
                description:
                    - The value of the attribute. Up to 128 letters (uppercase and lowercase),
                      numbers, hyphens, underscores, periods, at signs (@), forward slashes, colons,
                      and spaces are allowed.
                required: false
                type: str
    ec2_instance_id:
        description:
            - EC2 instance ID of ECS cluster container instance.
        required: true
        type: str
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: Set attributes
  community.aws.ecs_attribute:
    state: present
    cluster: test-cluster
    ec2_instance_id: "{{ ec2_id }}"
    attributes:
      - flavor: test
      - migrated
  delegate_to: localhost

- name: Delete attributes
  community.aws.ecs_attribute:
    state: absent
    cluster: test-cluster
    ec2_instance_id: "{{ ec2_id }}"
    attributes:
      - flavor: test
      - migrated
  delegate_to: localhost
a  
attributes:
    description: attributes
    type: complex
    returned: always
    contains:
        cluster:
            description: cluster name
            type: str
        ec2_instance_id:
            description: ec2 instance id of ecs container instance
            type: str
        attributes:
            description: list of attributes
            type: list
            elements: dict
            contains:
                name:
                    description: name of the attribute
                    type: str
                value:
                    description: value of the attribute
                    returned: if present
                    type: str
    N)ClientError)EndpointConnectionError)AnsibleCommunityAWSModulec                   R    e Zd ZdZd Zd ZeZd Zed        Z	d Z
ddZdd	Zd
 Zy)EcsAttributeszHandles ECS Cluster Attributec                 p    || _         | j                  |      r|| _        y | j                  |      | _        y N)module_validate_attrs_parse_attrs
attributes)selfr
   r   s      o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/ecs_attribute.py__init__zEcsAttributes.__init__|   s/    (,(<(<Z(H*dN_N_`jNk    c                 ,    t        | j                        S r	   )boolr   r   s    r   __bool__zEcsAttributes.__bool__       DOO$$r   c                 ,    t        | j                        S r	   )iterr   r   s    r   __iter__zEcsAttributes.__iter__   r   r   c                 &    t        d | D              S )Nc              3   R   K   | ]  }t        |j                               d v  ! yw))namevalue)r   r   N)tuplekeys).0attrs     r   	<genexpr>z0EcsAttributes._validate_attrs.<locals>.<genexpr>   s"     bTX5%)OObs   %')all)attrss    r   r   zEcsAttributes._validate_attrs   s    b\abbbr   c                    g }|D ]  }t        |t              rjt        |      dk7  r(| j                  j	                  dt        |              t        |j                               d   \  }}|j                  ||d       }t        |t
              r|j                  |d d       | j                  j	                  dt        |               |S )N   zIncorrect attribute format - msgr   r   zIncorrect attributes format - )	
isinstancedictlenr
   	fail_jsonstrlistitemsappend)r   r%   attrs_parsedr"   r   r   s         r   r   zEcsAttributes._parse_attrs   s     		YD$%t9>KK))0McRVi[.Y)Z"4::<03e##TE$BCD#&##TD$AB%%,J3u:,*W%X		Y r   Nc                 $    d||d}|s|||d<   |S )Ncontainer-instance)
targetTypetargetIdr   r    )r   ecs_arnr   r   
skip_valueattr_objs         r   _setup_attr_objzEcsAttributes._setup_attr_obj   s&    "6GUYZe/ %HWr   c                 f    | j                   D cg c]  } | j                  |fd|i| c}S c c}w )zt
        Returns list of attribute dicts ready to be passed to boto3
        attributes put/delete methods.
        r9   )r   r;   )r   r8   r9   r"   s       r   get_for_ecs_arnzEcsAttributes.get_for_ecs_arn   s9    
 Z^YhYhiQU$$$WLLtLiiis   .c                 t    | j                   D cg c]	  }||vs| }}t        | j                  |      S c c}w )z
        Returns EcsAttributes Object containing attributes which are present
        in self but are absent in passed attrs (EcsAttributes Object).
        )r   r   r
   )r   r%   r"   
attrs_diffs       r   diffzEcsAttributes.diff   s9    
 (,Lt$e:KdL
LT[[*55 Ms   	55)NF)F)__name__
__module____qualname____doc__r   r   __nonzero__r   staticmethodr   r   r;   r=   r@   r7   r   r   r   r   y   sG    'l% K% c cj6r   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)Ec2EcsInstancez$Handle ECS Cluster Remote Operationsc                 4   || _         || _        || _        	 |j                  d      | _        | j                         | _        y # t
        j                  j                  t
        j                  j                  f$ r}|j                  |d       Y d }~ed }~ww xY w)NecszFailed to connect to AWSr(   )r
   clusterec2_idclientrJ   botocore
exceptionsr   BotoCoreErrorfail_json_aws_get_ecs_arnr8   )r   r
   rK   rL   es        r   r   zEc2EcsInstance.__init__   s    	D}}U+DH ((* ##//1D1D1R1RS 	D  (B CC	Ds   A 7B:BBc                     	  j                   j                   j                        d   } j                   j                   j                  |      d   }	 t         fdD              d   }|S # t        t
        f$ r2} j                  j                  dt        |              Y d }~Vd }~ww xY w# t        $ r6  j                  j                  d	t         j                                Y S w xY w)
N)rK   containerInstanceArns)rK   containerInstancesrV   Can't connect to the cluster - r(   c              3   H   K   | ]  }|d    j                   k(  s|  yw)ec2InstanceIdN)rL   )r!   instr   s     r   r#   z.Ec2EcsInstance._get_ecs_arn.<locals>.<genexpr>   s$     bDT/=RVZVaVa=a4bs   ""containerInstanceArnz+EC2 instance Id not found in ECS cluster - )rJ   list_container_instancesrK   describe_container_instancesr   r   r
   r-   r.   nextStopIteration)r   ecs_instances_arnsec2_instancesrS   r8   s   `    r   rR   zEc2EcsInstance._get_ecs_arn   s    	R!%!B!B4<<!B!XYp!q HHAA9K B "$M	ibMbb&G  45 	RKK!!(GAx&P!QQ	R  	iKK!!(STWX\XdXdTeSf&g!h	is*   AA0 B4 0B1?(B,,B14;C32C3c                     	 | j                   j                  | j                  |j                  | j                               y# t
        $ r/}| j                  j                  t        |             Y d}~yd}~ww xY w)z)Puts attributes on ECS container instancerK   r   r(   N)	rJ   put_attributesrK   r=   r8   r   r
   r-   r.   r   r%   rS   s      r   	attrs_putzEc2EcsInstance.attrs_put   s`    	.HH##DLLUEZEZ[_[g[gEh#i 	.KK!!c!f!--	.s   A A 	A;%A66A;c                     	 | j                   j                  | j                  |j                  | j                  d             y# t
        $ r/}| j                  j                  t        |             Y d}~yd}~ww xY w)z/Deletes attributes from ECS container instance.T)r9   rc   r(   N)	rJ   delete_attributesrK   r=   r8   r   r
   r-   r.   re   s      r   attrs_deletezEc2EcsInstance.attrs_delete   sj    	.HH&&1F1Ft||`d1F1e '   	.KK!!c!f!--	.s   AA 	A=%A88A=c           	         |D cg c]
  }d|d   d }}	 |D cg c]4  } | j                   j                  dd| j                  i|d   D ]  }| 6 }}}D cg c]  }|d	   | j                  k(  s| }	}|	D 
cg c]  }
|
d   |
j                  d
d      d }}
t        | j                  |      S c c}w c c}}w # t        $ r2}| j                  j                  dt        |              Y d}~d}~ww xY wc c}w c c}
w )z
        Returns EcsAttributes object containing attributes from ECS container instance with names
        matching to attrs.attributes (EcsAttributes Object).
        r4   r   )r5   attributeNamerK   r   rW   r(   Nr6   r   r   r7   )
rJ   list_attributesrK   r   r
   r-   r.   r8   getr   )r   r%   r"   	attr_objsr:   
attr_foundmatched_ecs_targetsrS   targetmatched_objsmatchresultss               r   attrs_get_by_namez Ec2EcsInstance.attrs_get_by_name   s1   
 fkk]a$84PV<Xk	k	R !*#":$((":":"\4<<"\S["\]i"j#  ## # .Ag6F:DVZ^ZfZfDfgg[ghRWE&MEIIgt4LMhhT[['22 l#
  	RKK!!(GAx&P!QQ	R hhs?   B5C  9B:C  C>4C>>D:C   	C;	(C66C;N)	rA   rB   rC   rD   r   rR   rf   ri   ru   r7   r   r   rH   rH      s    .
+$..3r   rH   c            
         t        t        ddddg      t        dd      t        dd      t        ddd	
            } g dg}t        | d|      }|j                  d   }|j                  d   }|j                  d   }t        |||      }t	        ||      }d|||dgd}|j                  |      }	|j                  d   dk(  r<|j                  |	      }
|
s |j                  di | |j                  |
       d|d<   n<|j                  d   dk(  r*|	s |j                  di | |j                  |	       d|d<    |j                  di | y )NFpresentabsent)requireddefaultchoicesTr.   )ry   typer/   r+   )ry   r|   elements)staterK   ec2_instance_idr   )rK   r   r   )argument_specsupports_check_moderequired_togetherrK   r   r   )changedr   r~   r   r7   )
r+   AnsibleAWSModuleparamsrH   r   ru   r@   	exit_jsonrf   ri   )r   r   r
   rK   r   r   contir%   rt   attrs_presentr?   s              r   mainr      ss   E9y(>STd/d7FVD	M FF# +F mmI&Gmm$56O|,J67O<E&*-E  ##2(
	G ++E2M}}W*ZZ.
F'w'
#!		w	8	+F'w'=)!	Fwr   __main__)DOCUMENTATIONEXAMPLESRETURNrN   botocore.exceptionsr   r   ImportError>ansible_collections.community.aws.plugins.module_utils.modulesr   r   objectr   rH   r   rA   r7   r   r   <module>r      sz   4l0
4	/; y66F 66rE3V E3P3 l zF {  		s   A AA