
    Vh>                         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 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 Ww xY w)a	  
---
module: glue_crawler
version_added: 4.1.0
short_description: Manage an AWS Glue crawler
description:
  - Manage an AWS Glue crawler. See U(https://aws.amazon.com/glue/) for details.
  - Prior to release 5.0.0 this module was called C(community.aws.aws_glue_crawler).
    The usage did not change.
author:
  - 'Ivan Chekaldin (@ichekaldin)'
options:
  database_name:
    description:
      - The name of the database where results are written.
    type: str
  description:
    description:
      - Description of the crawler being defined.
    type: str
  name:
    description:
      - The name you assign to this crawler definition. It must be unique in your account.
    required: true
    type: str
  recrawl_policy:
    description:
      - A policy that specifies whether to crawl the entire dataset again, or to crawl only folders that were added since the last crawler run.
    suboptions:
      recrawl_behavior:
        description:
          - Specifies whether to crawl the entire dataset again or to crawl only folders that were added since the last crawler run.
          - Supported options are C(CRAWL_EVERYTHING) and C(CRAWL_NEW_FOLDERS_ONLY).
        type: str
    type: dict
  role:
    description:
      - The name or ARN of the IAM role associated with this crawler.
      - Required when I(state=present).
    type: str
  schema_change_policy:
    description:
      - The policy for the crawler's update and deletion behavior.
    suboptions:
      delete_behavior:
        description:
          - Defines the deletion behavior when the crawler finds a deleted object.
          - Supported options are C(LOG), C(DELETE_FROM_DATABASE), and C(DEPRECATE_IN_DATABASE).
        type: str
      update_behavior:
        description:
          - Defines the update behavior when the crawler finds a changed schema..
          - Supported options are C(LOG) and C(UPDATE_IN_DATABASE).
        type: str
    type: dict
  state:
    description:
      - Create or delete the AWS Glue crawler.
    required: true
    choices: [ 'present', 'absent' ]
    type: str
  table_prefix:
    description:
      - The table prefix used for catalog tables that are created.
    type: str
  targets:
    description:
      - A list of targets to crawl. See example below.
      - Required when I(state=present).
    type: dict
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.tags
  - amazon.aws.boto3
a  
# Note: These examples do not set authentication details, see the AWS Guide for details.

# Create an AWS Glue crawler
- community.aws.glue_crawler:
    name: my-glue-crawler
    database_name: my_database
    role: my-iam-role
    schema_change_policy:
      delete_behavior: DELETE_FROM_DATABASE
      update_behavior: UPDATE_IN_DATABASE
    recrawl_policy:
      recrawl_ehavior: CRAWL_EVERYTHING
    targets:
      S3Targets:
        - Path: "s3://my-bucket/prefix/folder/"
          ConnectionName: my-connection
          Exclusions:
            - "**.json"
            - "**.yml"
    state: present

# Delete an AWS Glue crawler
- community.aws.glue_crawler:
    name: my-glue-crawler
    state: absent
a  
creation_time:
    description: The time and date that this crawler definition was created.
    returned: when state is present
    type: str
    sample: '2021-04-01T05:19:58.326000+00:00'
database_name:
    description: The name of the database where results are written.
    returned: when state is present
    type: str
    sample: my_table
description:
    description: Description of the crawler.
    returned: when state is present
    type: str
    sample: My crawler
last_updated:
    description: The time and date that this crawler definition was last updated.
    returned: when state is present
    type: str
    sample: '2021-04-01T05:19:58.326000+00:00'
name:
    description: The name of the AWS Glue crawler.
    returned: always
    type: str
    sample: my-glue-crawler
recrawl_policy:
    description: A policy that specifies whether to crawl the entire dataset again, or to crawl only folders that were added since the last crawler run.
    returned: when state is present
    type: complex
    contains:
        RecrawlBehavior:
            description: Whether to crawl the entire dataset again or to crawl only folders that were added since the last crawler run.
            returned: when state is present
            type: str
            sample: CRAWL_EVERYTHING
role:
    description: The name or ARN of the IAM role associated with this crawler.
    returned: when state is present
    type: str
    sample: my-iam-role
schema_change_policy:
    description: The policy for the crawler's update and deletion behavior.
    returned: when state is present
    type: complex
    contains:
        DeleteBehavior:
            description: The deletion behavior when the crawler finds a deleted object.
            returned: when state is present
            type: str
            sample: DELETE_FROM_DATABASE
        UpdateBehavior:
            description: The update behavior when the crawler finds a changed schema.
            returned: when state is present
            type: str
            sample: UPDATE_IN_DATABASE

table_prefix:
    description: The table prefix used for catalog tables that are created.
    returned: when state is present
    type: str
    sample: my_prefix
targets:
    description: A list of targets to crawl.
    returned: when state is present
    type: complex
    contains:
        S3Targets:
            description: List of S3 targets.
            returned: when state is present
            type: list
        JdbcTargets:
            description: List of JDBC targets.
            returned: when state is present
            type: list
        MongoDBTargets:
            description: List of Mongo DB targets.
            returned: when state is present
            type: list
        DynamoDBTargets:
            description: List of DynamoDB targets.
            returned: when state is present
            type: list
        CatalogTargets:
            description: List of catalog targets.
            returned: when state is present
            type: list
    N)camel_dict_to_snake_dict)snake_dict_to_camel_dict)is_boto3_error_code)get_aws_account_info)AWSRetry)compare_aws_tags)AnsibleCommunityAWSModulec                     	 | j                  d|      d   S # t        d      $ r Y yt        j                  j                  t        j                  j
                  f$ r}|j                  |       Y d}~yd}~ww xY w)zK
    Get an AWS Glue crawler based on name. If not found, return None.
    T	aws_retryNameCrawlerEntityNotFoundExceptionN)get_crawlerr   botocore
