---
- name: "Initialize VARs"
  set_fact:
    api_path: '/s/{{ es_space }}/api/saved_objects'
    es_object_type: 'index-pattern'
    index_pattern_exists: False
    elastic_index_pattern_cleaned: {}

- name: "Get all index patterns in elasticsearch"
  delegate_to: localhost
  uri:
    url: "{{ shared_service_url_kibana }}{{ api_path }}/_find?per_page=10000&type={{ es_object_type }}"
    method: GET
    status_code: [200]
    user: "{{ elastic_admin_username }}" 
    password: "{{ elastic_admin_password }}"
    force_basic_auth: yes
  register: all_index_patterns
  become: false

- name: "Lookup index pattern <{{ elastic_index_pattern.attributes.title }}>"
  set_fact:
    lookup_indexpattern_object: '{{ all_index_patterns.json | community.general.json_query(querystr1) | first | community.general.json_query(indexpattern_query) }}'
  vars:
    querystr1: "[saved_objects[*]]"
    indexpattern_query: "[?attributes.title=='{{ elastic_index_pattern.attributes.title }}']"

- name: "Set switch VAR"
  set_fact:
    index_pattern_exists: True 
  when:
    - lookup_indexpattern_object | length > 0

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

- name: "Create <{{ es_object_type }}> <{{ elastic_index_pattern.attributes.title }}>"
  debug:
    msg: "{{ elastic_index_pattern_cleaned }}"
  become: false
  when:
    - not index_pattern_exists
    - elastic_index_pattern.elastic_state == 'present'

- name: "Create <{{ es_object_type }}> <{{ elastic_index_pattern.attributes.title }}>"
  delegate_to: localhost
  uri:
    url: "{{ shared_service_url_kibana }}{{ api_path }}/{{ es_object_type }}"
    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_index_pattern_cleaned | to_json }}'
  become: false
  when:
    - not index_pattern_exists
    - elastic_index_pattern.elastic_state == 'present'

- name: "Update {{ es_object_type }} <<{{ elastic_index_pattern.attributes.title }}>>"
  delegate_to: localhost
  uri:
    url: '{{ shared_service_url_kibana }}{{ api_path }}/{{ es_object_type }}/{{ lookup_indexpattern_object[0]["id"] }}'
    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_index_pattern_cleaned | to_json }}'
  become: false
  when:
    - index_pattern_exists
    - elastic_index_pattern.elastic_state == 'present'

- name: "DELETE {{ es_object_type }} <<{{ elastic_index_pattern.attributes.title }}>>"
  delegate_to: localhost
  uri:
    url: '{{ shared_service_url_kibana }}{{ api_path }}/{{ es_object_type }}/{{ lookup_indexpattern_object[0]["id"] }}'
    method: DELETE
    status_code: [200]
    user: "{{ elastic_admin_username }}" 
    password: "{{ elastic_admin_password }}"
    force_basic_auth: yes
    headers:
      Content-Type: application/json
      kbn-xsrf: true
  become: false
  when:
    - index_pattern_exists
    - elastic_index_pattern.elastic_state == 'absent'
