---
# ----------------------------------------------------------------------------
#
#     ***     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 }}-2"
    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 non-existent SSL cert
  google.cloud.gcp_sql_ssl_cert:
    common_name: "{{ resource_name }}"
    instance:
      name: "{{ instance['name'] }}"
    sha1_fingerprint: "f572d396fae9206628714fb2ce00f72e94f2258f"
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: absent
#----------------------------------------------------------
- name: Create an SSL cert
  google.cloud.gcp_sql_ssl_cert:
    common_name: "{{ resource_name }}"
    instance:
      name: "{{ instance['name'] }}"
    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: Peform a no-op update to verify the cert was created
  google.cloud.gcp_sql_ssl_cert:
    instance:
      name: "{{ instance['name'] }}"
    sha1_fingerprint: "{{ result['sha1Fingerprint'] }}"
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: present
  register: updates
- name: Verify that command succeeded
  ansible.builtin.assert:
    that:
      - updates.changed == false
#----------------------------------------------------------
- name: Delete an SSL cert
  google.cloud.gcp_sql_ssl_cert:
    common_name: "{{ resource_name }}"
    instance:
      name: "{{ instance['name'] }}"
    sha1_fingerprint: "{{ result['sha1Fingerprint'] }}"
    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
#---------------------------------------------------------
# Post-test teardown
# If errors happen, don't crash the playbook!
- name: Delete a instance
  google.cloud.gcp_sql_instance:
    name: "{{ resource_name }}-2"
    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