exceptionsClientErrorBotoCoreErrorfail_json_aws)
connectionmoduleglue_crawler_namees       n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/glue_crawler.py_get_glue_crawlerr      sw     %%;L%MiXX89 ''))   	Q	 s    A96A9A44A9c                 >    | D cg c]  }t        |       c}S c c}w )N)_trim_target)targetsts     r   _trim_targetsr       s    %,-LO---s   c                 t    | sy| j                         }|j                  dd      s|j                  dd       |S )z
    Some target types have optional parameters which AWS will fill in and return
    To compare the desired targets and the current targets we need to ignore the defaults
    N
Exclusions)copygetpop)targetretvals     r   r   r      s6    
 [[]F::lD)

<&M    c                 0   d| v r| d   |d   k7  ryd| v r| d   |d   k7  ryd| v r| d   |d   k7  ryd| v r| d   |d   k7  ryd| v r| d   |d   k7  ryd| v r| d   |d   k7  ryd| v rd	| d   v r$t        | d   d	         t        |d   d	         k7  ryd
| d   v r5| d   d
   |d   d
   k7  r$t        | d   d
         t        |d   d
         k7  ryd| d   v r| d   d   |d   d   k7  ryd| d   v r| d   d   |d   d   k7  ryd| d   v r| d   d   |d   d   k7  ryy)zj
    Compare Glue crawler params. If there is a difference, return True immediately else return False
    DatabaseNameTDescriptionRecrawlPolicyRoleSchemaChangePolicyTablePrefixTargets	S3TargetsJdbcTargetsMongoDBTargetsDynamoDBTargetsCatalogTargetsF)r    )user_paramscurrent_paramss     r   _compare_glue_crawler_paramsr8      s    $^)DWeHf)f#M(BnUbFc(c+%+o*F.YhJi*iV!4v8N!N+,-@T1UU#M(BnUbFc(cK+i00[3K@A]y)+6F  [33I&}5	9RS`9aa[3MBC}y)-8H  I 66I&'78N9<UVf<ggY!77I&'89^I=VWh=iiI 66I&'78N9<UVf<ggr(   c           
         d}|j                   j                  d      yt        |      \  }}d| d|j                   d| d|j                   j                  d       }	 | j	                  d|	      j                  d
i       }t        |j                   j                  d      |j                   j                  d            \  }	}
|
r#d}|j                  s	 | j                  d||
       |	r%d}|j                  s	 | j                  d||	       |S |S # t
        j                  j                  t
        j                  j                  f$ rI}|j                  ri }n/|j                  |d|j                   j                  d              Y d }~d }~ww xY w# t
        j                  j                  t
        j                  j                  f$ r:}|j                  |d|j                   j                  d              Y d }~d }~ww xY w# t
        j                  j                  t
        j                  j                  f$ r:}|j                  |d|j                   j                  d              Y d }~|S d }~ww xY w)NFtagszarn:z:glue::z	:crawler/nameT)r   ResourceArnTagsz$Unable to get tags for Glue crawler )msg
