---
- name: Check to see if PVC already exists
  oc_obj:
    state: list
    kind: pvc
    name: "{{ openshift_metrics_cassandra_pvc_prefix }}-{{ metrics_pvc_index }}"
    namespace: "{{ openshift_metrics_project }}"
  register: _metrics_pvc

# _metrics_pvc.results.results | length > 0 returns a false positive
# so we check for the presence of 'stderr' to determine if the obj exists or not
# the RC for existing and not existing is both 0
- when:
    - _metrics_pvc.results.stderr is defined
  block:
    - name: generate hawkular-cassandra persistent volume claims
      template:
        src: pvc.j2
        dest: "{{ mktemp.stdout }}/templates/hawkular-cassandra-pvc{{ metrics_pvc_index }}.yaml"
      vars:
        obj_name: "{{ openshift_metrics_cassandra_pvc_prefix }}-{{ metrics_pvc_index }}"
        labels:
          metrics-infra: hawkular-cassandra
        access_modes: "{{ openshift_metrics_cassandra_pvc_access | list }}"
        size: "{{ openshift_metrics_cassandra_pvc_size }}"
        pv_selector: "{{ openshift_metrics_cassandra_pv_selector }}"
        storage_class_name: "{{ openshift_metrics_cassandra_pvc_storage_class_name | default('', true) }}"
      when:
        - openshift_metrics_cassandra_storage_type != 'emptydir'
        - openshift_metrics_cassandra_storage_type != 'dynamic'
      changed_when: false

    - name: generate hawkular-cassandra persistent volume claims (dynamic)
      template:
        src: pvc.j2
        dest: "{{ mktemp.stdout }}/templates/hawkular-cassandra-pvc{{ metrics_pvc_index }}.yaml"
      vars:
        obj_name: "{{ openshift_metrics_cassandra_pvc_prefix }}-{{ metrics_pvc_index }}"
        labels:
          metrics-infra: hawkular-cassandra
        access_modes: "{{ openshift_metrics_cassandra_pvc_access | list }}"
        size: "{{ openshift_metrics_cassandra_pvc_size }}"
        pv_selector: "{{ openshift_metrics_cassandra_pv_selector }}"
        storage_class_name: "{{ openshift_metrics_cassandra_pvc_storage_class_name | default('', true) }}"
      when: openshift_metrics_cassandra_storage_type == 'dynamic'
      changed_when: false
