---

- name: Include vars in matrix_vars.yml
  include_vars:
    file: '{{ awx_cached_matrix_vars }}'
  no_log: True

- name: Install jq and curl on remote machine
  apt:
    name: 
      - jq
      - curl
    state: present

- name: Collect access token of Dimension user
  shell: |
    curl -X POST --header 'Content-Type: application/json' -d '{ "identifier": { "type": "m.id.user","user": "dimension" }, "password": "{{ awx_dimension_user_password }}", "type": "m.login.password"}' 'https://matrix.{{ matrix_domain }}/_matrix/client/r0/login' | jq -c '. | {access_token}' | sed 's/.*\":\"//' | sed 's/\"}//'
  register: awx_dimension_user_access_token

- name: Record Synapse variables locally on AWX
  delegate_to: 127.0.0.1
  lineinfile:
    path: '{{ awx_cached_matrix_vars }}'
    regexp: "^#? *{{ item.key | regex_escape() }}:"
    line: "{{ item.key }}: {{ item.value }}"
    insertafter: '# Dimension Settings Start'
  with_dict:
    'matrix_dimension_enabled': '{{ matrix_dimension_enabled }}'
    'matrix_dimension_access_token': '"{{ awx_dimension_user_access_token.stdout }}"'
  
- name: Set final users list if users are defined
  set_fact:
    awx_dimension_users_final: "{{ awx_dimension_users }}"
  when: awx_dimension_users | length > 0

- name: Set final users list if no users are defined
  set_fact:
    awx_dimension_users_final: '@dimension:{{ matrix_domain }}'
  when: awx_dimension_users | length == 0

- name: Remove Dimension Users
  delegate_to: 127.0.0.1
  replace:
    path: '{{ awx_cached_matrix_vars }}'
    regexp: '^  - .*\n'
    after: 'matrix_dimension_admins:'
    before: '# Dimension Settings End'

- name: Set Dimension Users Header
  delegate_to: 127.0.0.1
  lineinfile:
    path: '{{ awx_cached_matrix_vars }}'
    insertbefore: '# Dimension Settings End'
    line: "matrix_dimension_admins:"

- name: Set Dimension Users
  delegate_to: 127.0.0.1
  lineinfile:
    path: '{{ awx_cached_matrix_vars }}'
    insertafter: '^matrix_dimension_admins:'
    line: '  - "{{ item }}"'
  with_items: "{{ awx_dimension_users_final.splitlines() }}"

- name: Record Dimension Custom variables locally on AWX
  delegate_to: 127.0.0.1
  lineinfile:
    path: '{{ awx_cached_matrix_vars }}'
    regexp: "^#? *{{ item.key | regex_escape() }}:"
    line: "{{ item.key }}: {{ item.value }}"
    insertbefore: '# Dimension Settings End'
  with_dict:
    'awx_dimension_users': '{{ awx_dimension_users.splitlines() | to_json }}'

- name: Save new 'Configure Dimension' survey.json to the AWX tower, template
  delegate_to: 127.0.0.1
  template:
    src: 'roles/matrix-awx/surveys/configure_dimension.json.j2'
    dest: '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}//configure_dimension.json'

- name: Copy new 'Configure Dimension' survey.json to target machine
  copy:
    src: '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/configure_dimension.json'
    dest:  '/matrix/awx/configure_dimension.json'
    mode: '0660'

- name: Recreate 'Configure Dimension' job template
  delegate_to: 127.0.0.1
  awx.awx.tower_job_template:
    name: "{{ matrix_domain }} - 1 - Configure Dimension"
    description: "Configure Dimension, the self-hosted integrations server."
    extra_vars: "{{ lookup('file', '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/extra_vars.json') }}"
    job_type: run
    job_tags: "start,setup-all,setup-dimension"
    inventory: "{{ member_id }}"
    project: "{{ member_id }} - Matrix Docker Ansible Deploy"
    playbook: setup.yml
    credential: "{{ member_id }} - AWX SSH Key"
    survey_enabled: true
    survey_spec: "{{ lookup('file', '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/configure_dimension.json') }}"
    become_enabled: yes
    state: present
    verbosity: 1
    tower_host: "https://{{ awx_host }}"
    tower_oauthtoken: "{{ awx_session_token.ansible_facts.tower_token.token }}"
    validate_certs: yes