purge_tags)r   r=   TagsToRemovez$Unable to set tags for Glue crawler )r   r=   	TagsToAdd)paramsr$   r   regionget_tagsr   r   r   r   
check_moder   r   untag_resourcetag_resource)r   r   glue_crawlerchanged
account_id	partitionarnexisting_tagsr   tags_to_addtags_to_removes              r   ensure_tagsrQ   -  s_   G}} (08J	6&--*Yv}}GXGXY_G`Fa
bCl"++d+LPPQWY[\ #3v}}((0&--2C2CL2Q#K   p))DcXf)g   p''$CS^'_ N7N5 ++X-@-@-N-NO lM  *Nv}}O`O`agOhNi(j k	l ''33X5H5H5V5VW p$$Q.RSYS`S`SdSdekSlRm,n$oop ''33X5H5H5V5VW p$$Q.RSYS`S`SdSdekSlRm,n$ooNpsH   #D F :H	 7F	>FF7H/HH	7I: /I55I:c           	         d}t               }|j                  j                  d      |d<   |j                  j                  d      |d<   |j                  j                  d      |d<   |j                  j                  d      |j                  j                  d      |d
<   |j                  j                  d      |j                  j                  d      |d<   |j                  j                  d      )t        |j                  j                  d      d      |d<   |j                  j                  d      |j                  j                  d      |d<   |j                  j                  d      )t        |j                  j                  d      d      |d<   |j                  j                  d      |j                  j                  d      |d<   |j                  j                  d      |j                  j                  d      |d<   |r0t	        ||      rG	 |j
                  s | j                  dddi| d}n#	 |j
                  s | j                  dddi| d}t        | ||d         }|t        | ||      z  } |j                  dd|it!        |xs i g d       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 crawler
    Fr<   r   roler-   r   r0   database_nameNr*   descriptionr+   recrawl_policyT)capitalize_firstr,   schema_change_policyr.   table_prefixr/   r   rJ   )r.   r,   r0   )ignore_list )dictrC   r$   r   r8   rF   update_crawlerr   r   r   r   r   create_crawlerr   rQ   	exit_jsonr   )r   r   rI   rJ   rC   r   s         r   create_or_update_glue_crawlerr`   U  s   
 GVF]]&&v.F6N]]&&v.F6N)))4F9}})5!'!2!2?!C~}}'3 & 1 1- @}}})*6":6==;L;LM];^qu"v}} ,**62v}}/0<'?MM45(
#$ }}(4 & 1 1. A}}}#/"MM--i8y'=(((-J--GGG	$$$)
))CDCFCG %ZHL{:v|<<GF 
"<#52Cu
v ''33X5H5H5V5VW ($$Q''( ##//1D1D1R1RS 	$  ##	$s0   "J' ?"K< '7K9K44K9<7M3M		Mc                 $   d}|r%	 |j                   s| j                  d|d          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 crawler
    FTr   r   N)rJ   )rF   delete_crawlerr   r   r   r   r   r_   )r   r   rI   rJ   r   s        r   delete_glue_crawlerrc     s     G	$$$))D|F?S)TG W% ##//1D1D1R1RS 	$  ##	$s   $= 7B4B

Bc                     t        t        d      t        d      t        dd      t        dd      t        dt        t        d            	      t        d      t        dt        t        d      t        d      
      	      t        dddgd      t        d      t        ddg      t        d            } t        | ddddgfgd      }|j                  dt        j                  d            }|j
                  j                  d      }t        |||j
                  j                  d            }|dk(  rt        |||       y t        |||       y )Nstr)typeT)requiredrf   bool)rf   defaultr\   )recrawl_behavior)rf   options)delete_behaviorupdate_behaviorpresentabsent)rg   choicesrf   resource_tags)rf   aliases)rT   rU   r<   r@   rV   rS   rX   staterY   r:   r   rs   rS   r   )argument_specrequired_ifsupports_check_modeglue
   )retries)retry_decoratorr<   )
r\   AnsibleAWSModuleclientr   jittered_backoffrC   r$   r   r`   rc   )rt   r   r   rs   rI   s        r   mainr~     s2   &e$4e,VT2tQVGW1XYu!d6FX\bgXh!i
 D9h*?eLu%v'89&!M  #y69*=>? F vx7P7PY[7\]JMMg&E$Z9J9J69RSL	%j&,GJ=r(   __main__)DOCUMENTATIONEXAMPLESRETURNr   ImportError0ansible.module_utils.common.dict_transformationsr   r   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   7ansible_collections.amazon.aws.plugins.module_utils.iamr   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr   >ansible_collections.community.aws.plugins.module_utils.modulesr	   r{   r   r    r   r8   rQ   r`   rc   r~   __name__r[   r(   r   <module>r      s   KZ8W
r	 V U \ X P X x .
1h%P2j&" >F zF g  		s   A A$#A$