---
# NOTE: the split filter cuts the image path (quay.io:443/infrawatch/container_image:tag_name) on the colon. Field :-1 (everything but the final field) is the image path, field -1 (final field) is the image tag
# --- Smart Gateway Operator ---
- name: Generate Smart Gateway Operator CSV
  shell:
    chdir: working/smart-gateway-operator/build
    cmd: |
      WORKING_DIR="{{ playbook_dir }}/working/smart-gateway-operator-bundle" \
      RELATED_IMAGE_CORE_SMARTGATEWAY={{ sg_core_image_path | parse_image | quote }} \
      RELATED_IMAGE_BRIDGE_SMARTGATEWAY={{ sg_bridge_image_path | parse_image | quote }} \
      RELATED_IMAGE_CORE_SMARTGATEWAY_TAG={{ sg_core_image_path | parse_tag | quote }} \
      RELATED_IMAGE_BRIDGE_SMARTGATEWAY_TAG={{ sg_bridge_image_path | parse_tag | quote }} \
      OPERATOR_IMAGE={{ sgo_image_path | parse_image | quote }} \
      OPERATOR_TAG={{ sgo_image_path | parse_tag | quote }} \
      ./generate_bundle.sh
  register: generate_bundle_sgo

- name: Results of bundle generation
  debug:
    var: generate_bundle_sgo.stdout_lines

- name: Load Smart Gateway Operator RBAC
  command: oc apply -f working/smart-gateway-operator/deploy/{{ item }} -n "{{ namespace }}"
  loop:
    - service_account.yaml
    - role.yaml
    - role_binding.yaml
    - olm-catalog/smart-gateway-operator/manifests/smartgateway.infra.watch_smartgateways_crd.yaml

- name: Replace namespace in SGO CSV
  replace:
    path: "{{ playbook_dir }}/working/smart-gateway-operator-bundle/manifests/smart-gateway-operator.clusterserviceversion.yaml"
    regexp: 'placeholder'
    replace: '{{ namespace }}'

- name: Load Smart Gateway Operator CSV
  shell: oc apply -f working/smart-gateway-operator-bundle/manifests/smart-gateway-operator.clusterserviceversion.yaml -n "{{ namespace }}"

# --- Service Telemetry Operator ---
- name: Generate Service Telemetry Operator CSV
  shell:
    chdir: "{{ playbook_dir }}"
    cmd: |
      WORKING_DIR="{{ playbook_dir }}/working/service-telemetry-operator-bundle" \
      RELATED_IMAGE_PROMETHEUS_WEBHOOK_SNMP={{ prometheus_webhook_snmp_image_path | parse_image | quote }} \
      RELATED_IMAGE_PROMETHEUS_WEBHOOK_SNMP_TAG={{ prometheus_webhook_snmp_image_path | parse_tag | quote }} \
      OPERATOR_IMAGE={{ sto_image_path | parse_image | quote }} \
      OPERATOR_TAG={{ sto_image_path | parse_tag | quote }} \
      ./generate_bundle.sh

- name: Load Service Telemetry Operator RBAC
  command: oc apply -f ../deploy/{{ item }} -n "{{ namespace }}"
  loop:
    - service_account.yaml
    - role.yaml
    - role_binding.yaml
    - olm-catalog/service-telemetry-operator/manifests/infra.watch_servicetelemetrys_crd.yaml

- name: Replace namespace in STO CSV
  replace:
    path: "{{ playbook_dir }}/working/service-telemetry-operator-bundle/manifests/service-telemetry-operator.clusterserviceversion.yaml"
    regexp: 'placeholder'
    replace: '{{ namespace }}'

- name: Load Service Telemetry Operator CSV
  shell: oc apply -f working/service-telemetry-operator-bundle/manifests/service-telemetry-operator.clusterserviceversion.yaml -n "{{ namespace }}"
