####################################################################
# WARNING: These are designed specifically for Ansible tests       #
# and should not be used as examples of how to write Ansible roles #
####################################################################

# Copyright (c) 2023, Sergei Antipov <greendayonfire at gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

- name: Proxmox VE pool and pool membership management
  tags: ["pool"]
  block:
  - name: Make sure poolid parameter is not missing
    proxmox_pool:
      api_host: "{{ api_host }}"
      api_user: "{{ user }}@{{ domain }}"
      api_password: "{{ api_password | default(omit) }}"
      api_token_id: "{{ api_token_id | default(omit) }}"
      api_token_secret: "{{ api_token_secret | default(omit) }}"
      validate_certs: "{{ validate_certs }}"
    ignore_errors: true
    register: result

  - assert:
      that:
        - result is failed
        - "'missing required arguments: poolid' in result.msg"

  - name: Create pool (Check)
    proxmox_pool:
      api_host: "{{ api_host }}"
      api_user: "{{ user }}@{{ domain }}"
      api_password: "{{ api_password | default(omit) }}"
      api_token_id: "{{ api_token_id | default(omit) }}"
      api_token_secret: "{{ api_token_secret | default(omit) }}"
      validate_certs: "{{ validate_certs }}"
      poolid: "{{ poolid }}"
    check_mode: true
    register: result

  - assert:
      that:
        - result is changed
        - result is success

  - name: Create pool
    proxmox_pool:
      api_host: "{{ api_host }}"
      api_user: "{{ user }}@{{ domain }}"
      api_password: "{{ api_password | default(omit) }}"
      api_token_id: "{{ api_token_id | default(omit) }}"
      api_token_secret: "{{ api_token_secret | default(omit) }}"
      validate_certs: "{{ validate_certs }}"
      poolid: "{{ poolid }}"
    register: result

  - assert:
      that:
        - result is changed
        - result is success
        - result.poolid == "{{ poolid }}"

  - name: Delete pool (Check)
    proxmox_pool:
      api_host: "{{ api_host }}"
      api_user: "{{ user }}@{{ domain }}"
      api_password: "{{ api_password | default(omit) }}"
      api_token_id: "{{ api_token_id | default(omit) }}"
      api_token_secret: "{{ api_token_secret | default(omit) }}"
      validate_certs: "{{ validate_certs }}"
      poolid: "{{ poolid }}"
      state: absent
    check_mode: true
    register: result

  - assert:
      that:
        - result is changed
        - result is success

  - name: Delete non-existing pool should do nothing
    proxmox_pool:
      api_host: "{{ api_host }}"
      api_user: "{{ user }}@{{ domain }}"
      api_password: "{{ api_password | default(omit) }}"
      api_token_id: "{{ api_token_id | default(omit) }}"
      api_token_secret: "{{ api_token_secret | default(omit) }}"
      validate_certs: "{{ validate_certs }}"
      poolid: "non-existing-poolid"
      state: absent
    register: result

  - assert:
      that:
        - result is not changed
        - result is success

  - name: Deletion of non-empty pool fails
    block:
      - name: Add storage into pool
        proxmox_pool_member:
          api_host: "{{ api_host }}"
          api_user: "{{ user }}@{{ domain }}"
          api_password: "{{ api_password | default(omit) }}"
          api_token_id: "{{ api_token_id | default(omit) }}"
          api_token_secret: "{{ api_token_secret | default(omit) }}"
          validate_certs: "{{ validate_certs }}"
          poolid: "{{ poolid }}"
          member: "{{ member }}"
          type: "{{ member_type }}"
        diff: true
        register: result

      - assert:
          that:
            - result is changed
            - result is success
            - "'{{ member }}' in result.diff.after.members"

      - name: Add non-existing storage into pool should fail
        proxmox_pool_member:
          api_host: "{{ api_host }}"
          api_user: "{{ user }}@{{ domain }}"
          api_password: "{{ api_password | default(omit) }}"
          api_token_id: "{{ api_token_id | default(omit) }}"
          api_token_secret: "{{ api_token_secret | default(omit) }}"
          validate_certs: "{{ validate_certs }}"
          poolid: "{{ poolid }}"
          member: "non-existing-storage"
          type: "{{ member_type }}"
        ignore_errors: true
        register: result

      - assert:
          that:
            - result is failed
            - "'Storage non-existing-storage doesn\\'t exist in the cluster' in result.msg"

      - name: Delete non-empty pool
        proxmox_pool:
          api_host: "{{ api_host }}"
          api_user: "{{ user }}@{{ domain }}"
          api_password: "{{ api_password | default(omit) }}"
          api_token_id: "{{ api_token_id | default(omit) }}"
          api_token_secret: "{{ api_token_secret | default(omit) }}"
          validate_certs: "{{ validate_certs }}"
          poolid: "{{ poolid }}"
          state: absent
        ignore_errors: true
        register: result

      - assert:
          that:
            - result is failed
            - "'Please remove members from pool first.' in result.msg"

      - name: Delete storage from the pool
        proxmox_pool_member:
          api_host: "{{ api_host }}"
          api_user: "{{ user }}@{{ domain }}"
          api_password: "{{ api_password | default(omit) }}"
          api_token_id: "{{ api_token_id | default(omit) }}"
          api_token_secret: "{{ api_token_secret | default(omit) }}"
          validate_certs: "{{ validate_certs }}"
          poolid: "{{ poolid }}"
          member: "{{ member }}"
          type: "{{ member_type }}"
          state: absent
        register: result

      - assert:
          that:
            - result is success
            - result is changed

    rescue:
      - name: Delete storage from the pool if it is added
        proxmox_pool_member:
          api_host: "{{ api_host }}"
          api_user: "{{ user }}@{{ domain }}"
          api_password: "{{ api_password | default(omit) }}"
          api_token_id: "{{ api_token_id | default(omit) }}"
          api_token_secret: "{{ api_token_secret | default(omit) }}"
          validate_certs: "{{ validate_certs }}"
          poolid: "{{ poolid }}"
          member: "{{ member }}"
          type: "{{ member_type }}"
          state: absent
        ignore_errors: true

  - name: Delete pool
    proxmox_pool:
      api_host: "{{ api_host }}"
      api_user: "{{ user }}@{{ domain }}"
      api_password: "{{ api_password | default(omit) }}"
      api_token_id: "{{ api_token_id | default(omit) }}"
      api_token_secret: "{{ api_token_secret | default(omit) }}"
      validate_certs: "{{ validate_certs }}"
      poolid: "{{ poolid }}"
      state: absent
    register: result

  - assert:
      that:
        - result is changed
        - result is success
        - result.poolid == "{{ poolid }}"

  rescue:
    - name: Delete test pool if it is created
      proxmox_pool:
        api_host: "{{ api_host }}"
        api_user: "{{ user }}@{{ domain }}"
        api_password: "{{ api_password | default(omit) }}"
        api_token_id: "{{ api_token_id | default(omit) }}"
        api_token_secret: "{{ api_token_secret | default(omit) }}"
        validate_certs: "{{ validate_certs }}"
        poolid: "{{ poolid }}"
        state: absent
      ignore_errors: true
