
    Vh:                         d Z dZdZddlZddl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
 Zd Zd Zd Zd Zd Zedk(  r e        yy# e$ r Y Ew xY w)a!  
---
module: glue_connection
version_added: 1.0.0
short_description: Manage an AWS Glue connection
description:
  - Manage an AWS Glue connection. See U(https://aws.amazon.com/glue/) for details.
  - Prior to release 5.0.0 this module was called C(community.aws.aws_glue_connection).
    The usage did not change.
author:
  - "Rob White (@wimnat)"
options:
  availability_zone:
    description:
      - Availability Zone used by the connection
      - Required when I(connection_type=NETWORK).
    type: str
    version_added: 1.5.0
  catalog_id:
    description:
      - The ID of the Data Catalog in which to create the connection. If none is supplied,
        the AWS account ID is used by default.
    type: str
  connection_properties:
    description:
      - A dict of key-value pairs used as parameters for this connection.
      - Required when I(state=present).
    type: dict
  connection_type:
    description:
      - The type of the connection. Currently, SFTP is not supported.
    default: JDBC
    choices: [ 'CUSTOM', 'JDBC', 'KAFKA', 'MARKETPLACE', 'MONGODB', 'NETWORK' ]
    type: str
  description:
    description:
      - The description of the connection.
    type: str
  match_criteria:
    description:
      - A list of UTF-8 strings that specify the criteria that you can use in selecting this connection.
    type: list
    elements: str
  name:
    description:
      - The name of the connection.
    required: true
    type: str
  security_groups:
    description:
      - A list of security groups to be used by the connection. Use either security group name or ID.
      - Required when I(connection_type=NETWORK).
    type: list
    elements: str
  state:
    description:
      - Create or delete the AWS Glue connection.
    required: true
    choices: [ 'present', 'absent' ]
    type: str
  subnet_id:
    description:
      - The subnet ID used by the connection.
      - Required when I(connection_type=NETWORK).
    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.

# Create an AWS Glue connection
- community.aws.glue_connection:
    name: my-glue-connection
    connection_properties:
      JDBC_CONNECTION_URL: jdbc:mysql://mydb:3306/databasename
      USERNAME: my-username
      PASSWORD: my-password
    state: present

# Create an AWS Glue network connection
- community.aws.glue_connection:
    name: my-glue-network-connection
    availability_zone: us-east-1a
    connection_properties:
      JDBC_ENFORCE_SSL: "false"
    connection_type: NETWORK
    description: Test connection
    security_groups:
      - sg-glue
    subnet_id: subnet-123abc
    state: present

# Delete an AWS Glue connection
- community.aws.glue_connection:
    name: my-glue-connection
    state: absent
a  
connection_type:
    description: The type of the connection.
    returned: when state is present
    type: str
    sample: JDBC
creation_time:
    description: The time this connection definition was created.
    returned: when state is present
    type: str
    sample: "2018-04-21T05:19:58.326000+00:00"
description:
    description: Description of the job being defined.
    returned: when state is present
    type: str
    sample: My first Glue job
last_updated_time:
    description: The last time this connection definition was updated.
    returned: when state is present
    type: str
    sample: "2018-04-21T05:19:58.326000+00:00"
match_criteria:
    description: A list of criteria that can be used in selecting this connection.
    returned: when state is present
    type: list
    sample: []
name:
    description: The name of the connection definition.
    returned: when state is present
    type: str
    sample: my-glue-connection
physical_connection_requirements:
    description: A dict of physical connection requirements, such as VPC and SecurityGroup,
                 needed for making this connection successfully.
    returned: when state is present
    type: dict
    sample: {'subnet-id':'subnet-aabbccddee'}
raw_connection_properties:
    description: A dict of key-value pairs used as parameters for this connection.
    returned: when state is present
    type: dict
    sample: {'JDBC_CONNECTION_URL':'jdbc:mysql://mydb:3306/databasename','USERNAME':'x','PASSWORD':'y'}
    N)camel_dict_to_snake_dict)is_boto3_error_code)%get_ec2_security_group_ids_from_names)AWSRetry)AnsibleCommunityAWSModulec                     |j                   j                  d      }|j                   j                  d      }d|i}|||d<   	  | j                  d
