
    VhF                         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  G d de      Zd Z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 hw xY w)a  
---
module: codebuild_project
version_added: 1.0.0
short_description: Create or delete an AWS CodeBuild project
notes:
    - For details of the parameters and returns see U(http://boto3.readthedocs.io/en/latest/reference/services/codebuild.html).
    - I(tags) changed from boto3 format to standard dict format in release 6.0.0.
description:
    - Create or delete a CodeBuild projects on AWS, used for building code artifacts from source code.
    - Prior to release 5.0.0 this module was called C(community.aws.aws_codebuild).
      The usage did not change.
author:
    - Stefan Horning (@stefanhorning) <horning@mediapeers.com>
options:
    name:
        description:
            - Name of the CodeBuild project.
        required: true
        type: str
    description:
        description:
            - Descriptive text of the CodeBuild project.
        type: str
    source:
        description:
            - Configure service and location for the build input source.
            - I(source) is required when creating a new project.
        suboptions:
            type:
                description:
                    - "The type of the source. Allows one of these: C(CODECOMMIT), C(CODEPIPELINE), C(GITHUB), C(S3), C(BITBUCKET), C(GITHUB_ENTERPRISE)."
                required: true
                type: str
            location:
                description:
                    - Information about the location of the source code to be built. For I(type) C(CODEPIPELINE) location should not be specified.
                type: str
            git_clone_depth:
                description:
                    - When using git you can specify the clone depth as an integer here.
                type: int
            buildspec:
                description:
                    - The build spec declaration to use for the builds in this build project. Leave empty if part of the CodeBuild project.
                type: str
            insecure_ssl:
                description:
                    - Enable this flag to ignore SSL warnings while connecting to the project source code.
                type: bool
        type: dict
    artifacts:
        description:
            - Information about the build output artifacts for the build project.
            - I(artifacts) is required when creating a new project.
        suboptions:
            type:
                description:
                    - "The type of build output for artifacts. Can be one of the following: C(CODEPIPELINE), C(NO_ARTIFACTS), C(S3)."
                required: true
            location:
                description:
                    - Information about the build output artifact location. When choosing I(type) C(S3), set the bucket name here.
            path:
                description:
                    - Along with namespace_type and name, the pattern that AWS CodeBuild will use to name and store the output artifacts.
                    - Used for path in S3 bucket when type is C(S3).
            namespace_type:
                description:
                    - Along with path and name, the pattern that AWS CodeBuild will use to determine the name and location to store the output artifacts.
                    - Accepts C(BUILD_ID) and C(NONE).
                    - "See docs here: U(http://boto3.readthedocs.io/en/latest/reference/services/codebuild.html#CodeBuild.Client.create_project)."
            name:
                description:
                    - Along with path and namespace_type, the pattern that AWS CodeBuild will use to name and store the output artifact.
            packaging:
                description:
                    - The type of build output artifact to create on S3, can be NONE for creating a folder or ZIP for a ZIP file.
        type: dict
    cache:
        description:
            - Caching params to speed up following builds.
        suboptions:
            type:
                description:
                    - Cache type. Can be C(NO_CACHE) or C(S3).
                required: true
            location:
                description:
                    - Caching location on S3.
                required: true
        type: dict
    environment:
        description:
            - Information about the build environment for the build project.
        suboptions:
            type:
                description:
                    - The type of build environment to use for the project. Usually C(LINUX_CONTAINER).
                required: true
            image:
                description:
                    - The ID of the Docker image to use for this build project.
                required: true
            compute_type:
                description:
                    - Information about the compute resources the build project will use.
                    - "Available values include: C(BUILD_GENERAL1_SMALL), C(BUILD_GENERAL1_MEDIUM), C(BUILD_GENERAL1_LARGE)."
                required: true
            environment_variables:
                description:
                    - A set of environment variables to make available to builds for the build project. List of dictionaries with name and value fields.
                    - "Example: { name: 'MY_ENV_VARIABLE', value: 'test' }"
            privileged_mode:
                description:
                    - Enables running the Docker daemon inside a Docker container.
                    - Set to C(true) only if the build project is be used to build Docker images.
        type: dict
    service_role:
        description:
            - The ARN of the AWS IAM role that enables AWS CodeBuild to interact with dependent AWS services on behalf of the AWS account.
        type: str
    timeout_in_minutes:
        description:
            - How long CodeBuild should wait until timing out any build that has not been marked as completed.
        default: 60
        type: int
    encryption_key:
        description:
            - The AWS Key Management Service (AWS KMS) customer master key (CMK) to be used for encrypting the build output artifacts.
        type: str
    vpc_config:
        description:
            - The VPC config enables AWS CodeBuild to access resources in an Amazon VPC.
        type: dict
    state:
        description:
            - Create or remove CodeBuild project.
        default: 'present'
        choices: ['present', 'absent']
        type: str

extends_documentation_fragment:
    - amazon.aws.boto3.modules
    - amazon.aws.common.modules
    - amazon.aws.region.modules
    - amazon.aws.tags.modules
aV  
# Note: These examples do not set authentication details, see the AWS Guide for details.

- community.aws.codebuild_project:
    name: my_project
    description: My nice little project
    service_role: "arn:aws:iam::123123:role/service-role/code-build-service-role"
    source:
      # Possible values: BITBUCKET, CODECOMMIT, CODEPIPELINE, GITHUB, S3
      type: CODEPIPELINE
      buildspec: ''
    artifacts:
      namespaceType: NONE
      packaging: NONE
      type: CODEPIPELINE
      name: my_project
    environment:
      computeType: BUILD_GENERAL1_SMALL
      privilegedMode: "true"
      image: "aws/codebuild/docker:17.09.0"
      type: LINUX_CONTAINER
      environmentVariables:
        - name: 'PROFILE'
          value: 'staging'
    encryption_key: "arn:aws:kms:us-east-1:123123:alias/aws/s3"
    region: us-east-1
    state: present
a  
project:
  description: Returns the dictionary describing the code project configuration.
  returned: success
  type: complex
  contains:
    name:
      description: Name of the CodeBuild project.
      returned: always
      type: str
      sample: my_project
    arn:
      description: ARN of the CodeBuild project.
      returned: always
      type: str
      sample: arn:aws:codebuild:us-east-1:123123123:project/vod-api-app-builder
    description:
      description: A description of the CodeBuild project.
      returned: always
      type: str
      sample: My nice little project
    source:
      description: Information about the build input source code.
      returned: always
      type: complex
      contains:
        type:
          description: The type of the repository.
          returned: always
          type: str
          sample: CODEPIPELINE
        location:
          description: Location identifier, depending on the source type.
          returned: when configured
          type: str
        git_clone_depth:
          description: The git clone depth.
          returned: when configured
          type: int
        build_spec:
          description: The build spec declaration to use for the builds in this build project.
          returned: always
          type: str
        auth:
          description: Information about the authorization settings for AWS CodeBuild to access the source code to be built.
          returned: when configured
          type: complex
        insecure_ssl:
          description: True if set to ignore SSL warnings.
          returned: when configured
          type: bool
    artifacts:
      description: Information about the output of build artifacts
      returned: always
      type: complex
      contains:
        type:
          description: The type of build artifact.
          returned: always
          type: str
          sample: CODEPIPELINE
        location:
          description: Output location for build artifacts.
          returned: when configured
          type: str
        # and more... see http://boto3.readthedocs.io/en/latest/reference/services/codebuild.html#CodeBuild.Client.create_project
    cache:
      description: Cache settings for the build project.
      returned: when configured
      type: dict
    environment:
      description: Environment settings for the build.
      returned: always
      type: dict
    service_role:
      description: IAM role to be used during build to access other AWS services.
      returned: always
      type: str
      sample: arn:aws:iam::123123123:role/codebuild-service-role
    timeout_in_minutes:
      description: The timeout of a build in minutes.
      returned: always
      type: int
      sample: 60
    tags:
      description:
        - Tags added to the project in the boto3 list of dictionaries format.
        - I(tags) and I(reource_tags) represent the same information in
          different formats.
      returned: when configured
      type: list
    reource_tags:
      description:
        - A simple dictionary representing the tags added to the project.
        - I(tags) and I(reource_tags) represent the same information in
          different formats.
      returned: when configured
      type: dict
      version_added: 4.0.0
    created:
      description: Timestamp of the create time of the project.
      returned: always
      type: str
      sample: "2018-04-17T16:56:03.245000+02:00"
    N)camel_dict_to_snake_dict)snake_dict_to_camel_dict)"get_boto3_client_method_parameters)AnsibleAWSErroransible_dict_to_boto3_tag_list)boto3_tag_list_to_ansible_dict)AnsibleCommunityAWSModulec                       e Zd Zy)CodeBuildAnsibleAWSErrorN)__name__
