
    Vh:4                         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 Zd Zd	 Zd
 Zd ZddZddZd Zd Zd Zedk(  r e        yy# e$ r Y Aw xY w)a  
module: directconnect_gateway
author:
  - Gobin Sougrakpam (@gobins)
version_added: 1.0.0
short_description: Manage AWS Direct Connect gateway
description:
  - Creates AWS Direct Connect Gateway.
  - Deletes AWS Direct Connect Gateway.
  - Attaches Virtual Gateways to Direct Connect Gateway.
  - Detaches Virtual Gateways to Direct Connect Gateway.
  - Prior to release 5.0.0 this module was called C(community.aws.aws_direct_connect_gateway).
    The usage did not change.
options:
  state:
    description:
        - Set I(state=present) to ensure a resource is created.
        - Set I(state=absent) to remove a resource.
    default: present
    choices: [ "present", "absent"]
    type: str
  name:
    description:
        - Name of the Direct Connect Gateway to be created or deleted.
    type: str
  amazon_asn:
    description:
        - The Amazon side ASN.
        - Required when I(state=present).
    type: str
  direct_connect_gateway_id:
    description:
        - The ID of an existing Direct Connect Gateway.
        - Required when I(state=absent).
    type: str
  virtual_gateway_id:
    description:
        - The VPN gateway ID of an existing virtual gateway.
    type: str
  wait_timeout:
    description:
        - How long to wait for the association to be deleted.
    type: int
    default: 320
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
a  
- name: Create a new direct connect gateway attached to virtual private gateway
  community.aws.directconnect_gateway:
    state: present
    name: my-dx-gateway
    amazon_asn: 7224
    virtual_gateway_id: vpg-12345
  register: created_dxgw

- name: Create a new unattached dxgw
  community.aws.directconnect_gateway:
    state: present
    name: my-dx-gateway
    amazon_asn: 7224
  register: created_dxgw
a  
result:
  description:
    - The attributes of the Direct Connect Gateway
  type: complex
  returned: I(state=present)
  contains:
    amazon_side_asn:
      description: ASN on the amazon side.
      type: str
    direct_connect_gateway_id:
      description: The ID of the direct connect gateway.
      type: str
    direct_connect_gateway_name:
      description: The name of the direct connect gateway.
      type: str
    direct_connect_gateway_state:
      description: The state of the direct connect gateway.
      type: str
    owner_account:
      description: The AWS account ID of the owner of the direct connect gateway.
      type: str
    N)camel_dict_to_snake_dict)AnsibleCommunityAWSModulec                     	 | j                  |      }d   r|d   d   S y # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~]d }~ww xY w)NdirectConnectGatewayIdz$Failed to fetch gateway information.msgdirectConnectGatewaysr   ) describe_direct_connect_gatewaysbotocore
exceptionsBotoCoreErrorClientErrorfail_json_aws)client
gateway_idmodulerespes        w/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/directconnect_gateway.pydx_gateway_infor   n   s    L66#- 7 

 #$+,Q// % --x/B/B/N/NO LQ$JKKLs   " 7A6A11A6c                 l   d}d}d}t        d|      D ]C  }	 t        | |||      }	|	d   r(|	d   d   d   |k(  rd} nt        j                  |       nd} nE 	}||fS # t        j
                  j                  t        j
                  j                  f$ r}
|j                  |
d	
       Y d }
~
d }
~
ww xY w)N      Fr   r   virtual_gateway_id directConnectGatewayAssociationsassociationStateTz-Failed while waiting for gateway association.r   )	rangecheck_dxgw_associationtimesleepr   r   r   r   r   )r   r   r   r   statuspolling_increment_secsmax_retriesstatus_achievedxresponser   results               r   wait_for_statusr*   y   s    KO1k" Y	Y-%#5	H :;>?BCUVZ``&*O

12"& 3Y& FF""	 ##1183F3F3R3RS 	Y  (W XX	Ys   $AA7B3B..B3c                    t               }|j                  j                  d      |d<   	 | j                  ||d         }t        | |||d   d      \  }}|s|j                  d       }|S # t        j
                  j                  t        j
                  j                  f$ r}|j                  |d       Y d }~{d }~ww xY w)Nr   r   virtualGatewayIdzFailed to associate gatewayassociatingzFError waiting for dxgw to attach to vpg - please check the AWS consoler   )dictparamsget)create_direct_connect_gateway_associationr   r   r   r   r   r*   	fail_json)	r   r   r   r0   r(   r   r&   dxgwr)   s	            r    associate_direct_connect_gatewayr5      s    VF#)==#4#45I#JF ?CC#-#$89 D 
 ,FFJOcHdfstOTefFM --x/B/B/N/NO ?Q =>>?s   A- -7C $B;;C c                 ,   	 | j                  ||      }t        | |||d      \  }}|s|j                  d       }|S # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~yd }~ww xY w)Nr,   z%Failed to delete gateway association.r   disassociatingzIError waiting for  dxgw to detach from vpg - please check the AWS console))delete_direct_connect_gateway_associationr   r   r   r   r   r*   r3   )	r   r   r   r   r(   r   r&   r4   r)   s	            r   delete_associationr9      s    MCC#-/ D 
 ,FFJHZ\lmOThiFM --x/B/B/N/NO MQ$KLLMs   ? 7B6BBc                    t               }|j                  j                  d      |d<   |j                  j                  d      |d<   	 | j                  |d   t	        |d               }|}|S # t
        j                  j                  t
        j                  j                  f$ r }|j                  |d       Y d }~}|S d }~ww xY w)Nname
