---
- name: Copy initial heketi resource files
  copy:
    src: "{{ openshift.common.examples_content_version }}/{{ item }}"
    dest: "{{ mktemp.stdout }}/{{ item }}"
  with_items:
  - "deploy-heketi-template.yml"

- name: Create heketi topology secret
  oc_secret:
    namespace: "{{ glusterfs_namespace }}"
    state: present
    name: "heketi-{{ glusterfs_name }}-topology-secret"
    force: True
    files:
    - name: topology.json
      path: "{{ mktemp.stdout }}/topology.json"

- name: Create deploy-heketi template
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    kind: template
    name: "deploy-heketi"
    state: present
    files:
    - "{{ mktemp.stdout }}/deploy-heketi-template.yml"

- name: Deploy deploy-heketi pod
  oc_process:
    namespace: "{{ glusterfs_namespace }}"
    template_name: "deploy-heketi"
    create: True
    params:
      IMAGE_NAME: "{{ glusterfs_heketi_image }}"
      IMAGE_VERSION: "{{ glusterfs_heketi_version }}"
      HEKETI_ROUTE: "{{ glusterfs_heketi_url | default(['heketi-',glusterfs_name]|join) }}"
      HEKETI_USER_KEY: "{{ glusterfs_heketi_user_key }}"
      HEKETI_ADMIN_KEY: "{{ glusterfs_heketi_admin_key }}"
      HEKETI_EXECUTOR: "{{ glusterfs_heketi_executor }}"
      HEKETI_KUBE_NAMESPACE: "{{ glusterfs_namespace }}"
      CLUSTER_NAME: "{{ glusterfs_name }}"
      TOPOLOGY_PATH: "{{ mktemp.stdout }}"

- name: Wait for deploy-heketi pod
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    kind: pod
    state: list
    selector: "glusterfs=deploy-heketi-{{ glusterfs_name }}-pod"
  register: heketi_pod
  until:
  - "heketi_pod.results.results[0]['items'] | count > 0"
  # Pod's 'Ready' status must be True
  - "heketi_pod.results.results[0]['items'] | oo_collect(attribute='status.conditions') | oo_collect(attribute='status', filters={'type': 'Ready'}) | map('bool') | select | list | count == 1"
  delay: 10
  retries: "{{ (glusterfs_timeout | int / 10) | int }}"