__module____qualname__     s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/codebuild_project.pyr   r   3  s    r   r   c                    |d   |d   t        d      |d   t        |d   dd      |d<   t        | d	      t        fd
|j	                         D              }	  | j
                  di |}d}||fS # t        j                  j                  t        j                  j                  f$ r}t        d|      d }~ww xY w)Nsource	artifactszqThe source and artifacts parameters must be provided when creating a new project.  No existing project was found.)messagetagskeyvaluetag_name_key_nametag_value_key_namecreate_projectc              3   6   K   | ]  \  }}|v s||f  y wNr   ).0kvpermitted_create_paramss      r   	<genexpr>z$do_create_project.<locals>.<genexpr>C  "     "odaRSWnRnAq6"o   	Tz"Unable to create CodeBuild projectr   	exceptionr   )
r   r   r   dictitemsr   botocore
exceptionsClientErrorBotoCoreError)clientparamsformatted_paramsformatted_create_paramsrespchangeder#   s          @r   do_create_projectr6   7  s    h6+#6#>& H
 	
 f~!#A6Ne$
  AIYZ""o6F6L6L6N"oo
$v$$?'>?W}++X-@-@-N-NO 
&8
 	

s    A8 87C/B<<Cc                 F    |r|S t        |       }|j                  |       |S r   )r	   update)
found_tagsr   
purge_tagsmerged_tagss       r   
merge_tagsr<   Q  s(    0<Ktr   c                     t        | dd      S )Nr   r   r   r   )r   s    r   format_tagsr>   Z  s    )" r   c                   
 t        | d      
