
    VhXZ                     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  eddd      Z e
j(                  d"i ed        Z e
j(                  d"i ed        Zd Z e
j(                  d"i ed        Z e
j(                  d"i ed        Z e
j(                  d"i ed        Z e
j(                  d"i ed        Z e
j(                  d"i ed        Z e
j(                  d"i ed        Zd Zd Zd Z d Z!d Z"d Z#d Z$d  Z%e&d!k(  r e%        yy# e$ r Y &w xY w)#a  
---
module: dms_endpoint
version_added: 1.0.0
short_description: Creates or destroys a data migration services endpoint
description:
  - Creates or destroys a data migration services endpoint,
    that can be used to replicate data.
options:
    state:
      description:
        - State of the endpoint.
      default: present
      choices: ['present', 'absent']
      type: str
    endpointidentifier:
      description:
        - An identifier name for the endpoint.
      type: str
      required: true
    endpointtype:
      description:
        - Type of endpoint we want to manage.
        - Required when I(state=present).
      choices: ['source', 'target']
      type: str
    enginename:
      description:
        - Database engine that we want to use, please refer to
          the AWS DMS for more information on the supported
          engines and their limitations.
        - Required when I(state=present).
      choices: ['mysql', 'oracle', 'postgres', 'mariadb', 'aurora',
                         'redshift', 's3', 'db2', 'azuredb', 'sybase',
                         'dynamodb', 'mongodb', 'sqlserver']
      type: str
    username:
      description:
        - Username our endpoint will use to connect to the database.
      type: str
    password:
      description:
        - Password used to connect to the database
          this attribute can only be written
          the AWS API does not return this parameter.
      type: str
    servername:
      description:
        - Servername that the endpoint will connect to.
      type: str
    port:
      description:
        - TCP port for access to the database.
      type: int
    databasename:
      description:
        - Name for the database on the origin or target side.
      type: str
    extraconnectionattributes:
      description:
        - Extra attributes for the database connection, the AWS documentation
          states " For more information about extra connection attributes,
          see the documentation section for your data store."
      type: str
    kmskeyid:
      description:
        - Encryption key to use to encrypt replication storage and
          connection information.
      type: str
    tags:
      description:
        - A list of tags to add to the endpoint.
      type: dict
    certificatearn:
      description:
        -  Amazon Resource Name (ARN) for the certificate.
      type: str
    sslmode:
      description:
        - Mode used for the SSL connection.
      default: none
      choices: ['none', 'require', 'verify-ca', 'verify-full']
      type: str
    serviceaccessrolearn:
      description:
        -  Amazon Resource Name (ARN) for the service access role that you
           want to use to create the endpoint.
      type: str
    externaltabledefinition:
      description:
        - The external table definition.
      type: str
    dynamodbsettings:
      description:
        - Settings in JSON format for the target Amazon DynamoDB endpoint
          if source or target is dynamodb.
      type: dict
    s3settings:
      description:
        - S3 buckets settings for the target Amazon S3 endpoint.
      type: dict
    dmstransfersettings:
      description:
        - The settings in JSON format for the DMS transfer type of
          source endpoint.
      type: dict
    mongodbsettings:
      description:
        - Settings in JSON format for the source MongoDB endpoint.
      type: dict
    kinesissettings:
      description:
        - Settings in JSON format for the target Amazon Kinesis
          Data Streams endpoint.
      type: dict
    elasticsearchsettings:
      description:
        - Settings in JSON format for the target Elasticsearch endpoint.
      type: dict
    wait:
      description:
        - Whether Ansible should wait for the object to be deleted when I(state=absent).
      type: bool
      default: false
    timeout:
      description:
        - Time in seconds we should wait for when deleting a resource.
        - Required when I(wait=true).
      type: int
    retries:
      description:
        - number of times we should retry when deleting a resource
        - Required when I(wait=true).
      type: int
author:
  - "Rui Moreira (@ruimoreira)"
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
  - amazon.aws.tags
ay  
# Note: These examples do not set authentication details
- name: Endpoint Creation
  community.aws.dms_endpoint:
    state: absent
    endpointidentifier: 'testsource'
    endpointtype: source
    enginename: aurora
    username: testing1
    password: testint1234
    servername: testing.domain.com
    port: 3306
    databasename: 'testdb'
    sslmode: none
    wait: false
aY  
endpoint:
  description:
    - A description of the DMS endpoint.
  returned: success
  type: dict
  contains:
    database_name:
      description:
        - The name of the database at the endpoint.
      type: str
      returned: success
      example: "exampledb"
    endpoint_arn:
      description:
        - The ARN that uniquely identifies the endpoint.
      type: str
      returned: success
      example: "arn:aws:dms:us-east-1:123456789012:endpoint:1234556789ABCDEFGHIJKLMNOPQRSTUVWXYZ012"
    endpoint_identifier:
      description:
        - The database endpoint identifier.
      type: str
      returned: success
      example: "ansible-test-12345678-dms"
    endpoint_type:
      description:
        - The type of endpoint. Valid values are C(SOURCE) and C(TARGET).
      type: str
      returned: success
      example: "SOURCE"
    engine_display_name:
      description:
        - The expanded name for the engine name.
      type: str
      returned: success
      example: "Amazon Aurora MySQL"
    engine_name:
      description:
        - The database engine name.
      type: str
      returned: success
      example: "aurora"
    kms_key_id:
      description:
        - An KMS key ID that is used to encrypt the connection parameters for the endpoint.
      type: str
      returned: success
      example: "arn:aws:kms:us-east-1:123456789012:key/01234567-abcd-12ab-98fe-123456789abc"
    port:
      description:
        - The port used to access the endpoint.
      type: str
      returned: success
      example: 3306
    server_name:
      description:
        - The name of the server at the endpoint.
      type: str
      returned: success
      example: "ansible-test-123456789.example.com"
    ssl_mode:
      description:
        - The SSL mode used to connect to the endpoint.
      type: str
      returned: success
      example: "none"
    tags:
      description:
        - A dictionary representing the tags attached to the endpoint.
      type: dict
      returned: success
      example: {"MyTagKey": "MyTagValue"}
    username:
      description:
        - The user name used to connect to the endpoint.
      type: str
      returned: success
      example: "example-username"
    dms_transfer_settings:
      description:
        - Additional transfer related settings.
      type: dict
      returned: when additional DMS Transfer settings have been configured.
    s3_settings:
      description:
        - Additional settings for S3 endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(s3)
    mongo_db_settings:
      description:
        - Additional settings for MongoDB endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(mongodb)
    kinesis_settings:
      description:
        - Additional settings for Kinesis endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(kinesis)
    kafka_settings:
      description:
        - Additional settings for Kafka endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(kafka)
    elasticsearch_settings:
      description:
        - Additional settings for Elasticsearch endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(elasticsearch)
    neptune_settings:
      description:
        - Additional settings for Amazon Neptune endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(neptune)
    redshift_settings:
      description:
        - Additional settings for Redshift endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(redshift)
    postgre_sql_settings:
      description:
        - Additional settings for PostgrSQL endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(postgres)
    my_sql_settings:
      description:
        - Additional settings for MySQL endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(mysql)
    oracle_settings:
      description:
        - Additional settings for Oracle endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(oracle)
    sybase_settings:
      description:
        - Additional settings for Sybase endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(sybase)
    microsoft_sql_server_settings:
      description:
        - Additional settings for Microsoft SQL Server endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(sqlserver)
    i_b_m_db_settings:
      description:
        - Additional settings for IBM DB2 endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(db2)
    doc_db_settings:
      description:
        - Additional settings for DocumentDB endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(documentdb)
    redis_settings:
      description:
        - Additional settings for Redis endpoints.
      type: dict
      returned: when the I(endpoint_type) is C(redshift)
    N)camel_dict_to_snake_dict)is_boto3_error_code)AWSRetry)ansible_dict_to_boto3_tag_list)boto3_tag_list_to_ansible_dict)compare_aws_tags)AnsibleCommunityAWSModule      g      ?)retriesdelaybackoffc                 \     | j                   di |j                  dg       }t        |      S )checks if the endpoint existsTagList )list_tags_for_resourcegetr   )
