---
- set_fact:
    glusterfs_timeout: "{{ openshift_storage_glusterfs_registry_timeout }}"
    glusterfs_namespace: "{{ openshift_storage_glusterfs_registry_namespace }}"
    glusterfs_is_native: "{{ openshift_storage_glusterfs_registry_is_native | bool }}"
    glusterfs_name: "{{ openshift_storage_glusterfs_registry_name }}"
    glusterfs_nodeselector: "{{ openshift_storage_glusterfs_registry_nodeselector | default(['storagenode', openshift_storage_glusterfs_registry_name] | join('=')) | map_from_pairs }}"
    glusterfs_storageclass: "{{ openshift_storage_glusterfs_registry_storageclass }}"
    glusterfs_image: "{{ openshift_storage_glusterfs_registry_image }}"
    glusterfs_version: "{{ openshift_storage_glusterfs_registry_version }}"
    glusterfs_wipe: "{{ openshift_storage_glusterfs_registry_wipe | bool }}"
    glusterfs_heketi_is_native: "{{ openshift_storage_glusterfs_registry_heketi_is_native | bool }}"
    glusterfs_heketi_is_missing: "{{ openshift_storage_glusterfs_registry_heketi_is_missing | bool }}"
    glusterfs_heketi_deploy_is_missing: "{{ openshift_storage_glusterfs_registry_heketi_deploy_is_missing | bool }}"
    glusterfs_heketi_cli: "{{ openshift_storage_glusterfs_registry_heketi_cli }}"
    glusterfs_heketi_image: "{{ openshift_storage_glusterfs_registry_heketi_image }}"
    glusterfs_heketi_version: "{{ openshift_storage_glusterfs_registry_heketi_version }}"
    glusterfs_heketi_admin_key: "{{ openshift_storage_glusterfs_registry_heketi_admin_key }}"
    glusterfs_heketi_user_key: "{{ openshift_storage_glusterfs_registry_heketi_user_key }}"
    glusterfs_heketi_topology_load: "{{ openshift_storage_glusterfs_registry_heketi_topology_load | bool }}"
    glusterfs_heketi_wipe: "{{ openshift_storage_glusterfs_registry_heketi_wipe | bool }}"
    glusterfs_heketi_url: "{{ openshift_storage_glusterfs_registry_heketi_url }}"
    glusterfs_heketi_port: "{{ openshift_storage_glusterfs_registry_heketi_port }}"
    glusterfs_heketi_executor: "{{ openshift_storage_glusterfs_registry_heketi_executor }}"
    glusterfs_heketi_ssh_port: "{{ openshift_storage_glusterfs_registry_heketi_ssh_port }}"
    glusterfs_heketi_ssh_user: "{{ openshift_storage_glusterfs_registry_heketi_ssh_user }}"
    glusterfs_heketi_ssh_sudo: "{{ openshift_storage_glusterfs_registry_heketi_ssh_sudo | bool }}"
    glusterfs_heketi_ssh_keyfile: "{{ openshift_storage_glusterfs_registry_heketi_ssh_keyfile }}"
    glusterfs_nodes: "{{ groups.glusterfs_registry | default(groups.glusterfs) }}"

- include: glusterfs_common.yml
  when:
  - glusterfs_nodes | default([]) | count > 0
  - "'glusterfs' not in groups or glusterfs_nodes != groups.glusterfs"

- name: Delete pre-existing GlusterFS registry resources
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    kind: "{{ item.kind }}"
    name: "{{ item.name }}"
    state: absent
  with_items:
  - kind: "svc"
    name: "glusterfs-{{ glusterfs_name | default }}-endpoints"
  failed_when: False

- name: Generate GlusterFS registry endpoints
  template:
    src: "{{ openshift.common.examples_content_version }}/glusterfs-registry-endpoints.yml.j2"
    dest: "{{ mktemp.stdout }}/glusterfs-registry-endpoints.yml"

- name: Copy GlusterFS registry service
  template:
    src: "{{ openshift.common.examples_content_version }}/glusterfs-registry-service.yml.j2"
    dest: "{{ mktemp.stdout }}/glusterfs-registry-service.yml"

- name: Create GlusterFS registry endpoints
  oc_obj:
    namespace: "{{ openshift.hosted.registry.namespace | default('default') }}"
    state: present
    kind: endpoints
    name: "glusterfs-{{ glusterfs_name }}-endpoints"
    files:
    - "{{ mktemp.stdout }}/glusterfs-registry-endpoints.yml"

- name: Create GlusterFS registry service
  oc_obj:
    namespace: "{{ openshift.hosted.registry.namespace | default('default') }}"
    state: present
    kind: service
    name: "glusterfs-{{ glusterfs_name }}-endpoints"
    files:
    - "{{ mktemp.stdout }}/glusterfs-registry-service.yml"

- name: Check if GlusterFS registry volume exists
  command: "{{ glusterfs_heketi_client }} volume list"
  register: registry_volume

- name: Create GlusterFS registry volume
  command: "{{ glusterfs_heketi_client }} volume create --size={{ openshift.hosted.registry.storage.volume.size | replace('Gi','') }} --name={{ openshift.hosted.registry.storage.glusterfs.path }}"
  when: "openshift.hosted.registry.storage.glusterfs.path not in registry_volume.stdout"