t        
fd|j                         D              }|j                  dg       }|d   t	        t        ||d   |d               |d<   t        | |      }|d   }|j                  d       |j                  d       |j                  dg       }t        |      |d<   t        |      |d<   ||k7  }	||d<   ||	fS )	Nupdate_projectc              3   6   K   | ]  \  }}|v s||f  y wr   r   )r    r!   r"   permitted_update_paramss      r   r$   z$do_update_project.<locals>.<genexpr>d  r%   r&   r   r:   r/   r0   projectlastModifiedResourceTags)r   r)   r*   popr>   r<   r@   r	   )r/   r0   r1   found_projectformatted_update_paramsr9   r3   updated_projectupdated_tagsr4   rB   s             @r   do_update_projectrL   b  s    @IYZ""o6F6L6L6N"oo""62.Jf~!*5z6&>6,3GH+
' 0GHD9oO n%'"&&vr2L$B:$NM.!&D\&RON#.G*OF=r   c                     |d   }t        t        d |j                         D                    }t        | |      }d|vrt	        | ||      S t        | |||      S )Nnamec              3   0   K   | ]  \  }}|	||f  y wr   r   )r    r!   r"   s      r   r$   z+create_or_update_project.<locals>.<genexpr>  s     4h1Z[ZgaV4hs   
	r/   rN   )r   r)   r*   describe_projectr6   rL   )r/   r0   rN   r1   founds        r   create_or_update_projectrS   |  s_    &>D/4h4h0hi F6EU 1ABBVV-=uEEr   c                     	  | j                   di |}|S # t        j                  j                  t        j                  j                  f$ r}t        d|      d }~ww xY w)N"Unable to update CodeBuild projectr'   r   )r@   r+   r,   r-   r.   r   )r/   r0   r3   r5   s       r   r@   r@     sa    