connectionparamstagss      n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/dms_endpoint.pydms_describe_tagsr   Z  s1     -:,,6v6::9bID)$//    c                 t    	  | j                   di |}|j                  dd       S # t        d      $ r Y y w xY w)NResourceNotFoundFault	Endpointsr   )describe_endpointsr   r   )r   r   	endpointss      r   dms_describe_endpointsr    a  sI    1J11;F;	 ==d++ 67 s   & 77c                    t        d|g      }	 t        | |g      }sy|d   }	 t        | |d   	      }||d
<   |S # t        j                  j                  t        j                  j
                  f$ r!}t        j                  |d       Y d}~sd}~ww xY w# t        j                  j                  t        j                  j
                  f$ r"}t        j                  |d       Y d}~|S d}~ww xY w)r   zendpoint-idNameValues)Filtersz$Failed to describe the DMS endpoint.msgNr   EndpointArnResourceArnr   z(Failed to describe the DMS endpoint tags)	dictr    botocore
exceptionsClientErrorBotoCoreErrormodulefail_json_awsr   )r   endpoint_identifierendpoint_filterr   eendpointr   s          r   describe_endpointr6   j  s    7J6KLOL*:?PQ	 |HP -9PQ O ++X-@-@-N-NO LQ$JKKL ++X-@-@-N-NO PQ$NOOOPs,   > B 7B5BB7C2C--C2c                 z    t         j                  j                  d      rt        |       S  | j                  di |S )z1deletes the DMS endpoint based on the EndpointArnwaitr   )r0   r   r   delete_dms_endpointdelete_endpointclientr   s     r   dms_delete_endpointr=   ~  s7     }} "6**%v%%///r   c                 &     | j                   di |S )zcreates the DMS endpointr   )create_endpointr;   s     r   dms_create_endpointr@          "6!!+F++r   c                 &     | j                   di |S )zupdates the endpointr   )modify_endpointr;   s     r   dms_modify_endpointrD     rA   r   c                 $    | j                  d      S )Nendpoint_deleted)
get_waiter)r<   s    r   get_endpoint_deleted_waiterrH     s    /00r   c                 &     | j                   di |S Nr   )remove_tags_from_resourcer;   s     r   dms_remove_tagsrL     s    +6++5f55r   c                 &     | j                   di |S rJ   )add_tags_to_resourcer;   s     r   dms_add_tagsrO     s    &6&&000r   c                 0    t        t        | d               S )zReturns boolean based on the existence of the endpoint
    :param endpoint: dict containing the described endpoint
    :return: bool
    r   )boollen)r5   s    r   endpoint_existsrS     s    
 H[)*++r   c                    	 t        |      }t        j                  j                  d      rt | j                  di |}t        |       }|j                  d|gdgt        j                  j                  d      t        j                  j                  d      d       |S  | j                  di |S # t        j                  j                  t        j                  j                  f$ r!}t        j                  |d	
       Y d }~y d }~ww xY w)N)r(   r8   zendpoint-arnr"   timeoutr   )DelayMaxAttempts)r%   WaiterConfigz#Failed to delete the  DMS endpoint.r&   r   )r+   r0   r   r   r:   rH   r8   r,   r-   r.   r/   r1   )r   endpoint_arn
delete_arndelete_outputdelete_waiterr4   s         r   r9   r9     s    Kl3
==V$6J66DDM7
CM"0\NKL'-}}'8'8'CTZTaTaTeTefoTpq   ! -:--;
;;++X-@-@-N-NO KQ$IJJKs   BB3 !B3 37D*DDc                  ~
   t        t        j                  j                  d      t        j                  j                  d      t        j                  j                  d      t        j                  j                  d      t        j                  j                  d      t        j                  j                  d      t        j                  j                  d      t        j                  j                  d      t        j                  j                  d	      
	      } t        j                  j                  d      r"t        j                  j                  d      | d<   t        j                  j                  d      r"t        j                  j                  d      | d<   t        j                  j                  d      r"t        j                  j                  d      | d<   t        j                  j                  d      r"t        j                  j                  d      | d<   t        j                  j                  d      r"t        j                  j                  d      | d<   t        j                  j                  d      r"t        j                  j                  d      | d<   t        j                  j                  d      r"t        j                  j                  d      | d<   t        j                  j                  d      r"t        j                  j                  d      | d<   t        j                  j                  d      r"t        j                  j                  d      | d<   t        j                  j                  d      r"t        j                  j                  d      | d<   t        j                  j                  d      r"t        j                  j                  d      | d<   t        j                  j                  d       r"t        j                  j                  d       | d!<   t        j                  j                  d"      r"t        j                  j                  d"      | d#<   t        j                  j                  d$      r5t        j	                  t        j                  j                  d$            | d$<   t        j                  j                  d%      r"t        j                  j                  d%      | d%<   t        j                  j                  d&      r"t        j                  j                  d&      | d&<   | S )'zJ
    Reads the module parameters and returns a dict
    :return: dict
    endpointidentifierendpointtype
