---
# Create the required PVs for the App and the DB
- name: Note the App PV Size from Template Parameters
  set_fact:
    openshift_management_app_pv_size: "{{ openshift_management_template_parameters.APPLICATION_VOLUME_CAPACITY }}"
  when:
    - openshift_management_template_parameters.APPLICATION_VOLUME_CAPACITY is defined

- name: Note the App PV Size from defaults
  set_fact:
    openshift_management_app_pv_size: "{{ __openshift_management_app_pv_size }}"
  when:
    - openshift_management_template_parameters.APPLICATION_VOLUME_CAPACITY is not defined

- when: not __openshift_management_use_ext_db | bool
  block:
    - name: Note the DB PV Size from Template Parameters
      set_fact:
        openshift_management_db_pv_size: "{{ openshift_management_template_parameters.DATABASE_VOLUME_CAPACITY }}"
      when:
        - openshift_management_template_parameters.DATABASE_VOLUME_CAPACITY is defined

    - name: Note the DB PV Size from defaults
      set_fact:
        openshift_management_db_pv_size: "{{ __openshift_management_db_pv_size }}"
      when:
        - openshift_management_template_parameters.DATABASE_VOLUME_CAPACITY is not defined

- name: Check if the Management App PV has been created
  oc_obj:
    namespace: "{{ openshift_management_project }}"
    state: list
    kind: pv
    name: "{{ __openshift_management_flavor_short }}-app"
  register: miq_app_pv_check

- name: Check if the Management DB PV has been created
  oc_obj:
    namespace: "{{ openshift_management_project }}"
    state: list
    kind: pv
    name: "{{ __openshift_management_flavor_short }}-db"
  register: miq_db_pv_check
  when:
    - not __openshift_management_use_ext_db | bool

- name: Ensure the Management App PV is created
  oc_process:
    namespace: "{{ openshift_management_project }}"
    template_name: "{{ __openshift_management_flavor }}-app-pv"
    create: True
    params:
      PV_SIZE: "{{ openshift_management_app_pv_size }}"
      BASE_PATH: "{{ openshift_management_storage_nfs_base_dir }}"
      NFS_HOST: "{{ openshift_management_nfs_server }}"
  when: miq_app_pv_check.module_results.results == [{}]

- name: Ensure the Management DB PV is created
  oc_process:
    namespace: "{{ openshift_management_project }}"
    template_name: "{{ __openshift_management_flavor }}-db-pv"
    create: True
    params:
      PV_SIZE: "{{ openshift_management_db_pv_size }}"
      BASE_PATH: "{{ openshift_management_storage_nfs_base_dir }}"
      NFS_HOST: "{{ openshift_management_nfs_server }}"
  when:
    - not __openshift_management_use_ext_db | bool
    - miq_db_pv_check.module_results.results == [{}]
