---
# Pre-test setup
- name: Create a temp file for uploading
  ansible.builtin.tempfile:
    state: file
  register: upload_temp
- name: Create a temp file for downloading
  ansible.builtin.tempfile:
    state: file
  register: download_temp
- name: Put content in the tempfile
  ansible.builtin.copy:
    content: Ansible GCS test file
    dest: "{{ upload_temp.path }}"
    mode: 0644
- name: Create a bucket
  google.cloud.gcp_storage_bucket:
    name: "{{ resource_name }}"
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: present
  register: bucket
#----------------------------------------------------------
- name: Upload the object to gcs
  google.cloud.gcp_storage_object:
    action: upload
    bucket: "{{ bucket.name }}"
    src: "{{ upload_temp.path }}"
    dest: ansible/{{ resource_name }}
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
  register: result
- name: Assert changed is true
  ansible.builtin.assert:
    that:
      - result.changed == true
# ----------------------------------------------------------------------------
- name: Download the object to disk
  google.cloud.gcp_storage_object:
    action: download
    bucket: "{{ bucket.name }}"
    src: ansible/{{ resource_name }}
    dest: "{{ download_temp.path }}"
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
  register: result
- name: Assert changed is true
  ansible.builtin.assert:
    that:
      - result.changed == true
# ----------------------------------------------------------------------------
- name: Delete the object
  google.cloud.gcp_storage_object:
    action: delete
    bucket: "{{ bucket.name }}"
    src: ansible/{{ resource_name }}
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
  register: result
- name: Assert changed is true
  ansible.builtin.assert:
    that:
      - result.changed == true
# ----------------------------------------------------------------------------
- name: Delete the bucket
  google.cloud.gcp_storage_bucket:
    name: "{{ resource_name }}"
    project: "{{ gcp_project }}"
    auth_kind: "{{ gcp_cred_kind }}"
    service_account_file: "{{ gcp_cred_file | default(omit) }}"
    state: absent
  register: bucket
