
    Vh.                        d Z dZdZddlmZ ddlmZ 	 ddlZddlmZ ddl	m
Z
 dd	lmZ  e
j                  d
d
dg      d        Zd Z e
j                  d
d
dg      d        ZddZ e
j                  d
d
      d        Zd Z e
j                  d
d
      d        Zd Zd Zd Zedk(  r e        yy# e$ r Y w xY w)a	  
---
module: elb_target
version_added: 1.0.0
short_description: Manage a target in a target group
description:
  - Used to register or deregister a target in a target group.
author:
  - "Rob White (@wimnat)"
options:
  deregister_unused:
    description:
      - The default behaviour for targets that are unused is to leave them registered.
      - If instead you would like to remove them set I(deregister_unused=true).
    default: false
    type: bool
  target_az:
    description:
      - An Availability Zone or C(all). This determines whether the target receives traffic from the load balancer nodes in the specified
        Availability Zone or from all enabled Availability Zones for the load balancer. This parameter is not supported if the target
        type of the target group is instance.
    type: str
  target_group_arn:
    description:
      - The Amazon Resource Name (ARN) of the target group.
      - Mutually exclusive of I(target_group_name).
    type: str
  target_group_name:
    description:
      - The name of the target group.
      - Mutually exclusive of I(target_group_arn).
    type: str
  target_id:
    description:
      - The ID of the target.
    required: true
    type: str
  target_port:
    description:
      - The port on which the target is listening. You can specify a port override. If a target is already registered,
        you can register it again using a different port.
      - The default port for a target is the port for the target group.
    required: false
    type: int
  target_status:
    description:
      - Blocks and waits for the target status to equal given value. For more detail on target status see
        U(https://docs.aws.amazon.com/elasticloadbalancing/latest/application/target-group-health-checks.html#target-health-states)
    required: false
    choices: [ 'initial', 'healthy', 'unhealthy', 'unused', 'draining', 'unavailable' ]
    type: str
  target_status_timeout:
    description:
      - Maximum time in seconds to wait for I(target_status) change.
    required: false
    default: 60
    type: int
  state:
    description:
      - Register or deregister the target.
    required: true
    choices: [ 'present', 'absent' ]
    type: str

notes:
  - If you specified a port override when you registered a target, you must specify both the target ID and the port when you deregister it.

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: Register an IP address target to a target group
  community.aws.elb_target:
    target_group_name: myiptargetgroup
    target_id: i-1234567
    state: present

- name: Register an instance target to a target group
  community.aws.elb_target:
    target_group_name: mytargetgroup
    target_id: i-1234567
    state: present

- name: Deregister a target from a target group
  community.aws.elb_target:
    target_group_name: mytargetgroup
    target_id: i-1234567
    state: absent

# Modify a target to use a different port
- name: Register a target to a target group
  community.aws.elb_target:
    target_group_name: mytargetgroup
    target_id: i-1234567
    target_port: 8080
    state: present

    )sleep)timeN)camel_dict_to_snake_dict)AWSRetry)AnsibleCommunityAWSModule
   TargetGroupNotFound)retriesdelaycatch_extra_error_codesc                 (    | j                  |g      S )N)Names)describe_target_groups)
connectiontg_names     l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/elb_target.py#describe_target_groups_with_backoffr      s    ,,G9,==    c                     	 t        | |      }d   d   d   }|S # t        j                  j                  t        j                  j                  f$ r }|j                  |d|        Y d }~_d }~ww xY w)Nz Unable to describe target group msgTargetGroupsr   TargetGroupArn)r   botocore
exceptionsClientErrorBotoCoreErrorfail_json_aws)r   moduler   responseetg_arns         r   convert_tg_name_to_arnr$      s    R6z7K n%a()9:FM ++X-@-@-N-NO RQ&Fwi$PQQRs    7A2A--A2c                 8    |g }n|g}| j                  ||      S N)r   Targets)describe_target_health)r   r#   targettgs       r   describe_targets_with_backoffr+      s)    ~X,,FB,OOr   c                     	 t        | ||      d   }|si S |d   S # t        j                  j                  t        j                  j                  f$ r }|j                  |d|        Y d}~yd}~ww xY w)z
    Describe targets in a target group

    :param module: ansible module object
    :param connection: boto3 connection
    :param tg_arn: target group arn
    :param target: dictionary containing target id and port
    :return:
    TargetHealthDescriptionsr   z,Unable to describe target health for target r   N)r+   r   r   r   r   r   )r   r    r#   r)   targetsr"   s         r   describe_targetsr/      s}    ]/
