- module_defaults:
    group/aws:
      aws_secret_key: '{{ aws_secret_key }}'
      aws_access_key: '{{ aws_access_key }}'
      security_token: '{{ security_token|default(omit) }}'
      region: '{{ aws_region }}'
  collections:
    - community.general
  block:
  - name: create minimal lambda role
    iam_role:
      name: '{{ sns_topic_lambda_role }}'
      assume_role_policy_document: '{{ lookup("file", "lambda-trust-policy.json") }}'
      create_instance_profile: false
      managed_policies:
      - 'arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess'
    register: iam_role
  - name: pause if role was created
    pause:
      seconds: 10
    when: iam_role is changed

  - name: create topic
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My topic name
    register: sns_topic_create
  - name: assert that creation worked
    assert:
      that:
      - sns_topic_create.changed
  - name: set sns_arn fact
    set_fact:
      sns_arn: '{{ sns_topic_create.sns_arn }}'
  - name: create topic again (expect changed=False)
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My topic name
    register: sns_topic_no_change
  - name: assert that recreation had no effect
    assert:
      that:
      - not sns_topic_no_change.changed
      - sns_topic_no_change.sns_arn == sns_topic_create.sns_arn
  - name: update display name
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My new topic name
    register: sns_topic_update_name
  - name: assert that updating name worked
    assert:
      that:
      - sns_topic_update_name.changed
      - sns_topic_update_name.sns_topic.display_name == "My new topic name"
  - name: add policy
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My new topic name
      policy: '{{ lookup(''template'', ''initial-policy.json'') }}'
    register: sns_topic_add_policy
  - name: assert that adding policy worked
    assert:
      that:
      - sns_topic_add_policy.changed
  - name: rerun same policy
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My new topic name
      policy: '{{ lookup(''template'', ''initial-policy.json'') }}'
    register: sns_topic_rerun_policy
  - name: assert that rerunning policy had no effect
    assert:
      that:
      - not sns_topic_rerun_policy.changed
  - name: update policy
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My new topic name
      policy: '{{ lookup(''template'', ''updated-policy.json'') }}'
    register: sns_topic_update_policy
  - name: assert that updating policy worked
    assert:
      that:
      - sns_topic_update_policy.changed
  - name: add delivery policy
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My new topic name
      delivery_policy:
        http:
          defaultHealthyRetryPolicy:
            minDelayTarget: 20
            maxDelayTarget: 20
            numRetries: 3
            numMaxDelayRetries: 0
            numNoDelayRetries: 0
            numMinDelayRetries: 0
            backoffFunction: linear
    register: sns_topic_add_delivery_policy
  - name: assert that adding delivery policy worked
    vars:
      delivery_policy: '{{ sns_topic_add_delivery_policy.sns_topic.delivery_policy | from_json }}'
    assert:
      that:
      - sns_topic_add_delivery_policy.changed
      - delivery_policy.http.defaultHealthyRetryPolicy.minDelayTarget == 20
      - delivery_policy.http.defaultHealthyRetryPolicy.maxDelayTarget == 20
      - delivery_policy.http.defaultHealthyRetryPolicy.numRetries == 3
  - name: rerun same delivery policy
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My new topic name
      delivery_policy:
        http:
          defaultHealthyRetryPolicy:
            minDelayTarget: 20
            maxDelayTarget: 20
            numRetries: 3
            numMaxDelayRetries: 0
            numNoDelayRetries: 0
            numMinDelayRetries: 0
            backoffFunction: linear
    register: sns_topic_rerun_delivery_policy
  - name: assert that rerunning delivery_policy had no effect
    vars:
      delivery_policy: '{{ sns_topic_rerun_delivery_policy.sns_topic.delivery_policy | from_json }}'
    assert:
      that:
      - not sns_topic_rerun_delivery_policy.changed
      - delivery_policy.http.defaultHealthyRetryPolicy.minDelayTarget == 20
      - delivery_policy.http.defaultHealthyRetryPolicy.maxDelayTarget == 20
      - delivery_policy.http.defaultHealthyRetryPolicy.numRetries == 3
  - name: rerun a slightly different delivery policy
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My new topic name
      delivery_policy:
        http:
          defaultHealthyRetryPolicy:
            minDelayTarget: 40
            maxDelayTarget: 40
            numRetries: 6
            numMaxDelayRetries: 0
            numNoDelayRetries: 0
            numMinDelayRetries: 0
            backoffFunction: linear
    register: sns_topic_rerun_delivery_policy
  - name: assert that rerunning delivery_policy worked
    vars:
      delivery_policy: '{{ sns_topic_rerun_delivery_policy.sns_topic.delivery_policy | from_json }}'
    assert:
      that:
      - sns_topic_rerun_delivery_policy.changed
      - delivery_policy.http.defaultHealthyRetryPolicy.minDelayTarget == 40
      - delivery_policy.http.defaultHealthyRetryPolicy.maxDelayTarget == 40
      - delivery_policy.http.defaultHealthyRetryPolicy.numRetries == 6
  - name: create temp dir
    tempfile:
      state: directory
    register: tempdir
  - name: ensure zip file exists
    archive:
      path: '{{ lookup(''first_found'', sns_topic_lambda_function) }}'
      dest: '{{ tempdir.path }}/{{ sns_topic_lambda_function }}.zip'
      format: zip
  - name: create lambda for subscribing (only auto-subscribing target available)
    lambda:
      name: '{{ sns_topic_lambda_name }}'
      state: present
      zip_file: '{{ tempdir.path }}/{{ sns_topic_lambda_function }}.zip'
      runtime: python2.7
      role: '{{ sns_topic_lambda_role }}'
      handler: '{{ sns_topic_lambda_function }}.handler'
    register: lambda_result
  - set_fact:
      sns_topic_subscriber_arn: '{{ lambda_result.configuration.function_arn }}'
  - name: subscribe to topic
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My new topic name
      purge_subscriptions: false
      subscriptions: '{{ sns_topic_subscriptions }}'
    register: sns_topic_subscribe
  - name: assert that subscribing worked
    assert:
      that:
      - sns_topic_subscribe.changed
      - sns_topic_subscribe.sns_topic.subscriptions|length == 1
  - name: run again with purge_subscriptions set to false
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My new topic name
      purge_subscriptions: false
    register: sns_topic_no_purge
  - name: assert that not purging subscriptions had no effect
    assert:
      that:
      - not sns_topic_no_purge.changed
      - sns_topic_no_purge.sns_topic.subscriptions|length == 1
  - name: run again with purge_subscriptions set to true
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      display_name: My new topic name
      purge_subscriptions: true
    register: sns_topic_purge
  - name: assert that purging subscriptions worked
    assert:
      that:
      - sns_topic_purge.changed
      - sns_topic_purge.sns_topic.subscriptions|length == 0
  - name: delete topic
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      state: absent
  - name: no-op with third party topic (effectively get existing subscriptions)
    sns_topic:
      name: '{{ sns_topic_third_party_topic_arn }}'
      region: '{{ sns_topic_third_party_region }}'
    register: third_party_topic
  - name: subscribe to third party topic
    sns_topic:
      name: '{{ sns_topic_third_party_topic_arn }}'
      subscriptions: '{{ sns_topic_subscriptions }}'
      region: '{{ sns_topic_third_party_region }}'
    register: third_party_topic_subscribe
  - name: assert that subscribing worked
    assert:
      that:
      - third_party_topic_subscribe is changed
      - (third_party_topic_subscribe.sns_topic.subscriptions|length) - (third_party_topic.sns_topic.subscriptions|length) == 1
  - name: attempt to change name of third party topic
    sns_topic:
      name: '{{ sns_topic_third_party_topic_arn }}'
      display_name: This should not work
      subscriptions: '{{ sns_topic_subscriptions }}'
      region: '{{ sns_topic_third_party_region }}'
    ignore_errors: true
    register: third_party_name_change
  - name: assert that attempting to change display name does not work
    assert:
      that:
      - third_party_name_change is failed
  - name: unsubscribe from third party topic (purge_subscription defaults to true)
    sns_topic:
      name: '{{ sns_topic_third_party_topic_arn }}'
      subscriptions: '{{ third_party_topic.sns_topic.subscriptions }}'
      region: '{{ sns_topic_third_party_region }}'
    register: third_party_unsubscribe
  - name: assert that unsubscribing from third party topic works
    assert:
      that:
      - third_party_unsubscribe.changed
      - third_party_topic.sns_topic.subscriptions|length == third_party_unsubscribe.sns_topic.subscriptions|length
  - name: attempt to delete third party topic
    sns_topic:
      name: '{{ sns_topic_third_party_topic_arn }}'
      state: absent
      subscriptions: '{{ subscriptions }}'
      region: '{{ sns_topic_third_party_region }}'
    ignore_errors: true
    register: third_party_deletion
  - name: no-op after third party deletion
    sns_topic:
      name: '{{ sns_topic_third_party_topic_arn }}'
      region: '{{ sns_topic_third_party_region }}'
    register: third_party_deletion_facts
  - name: assert that attempting to delete third party topic does not work and preser
    assert:
      that:
      - third_party_deletion is failed
      - third_party_topic.sns_topic.subscriptions|length == third_party_deletion_facts.sns_topic.subscriptions|length
  always:
  - name: announce teardown start
    debug:
      msg: '************** TEARDOWN STARTS HERE *******************'
  - name: remove topic
    sns_topic:
      name: '{{ sns_topic_topic_name }}'
      state: absent
    ignore_errors: true
  - name: unsubscribe from third party topic
    sns_topic:
      name: '{{ sns_topic_third_party_topic_arn }}'
      subscriptions: []
      purge_subscriptions: true
      region: '{{ sns_topic_third_party_region }}'
    ignore_errors: true
  - name: remove lambda
    lambda:
      name: '{{ sns_topic_lambda_name }}'
      state: absent
    ignore_errors: true
  - name: remove tempdir
    file:
      path: '{{ tempdir.path }}'
      state: absent
    when: tempdir is defined
    ignore_errors: true
  - name: remove iam role
    iam_role:
      name: '{{ sns_topic_lambda_role }}'
      state: absent
    ignore_errors: true