enginenameusernamepassword
servernameportdatabasenamesslmode)	EndpointIdentifierEndpointType
EngineNameUsernamePassword
ServerNamePortDatabaseNameSslModer(   certificatearnCertificateArndmstransfersettingsDmsTransferSettingsextraconnectionattributesExtraConnectionAttributeskmskeyidKmsKeyIdr   TagsserviceaccessrolearnServiceAccessRoleArnexternaltabledefinitionExternalTableDefinitiondynamodbsettingsDynamoDbSettings
s3settings
S3SettingsmongodbsettingsMongoDbSettingskinesissettingsKinesisSettingselasticsearchsettingsElasticsearchSettingsr8   rU   r   )r+   r0   r   r   boolean)endpoint_parameterss    r   create_module_paramsr     s   
 !==,,-AB]]&&~6==$$\2"":."":.==$$\2]]v&]]&&~6!!),
 }}'-3]]->->}-MM*}})*060A0ABR0S,-}}./5;]]5F5FG\5]12}}45;A==;L;LMh;i78}}$*0--*;*;J*GJ'}} &,mm&7&7&?F#}}/06<mm6G6GH^6_23}}239?9J9JKd9e56}}+,28--2C2CDV2W./}}&,2MM,=,=l,KL)}}*+171B1BCT1U-.}}*+171B1BCT1U-.}}017=}}7H7HI`7a34}} &,nnV]]5F5Fv5N&OF#}}#)/):):9)EI&}}#)/):):9)EI&r   c                     t        |       } t               }|j                         D ]:  \  }}|dv r|| vr y| |   |k7  st        | |         j	                         |k7  s: y y)a*  
    Compares the dict obtained from the describe DMS endpoint and
    what we are reading from the values in the template We can
    never compare the password as boto3's method for describing
    a DMS endpoint does not return the value for
    the password for security reasons ( I assume )
    )rx   rk   TF)r+   r   itemsstrlower)param_described	modparams
