---
- name: "Initialize VARs"
  set_fact:
    api_path: '/api/spaces/space'
    space_exists: False
    elastic_space_cleaned: {}

- name: "Get all spaces in elasticsearch"
  delegate_to: localhost
  uri:
    url: "https://{{ kibana_api_endpoint }}{{ api_path }}"
    method: GET
    status_code: [200]
    user: "{{ elastic_admin_username }}"
    password: "{{ elastic_admin_password }}"
    force_basic_auth: yes
  retries: 5
  delay: 15
  register: all_spaces
  until: all_spaces.status in [200]
  become: false

- name: "Lookup space object if exists"
  set_fact:
    lookup_space_object: "{{ all_spaces.json | community.general.json_query(spaces_query) }}"
  vars:
    spaces_query: "[?name=='{{ elastic_space.name }}']"

- name: "Set switch VAR"
  set_fact:
    space_exists: True 
  when:
    - lookup_space_object | length > 0

- name: "Drop not needed keys from dict"
  set_fact:
    elastic_space_cleaned: "{{ elastic_space_cleaned | combine({item.key: item.value}) }}"
  with_dict: "{{ elastic_space }}"
  when:
    - item.key not in ['elastic_state']

- name: "DEBUG"
  debug:
    msg: "{{ lookup_space_object | to_json }}"

- name: "Create space <<{{ elastic_space.name }}>>"
  delegate_to: localhost
  uri:
    url: "https://{{ kibana_api_endpoint }}{{ api_path }}"
    method: POST
    status_code: [200]
    user: "{{ elastic_admin_username }}"
    password: "{{ elastic_admin_password }}"
    force_basic_auth: yes
    headers:
      Content-Type: application/json
      kbn-xsrf: true
    body_format: json
    body: '{{ elastic_space_cleaned | to_json }}'
  become: false
  when:
    - not space_exists
    - elastic_space.elastic_state == 'present'

- name: "Update space <<{{ elastic_space.name }}>>"
  delegate_to: localhost
  uri:
    url: "https://{{ kibana_api_endpoint }}{{ api_path }}/{{ elastic_space.name }}"
    method: PUT
    status_code: [200]
    user: "{{ elastic_admin_username }}"
    password: "{{ elastic_admin_password }}"
    force_basic_auth: yes
    headers:
      Content-Type: application/json
      kbn-xsrf: true
    body_format: json
    body: '{{ elastic_space_cleaned | to_json }}'
  become: false
  when:
    - space_exists
    - elastic_space.elastic_state == 'present'

- name: "Update space advanced settings <<{{ elastic_space.name }}>>"
  delegate_to: localhost
  uri:
    url: "https://{{ kibana_api_endpoint }}/s/{{ elastic_space.name }}/api/kibana/settings"
    method: POST
    status_code: [200]
    user: "{{ elastic_admin_username }}"
    password: "{{ elastic_admin_password }}"
    force_basic_auth: yes
    headers:
      Content-Type: application/json
      kbn-xsrf: true
    body_format: json
    body: '{{ kibana_advanced_settings | to_json }}'
  become: false
  when:
    - space_exists
    - elastic_space.elastic_state == 'present'

- name: "DELETE space <<{{ elastic_space.name }}>>"
  delegate_to: localhost
  uri:
    url: "https://{{ kibana_api_endpoint }}{{ api_path }}/{{ elastic_space.name }}"
    method: DELETE
    status_code: [204]
    user: "{{ elastic_admin_username }}"
    password: "{{ elastic_admin_password }}"
    force_basic_auth: yes
    headers:
      Content-Type: application/json
      kbn-xsrf: true
  become: false
  when:
    - space_exists
    - elastic_space.elastic_state == 'absent'
