# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

---

- name:                                    "SAP CAL Integration"
  hosts:                                   "{{ sap_sid | upper }}_DB  :
                                            {{ sap_sid | upper }}_SCS :
                                            {{ sap_sid | upper }}_PAS :
                                            {{ sap_sid | upper }}_APP"
  become:                                  true
  gather_facts:                            true
  vars_files:                              vars/ansible-input-api.yaml
  tasks:
    - name:                                "SAP-CAL Integration"
      become:                              true
      when:
        - ansible_os_family | upper == "SUSE" or ansible_os_family | upper == "REDHAT"
        - enable_sap_cal is defined and enable_sap_cal
      block:
        - name:                            "6.0.0-sapcal-install - Extend logical volumes"
          when: ansible_os_family | upper == "REDHAT"
          ansible.builtin.include_role:
            name:                          roles-os/1.5.3-disk-setup-sapcal

        - name:                            "Retrieve Resource Group Name and ResourceID"
          ansible.builtin.uri:
            url:                           http://169.254.169.254/metadata/instance?api-version=2021-02-01
            use_proxy:                     false
            headers:
              Metadata:                    true
          register: azure_metadata

        - name:                            "Set ResourceID for SCS"
          ansible.builtin.set_fact:
            subscription_id:               "{{ azure_metadata.json.compute.subscriptionId }}"
            resource_group_name:           "{{ azure_metadata.json.compute.resourceGroupName }}"
            scs_resource_id:               "{{ azure_metadata.json.compute.resourceId }}"
            scs_physical_hostname:         "{{ ansible_hostname }}"
            scs_virtual_hostname:          "{{ virtual_host }}"
          when:
            - "'scs' in supported_tiers"
            - not scs_high_availability

        - name:                            "Set ResourceID for DB"
          ansible.builtin.set_fact:
            db_resource_id:                "{{ azure_metadata.json.compute.resourceId }}"
            db_physical_hostname:          "{{ ansible_hostname }}"
            db_virtual_hostname:           "{{ virtual_host }}"
          when:
            - "'hana' in supported_tiers"
            - not db_high_availability

        - name:                            "Set ResourceID for PAS"
          ansible.builtin.set_fact:
            pas_resource_id:               "{{ azure_metadata.json.compute.resourceId }}"
            pas_physical_hostname:         "{{ ansible_hostname }}"
            pas_virtual_hostname:          "{{ virtual_host }}"
          when:
            - "'pas' in supported_tiers"

        - name:                            "Set ResourceID for APP"
          ansible.builtin.set_fact:
            app_resource_id:               "{{ azure_metadata.json.compute.resourceId }}"
            app_physical_hostname:         "{{ ansible_hostname }}"
            app_virtual_hostname:          "{{ virtual_host }}"
          when:
            - "'app' in supported_tiers"

- name:                                    "Provision a new SAP environment"
  hosts:                                   localhost
  connection:                              local
  gather_facts:                            true
  vars_files:                              vars/ansible-input-api.yaml
  tasks:

    - name:                                "Check if Enable SAP CAL is true"
      ansible.builtin.assert:
        that:
          - enable_sap_cal is defined
          - enable_sap_cal | bool
        fail_msg: "Please set enable_sap_cal to true in the sap-parameters.yaml file to enable SAP CAL integration"

    - name:                                Run the keyvault role
      ansible.builtin.include_role:
        name:                              roles-misc/0.2-kv-secrets
      vars:
        operation:                         sapcal
      tags:
                                           - kv-secrets

# Once the Ansible Module is updated, this task will be moved to OS configuration playbook
    - name:                                "SAP-CAL Integration: - Ensure azure-keyvault is installed"
      become:                              true
      when:                                enable_sap_cal is defined and enable_sap_cal
      block:
        - name:                            "SAP-CAL Integration: - Ensure azure-keyvault is installed"
          ansible.builtin.pip:
            name:
              - azure-keyvault==1.1.0
              - azure-keyvault-secrets
            state:                         present
          tags:
            - always

        - name:                            "Set facts from other hosts"
          ansible.builtin.set_fact:
            "{{ item.key }}": "{{ hostvars[groups[sap_sid | upper + '_' + item.value][0]][item.key] }}"
          loop:
            - { key: 'subscription_id',         value: 'SCS' }
            - { key: 'resource_group_name',     value: 'SCS' }
            - { key: 'scs_resource_id',         value: 'SCS' }
            - { key: 'scs_physical_hostname',   value: 'SCS' }
            - { key: 'scs_virtual_hostname',    value: 'SCS' }
            - { key: 'db_resource_id',          value: 'DB'  }
            - { key: 'db_physical_hostname',    value: 'DB'  }
            - { key: 'db_virtual_hostname',     value: 'DB'  }
            - { key: 'pas_resource_id',         value: 'PAS' }
            - { key: 'pas_physical_hostname',   value: 'PAS' }
            - { key: 'pas_virtual_hostname',    value: 'PAS' }
            - { key: 'app_resource_id',         value: 'APP' }
            - { key: 'app_physical_hostname',   value: 'APP' }
            - { key: 'app_virtual_hostname',    value: 'APP' }

    - name:                                "6.0.0-sapcal-install - CALL SAP CAL API"
      when:                                enable_sap_cal is defined and enable_sap_cal
      block:
        - name:                            "Import the 6.0.0-sapcal-install role"
          ansible.builtin.import_role:
            name:                          "roles-sap/6.0.0-sapcal-install"