FFKLfgIqz++X-@-@-N-NO ]Q&RSYRZ$[\\]s     7A2A--A2)r   r   c                 ,    | j                  ||g       y r&   )register_targetsr   target_group_arnr)   s      r   register_target_with_backoffr4      s    /?&Rr   c                 \   |j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }d}|s&t        | ||j                   j                  d            }t        |	      }	|r||	d
<   |r||	d<   t	        | |||	      }
d|
d   v r-|
d   d   dk(  r"	 t        | ||	       d}|rt        | |||	||       t	        | ||      }|j                  |t        |      |       y# t        j                  j                  t        j                  j                  f$ r }|j                  |d|	        Y d}~}d}~ww xY w)z
    Registers a target to a target group

    :param module: ansible module object
    :param connection: boto3 connection
    :return:
    	target_azr3   	target_idtarget_porttarget_statustarget_status_timeoutFtarget_group_nameIdAvailabilityZonePortReasonTargetHealthTarget.NotRegisteredTUnable to deregister target r   Nchangedtarget_health_descriptionsr3   )paramsgetr$   dictr/   r4   target_status_checkr   r   r   r   r   	exit_jsonr   )r   r    r6   r3   r7   r8   r9   r:   rE   r)   target_descriptionr"   target_descriptionss                r   register_targetrN      s    !!+.I}}(();<!!+.I--##M2KMM%%o6M"MM--.EFG1*ffmmFWFWXkFlmYF%.!"$v)*f>NPVW%n55n-h7;QQU,Z9I6R '"F,<fmUj +:v?OP
#;<O#P)   ''33X5H5H5V5VW U$$Q.J6(,S$TTUs   !E 7F+F&&F+c                 ,    | j                  ||g       y r&   )deregister_targetsr2   s      r   deregister_target_with_backoffrQ      s    !!1AF8!Tr   c                    |j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|j                   j                  d      }d}|s&t        | ||j                   j                  d            }t        |	      }	|r||	d
<   t	        | |||	      }
|
d   d   }|
d   j                  d      }d}|r|dk(  r|dk7  r	d}n|dvrd}|r	 t        | ||	       d}n|dk7  r|dk7  r|j                  d       |rt        | |||	||       t	        | ||      }|j                  |t        |      |       y# t        j                  j                  t        j                  j                  f$ r |j                  d|	        Y w xY w)z
    Deregisters a target to a target group

    :param module: ansible module object
    :param connection: boto3 connection
    :return:
    deregister_unusedr3   r7   r8   r9   r:   Fr;   r<   r?   rA   Stater@   unusedrB   T)rU   drainingrC   r   rV   zYour specified target has an 'unused' state but is still registered to the target group. To force deregistration use the 'deregister_unused' option.)warningrD   N)rG   rH   r$   rI   r/   rQ   r   r   r   r   	fail_jsonwarnrJ   rK   r   )r   r    rS   r3   r7   r8   r9   r:   rE   r)   rL   current_target_statecurrent_target_reasonneeds_deregisterrM   s                  r   deregister_targetr]      s    ))*=>}}(();<!!+.I--##M2KMM%%o6M"MM--.EFG1*ffmmFWFWXkFlmYF$v)*f>NPVW-n=gF.~>BB8L1X= $::#	%;	;	J*:7GPG !$::?SWa?aKKP  
 J0@&-Yno +:v?OP
#;<O#P)   ##//1D1D1R1RS 	J#?x!HI	Js   $F AGGc                     d}|t               z   }t               |k  r5t        | |||      d   d   }||k(  rd}nt        d       t               |k  r5|s|j                  d| d d	       y y )
NFrA   rT   T   zStatus check timeout of z exceeded, last status was z: r   )r   r/   r   rX   )	r   r    r3   r)   r9   r:   reached_statetimeouthealth_states	            r   rJ   rJ   (  s    M#df,G
&7
'
F<LfUVdefmn=( Ma &7
 *+@*AA\]i\jjlm 	 	
 r   c                  X   t        t        dd      t        d      t        d      t        d      t        dd      t        d      t        g d	d
      t        dd      t        dddgd      	      } t        | ddgg      }	 |j                  d      }|j                  j                  d      }|dk(  rt        |       y t        |       y # t        j                  j
                  t        j                  j                  f$ r}|j                  |d       Y d }~d }~ww xY w)NboolF)typedefaultstr)re   T)re   requiredint)initialhealthy	unhealthyrU   rV   unavailable)choicesre   <   presentabsent)rh   rn   re   )	rS   r6   r3   r;   r7   r8   r9   r:   stater3   r;   )argument_specmutually_exclusiveelbv2zFailed to connect to AWSr   rr   )rI   AnsibleAWSModuleclientr   r   r   r   r   rG   rH   rN   r]   )rs   r    r   r"   rr   s        r   mainrx   7  s   FE:E"5)E*ED1e$\ch
 #r:D9h*?eLM #/1DEFF
@]]7+
 MMg&E	
F+*f- ++X-@-@-N-NO @Q$>??@s   
C 7D)D$$D)__main__)N)DOCUMENTATIONEXAMPLESRETURNr   r   r   ImportError0ansible.module_utils.common.dict_transformationsr   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   >ansible_collections.community.aws.plugins.module_utils.modulesr   rv   jittered_backoffr   r$   r+   r/   r4   rN   rQ   r]   rJ   rx   __name__ r   r   <module>r      s#  GR<
  	 V P x 2RJ_I`a> b> 2RJ_I`aP bP]( 2R0S 1S/d 2R0U 1U;|
.B zF E  		s   B7 7B?>B?