---
- include_tasks: modify_imagestreams.yml
  vars:
    openshift_example_remote_imagestream: "{{ outer_item.path }}"
  with_items:
  - { path: "{{ rhel_image_streams }}", check: "{{ openshift_examples_load_rhel }}" }
  - { path: "{{ rhel_dotnet_streams }}", check: "{{ openshift_examples_load_rhel }}" }
  - { path: "{{ centos_image_streams }}", check: "{{ openshift_examples_load_centos }}" }
  - { path: "{{ centos_dotnet_streams }}", check: "{{ openshift_examples_load_centos }}" }
  loop_control:
    loop_var: outer_item
  when:
  - outer_item.check | default(true) | bool
  - not (openshift_hosted_manage_registry | default(true) | bool)

- name: Get xPaaS ImageStream files
  find:
    paths: "{{ xpaas_image_streams }}"
    patterns: "*.json"
  register: openshift_examples_xpaas_dir_list
  when:
  - openshift_examples_load_xpaas | bool
  - not (openshift_hosted_manage_registry | default(true) | bool)

- include_tasks: modify_imagestreams.yml
  vars:
    openshift_example_remote_imagestream: "{{ outer_item.path }}"
  with_items: "{{ openshift_examples_xpaas_dir_list.files }}"
  loop_control:
    loop_var: outer_item
  when:
  - openshift_examples_load_xpaas | bool
  - not (openshift_hosted_manage_registry | default(true) | bool)

# RHEL and Centos image streams are mutually exclusive
- name: Import RHEL streams
  command: >
    {{ openshift_client_binary }} {{ openshift_examples_import_command }} --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift -f {{ item }}
  when:
  - openshift_examples_load_rhel | bool
  with_items:
  - "{{ rhel_image_streams }}"
  - "{{ rhel_dotnet_streams }}"
  register: oex_import_rhel_streams
  failed_when: "'already exists' not in oex_import_rhel_streams.stderr and oex_import_rhel_streams.rc != 0"
  changed_when: false
  retries: 3
  until: oex_import_rhel_streams is succeeded

- name: Import Centos Image streams
  command: >
    {{ openshift_client_binary }} {{ openshift_examples_import_command }} --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift -f {{ item }}
  when:
  - openshift_examples_load_centos | bool
  with_items:
  - "{{ centos_image_streams }}"
  - "{{ centos_dotnet_streams }}"
  register: oex_import_centos_streams
  failed_when: "'already exists' not in oex_import_centos_streams.stderr and oex_import_centos_streams.rc != 0"
  changed_when: false
  retries: 3
  until: oex_import_centos_streams is succeeded

- name: Import db templates
  command: >
    {{ openshift_client_binary }} {{ openshift_examples_import_command }} --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift -f {{ db_templates_base }}
  when: openshift_examples_load_db_templates | bool
  register: oex_import_db_templates
  failed_when: "'already exists' not in oex_import_db_templates.stderr and oex_import_db_templates.rc != 0"
  changed_when: false
  retries: 3
  until: oex_import_db_templates is succeeded

- name: Remove defunct quickstart template files
  file:
    path: "{{ item }}"
    state: absent
  with_items:
  - "{{ quickstarts_base }}/nodejs.json"
  - "{{ quickstarts_base }}/cakephp.json"
  - "{{ quickstarts_base }}/dancer.json"
  - "{{ quickstarts_base }}/django.json"

- name: Remove defunct quickstart templates from openshift namespace
  command: "{{ openshift_client_binary }} --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift delete templates/{{ item }}"
  with_items:
  - nodejs-example
  - cakephp-example
  - dancer-example
  - django-example
  register: oex_delete_defunct_quickstart_templates
  failed_when: "'not found' not in oex_delete_defunct_quickstart_templates.stderr and oex_delete_defunct_quickstart_templates.rc != 0"
  changed_when: false

- name: Import quickstart-templates
  command: >
    {{ openshift_client_binary }} {{ openshift_examples_import_command }} --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift -f {{ quickstarts_base }}
  when: openshift_examples_load_quickstarts | bool
  register: oex_import_quickstarts
  failed_when: "'already exists' not in oex_import_quickstarts.stderr and oex_import_quickstarts.rc != 0"
  changed_when: false
  retries: 3
  until: oex_import_quickstarts is succeeded

- name: Remove old xPaas template files
  file:
    path: "{{ item }}"
    state: absent
  with_items:
  - "{{ xpaas_templates_base }}/sso70-basic.json"

- name: Remove old xPaas templates from openshift namespace
  command: "{{ openshift_client_binary }} --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift delete templates/{{ item }}"
  with_items:
  - sso70-basic
  register: oex_delete_old_xpaas_templates
  failed_when: "'not found' not in oex_delete_old_xpaas_templates.stderr and oex_delete_old_xpaas_templates.rc != 0"
  changed_when: false

- name: Import xPaas image streams
  command: >
    {{ openshift_client_binary }} {{ openshift_examples_import_command }} --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift -f {{ xpaas_image_streams }}
  when:
  - openshift_examples_load_xpaas | bool
  register: oex_import_xpaas_streams
  failed_when: "'already exists' not in oex_import_xpaas_streams.stderr and oex_import_xpaas_streams.rc != 0"
  changed_when: false
  retries: 3
  until: oex_import_xpaas_streams is succeeded

- name: Import xPaas templates
  command: >
    {{ openshift_client_binary }} {{ openshift_examples_import_command }} --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift -f {{ xpaas_templates_base }}
  when:
  - openshift_examples_load_xpaas | bool
  register: oex_import_xpaas_templates
  failed_when: "'already exists' not in oex_import_xpaas_templates.stderr and oex_import_xpaas_templates.rc != 0"
  changed_when: false
  retries: 3
  until: oex_import_xpaas_templates is succeeded