amazon_asn)directConnectGatewayNameamazonSideAsnz(Failed to create direct connect gateway.r   )
r/   r0   r1   create_direct_connect_gatewayintr   r   r   r   r   )r   r   r0   r(   r   r)   s         r   create_dx_gatewayrA      s    VF]]&&v.F6N!==,,\:F<P77%+F^f\23 8 
 FM	 --x/B/B/N/NO PQ$NOOFM	Ps   "A. .7C%C  Cc                    t               }t               }|||d<   	 	  | j                  d
i |}|j                  d          d|v r	|d   |d<   nn6|g k7  r3d}|D ],  }|j                  j                  d      |d   k(  s%|d	z  }|c S  y # t        j                  j
                  t        j                  j                  f$ r}|j                  |d       Y d }~d }~ww xY w)Nr   zFailed to describe gatewaysr   r
   	nextTokenr   r;   r=       )r/   listr   r   r   r   r   r   extendr0   r1   )	r   r   r   r0   gatewaysr   r   countgateways	            r   find_dx_gatewayrK      s   VFvH+5'(
	G:6::DVDD 	456$"&{"3F;  2~ 	G}}  (G4N,OO
	  ##1183F3F3R3RS 	G  (E FF	Gs   B 7C CC c                    	 || j                  |      }|S | j                  ||      }	 |S # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~S d }~ww xY w)Nr   r,   z#Failed to check gateway associationr   ),describe_direct_connect_gateway_associationsr   r   r   r   r   )r   r   r   r   r   r   s         r   r    r       s    K%FF'1 G D K FF'1!3 G D K --x/B/B/N/NO KQ$IJJKKs   . . 7B%A>>Bc                    d}t               }t               }|j                  j                  d      |d<   |j                  j                  d      |d<   |j                  j                  d      |d<   t        | |      }|}|d   dk7  ru|d   }|d   r't	        | |||d         }|d	   st        | ||       d
}nt        | |      }t	        | ||      }|d	   rb|d	   D ]  }|d   dvst        | |||d           n=t        | |      }	d
}|	d   d   }|d   r"t        | ||       t	        | ||      }|d	   rd
}t        | ||      }||fS )NFr;   r<   r   directConnectGatewayStatedeletedr   r   r   Tr   r   r7   disassociatedr-   directConnectGateway)	r/   r0   r1   rK   r    r5   r9   rA   r   )
r   r   changedr0   r)   existing_dxgwr   r   associationnew_dxgws
             r   ensure_presentrY      s    GVFVF]]&&v.F6N!==,,\:F<#)==#4#45I#JF  $FF3M ]3N%OS\%\"#;<
&'):&QeJfD :;0L ,FF;M)&&ZPD67#'(J#K K"#56>aa*""'1/:;M/N	 %VV4456NO
 &',VVZH)%D
 67VZ8FF?    c                    d}|j                   j                  d      }t        | ||      }|t        | ||      }|d   r$|d   D ]  }|d   dvst	        | |||d           t        j
                         |j                   j                  d	      z   }t        j
                         |k  rEt        | ||      }|d   g k7  rt        j                  d
       nnt        j
                         |k  rE	 | j                  |      }|S |S # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~|S d }~ww xY w)NFdirect_connect_gateway_idrQ   r   r   rR   r-   r   wait_timeoutr   r   zFailed to delete gatewayr   )r0   r1   rK   r    r9   r!   r"   delete_direct_connect_gatewayr   r   r   r   r   )	r   r   rU   dx_gateway_idrV   r   rW   timeoutr   s	            r   ensure_absentra   -  sf    GMM%%&ABM#FFMBM %ffO23#$FG 12:]]&#0+67I+J	 ))+ 1 1. AAiikG#)&&]SD672=

2 iikG#	D77'4 8 D
 N7N ##1183F3F3R3RS 	D  (B CCN	Ds   1D 7E>EEc                  \   t        t        dddg      t               t               t               t               t        dd            } ddd	d
gfdddgfg}t        | |      }|j                  j                  d      }	 |j	                  d      }|dk(  rt        |      \  }}n|dk(  rt        |      }i } |j                  ddit               y # t
        j                  j                  t
        j                  j                  f$ r}|j                  |d       Y d }~d }~ww xY w)Npresentabsent)defaultchoicesr@   i@  )typere   )stater;   r<   r   r\   r]   rh   r;   r<   r\   )argument_specrequired_ifdirectconnectzFailed to connect to AWSr   rU   rE   )r/   AnsibleAWSModuler0   r1   r   r   r   r   r   r   rY   ra   	exit_jsonr   )ri   rj   r   rh   r   r   rU   resultss           r   mainro   Q  s0   9y(.CDV66"&&uc2M Y(>?'8VqUrAstK#F
 MMg&E@/ 	+FF;'	(	/FJWJ(@(IJ ++X-@-@-N-NO @Q$>??@s    C 7D+D&&D+__main__)N)DOCUMENTATIONEXAMPLESRETURNr!   r   ImportError0ansible.module_utils.common.dict_transformationsr   >ansible_collections.community.aws.plugins.module_utils.modulesr   rl   r   r*   r5   r9   rA   rK   r    rY   ra   ro   __name__rE   rZ   r   <module>rx      s   0d"
0 	 V x0#8&" 0 9x!HK> zF U  		s   A
 
AA