# Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
---

- name: Include powervault_me4_vars.yml
  include_vars: "{{ playbook_dir }}/control_plane/input_params/powervault_me4_vars.yml"

- name: Include powervault_me4 role variables
  include_vars: "{{ playbook_dir }}/control_plane/roles/powervault_me4/vars/main.yml"

- name: Check if login_vars.yml file is encrypted
  command: cat {{ login_pv_file }}
  changed_when: false
  no_log: true
  register: config_content
  run_once: true

- name: Decrpyt login_vars.yml
  command: >-
    ansible-vault decrypt {{ login_pv_file }}
    --vault-password-file {{ login_pv_vault_file }}
  changed_when: false
  run_once: true
  when: "'$ANSIBLE_VAULT;' in config_content.stdout"

- name: Include variable file login_vars.yml
  include_vars: "{{ login_pv_file }}"
  no_log: true

- name: Get auth string
  shell: echo -n {{ powervault_me4_username }}_{{ powervault_me4_password }}  | sha256sum
  register: map_auth_string
  changed_when: false
  ignore_errors: yes
  delegate_to: localhost
  tags: install

- name: Get session key
  uri:
    url: https://{{ groups['powervault_me4'][0] }}/api/login/{{ map_auth_string.stdout | replace(" -", "") }}
    method: GET
    headers:
      {'datatype': 'json'}
    validate_certs: no
  register: map_session_key
  delegate_to: localhost
  tags: install

- name: Map volume
  uri:
    url: https://{{ groups['powervault_me4'][0] }}/api/map/volume/{{ powervault_me4_k8s_volume_name }}/access/{{ access }}/ports/{{ item.0 }}/lun/{{ lun1 }}/initiator/{{ hostvars['server_iqdn_id']['server_iqdn'] }}
    method: GET
    body_format: json
    validate_certs: no
    use_proxy: no
    headers:
      {'sessionKey': "{{ map_session_key.json.status[0].response }}", 'datatype':'json'}
  register: map_vol1
  with_together:
    - "{{ up_port }}"
  delegate_to: localhost
  tags: install

- name: Map volume
  uri:
    url: https://{{ groups['powervault_me4'][0] }}/api/map/volume/{{ powervault_me4_slurm_volume_name }}/access/{{ access }}/ports/{{ item.0 }}/lun/{{ lun2 }}/initiator/{{ hostvars['server_iqdn_id']['server_iqdn']  }}
    method: GET
    body_format: json
    validate_certs: no
    use_proxy: no
    headers:
      {'sessionKey': "{{ map_session_key.json.status[0].response }}", 'datatype':'json'}
  register: map_vol2
  with_together:
    - "{{ up_port }}"
  delegate_to: localhost
  tags: install

- name: Encypt login_vars.yml
  command: >-
    ansible-vault encrypt {{ login_pv_file }}
    --vault-password-file {{ login_pv_vault_file }}
  changed_when: false
  run_once: true
  when: "'$ANSIBLE_VAULT;' in config_content.stdout"