---
- name: 's3_lifecycle 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:

    # ============================================================
    - name: Create simple s3_bucket
      s3_bucket:
        name: "{{ resource_prefix }}-testbucket-ansible"
        state: present
      register: output

    - assert:
        that:
          - output.changed
          - output.name == '{{ resource_prefix }}-testbucket-ansible'
          - not output.requester_pays
  # ============================================================
    - name: Create a lifecycle policy
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        expiration_days: 300
        prefix: ''
      register: output

    - assert:
        that:
          - output is changed
  # ============================================================
    - name: Create a lifecycle policy (idempotency)
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        expiration_days: 300
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
    - name: Create a second lifecycle policy
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        transition_days: 30
        prefix: /something
      register: output

    - assert:
        that:
          - output is changed
  # ============================================================
    - name: Create a second lifecycle policy (idempotency)
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        transition_days: 30
        prefix: /something
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
    - name: Disable the second lifecycle policy
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        status: disabled
        transition_days: 30
        prefix: /something
      register: output

    - assert:
        that:
          - output is changed
  # ============================================================
    - name: Disable the second lifecycle policy (idempotency)
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        status: disabled
        transition_days: 30
        prefix: /something
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
    - name: Re-enable the second lifecycle policy
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        status: enabled
        transition_days: 300
        prefix: /something
      register: output

    - assert:
        that:
          - output is changed
  # ============================================================
    - name: Re-enable the second lifecycle policy (idempotency)
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        status: enabled
        transition_days: 300
        prefix: /something
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
    - name: Delete the second lifecycle policy
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        state: absent
        prefix: /something
      register: output

    - assert:
        that:
          - output is changed
  # ============================================================
    - name: Delete the second lifecycle policy (idempotency)
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        state: absent
        prefix: /something
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
    - name: Create a second lifecycle policy, with infrequent access
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        transition_days: 30
        storage_class: standard_ia
        prefix: /something
      register: output

    - assert:
        that:
          - output is changed
  # ============================================================
    - name: Create a second lifecycle policy, with infrequent access (idempotency)
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        storage_class: standard_ia
        transition_days: 30
        prefix: /something
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
    - name: Create a second lifecycle policy, with glacier
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        transition_days: 300
        prefix: /something
      register: output

    - assert:
        that:
          - output is changed
  # ============================================================
    - name: Create a second lifecycle policy, with glacier (idempotency)
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        transition_days: 300
        prefix: /something
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
    - name: Create a lifecycle policy with infrequent access
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        transition_days: 30
        storage_class: standard_ia
        prefix: /something
      register: output

    - name: Create a second lifecycle policy, with glacier
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        transition_days: 300
        prefix: /something
        purge_transitions: false
      register: output

    - name: Create a lifecycle policy with infrequent access (idempotency)
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        storage_class: standard_ia
        transition_days: 30
        prefix: /something
        purge_transitions: false
      register: output

    - assert:
        that:
          - output is not changed

    - name: Create a second lifecycle policy, with glacier (idempotency)
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        transition_days: 300
        prefix: /something
        purge_transitions: false
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
    - name: Create a lifecycle policy, with noncurrent expiration
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        noncurrent_version_expiration_days: 300
        prefix: /something
      register: output

    - assert:
        that:
          - output is changed
  # ============================================================
    - name: Create a lifecycle policy, with noncurrent expiration
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        noncurrent_version_expiration_days: 300
        prefix: /something
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
    - name: Create a lifecycle policy, with noncurrent transition
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        noncurrent_version_transition_days: 300
        prefix: /something
      register: output

    - assert:
        that:
          - output is changed
  # ============================================================
    - name: Create a lifecycle policy, with noncurrent transitions and expirations
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        noncurrent_version_transition_days: 300
        prefix: /something
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
    - name: Create a lifecycle policy, with noncurrent transition
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        noncurrent_version_transition_days: 300
        noncurrent_version_storage_class: standard_ia
        prefix: /something
      register: output

    - assert:
        that:
          - output is changed
  # ============================================================
    - name: Create a lifecycle policy, with noncurrent transitions and expirations
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        noncurrent_version_storage_class: standard_ia
        noncurrent_version_transition_days: 300
        prefix: /something
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
    - name: Create a lifecycle policy, with noncurrent transitions
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        noncurrent_version_transitions:
          - transition_days: 30
            storage_class: standard_ia
          - transition_days: 60
            storage_class: onezone_ia
          - transition_days: 90
            storage_class: glacier
        prefix: /something
      register: output

    - assert:
        that:
          - output is changed
  # ============================================================
    - name: Create a lifecycle policy, with noncurrent transitions
      s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        noncurrent_version_transitions:
          - transition_days: 30
            storage_class: standard_ia
          - transition_days: 60
            storage_class: onezone_ia
          - transition_days: 90
            storage_class: glacier
        prefix: /something
      register: output

    - assert:
        that:
          - output is not changed
  # ============================================================
  # test all the examples
  # Configure a lifecycle rule on a bucket to expire (delete) items with a prefix of /logs/ after 30 days
    - s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        expiration_days: 30
        prefix: /logs/
        status: enabled
        state: present

    # Configure a lifecycle rule to transition all items with a prefix of /logs/ to glacier after 7 days and then delete after 90 days
    - s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        transition_days: 7
        expiration_days: 90
        prefix: /logs/
        status: enabled
        state: present

    # Configure a lifecycle rule to transition all items with a prefix of /logs/ to glacier on 31 Dec 2020 and then delete on 31 Dec 2030.
    # Note that midnight GMT must be specified.
    # Be sure to quote your date strings
    - s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        transition_date: "2020-12-30T00:00:00.000Z"
        expiration_date: "2030-12-30T00:00:00.000Z"
        prefix: /logs/
        status: enabled
        state: present

    # Disable the rule created above
    - s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        prefix: /logs/
        status: disabled
        state: present

    # Delete the lifecycle rule created above
    - s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        prefix: /logs/
        state: absent

    # Configure a lifecycle rule to transition all backup files older than 31 days in /backups/ to standard infrequent access class.
    - s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        prefix: /backups/
        storage_class: standard_ia
        transition_days: 31
        state: present
        status: enabled

    # Configure a lifecycle rule to transition files to infrequent access after 30 days and glacier after 90
    - s3_lifecycle:
        name: "{{ resource_prefix }}-testbucket-ansible"
        prefix: /other_logs/
        state: present
        status: enabled
        transitions:
          - transition_days: 30
            storage_class: standard_ia
          - transition_days: 90
            storage_class: glacier
 # ============================================================
  always:
    - name: Ensure all buckets are deleted
      s3_bucket:
        name: "{{item}}"
        state: absent
      ignore_errors: yes
      with_items:
        - "{{ resource_prefix }}-testbucket-ansible"
