# 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: Remove the SCP file if exists
  file:
    path: "{{ role_path }}/files/{{ scp_filename }}"
    state: absent
  run_once: true

- name: Create SCP file
  copy:
    src: "{{ role_path }}/files/temp_scp.xml"
    dest: "{{ role_path }}/files/{{ scp_filename }}"
    mode: preserve
  run_once: true

- name: Add system profile value to SCP file
  lineinfile:
    path: "{{ role_path }}/files/{{ scp_filename }}"
    line: "{{ item.line }}"
    insertafter: '^(.*)BootSeqRetry'
  when: idrac_system_profile == item.name
  with_items:
    - { name: "Performance", line: "  <Attribute Name=\"SysProfile\">PerfOptimized</Attribute>" }
    - { name: "PerformancePerWatt(OS)", line: "  <Attribute Name=\"SysProfile\">PerfPerWattOptimizedOs</Attribute>" }
    - { name: "PerformancePerWatt(DAPC)", line: "  <Attribute Name=\"SysProfile\">PerfPerWattOptimizedDapc</Attribute>" }
    - { name: "WorkstationPerformance", line: "  <Attribute Name=\"SysProfile\">PerfWorkStationOptimized</Attribute>" }
  run_once: true

- name: Add PXE attributes to SCP file
  lineinfile:
    path: "{{ role_path }}/files/{{ scp_filename }}"
    line: "{{ item }}"
    insertafter: '^(.*)SysProfile'
  with_items:
    - '  <Attribute Name="PxeDev1EnDis">Enabled</Attribute>'
    - '  <Attribute Name="PxeDev2EnDis">Enabled</Attribute>'
    - '  <Attribute Name="PxeDev3EnDis">Enabled</Attribute>'
    - '  <Attribute Name="PxeDev4EnDis">Enabled</Attribute>'
    - '  <Attribute Name="HttpDev1EnDis">Disabled</Attribute>'
    - '  <Attribute Name="HttpDev2EnDis">Disabled</Attribute>'
    - '  <Attribute Name="HttpDev3EnDis">Disabled</Attribute>'
    - '  <Attribute Name="HttpDev4EnDis">Disabled</Attribute>'
  when: provision_method == provision_method_pxe
  run_once: true

- name: Disable PXE attributes to SCP file
  lineinfile:
    path: "{{ role_path }}/files/{{ scp_filename }}"
    line: "{{ item }}"
    insertafter: '^(.*)SysProfile'
  with_items:
    - '  <Attribute Name="PxeDev1EnDis">Disabled</Attribute>'
    - '  <Attribute Name="PxeDev2EnDis">Disabled</Attribute>'
    - '  <Attribute Name="PxeDev3EnDis">Disabled</Attribute>'
    - '  <Attribute Name="PxeDev4EnDis">Disabled</Attribute>'
  when: provision_method == provision_method_idrac
  run_once: true

- name: Add SNMP community name attribute to SCP file
  lineinfile:
    path: "{{ role_path }}/files/{{ scp_filename }}"
    line: '  <Attribute Name="SNMP.1#AgentCommunity">{{ snmp_community_name }}</Attribute>'
    insertafter: '^(.*)SNMP.1#AgentEnable'
  run_once: true

- name: Add SNMP trap destination attributes to SCP file
  lineinfile:
    path: "{{ role_path }}/files/{{ scp_filename }}"
    line: "{{ item }}"
    insertafter: '^(.*)SNMP.1#AlertPort'
  with_items:
    - '  <Attribute Name="SNMPAlert.1#Destination">{{ snmp_trap_destination }}</Attribute>'
    - '  <Attribute Name="SNMPAlert.1#State">Enabled</Attribute>'
  when: snmp_trap_status
  run_once: true

- block:
    - name: Import SCP from a local path and wait for this job to get completed
      dellemc.openmanage.idrac_server_config_profile:
        idrac_ip: "{{ inventory_hostname }}"
        idrac_user: "{{ idrac_username }}"
        idrac_password: "{{ idrac_password }}"
        share_name: "{{ role_path }}/files/"
        command: "import"
        scp_file: "{{ scp_filename }}"
        scp_components: "ALL"
        shutdown_type: "Forced"
        job_wait: "True"
      register: import_scp_status
      until: not import_scp_status.failed
      retries: "{{ retries_count }}"
  rescue:
    - name: Import SCP failed
      fail:
        msg: "{{ import_scp_fail_msg }}"
        
- name: Remove the SCP file
  file:
    path: "{{ role_path }}/files/{{ scp_filename }}"
    state: absent
  run_once: true