
    Vh\0                     L   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	 ddl
mZ dd	l
mZ dd
l
mZ ddl
mZ ddlmZ ddlmZ ddddgdZd ZddZd Zd Z ej2                  di ed        Zd Z ej2                  di ed        Zd Zedk(  r e        yy# e$ r Y w xY w)a  
---
module: directconnect_connection
version_added: 1.0.0
short_description: Creates, deletes, modifies a DirectConnect connection
description:
  - Create, update, or delete a Direct Connect connection between a network and a specific AWS Direct Connect location.
  - Upon creation the connection may be added to a link aggregation group or established as a standalone connection.
  - The connection may later be associated or disassociated with a link aggregation group.
  - Prior to release 5.0.0 this module was called C(community.aws.aws_direct_connect_connection).
    The usage did not change.
author:
  - "Sloane Hertel (@s-hertel)"
options:
  state:
    description:
      - The state of the Direct Connect connection.
    choices:
      - present
      - absent
    type: str
    required: true
  name:
    description:
      - The name of the Direct Connect connection. This is required to create a
        new connection.
      - One of I(connection_id) or I(name) must be specified.
    type: str
  connection_id:
    description:
      - The ID of the Direct Connect connection.
      - Modifying attributes of a connection with I(forced_update) will result in a new Direct Connect connection ID.
      - One of I(connection_id) or I(name) must be specified.
    type: str
  location:
    description:
      - Where the Direct Connect connection is located.
      - Required when I(state=present).
    type: str
  bandwidth:
    description:
      - The bandwidth of the Direct Connect connection.
      - Required when I(state=present).
    choices:
      - 1Gbps
      - 10Gbps
    type: str
  link_aggregation_group:
    description:
      - The ID of the link aggregation group you want to associate with the connection.
      - This is optional when a stand-alone connection is desired.
    type: str
  forced_update:
    description:
      - To modify I(bandwidth) or I(location) the connection needs to be deleted and recreated.
      - By default this will not happen.  This option must be explicitly set to C(true) to change I(bandwith) or I(location).
    type: bool
    default: false
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
a  

# create a Direct Connect connection
- community.aws.directconnect_connection:
    name: ansible-test-connection
    state: present
    location: EqDC2
    link_aggregation_group: dxlag-xxxxxxxx
    bandwidth: 1Gbps
  register: dc

# disassociate the LAG from the connection
- community.aws.directconnect_connection:
    state: present
    connection_id: dc.connection.connection_id
    location: EqDC2
    bandwidth: 1Gbps

# replace the connection with one with more bandwidth
- community.aws.directconnect_connection:
    state: present
    name: ansible-test-connection
    location: EqDC2
    bandwidth: 10Gbps
    forced_update: true

# delete the connection
- community.aws.directconnect_connection:
    state: absent
    name: ansible-test-connection
a5  
connection:
  description: The attributes of the direct connect connection.
  type: complex
  returned: I(state=present)
  contains:
    aws_device:
      description: The endpoint which the physical connection terminates on.
      returned: when the requested state is no longer 'requested'
      type: str
      sample: EqDC2-12pmo7hemtz1z
    bandwidth:
      description: The bandwidth of the connection.
      returned: always
      type: str
      sample: 1Gbps
    connection_id:
      description: The ID of the connection.
      returned: always
      type: str
      sample: dxcon-ffy9ywed
    connection_name:
      description: The name of the connection.
      returned: always
      type: str
      sample: ansible-test-connection
    connection_state:
      description: The state of the connection.
      returned: always
      type: str
      sample: pending
    loa_issue_time:
      description: The issue time of the connection's Letter of Authorization - Connecting Facility Assignment.
      returned: when the LOA-CFA has been issued (the connection state will no longer be 'requested')
      type: str
      sample: '2018-03-20T17:36:26-04:00'
    location:
      description: The location of the connection.
      returned: always
      type: str
      sample: EqDC2
    owner_account:
      description: The account that owns the direct connect connection.
      returned: always
      type: str
      sample: '123456789012'
    region:
      description: The region in which the connection exists.
      returned: always
      type: str
      sample: us-east-1
    N)BotoCoreError)ClientError)camel_dict_to_snake_dict)DirectConnectError)associate_connection_and_lagdelete_connection)disassociate_connection_and_lag)AWSRetry)AnsibleCommunityAWSModule
      g333333?DirectConnectClientException)retriesdelaybackoffcatch_extra_error_codesc                      t        | |d d      S )NF)connection_idconnection_nameverify)connection_exists)clientr   s     z/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/directconnect_connection.pyconnection_statusr      s    V=RV_dee    c                    i }|r||d<   	   t        j                  di t        | j                        di |}g }g }	t        |j                  dg             dk(  r>|r<|d   d   d   d	k7  r.|j                  |d   d   d          |	j                  |d          |j                  dg       D ]9  }
