
    VhTH                         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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 Kw xY w)a  
---
module: glue_job
version_added: 1.0.0
short_description: Manage an AWS Glue job
description:
  - Manage an AWS Glue job. See U(https://aws.amazon.com/glue/) for details.
  - Prior to release 5.0.0 this module was called C(community.aws.aws_glue_job).
    The usage did not change.
author:
  - "Rob White (@wimnat)"
  - "Vijayanand Sharma (@vijayanandsharma)"
options:
  allocated_capacity:
    description:
      - The number of AWS Glue data processing units (DPUs) to allocate to this Job. From 2 to 100 DPUs
        can be allocated; the default is 10. A DPU is a relative measure of processing power that consists
        of 4 vCPUs of compute capacity and 16 GB of memory.
    type: int
  command_name:
    description:
      - The name of the job command. This must be 'glueetl'.
    default: glueetl
    type: str
  command_python_version:
    description:
      - Python version being used to execute a Python shell job.
      - AWS currently supports C('2') or C('3').
    type: str
    version_added: 2.2.0
  command_script_location:
    description:
      - The S3 path to a script that executes a job.
      - Required when I(state=present).
    type: str
  connections:
    description:
      - A list of Glue connections used for this job.
    type: list
    elements: str
  default_arguments:
    description:
      - A dict of default arguments for this job.  You can specify arguments here that your own job-execution
        script consumes, as well as arguments that AWS Glue itself consumes.
    type: dict
  description:
    description:
      - Description of the job being defined.
    type: str
  glue_version:
    description:
      - Glue version determines the versions of Apache Spark and Python that AWS Glue supports.
    type: str
    version_added: 1.5.0
  max_concurrent_runs:
    description:
      - The maximum number of concurrent runs allowed for the job. The default is 1. An error is returned when
        this threshold is reached. The maximum value you can specify is controlled by a service limit.
    type: int
  max_retries:
    description:
      -  The maximum number of times to retry this job if it fails.
    type: int
  name:
    description:
      - The name you assign to this job definition. It must be unique in your account.
    required: true
    type: str
  number_of_workers:
    description:
      - The number of workers of a defined workerType that are allocated when a job runs.
    type: int
    version_added: 1.5.0
  role:
    description:
      - The name or ARN of the IAM role associated with this job.
      - Required when I(state=present).
    type: str
  state:
    description:
      - Create or delete the AWS Glue job.
    required: true
    choices: [ 'present', 'absent' ]
    type: str
  timeout:
    description:
      - The job timeout in minutes.
    type: int
  worker_type:
    description:
      - The type of predefined worker that is allocated when a job runs.
      - Support for instance types C(G.4X( and C(G.8X) was added in community.aws release 7.2.0.
    choices: [ 'Standard', 'G.1X', 'G.2X', 'G.4X', 'G.8X' ]
    type: str
    version_added: 1.5.0
notes:
  - Support for I(tags) and I(purge_tags) was added in release 2.2.0.
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 job
- community.aws.glue_job:
    command_script_location: "s3://s3bucket/script.py"
    default_arguments:
      "--extra-py-files": s3://s3bucket/script-package.zip
      "--TempDir": "s3://s3bucket/temp/"
    name: my-glue-job
    role: my-iam-role
    state: present

# Delete an AWS Glue job
- community.aws.glue_job:
    name: my-glue-job
    state: absent
a  
allocated_capacity:
    description: The number of AWS Glue data processing units (DPUs) allocated to runs of this job. From 2 to
                 100 DPUs can be allocated; the default is 10. A DPU is a relative measure of processing power
                 that consists of 4 vCPUs of compute capacity and 16 GB of memory.
    returned: when state is present
    type: int
    sample: 10
command:
    description: The JobCommand that executes this job.
    returned: when state is present
    type: complex
    contains:
        name:
            description: The name of the job command.
            returned: when state is present
            type: str
            sample: glueetl
        script_location:
            description: Specifies the S3 path to a script that executes a job.
            returned: when state is present
            type: str
            sample: mybucket/myscript.py
        python_version:
            description: Specifies the Python version.
            returned: when state is present
            type: str
            sample: 3
connections:
    description: The connections used for this job.
    returned: when state is present
    type: dict
    sample: "{ Connections: [ 'list', 'of', 'connections' ] }"
created_on:
    description: The time and date that this job definition was created.
    returned: when state is present
    type: str
    sample: "2018-04-21T05:19:58.326000+00:00"
default_arguments:
    description: The default arguments for this job, specified as name-value pairs.
    returned: when state is present
    type: dict
    sample: "{ 'mykey1': 'myvalue1' }"
description:
    description: Description of the job being defined.
    returned: when state is present
    type: str
    sample: My first Glue job
glue_version:
    description: Glue version.
    returned: when state is present
    type: str
    sample: 2.0
job_name:
    description: The name of the AWS Glue job.
    returned: always
    type: str
    sample: my-glue-job
execution_property:
    description: An ExecutionProperty specifying the maximum number of concurrent runs allowed for this job.
    returned: always
    type: complex
    contains:
        max_concurrent_runs:
            description: The maximum number of concurrent runs allowed for the job. The default is 1. An error is
                         returned when this threshold is reached. The maximum value you can specify is controlled by
                         a service limit.
            returned: when state is present
            type: int
            sample: 1
last_modified_on:
    description: The last point in time when this job definition was modified.
    returned: when state is present
    type: str
    sample: "2018-04-21T05:19:58.326000+00:00"
max_retries:
    description: The maximum number of times to retry this job after a JobRun fails.
    returned: when state is present
    type: int
    sample: 5
name:
    description: The name assigned to this job definition.
    returned: when state is present
    type: str
    sample: my-glue-job
role:
    description: The name or ARN of the IAM role associated with this job.
    returned: when state is present
    type: str
    sample: my-iam-role
timeout:
    description: The job timeout in minutes.
    returned: when state is present
    type: int
    sample: 300
    N)camel_dict_to_snake_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)a  
    Get an AWS Glue job based on name. If not found, return None.

    :param connection: AWS boto3 glue connection
    :param module: Ansible module
    :param glue_job_name: Name of Glue job to get
    :return: boto3 Glue job dict or None if not found
    T	aws_retryJobNameJobEntityNotFoundExceptionN)get_jobr   botocore
