---
######################################################################
# Begin image streams

- name: Create imagestream import secret for oreg_url
  command: >
    {{ openshift_client_binary }} create secret docker-registry imagestreamsecret
    --docker-server={{ registry_host }} --docker-username={{ oreg_auth_user }}
    --docker-email=openshift@openshift.com --docker-password={{ oreg_auth_password }}
    --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift
  when:
    - openshift_examples_load_rhel | bool
    - oreg_auth_password is defined
  register: oex_imagestream_import_secret
  failed_when: "'already exists' not in oex_imagestream_import_secret.stderr and oex_imagestream_import_secret.rc != 0"
  changed_when: false

- name: Create imagestream import secrets for any additional registries
  command: >
      {{ openshift_client_binary }} create secret docker-registry o-a-{{ item.host | hash('md5') }}
      --docker-server={{ item.host }} --docker-username={{ item.user }}
      --docker-email=openshift@openshift.com --docker-password={{ item.password }}
      --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift
  when:
    - openshift_additional_registry_credentials != []
  with_items:
    - "{{ openshift_additional_registry_credentials }}"
  register: oex_additional_creds
  failed_when: "'already exists' not in oex_additional_creds.stderr and oex_additional_creds.rc != 0"

- name: Modify registry paths if registry_url is not registry.redhat.io
  shell: >
    find {{ examples_base }} -type f | xargs -n 1 sed -i 's|registry.redhat.io|{{ registry_host | quote }}|g'
  when:
    - openshift_facts_registry_url.split('/') | length > 2
    - registry_host != ''
    - openshift_examples_modify_imagestreams | default(False) | bool

- name: Modify registry paths if registry_url is not registry.redhat.io and using Satellite
  shell: >
    find {{ examples_base }} -type f | xargs -n 1 sed -i -e 's|registry.redhat.io/\([^/]*\)/\(.*\)$|registry.redhat.io/\1_\2|g' \
         -e 's|registry.redhat.io/\([^/]*\)$|{{ satellite_prefix | quote }}\1|g' \
         -e 's/openshift3[-_]ose-//g'
  when:
    - openshift_facts_registry_url.split('/') | length == 2
    - satellite_prefix != ''
    - openshift_examples_modify_imagestreams | default(False) | bool
