---
- name: set up aws connection info
  set_fact:
    aws_connection_info: &aws_connection_info
      aws_access_key: "{{ aws_access_key }}"
      aws_secret_key: "{{ aws_secret_key }}"
      security_token: "{{ security_token }}"
      region: "{{ aws_region }}"
  no_log: yes
- block:
  - name: Test creating SQS queue
    sqs_queue:
      name: "{{ resource_prefix }}{{ 1000 | random }}"
      <<: *aws_connection_info
    register: create_result
  - name: Assert SQS queue created
    assert:
      that:
        - create_result.changed
        - create_result.region == "{{ aws_region }}"
  always:
  - name: Test deleting SQS queue
    sqs_queue:
      name: "{{ create_result.name }}"
      state: absent
      <<: *aws_connection_info
    register: delete_result
    retries: 3
    delay: 3
    until: delete_result.changed
  - name: Assert SQS queue deleted
    assert:
      that:
        - delete_result.changed
  - name: Test delete SQS queue that doesn't exist
    sqs_queue:
      name: "{{ resource_prefix }}{{ 1000 | random }}"
      state: absent
      <<: *aws_connection_info
    register: delete_result
  - name: Assert delete non-existant queue returns cleanly
    assert:
      that:
        - delete_result.changed == False
- name: Test queue features
  block:
    - name: Test create queue with attributes
      sqs_queue:
        name: "{{ resource_prefix }}{{ 1000 | random }}"
        default_visibility_timeout: 900
        delivery_delay: 900
        maximum_message_size: 9009
        message_retention_period: 900
        receive_message_wait_time: 10
        policy:
          Version: "2012-10-17"
          Statement:
            Effect: Allow
            Action: "*"
        <<: *aws_connection_info
      register: create_result
    - name: Assert queue created with configuration
      assert:
        that:
          - create_result.changed
          - create_result.default_visibility_timeout == "900"
          - create_result.delivery_delay == "900"
          - create_result.maximum_message_size == "9009"
          - create_result.message_retention_period == "900"
          - create_result.receive_message_wait_time == "10"
          - create_result.policy.Version == "2012-10-17"
          - create_result.policy.Statement.Effect == "Allow"
          - create_result.policy.Statement.Action == "*"
  always:
    - name: Cleaning up queue
      sqs_queue:
        name: "{{ create_result.name }}"
        state: absent
        <<: *aws_connection_info
      register: delete_result
      retries: 3
      delay: 3
      until: delete_result.changed
- name: Test queue with redrive
  block:
    - name: Creating dead letter queue
      sqs_queue:
        name: "{{ resource_prefix }}{{ 1000 | random }}"
        <<: *aws_connection_info
      register: dead_letter_queue
    - name: Test create queue with redrive_policy
      sqs_queue:
        name: "{{ resource_prefix }}{{ 1000 | random }}"
        redrive_policy:
          maxReceiveCount: 5
          deadLetterTargetArn: "{{ dead_letter_queue.queue_arn }}"
        <<: *aws_connection_info
      register: create_result
    - name: Assert queue created with configuration
      assert:
        that:
          - create_result.changed
  always:
    - name: Cleaning up queue
      sqs_queue:
        name: "{{ item.name }}"
        state: absent
        <<: *aws_connection_info
      register: delete_result
      retries: 3
      delay: 3
      with_items:
        - { name: "{{ create_result.name }}" }
        - { name: "{{ dead_letter_queue.name }}" }
