---

- name: ensure the tripleo_ui_config.js file exists
  stat:
    path: /var/www/openstack-tripleo-ui/dist/tripleo_ui_config.js
  register: ooo_ui_config_file

- when:
    - enable_port_forward_for_tripleo_ui|bool
    - hostvars[groups['virthost'][0]].ansible_host is defined
    - tripleo_ui_secure_access|bool
    - ooo_ui_config_file.stat.exists
  block:
    - name: get the heat public url
      shell: |
        source {{ working_dir }}/stackrc
        openstack catalog show heat --format value | grep public | awk -F '/' '{print $5}'
      register: heat_public_url

    - name: get the swift public url
      shell: |
        source {{ working_dir }}/stackrc
        openstack catalog show swift --format value | grep public | awk -F '/' '{print $5}'
      register: swift_public_url

    - name: update the keystone config in tripleo_ui_config.js, connect via virthost ip
      lineinfile:
        regexp: "'keystone':"
        line: "  'keystone': 'https://{{hostvars[groups['virthost'][0]].ansible_default_ipv4.address}}/keystone/v3/',"
        dest: /var/www/openstack-tripleo-ui/dist/tripleo_ui_config.js
        backup: true
      become: true
      when: release not in ['newton', 'ocata']

    - name: update the keystone config in tripleo_ui_config.js, connect via virthost ip
      lineinfile:
        regexp: "'keystone':"
        line: "  'keystone': 'https://{{hostvars[groups['virthost'][0]].ansible_default_ipv4.address}}/keystone/v2/',"
        dest: /var/www/openstack-tripleo-ui/dist/tripleo_ui_config.js
        backup: true
      become: true
      when: release in ['newton', 'ocata']

    - name: update the heat config in tripleo_ui_config.js, connect via virthost ip
      lineinfile:
        regexp: "'heat':"
        line: "  'heat': 'https://{{hostvars[groups['virthost'][0]].ansible_default_ipv4.address}}/heat/v1/{{ heat_public_url.stdout }}',"
        dest: /var/www/openstack-tripleo-ui/dist/tripleo_ui_config.js
        backup: true
      become: true

    - name: update the ironic config in tripleo_ui_config.js, connect via virthost ip
      lineinfile:
        regexp: "'ironic':"
        line: "  'ironic': 'https://{{hostvars[groups['virthost'][0]].ansible_default_ipv4.address}}/ironic',"
        dest: /var/www/openstack-tripleo-ui/dist/tripleo_ui_config.js
        backup: true
      become: true

    - name: update the ironic-inspector config in tripleo_ui_config.js, connect via virthost ip
      lineinfile:
        regexp: "'ironic-inspector':"
        line: "  'ironic-inspector': 'https://{{hostvars[groups['virthost'][0]].ansible_default_ipv4.address}}/ironic-inspector',"
        dest: /var/www/openstack-tripleo-ui/dist/tripleo_ui_config.js
        backup: true
      become: true

    - name: update the mistral config in tripleo_ui_config.js, connect via virthost ip
      lineinfile:
        regexp: "'mistral':"
        line: "  'mistral': 'https://{{hostvars[groups['virthost'][0]].ansible_default_ipv4.address}}/mistral/v2',"
        dest: /var/www/openstack-tripleo-ui/dist/tripleo_ui_config.js
        backup: true
      become: true

    - name: update the swift config in tripleo_ui_config.js, connect via virthost ip
      lineinfile:
        regexp: "'swift':"
        line: "  'swift': 'https://{{hostvars[groups['virthost'][0]].ansible_default_ipv4.address}}/swift/v1/{{ swift_public_url.stdout }}',"
        dest: /var/www/openstack-tripleo-ui/dist/tripleo_ui_config.js
        backup: true
      become: true

    - name: update the zaqar-websocket config in tripleo_ui_config.js, connect via virthost ip
      lineinfile:
        regexp: "'zaqar-websocket':"
        line: "  'zaqar-websocket': 'wss://{{hostvars[groups['virthost'][0]].ansible_default_ipv4.address}}/zaqar',"
        dest: /var/www/openstack-tripleo-ui/dist/tripleo_ui_config.js
        backup: true
      become: true

    - name: restart the httpd service
      service:
        name: httpd
        state: restarted
      become: true

- when:
    - enable_port_forward_for_tripleo_ui|bool
    - hostvars[groups['virthost'][0]].ansible_host is defined
    - tripleo_ui_secure_access|bool == false
    - ooo_ui_config_file.stat.exists
  block:
    - name: update config in tripleo_ui_config.js, connect via virthost ip with ssl
      replace:
        regexp: \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}
        replace: "{{hostvars[groups['virthost'][0]].ansible_default_ipv4.address}}"
        dest: /var/www/openstack-tripleo-ui/dist/tripleo_ui_config.js
        backup: true
      become: true

    - name: update config in tripleo_ui_config.js, for keystone v3
      replace:
        regexp: keystone\/v2.0
        replace: "keystone/v3"
        dest: /var/www/openstack-tripleo-ui/dist/tripleo_ui_config.js
        backup: true
      become: true
      when: release in ['master', 'rocky', 'queens', 'pike']

    - name: restart the httpd service
      service:
        name: httpd
        state: restarted
      become: true
