---
# ----------------------------------------------------------------------------
#
#     ***     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 instance
  google.cloud.gcp_sql_instance:
    name: "{{ resource_name }}-1"
    settings:
      ip_configuration:
        authorized_networks:
          - name: google dns server
            value: 8.8.8.8/32
      tier: db-n1-standard-1
    region: us-central1
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: present
  register: instance
- name: Delete a user
  google.cloud.gcp_sql_user:
    name: test-user
    host: 10.1.2.3
    password: secret-password
    instance: "{{ instance }}"
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: absent
#----------------------------------------------------------
- name: Create a user
  google.cloud.gcp_sql_user:
    name: test-user
    host: 10.1.2.3
    password: secret-password
    instance: "{{ instance }}"
    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 user was created
  google.cloud.gcp_sql_user_info:
    instance: "{{ instance }}"
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    scopes:
      - https://www.googleapis.com/auth/sqlservice.admin
  register: results
- name: Verify that command succeeded
  ansible.builtin.assert:
    that:
      - "'test-user' in \"{{ results['resources'] | map(attribute='name') | list }}\""
# ----------------------------------------------------------------------------
- name: Create a user that already exists
  google.cloud.gcp_sql_user:
    name: test-user
    host: 10.1.2.3
    password: secret-password
    instance: "{{ instance }}"
    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: Delete a user
  google.cloud.gcp_sql_user:
    name: test-user
    host: 10.1.2.3
    password: secret-password
    instance: "{{ instance }}"
    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 user was deleted
  google.cloud.gcp_sql_user_info:
    instance: "{{ instance }}"
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    scopes:
      - https://www.googleapis.com/auth/sqlservice.admin
  register: results
- name: Verify that command succeeded
  ansible.builtin.assert:
    that:
      - "'test-user' not in \"{{ results['resources'] | map(attribute='name') | list }}\""
# ----------------------------------------------------------------------------
- name: Delete a user that does not exist
  google.cloud.gcp_sql_user:
    name: test-user
    host: 10.1.2.3
    password: secret-password
    instance: "{{ instance }}"
    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 instance
  google.cloud.gcp_sql_instance:
    name: "{{ resource_name }}-1"
    settings:
      ip_configuration:
        authorized_networks:
          - name: google dns server
            value: 8.8.8.8/32
      tier: db-n1-standard-1
    region: us-central1
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: absent
  register: instance
  ignore_errors: true
