# 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: Configure DNS
  community.general.idrac_redfish_config:
    category: Manager
    command: SetManagerAttributes
    resource_id: iDRAC.Embedded.1
    manager_attributes:
      NIC.1.DNSRegister: "Enabled"
      NIC.1.DNSDomainName: "{{ dns_domain_name }}"
      IPv4Static.1.DNS1: "{{ ipv4_static_dns1 }}"
      IPv4Static.1.DNS2: "{{ ipv4_static_dns2 }}"
    baseuri: "{{ inventory_hostname }}"
    username: "{{ idrac_username }}"
    password: "{{ idrac_password }}"
    
- name: Configure SMTP
  community.general.idrac_redfish_config:
    category: Manager
    command: SetManagerAttributes
    resource_id: iDRAC.Embedded.1
    manager_attributes:
      RemoteHosts.1.SMTPServerIPAddress: "{{ smtp_server_ip }}"
      RemoteHosts.1.SMTPPort: 25
      RemoteHosts.1.SMTPAuthentication: "Enabled"
      RemoteHosts.1.SMTPUserName: "{{ smtp_username }}"
      RemoteHosts.1.SMTPPassword: "{{ smtp_password }}"
      EmailAlert.1.Address: "{{ use_email_address_2fa }}"
      EmailAlert.1.Enable: "Enabled"
    baseuri: "{{ inventory_hostname }}"
    username: "{{ idrac_username }}"
    password: "{{ idrac_password }}"
    
- name: Wait for one minute
  pause:
    minutes: 1
  run_once: true

- name: Sent SMTP test email
  command: sshpass -p {{ idrac_password }} ssh -o "StrictHostKeyChecking no" {{ idrac_username }}@{{ inventory_hostname }} 'racadm testemail -i 1'
  changed_when: false
  register: test_email_check
  retries: 5
  no_log: true
  failed_when: false
  until: not test_email_check.failed

- name: Check test email status
  assert:
    that: test_email_success_msg in test_email_check.stdout
    fail_msg: "{{ test_email_check_fail_msg }}"
    success_msg: "{{ test_email_check_success_msg }}"

- name: Disable email alert
  community.general.idrac_redfish_config:
    category: Manager
    command: SetManagerAttributes
    resource_id: iDRAC.Embedded.1
    manager_attributes:
      EmailAlert.1.Address: ""
      EmailAlert.1.Enable: "Disabled"
    baseuri: "{{ inventory_hostname }}"
    username: "{{ idrac_username }}"
    password: "{{ idrac_password }}"
