---
- name: 'aws_codebuild integration tests'
  collections:
    - amazon.aws
  module_defaults:
    group/aws:
      aws_access_key: '{{ aws_access_key }}'
      aws_secret_key: '{{ aws_secret_key }}'
      security_token: '{{ security_token | default(omit) }}'
      region: '{{ aws_region }}'
  block:

    # ==================== preparaions ========================================

    - name: create IAM role needed for CodePipeline test
      iam_role:
        name: "{{ codepipeline_service_role_name }}"
        description: Role with permissions for CodePipeline actions.
        assume_role_policy_document: "{{ lookup('file', 'codepipeline_iam_trust_policy.json') }}"
        state: present
      register: codepipeline_iam_role

    # ================== integration test ==========================================

    - name: create CodePipeline
      aws_codepipeline:
        name: "{{ codepipeline_name }}"
        role_arn: "{{ codepipeline_iam_role.iam_role.arn }}"
        artifact_store:
          type: S3
          location: foo
        stages:
          - name: step_1
            actions:
              - name: action
                actionTypeId:
                  category: Source
                  owner: AWS
                  provider: S3
                  version: '1'
                configuration:
                  S3Bucket: foo
                  S3ObjectKey: bar
                outputArtifacts:
                  - { name: step_one_output }
          - name: step_2
            actions:
              - name: action
                actionTypeId:
                  category: Build
                  owner: AWS
                  provider: CodeBuild
                  version: '1'
                inputArtifacts:
                  - { name: step_one_output }
                outputArtifacts:
                  - { name: step_two_output }
                configuration:
                  ProjectName: foo
        state: present
      register: output
      retries: 10
      delay: 5
      until: output is success

    - assert:
        that:
          - output.changed == True
          - output.pipeline.name == "{{ codepipeline_name  }}"
          - output.pipeline.stages|length > 1

    - name: idempotence check rerunning same CodePipeline task
      aws_codepipeline:
        name: "{{ codepipeline_name }}"
        role_arn: "{{ codepipeline_iam_role.iam_role.arn }}"
        artifact_store:
          type: S3
          location: foo
        stages:
          - name: step_1
            actions:
              - name: action
                actionTypeId:
                  category: Source
                  owner: AWS
                  provider: S3
                  version: '1'
                configuration:
                  S3Bucket: foo
                  S3ObjectKey: bar
                outputArtifacts:
                  - { name: step_one_output }
          - name: step_2
            actions:
              - name: action
                actionTypeId:
                  category: Build
                  owner: AWS
                  provider: CodeBuild
                  version: '1'
                inputArtifacts:
                  - { name: step_one_output }
                outputArtifacts:
                  - { name: step_two_output }
                configuration:
                  ProjectName: foo
        state: present
      register: rerun_test_output

    - assert:
        that:
          - rerun_test_output.changed == False
          - rerun_test_output.pipeline == output.pipeline

    - name: Test deletion of CodePipeline
      aws_codepipeline:
        name: "{{ codepipeline_name }}"
        role_arn: ''
        artifact_store: {}
        stages: []
        state: absent
      register: absent_test_output

    - assert:
        that:
          - absent_test_output.changed == True
          - absent_test_output.pipeline is undefined

  # ==================== cleanup =======================

  always:

    - name: Cleanup - delete test CodePipeline
      aws_codepipeline:
        name: "{{ codepipeline_name }}"
        role_arn: ''
        artifact_store: {}
        stages: []
        state: absent
      ignore_errors: true

    - name: Cleanup - delete IAM role needed for CodePipeline test
      iam_role:
        name: "{{ codepipeline_name }}"
        state: absent
      ignore_errors: true