||
d
   k(  s|
d   d	k7  s|j                  |
d          |	j                  |
       ; |rt        |      dk(  r|d   S |ryt        |	      dk(  rd|	d   iS di iS # t        t
        f$ r/}|rd| }nd}t        |t        j                         |      d }~ww xY w)NconnectionIdz$Failed to describe DirectConnect ID z,Failed to describe DirectConnect connectionsmsglast_traceback	exceptionconnections   r   connectionStatedeletedconnectionNameF
connection )r   jittered_backoffretry_paramsdescribe_connectionsr   r   r   	traceback
format_exclengetappendextend)r   r   r   r   paramsresponseer    matchr(   conns              r   r   r      s   F!.~^Y<8,,<|<V=X=XYc\bc EJ 8<<r*+q0]M"1%&78IELL-03NCDh}56]B/ $d#344>O9PT]9]LLn-.d#$ #e*/Qx		ZA	jm,,"= ;' ^8HC@C S9M9M9O[\]]^s   /D E(*EEc                    |st        d      |||d}|r||d<   	   t        j                  di t        | j                        di |}|d   S # t
        t        f$ r(}t        d| t        j                         |      d }~ww xY w)	Nz<Failed to create a Direct Connect connection: name required.r    )location	bandwidthr'   lagIdz*Failed to create DirectConnect connection r   r   r)   )	r   r   r*   r+   create_connectionr   r   r-   r.   )r   r:   r;   namelag_idr3   r(   r5   s           r   r=   r=      s     %cddF
  w
X>X..>>v?W?WXb[ab
 n%% ;' 
 <TFC$//1
 	

s   /A B #BBc                 .    | d   }| d   }||k7  xs ||k7  S )Nr;   r:   r)   )current_statusr:   r;   current_bandwidthcurrent_locations        r   changed_propertiesrD      s.    &{3%j1	)I-=-IIr   c                 z    d}d|v r||d   k7  rt        | ||d          d}|r|s|rd|vrt        | ||       d}|S )NFr<   )r?   T)r
   r   )r   latest_stater   r?   changeds        r   update_associationsrH      sV    G,6\'-B#B'lSZF[\F7,+F$V]FCNr   c           	          |rSt        | |      d   }t        |||      r |rt        | |       t        | d |||||      S t	        | |||      rd|fS d|fS dt        | ||||      fS )N)r   r(   )r   r   r   r:   r;   r?   forced_updateTF)r   rD   ensure_absentensure_presentrH   r=   )r   r   r   r:   r;   r?   rJ   rF   s           r   rL   rL     s    (}Ml[lHi@]&-0!" /!#+  !}fM&& - &vxOU[\\\r   c                 *    d}|rt        | |       d}|S )NFTr   )r   r   rG   s      r   rK   rK     s    G&-0Nr   c                     t        t        dddg      t               t               t        ddg      t               t               t        dd	
            } t        | dgdg      }|j                  d      }|j                  j	                  d      }	 t        ||j                  j	                  d      |j                  j	                  d            }|s>|j                  j	                  d      r#|j                  d|j                  d    d       |dk(  rt        |||j                  j	                  d      |j                  j	                  d      |j                  j	                  d      |j                  j	                  d      |j                  j	                  d            \  }}t        ||      }n|dk(  rt        ||      }i } |j                   ddit               y # t        $ ry}|j                  rG |j                  d|j                  |j                  dt        |j                  j                         n|j                  |j                         Y d }~d }~ww xY w)NTpresentabsent)requiredchoices1Gbps10Gbps)rR   boolF)typedefault)stater>   r:   r;   link_aggregation_groupr   rJ   )r   r>   )rX   rO   )r:   r;   )argument_specrequired_one_ofrequired_ifdirectconnectrX   r   r>   )r   r   zThe Direct Connect connection z does not exist.r9   r:   r;   rY   rJ   )r   r   r:   r;   r?   rJ   )r    r"   rG   r)   )dictAnsibleAWSModuler   r3   r0   r   	fail_jsonrL   r   rK   r   r!   r    r   r"   r4   	exit_json)rZ   moduler(   rX   r   rG   r4   r5   s           r   mainrc   (  s/   D9h*?@V23#vf6M #23DEF /JMMg&E()fmm&7&7&HZ`ZgZgZkZklrZs
 !2!2?!C4V]]?5S4TTde   I%3+ & 1 1& 9**:6 --++K8}}(()AB$mm//@&"G] )]CHh#J>GH FKWK(@(JK  (F EE** +1;;+?+?@ '(s   D7G) )	I+2A/I&&I+__main__)NNTr)   )DOCUMENTATIONEXAMPLESRETURNr-   botocore.exceptionsr   r   ImportError0ansible.module_utils.common.dict_transformationsr   Bansible_collections.amazon.aws.plugins.module_utils.direct_connectr   r   r	   r
   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   >ansible_collections.community.aws.plugins.module_utils.modulesr   r_   r+   r   r   r=   rD   r*   rH   rL   rK   rc   __name__r)   r   r   <module>ro      s   >@@3
j 	1/ V a k ` n P xcWuVvwf$N&,J *\* + 2 *\* +5Lp zF   		s   B B#"B#