param_nameparam_values       r   compare_paramsr     sz     ?+O$&I $-??#4 
K--_,:&+5#oj>Y:Z:`:`:bfq:q r   c                    |d   }	 t               }|j                  dd        t        | fd|i|S # t        j                  j
                  t        j                  j                  f$ r"}t        j                  |d       Y d }~y d }~ww xY w)Nr(   rx   zFailed to update DMS endpoint.)r'   r   )	r   poprD   r,   r-   r.   r/   r0   r1   )r   r5   arnr   r4   s        r   modify_dms_endpointr     s    
=
!CU%'

64 ":I3I&II++X-@-@-N-NO UQ$DVTTUs   )1 7B
(BB
c                 $   t         j                  j                  dd       }|y|j                  di       }t        ||t         j                  j                  d            \  }}|s|syt         j                  ry|j                  d      }	 |rt        | ||       |rt        |      }t        | ||       y# t        j                  j                  t        j                  j                  f$ r!}t         j                  |d	       Y d }~yd }~ww xY w)
Nr   F
purge_tagsTr(   )r*   TagKeys)r*   rx   z#Failed to update DMS endpoint tags.r&   )r0   r   r   r   
check_moderL   r   rO   r,   r-   r.   r/   r1   )	r   r5   desired_tagscurrent_tagstags_to_addtags_to_remover   tag_listr4   s	            r   ensure_tagsr     s    ==$$VT2L<<+L"2<v}}O`O`amOn"oK+
,,}
%CKJCP5kBH8D  ++X-@-@-N-NO KQ$IJJKs   +B7 77D.D