exceptionsClientErrorBotoCoreErrorfail_json_aws)
connectionmoduleglue_job_namees       j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/glue_job.py_get_glue_jobr      sv     !!D-!HOO89 ''))   	Q	 s    A96A9A44A9c                    d|vrd|d<   d|vrt               |d<   d| v r| d   |d   k7  ryd| v r$| d   d   |d   d   k7  ry| d   d   |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   |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 r| d   |d   k7  ryd| v r| d   |d   k7  ryy)a.  
    Compare Glue job params. If there is a difference, return True immediately else return False

    :param user_params: the Glue job parameters passed by the user
    :param current_params: the Glue job parameters currently configured
    :return: True if any parameter is mismatched else False
    Description DefaultArgumentsAllocatedCapacityTCommandScriptLocationPythonVersionConnectionsExecutionPropertyMaxConcurrentRunsGlueVersion
MaxRetriesRoleTimeout
WorkerTypeNumberOfWorkersF)dict)user_paramscurrent_paramss     r   _compare_glue_job_paramsr/     s    N*(*}%/-1V)*k)k:M.NR`atRu.uKy!"23~i7PQa7bby!/2nY6OP_6``#M(BnUbFc(c[([9K-LP^_qPr-r#M(BnUbFc(c{*+,-@A-./BCD #M(BnUbFc(c{"{<'@NS_D`'`V!4v8N!NKK	$:nY>W$W#M(BnUbFc(c{"{<'@NS_D`'`K'K8I,Jn]nNo,o    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:job/nameT)r   ResourceArnTagsz Unable to get tags for Glue job )msg
purge_tags)r   r5   TagsToRemovez Unable to set tags for Glue job )r   r5   	TagsToAdd)paramsgetr   regionget_tagsr   r   r   r   
check_moder   r   untag_resourcetag_resource)r   r   glue_jobchanged
account_id	partitionarnexisting_tagsr   tags_to_addtags_to_removes              r   ensure_tagsrJ   =  sY   G}} (08J	6&--*U6==CTCTU[C\B]
^Ch"++d+LPPQWY[\ #3v}}((0&--2C2CL2Q#K   l))DcXf)g   l''$CS^'_ N7N5 ++X-@-@-N-NO hM  *J6==K\K\]cKdJe(f g	h ''33X5H5H5V5VW l$$Q.Nv}}O`O`agOhNi,j$kkl ''33X5H5H5V5VW l$$Q.Nv}}O`O`agOhNi,j$kkNlsH   #D F :H	 7F	>FF7H/HH	7I: /I55I:c                 l	   d}t               }|j                  j                  d      |d<   |j                  j                  d      |d<   |j                  j                  d      |j                  j                  d      |d<   |j                  j                  d	      v|j                  j                  d
      |j                  j                  d	      d|d<   |j                  j                  d      !|j                  j                  d      |d   d<   |j                  j                  d       d|j                  j                  d      i|d<   |j                  j                  d      |j                  j                  d      |d<   |j                  j                  d      |j                  j                  d      |d<   |j                  j                  d      |j                  j                  d      |d<   |j                  j                  d       d|j                  j                  d      i|d<   |j                  j                  d      |j                  j                  d      |d<   |j                  j                  d      |j                  j                  d      |d<   |j                  j                  d      |j                  j                  d      |d<   |j                  j                  d      |j                  j                  d      |d<   |j                  j                  d       |j                  j                  d       |d!<   |rQt        ||      rh	 |d   t	        j
                  |      d"}|d#   d= |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 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 job

    :param connection: AWS boto3 glue connection
    :param module: Ansible module
    :param glue_job: a dict of AWS Glue job parameters or None
    :return:
    Fr4   Nameroler(   allocated_capacityNr   command_script_locationcommand_name)rL   r!   r    command_python_versionr"   connectionsr#   default_argumentsr   descriptionr   glue_versionr&   max_concurrent_runsr%   r$   max_retriesr'   timeoutr)   worker_typer*   number_of_workersr+   )r   	JobUpdater[   r   TrC   )ignore_list )r,   r;   r<   r/   copydeepcopyr?   
update_jobr   r   r   r   r   
create_jobr   rJ   	exit_jsonr   )r   r   rB   rC   r;   update_paramsr   s          r   create_or_update_glue_jobrd   e  s&    GVF]]&&v.F6N]]&&v.F6N}}-.:&,mm&7&78L&M"#}}23?MM%%n5$mm//0IJ
y ==56B171B1BC[1\F9o.}}'3!.0A0A-0P Q}}},-9%+]]%6%67J%K!"}}'3 & 1 1- @}}}(4 & 1 1. A}}}./;':FMM<M<MNc<d&e"#}}'3%}}00?|}}#/"MM--i8y}}(4 & 1 1. A}}}'3%}}00?|}},-9$*MM$5$56I$J ! #FH5(,26NW]I^ _!+.v6(()J))JDJMJ	$$$%
%%???G Z@H{:vx88GFsWs(@R^p]q(rs ''33X5H5H5V5VW ($$Q''( ##//1D1D1R1RS 	$  ##	$s1    AP %"Q! 7QQQ!7R3R..R3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 job

    :param connection: AWS boto3 glue connection
    :param module: Ansible module
    :param glue_job: a dict of AWS Glue job parameters or None
    :return:
    FTrL   r
   N)rC   )r?   
delete_jobr   r   r   r   r   rb   )r   r   rB   rC   r   s        r   delete_glue_jobrg     s     G	$$$%%hv>N%OG W% ##//1D1D1R1RS 	$  ##	$s   $= 7B4B

Bc            
      @   t        d*i dt        d      dt        dd      dt        d      d	t        d      d
t        d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d      dt        d      dt        dddgd      dt        ddg       d!t        d      d"t        g d#d$      } t        | dddd	gfgd%      }t        j                  d&'      }|j	                  d(|)      }|j
                  j                  d      }t        |||j
                  j                  d            }|dk(  rt        |||       y t        |||       y )+NrN   int)typerP   strglueetl)rj   defaultrQ   rO   rR   list)rj   elementsrS   r,   rT   rU   rV   rW   r4   T)requiredrj   rZ   r8   boolrM   statepresentabsent)rp   choicesrj   r2   resource_tags)rj   aliasesrX   rY   )StandardzG.1XzG.2XzG.4XzG.8X)ru   rj   )argument_specrequired_ifsupports_check_mode
   )retriesglue)retry_decoratorr]   )
r,   AnsibleAWSModuler   jittered_backoffclientr;   r<   r   rd   rg   )ry   r   r   r   rr   rB   s         r   mainr     s    U+ui8  $/ !%% 0	
 fu5 F+ e$ u% !e, e$ 4e, E* VT2 u D9h*?eL  v'89!" % #$ !MTYZ%M* #y63L*MNO F //;OvGJMMg&EZ1B1B61JKH	!*fh?
FH5r0   __main__)DOCUMENTATIONEXAMPLESRETURNr^   r   ImportError0ansible.module_utils.common.dict_transformationsr   <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/   rJ   rd   rg   r   __name__r]   r0   r   <module>r      s   fP&_
B 	 V \ X P X x (2j%PDtN&,&6R zF I  		s   A AA