---

# Parameters:
#   playbook inventory
#     stage := the name of the stage (e.g. devnso, qanso, prodnso)
#     tenant := object with tenant related data
#       key := 
#       name := 
#     cluster := object with cluster specific data (optional)
#       ...
#     data := object with action specific data (optional)
#       ...
#   smardigo message callback
#     scope_id := (scope id of the management process)
#     process_instance_id := (process instance id of the management process)
#     smardigo_management_action := (smardigo management action anme of the management process)

#############################################################
#   Creating inventory dynamically for given parameters
#############################################################

- import_playbook: pmci-inventory-cluster.yml

#############################################################
#   Running the PMCI roles
#############################################################

- hosts: "stage_{{ stage }}:!{{ stage }}-virtual-host-to-read-groups-vars"
  serial: "{{ serial_number | default(1) }}"
  gather_facts: no
  remote_user: root
  vars:
    ansible_connection: local
    ansible_ssh_host: "{{ stage_server_domain }}"
    tenant_id: "{{ tenant.key }}" # legacy paramater, backwards compatibility
    cluster_name: "{{ cluster.key }}" # legacy paramater, backwards compatibility
    cluster_service: "{{ cluster.service }}"
    elastic_state: present
    elastic_users:
      -
        username: '{{ stage }}-{{ tenant_id }}'
        roles:
        - '{{ stage }}-{{ tenant_id }}'
        full_name: ''
        password: '{{ password | default( stage + "-" + tenant_id ) }}'
        email: '{{ email | default("") }}'
        enabled: true
        elastic_state: '{{ elastic_state }}'
    elastic_roles:
      -
        elastic_state: '{{ elastic_state }}'
        name: '{{ stage }}-{{ tenant_id }}'
        elasticsearch:
          cluster: []
          indices:
          - names:
            - '{{ stage }}-{{ tenant_id }}-*'
            privileges:
            - read
            - read_cross_cluster
            - view_index_metadata
            allow_restricted_indices: false
          run_as: []
        kibana:
        - base: []
          feature:
            advancedSettings:
            - all
            dashboard:
            - all
            dev_tools:
            - all
            discover:
            - all
            indexPatterns:
            - all
            savedObjectsManagement:
            - all
            visualize:
            - all
          spaces:
          - '{{ stage }}-{{ tenant_id }}'
    elastic_spaces:
      -
        elastic_state: '{{ elastic_state }}'
        id: &es_space_name '{{ stage }}-{{ tenant_id }}'
        name: '{{ stage }}-{{ tenant_id }}'
        description: ''
        disabledFeatures:
          - canvas
          - maps
          - ml
          - visualize
          - enterpriseSearch
          - logs
          - infrastructure
          - apm
          - uptime
          - observabilityCases
          - siem
          - monitoring
          - fleet
          - stackAlerts
          - actions
          - osquery
          - savedObjectsTagging

  tasks:
    - name: "Create Index Pattern for <{{ stage }}-{{ tenant_id }}-*>"
      set_fact:
        es_index_pattern_tenant: '{{ stage }}-{{ tenant_id }}-*'
        elastic_index_patterns:
          - id: "{{ stage }}-{{ tenant_id }}-*"
            name: "{{ stage }}-{{ tenant_id }}"
            search_name: "{{ stage }}-{{ tenant_id }}-*"
            dashboard_name: "{{ stage }}-{{ tenant_id }}-*"
            with_container_filter: false
      when:
        - cluster_name is not defined
        - cluster_service is not defined

    - name: "Create Index Pattern for <{{ stage }}-{{ tenant_id }}-{{ cluster_name }}>"
      set_fact:
        es_index_pattern_tenant: '{{ stage }}-{{ tenant_id }}-*'
        elastic_index_patterns:
          - id: "{{ stage }}-{{ tenant_id }}-*"
            name: "{{ stage }}-{{ tenant_id }}"
            search_name: "{{ stage }}-{{ tenant_id }}-*"
            dashboard_name: "{{ stage }}-{{ tenant_id }}-*"
          - id: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-*"
            name: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}"
            search_name: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-*"
            dashboard_name: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-*"
      when:
        - cluster_name is defined
        - cluster_service is not defined

    - name: "Create Index Pattern for <{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-{{ cluster_service }}>"
      set_fact:
        es_index_pattern_tenant: ''
        elastic_index_patterns:
          - id: "{{ stage }}-{{ tenant_id }}-*"
            name: "{{ stage }}-{{ tenant_id }}"
            search_name: "{{ stage }}-{{ tenant_id }}-*"
            dashboard_name: "{{ stage }}-{{ tenant_id }}-*"
          - id: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-*"
            name: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}"
            search_name: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-*"
            dashboard_name: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-*"
          - id: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-*-{{ cluster_service }}-*"
            name: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-{{ cluster_service }}"
            search_name: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-{{ cluster_service }}-*"
            dashboard_name: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-{{ cluster_service }}-*"
      when:
        - cluster_service is defined
        - cluster_name is defined

    - name: "Do some stuff in elastic with spaces ... "
      include_role:
        name: kibana
        tasks_from: _configure_spaces.yml
        apply:
          tags:
            - es-spaces
      loop: "{{ elastic_spaces }}"
      loop_control:
        loop_var: elastic_space
      tags:
        - es-spaces

    - name: "Do some stuff in elastic with roles ..."
      include_role:
        name: kibana
        tasks_from: _configure_roles.yml
        apply:
          tags:
            - es-roles
      loop: "{{ elastic_roles }}"
      loop_control:
        loop_var: elastic_role
      tags:
        - es-roles

    - name: "Do some stuff in elastic with users ..."
      include_role:
        name: kibana
        tasks_from: _configure_users.yml
        apply:
          tags:
            - es-users
      loop: "{{ elastic_users }}"
      loop_control:
        loop_var: elastic_user
      tags:
        - es-users

    - name: "Do some stuff in elastic with spaces ..."
      include_role:
        name: kibana
        tasks_from: _import_savedobjects.yml
        apply:
          tags:
            - es-importobjects
      vars:
        es_space: *es_space_name
        es_indexpattern_title: '{{ es_indexpattern_name }}'
        es_panel_uuid: "{{ 'panel_' + elastic_dashboard_name | to_uuid }}"
      tags:
        - es-importobjects

#############################################################
#   Sending smardigo management message to process
#############################################################

- import_playbook: pmci-callback.yml