$v$$.v.++X-@-@-N-NO 
&8
 	

s    7AAAc                     t        | |      }d|vri dfS 	 | j                  |      }|dfS # t        j                  j                  t        j                  j
                  f$ r}t        d|      d }~ww xY w)NrP   rN   F)rN   TrU   r'   )rQ   delete_projectr+   r,   r-   r.   r   )r/   rN   rR   r3   r5   s        r   rW   rW     s    F6EU5y
$$$$/Tz++X-@-@-N-NO 
&8
 	

s   - 7A6$A11A6c                     i }	 | j                  |g      d   }t        |      dkD  r|d   }|S # t        j                  j                  t        j                  j
                  f$ r}t        d|      d }~ww xY w)N)namesprojectsr   z%Unable to describe CodeBuild projectsr'   )batch_get_projectslenr+   r,   r-   r.   r   )r/   rN   rD   rZ   r5   s        r   rQ   rQ     s    G	
,,D6,::Fx=1qkG++X-@-@-N-NO 
&;
 	

s   */ 7A8&A33A8c                     t        |       }| j                  di       }|r#|j                  dg       }t        |      |d   d<   | |d<   |S )NrD   r   resource_tagsORIGINAL)r   getr	   )project_resultformated_resultrD   r   s       r   format_project_resultrc     sV    .~>O  B/G{{62&6TUY6Z	"?3"0OJr   c                  @   t        t        d      t               t        d      t        d      t        d      t        d      t               t        dd      t        d	      t        dd
g      t        dd      t        d      t        ddgd            } t        |       }|j                  d      }|j                  j	                  d      }d}	 |dk(  rt        ||j                        \  }}n"|dk(  rt        ||j                  d         \  }}t              } |j                  dd|i| y # t        $ rY}|j                  r'|j                  |j                  |j                         |j                  |j                         Y d }~}d }~ww xY w)NT)requiredr)   )typeint<   )rf   defaultF)no_logr^   )rf   aliasesboolpresentabsent)choicesri   )rN   descriptionr   r   cacheenvironmentservice_roletimeout_in_minutesencryption_keyr   r:   
vpc_configstate)argument_spec	codebuildrw   rC   rN   rP   )msgr4   r   )r)   AnsibleAWSModuler/   r0   r`   rS   rW   r   r(   fail_json_awsr   	fail_jsonrc   	exit_json)rx   moduleclient_connrw   r4   ra   r5   formatted_results           r   mainr     sq   4 F F#f%VUB75)v'89VT2V$Ix0)DM  M:F--,KMMg&EG(I&>"}}'#NG h&4"]]6*'#NG -^<F9W9(89 $ (;;  !)) <QYY''(s   AD; ;	FAFF__main__)DOCUMENTATIONEXAMPLESRETURNr+   ImportError0ansible.module_utils.common.dict_transformationsr   r   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   >ansible_collections.amazon.aws.plugins.module_utils.exceptionsr   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr   r	   >ansible_collections.community.aws.plugins.module_utils.modulesr
   r{   r   r6   r<   r>   rL   rS   r@   rW   rQ   rc   r   r   r   r   r   <module>r      s   Sj:h
T	 V U k Z f f x	 	
44F


(:V zF E  		s   A- -A54A5