Dc                     	 t               }t        | fi |S # t        j                  j                  t        j                  j
                  f$ r!}t        j                  |d       Y d}~yd}~ww xY w)z
    Function to create the dms endpoint
    :param connection: boto3 aws connection
    :return: information about the dms endpoint object
    zFailed to create DMS endpoint.r&   N)r   r@   r,   r-   r.   r/   r0   r1   )r   r   r4   s      r   create_dms_endpointr   <  sf    F%'":888++X-@-@-N-NO FQ$DEEFs    7A0A++A0c            
      ^   t        d:i dt        ddgd      dt        d      dt        d	d
g      dt        g dd      dt               dt        d      dt               dt        d      dt               dt               dt        d      dt        ddg      dt        dd       d!t               d"t        g d#d$      d%t               d&t               d't        d      d(t        d      d)t        d      d*t        d      d+t        d      d,t        d      d-t        dd       d.t        d      d/t        d      } t        | dddggdddggddd-ggd-d0d.ggd-d0d/gggd1      ad }d}t        j                  j	                  d      }t        j                  d2      }t        |t        j                  j	                  d            }|dk(  ry|rA|t        ||      z  }t        |      }|r"t        ||      }|}|j	                  d3      }d}n!d4}nt        |      }|j	                  d3      }d}|r7t        ||d5   6      }||d<   n!|dk(  r|rt        ||d5         }	|	}d }d}nd}d7}t        |xs i dg8      }t        j                  |||9       y );Nstatepresentabsent)choicesdefaultr^   T)requiredr_   sourcetarget)r   r`   )mysqloraclepostgresmariadbauroraredshifts3db2azuredbsybasedynamodbmongodb	sqlserverF)r   r   ra   rb   )no_logrc   rd   int)typere   rt   rv   r   r+   resource_tags)r   aliasesr   rQ   )r   r   rp   rf   )nonerequirez	verify-cazverify-fullr   ry   r{   r}   r   rr   r   r   r   r8   rU   r   True)argument_specrequired_ifsupports_check_modedmsEndpointzEndpoint Already Existsr(   r)   zDMS Endpoint does not exist)ignore_list)changedr5   r'   r   )r+   AnsibleAWSModuler0   r   r   r<   r6   r   r   r   r   r   r9   r   	exit_json)
r   exit_messager   r   	dmsclientr5   params_changedupdated_dmsr   delete_resultss
             r   mainr   J  sB    ,Ix0)D,., 8X"67,  !
	,, -,. T"/,0 61,2 u3,4 V5,6 #'&7,8 U#9,: v'89;,< VT2=,> v?,@ LV\]A,B "VC,D !%E,F 6*G,H V$I,J !f-K,L &)M,N &)O,P #/Q,R vu-S,T % U,V % W,M\ #i.!12i,0h)Vi[)Vi[)
 "
F LGMMg&Ee$I FMM,=,=>R,STH	{9h77G+H5N1)XF*'++J78.y9L#''
3HG$YH]<STD#HV	(	0H]<STN)LHGG8L'BVHMH
Wx\Jr   __main__r   )'DOCUMENTATIONEXAMPLESRETURNr,   ImportError0ansible.module_utils.common.dict_transformationsr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr   r   r   >ansible_collections.community.aws.plugins.module_utils.modulesr	   r   r+   backoff_paramsjittered_backoffr   r    r6   r=   r@   rD   rH   rL   rO   rS   r9   r   r   r   r   r   r   __name__r   r   r   <module>r      s  M^"_
B	 V \ P f f X xaq#6 ,^,0 -0 ,^,, -,( ,^,0 -0 ,^,, -,
 ,^,, -,
 ,^,1 -1 ,^,6 -6 ,^,1 -1,K"?D.U:F`KF zF G  		s   D* *D32D3