# 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: Ping the powervault device to validate connectivity
  command: ping -c1 {{ inventory_hostname }}
  register: validate_login
  changed_when: false
  ignore_errors: yes
  tags: install

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

- name: Get session key
  uri:
    url: https://{{ inventory_hostname }}/api/login/{{ auth_string.stdout | replace(" -", "") }}
    method: GET
    headers:
      {'datatype': 'json'}
    validate_certs: no
  register: session_key
  tags: install

- name: Set powervault ip
  add_host:
    name: "pv_ip"
    powervault_ip: "{{ inventory_hostname }}"
  tags: install

- name: Execute show system command
  uri:
    url: https://{{ inventory_hostname }}/api/show/system
    method: GET
    body_format: json
    validate_certs: no
    use_proxy: no
    headers:
      {'sessionKey': "{{ session_key.json.status[0].response }}", 'datatype':'json'}
  register: system_info
  tags: install

- name: Get the product id
  set_fact:
    pv_id: "{{ system_info.json.system[0]['product-id'] }}"

- name: Verify the product id and model no. of device
  fail:
    msg: "{{ fail_pv_support }}"
  when:
    - scsi_product_id not in system_info.json.system[0]['scsi-product-id']
    - pv_id  != "ME4084" or pv_id != "ME4024"  or pv_id != "ME4012"

- name: Set system name
  uri:
    url: https://{{ inventory_hostname }}/api/set/system/name/{{ powervault_me4_system_name }}
    method: GET
    body_format: json
    validate_certs: no
    use_proxy: no
    headers:
      {'sessionKey': "{{ session_key.json.status[0].response }}", 'datatype':'json'}
  register: system_name
  when: powervault_me4_system_name
  tags: install