ddi|d   S # t        d	      $ r Y yw xY w)z
    Get an AWS Glue connection based on name. If not found, return None.

    :param connection: AWS boto3 glue connection
    :param module: Ansible module
    :return: boto3 Glue connection dict or None if not found
    name
catalog_idNameN	CatalogId	aws_retryT
ConnectionEntityNotFoundException )paramsgetget_connectionr   )
connectionmoduleconnection_nameconnection_catalog_idr   s        q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/glue_connection.py_get_glue_connectionr      s     mm''/O"MM--l;o&F(3{(z((B4B6B<PP89 s   A A+*A+c                 :   d|vrd|d<   d|vrt               |d<   d|vrt               |d<   g |d   d<   d|d   d<   d| d   v r| d   d   |d   k7  ry	d
| d   v r| d   d
   |d
   k7  ry	d| d   v r| d   d   |d   k7  ry	d| d   v r!t        | d   d         t        |d         k7  ry	d| d   v rod| d   d   v r't        | d   d   d         t        |d   d         k7  ry	d| d   d   v r| d   d   d   |d   d   k7  ry	d| d   d   v r| d   d   d   |d   d   k7  ry	y)aC  
    Compare Glue connection params. If there is a difference, return True immediately else return False

    :param user_params: the Glue connection parameters passed by the user
    :param current_params: the Glue connection parameters currently configured
    :return: True if any parameter is mismatched else False
    Description MatchCriteriaPhysicalConnectionRequirementsSecurityGroupIdListSubnetIdConnectionPropertiesConnectionInputTConnectionTypeAvailabilityZoneF)listdictset)user_paramscurrent_paramss     r   _compare_glue_connection_paramsr*      s    N*(*}%n,*.&''~=;?678RT789NOGI78D 	+.?"@@)*+ABnUkFllK(9::)*+;<O_@``%677)*=9^M=ZZ+&788S%&7>	^O,	->. ';7H+II K0A$BCc$ddil)*+KLMbcj
 @ABWXYjZ +&789YZZ-./OPQ[\>?
KL +.?"@Aa"bb-./OPQcd>?@RST     c                 `   t        j                          }|dz   }d}|t        j                          kD  rNt        | |      }|r|j                  d      r|S t        j                  |       |t        j                          kD  rN|j	                  d|j
                  j                  d              y )N      r   z$Timeout waiting for Glue connection r	   )msg)timer   r   sleep	fail_jsonr   )r   r   
start_timewait_timeoutcheck_intervalglue_connections         r   _await_glue_connectionr7      s    J?LN

$.z6B226:""

>"	 
$ ?@Q@QRX@Y?Z[\r+   c                 l   d}t               }t               |d<   |j                  j                  d      |d   d<   |j                  j                  d      |d   d<   |j                  j                  d      |d   d<   |j                  j                  d	      |j                  j                  d	      |d<   |j                  j                  d      !|j                  j                  d      |d   d<   |j                  j                  d      !|j                  j                  d      |d   d<   |j                  j                  d      |j                  j                  d      t               |d   d<   |j                  j                  d      0t        |j                  j                  d      |      }||d   d   d<   |j                  j                  d      $|j                  j                  d      |d   d   d<   |j                  j                  d      $|j                  j                  d      |d   d   d<   |rPt	        ||      rg	 t        j                  |      }|d   d   |d<   |j                  s | j                  dddi| d}n#	 |j                  s | j                  dddi| d}|r|j                  st        | |      }|r|d   |d<   |j!                  d        |j"                  dd|it%        |xs i dg       y
# t        j                  j                  t        j                  j                  f$ r}|j                  |       Y d
}~d
}~ww xY w# t        j                  j                  t        j                  j                  f$ r}|j                  |       Y d
}~d
}~ww xY w)z
    Create or update an AWS Glue connection

    :param connection: AWS boto3 glue connection
    :param module: Ansible module
    :param glue_connection: a dict of AWS Glue connection parameters or None
    :return:
    Fr"   r	   r   connection_typer#   connection_propertiesr!   r
   Nr   descriptionr   match_criteriar   security_groups	subnet_idr   r   r    availability_zoner$   r   TRawConnectionPropertieschanged)ignore_listr   )r&   r   r   r   r*   copydeepcopy
check_modeupdate_connectionbotocore
exceptionsClientErrorBotoCoreErrorfail_json_awscreate_connectionr7   pop	exit_jsonr   )	r   connection_ec2r   r6   rA   r   security_group_idsupdate_paramses	            r    create_or_update_glue_connectionrS     s    GVF $F(.(9(9&(AFf%28--2C2CDU2VF./8>8I8IJa8bF45}}&2$mm//={}}'339==3D3D]3S !-0}})*65;]]5F5FGW5X !/2}}*+76==;L;L[;Y;eFJf !"BC}}*+7B6==CTCTUfCgiwx]o !"BCDYZ}}%1RXR_R_RcRcdoRp !"BCJO}},-9Z`ZgZgZkZk[
 !"BCDVW
 *6?C( $f 5(56G(H(Pf%((0J00Q4Q=Q	$$$,
,,FtFvFG
 v((0VD5DE[5\1223F 3O4IrXqWrs% ''33X5H5H5V5VW ($$Q''( ##//1D1D1R1RS 	$  ##	$s1   AL "M! 7MMM!7N3N..N3c                    d}d|j                   j                  d      i}|j                   j                  d      |j                   j                  d      |d<   |r#	 |j                  s | j                  d
ddi| d}|j                  |	       y# t        j
                  j                  t        j
                  j                  f$ r}|j                  |       Y d}~`d}~ww xY w)z
    Delete an AWS Glue connection

    :param connection: AWS boto3 glue connection
    :param module: Ansible module
    :param glue_connection: a dict of AWS Glue connection parameters or None
    :return:
    FConnectionNamer	   r
   Nr   r   T)rA   r   )
r   r   rE   delete_connectionrG   rH   rI   rJ   rK   rN   )r   r   r6   rA   r   rR   s         r   delete_glue_connectionrW   P  s     G 1 1& 9:F}}&2$mm//={	$$$,
,,FtFvFG W% ##//1D1D1R1RS 	$  ##	$s   "B 7C#CC#c                  B   t        t        d      t        d      t        d      t        ddg d      t        d      t        dd      t        d	d
      t        dd      t        d	ddgd      t        d      
      } t        | dddgfddg dfgd	      }t        j                  d      }|j	                  d|      }|j	                  d|      }t        ||      }|j                  j                  d      dk(  rt        ||||       y t        |||       y )Nstr)typer&   JDBC)CUSTOMr[   KAFKAMARKETPLACEMONGODBNETWORK)rZ   defaultchoicesr%   )rZ   elementsT)requiredrZ   presentabsent)rd   rb   rZ   )
r?   r
   r:   r9   r;   r<   r	   r=   stater>   rg   r:   r9   r`   )r?   r=   r>   )argument_specrequired_ifsupports_check_mode
   )retriesglue)retry_decoratorec2)
r&   AnsibleAWSModuler   jittered_backoffclientr   r   r   rS   rW   )rh   r   rn   connection_gluerO   r6   s         r   mainrt   j  s   E*U#"/0p
 e$%84e,&59D9h*?eLE"M #i"9!:;	+`a
 !F //;OmmFOmLO]]5/]JN*?FCO}}!Y.(.&RabHr+   __main__)DOCUMENTATIONEXAMPLESRETURNrC   r0   rG   ImportError0ansible.module_utils.common.dict_transformationsr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   7ansible_collections.amazon.aws.plugins.module_utils.ec2r   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   >ansible_collections.community.aws.plugins.module_utils.modulesr   rp   r   r*   r7   rS   rW   rt   __name__r   r+   r   <module>r      s   EN>*
X  	 V \ i P x,:|]AH&4"IJ zF c  		s   A AA