---
# ----------------------------------------------------------------------------
#
#     ***     AUTO GENERATED CODE    ***    Type: MMv1     ***
#
# ----------------------------------------------------------------------------
#
#     This file is automatically generated by Magic Modules and manual
#     changes will be clobbered when the file is regenerated.
#
#     Please read more about how to change this file at
#     https://www.github.com/GoogleCloudPlatform/magic-modules
#
# ----------------------------------------------------------------------------
# Pre-test setup
- name: Create a topic
  google.cloud.gcp_pubsub_topic:
    name: topic-subscription
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: present
  register: topic
- name: Create a bucket
  google.cloud.gcp_storage_bucket:
    name: topic-subscription-bucket
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: present
- name: Delete a subscription
  google.cloud.gcp_pubsub_subscription:
    name: "{{ resource_name }}"
    topic: "{{ topic }}"
    ack_deadline_seconds: 300
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: absent
#----------------------------------------------------------
- name: Create a subscription
  google.cloud.gcp_pubsub_subscription:
    name: "{{ resource_name }}"
    topic: "{{ topic }}"
    ack_deadline_seconds: 300
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: present
  register: result
- name: Assert changed is true
  ansible.builtin.assert:
    that:
      - result.changed == true
- name: Verify that subscription was created
  google.cloud.gcp_pubsub_subscription_info:
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    scopes:
      - https://www.googleapis.com/auth/pubsub
  register: results
- name: Verify that command succeeded
  ansible.builtin.assert:
    that:
      - results['resources'] | map(attribute='name') | select("match", ".*{{ resource_name }}.*") | list | length == 1
# ----------------------------------------------------------------------------
- name: Create a subscription that already exists
  google.cloud.gcp_pubsub_subscription:
    name: "{{ resource_name }}"
    topic: "{{ topic }}"
    ack_deadline_seconds: 300
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: present
  register: result
- name: Assert changed is false
  ansible.builtin.assert:
    that:
      - result.changed == false
#----------------------------------------------------------
- name: Update a subscription
  google.cloud.gcp_pubsub_subscription:
    name: "{{ resource_name }}"
    topic: "{{ topic }}"
    ack_deadline_seconds: 60
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: present
  register: result
- name: Assert changed is true
  ansible.builtin.assert:
    that:
      - result.changed == true
#----------------------------------------------------------
- name: Update cloudStorageConfig of a subscription that already exists
  google.cloud.gcp_pubsub_subscription:
    name: "{{ resource_name }}"
    topic: "{{ topic }}"
    ack_deadline_seconds: 300
    cloud_storage: {
      bucket: "topic-subscription-bucket",
      file_prefix: "test_",
      file_suffix: "_test",
      max_bytes: 10737418240,
      max_duration: "600s",
      output_format: "avro",
      write_metadata: true
    }
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: present
  register: result
- name: Assert changed is true
  ansible.builtin.assert:
    that:
      - result.changed == true
#----------------------------------------------------------
- name: Delete a subscription
  google.cloud.gcp_pubsub_subscription:
    name: "{{ resource_name }}"
    topic: "{{ topic }}"
    ack_deadline_seconds: 300
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: absent
  register: result
- name: Assert changed is true
  ansible.builtin.assert:
    that:
      - result.changed == true
- name: Verify that subscription was deleted
  google.cloud.gcp_pubsub_subscription_info:
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    scopes:
      - https://www.googleapis.com/auth/pubsub
  register: results
- name: Verify that command succeeded
  ansible.builtin.assert:
    that:
      - results['resources'] | map(attribute='name') | select("match", ".*{{ resource_name }}.*") | list | length == 0
# ----------------------------------------------------------------------------
- name: Delete a subscription that does not exist
  google.cloud.gcp_pubsub_subscription:
    name: "{{ resource_name }}"
    topic: "{{ topic }}"
    ack_deadline_seconds: 300
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: absent
  register: result
- name: Assert changed is false
  ansible.builtin.assert:
    that:
      - result.changed == false
#---------------------------------------------------------
# Post-test teardown
# If errors happen, don't crash the playbook!
- name: Delete a topic
  google.cloud.gcp_pubsub_topic:
    name: topic-subscription
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: absent
  register: topic
  ignore_errors: true

- name: Delete a bucket
  google.cloud.gcp_storage_bucket:
    name: topic-subscription-bucket
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: absent
