#  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.

---
# Testcase OMNIA_1.1_EF_TC_002
# Execute ethernetfacts.yml with valid IP with valid credentials in ethernet inventory group
- name: OMNIA_1.1_EF_TC_002
  hosts: ethernet
  gather_facts: false
  tags: TC_002
  connection: local
  vars_files:
    - test_vars/test_ethernet_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
    - name: Execute ethernet_facts with valid creds and valid IP
      vars:
       inventory_name: "{{ eth_inventory_name }}"
       host_name: "{{ eth_host_name }}"
       template_name: "{{ fact_template_value }}"
       job_template_name: "{{ fact_job_name }}"
       playbook_path: "{{ eth_facts_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}"
      tags: TC_002
   
    - block:
       - name: Validate default flow with valid IP and valid credentials
         assert:
           that:
             - "'successful' in job_status.status"
           success_msg: "{{ success_message }}"
           fail_msg: "{{ fail_case }}"
         changed_when: false

# Testcase OMNIA_1.1_EF_TC_003
# Execute ethernetfacts.yml with Invalid IP in ethernet inventory group
- name: OMNIA_1.1_EF_TC_003
  hosts: ethernet
  gather_facts: false
  tags: TC_003
  connection: local
  vars_files:
    - test_vars/test_ethernet_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
    - name: setting ip
      set_fact:
        eth_host_name: "{{ random_ip }}"
         
    - name: Execute ethernet_facts with random IP
      vars:
       inventory_name: "{{ eth_inventory_name }}"
       host_name: "{{ eth_host_name }}"
       template_name: "{{ fact_template_value }}"
       job_template_name: "{{ fact_job_name }}"
       playbook_path: "{{ eth_facts_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}"

    - block:
        - name: Validate invalid IP and valid credentials
          assert:
            that:
              - "'failed' in job_status.status"
            success_msg: "{{ success_message }}"
            fail_msg: "{{ fail_case }}"
          changed_when: false

# Testcase OMNIA_1.1_EF_TC_001
# Execute ethernetfacts.yml with no hosts in ethernet inventory group
- name: OMNIA_1.1_EF_TC_001
  hosts: ethernet
  gather_facts: false
  tags: TC_001
  connection: local
  vars_files:
    - test_vars/test_ethernet_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
    - name: Execute ethernet_facts with no host details
      vars:
       inventory_name: "{{ eth_inventory_name }}"       
       template_name: "{{ fact_template_value }}"
       job_template_name: "{{ fact_job_name }}"
       playbook_path: "{{ eth_facts_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}"
    
    - block:
       - name: Validate no hosts and valid credentials
         assert:
           that:
             - "'successful' in job_status.status"
           success_msg: "{{ success_message }}"
           fail_msg: "{{ fail_case }}"
         changed_when: false

# Testcase OMNIA_1.1_EF_TC_004
# Execute ethernetfacts.yml with valid IP in ethernet inventory group with incorrect credentials
- name: OMNIA_1.1_EF_TC_004
  hosts: ethernet
  gather_facts: false
  tags: TC_004
  connection: local
  vars_files:
    - test_vars/test_ethernet_vars.yml
    - ../roles/webui_awx/vars/main.yml    
    - ../input_params/base_vars.yml
  tasks:
    - name: Making ethernet_credentials invalid
      tower_credential:
        name: "ethernet_credential"
        credential_type: "Machine"
        inputs:
          username: "{{ invalid_username }}"
       
    - name: Execute ethernet_facts with invalid credentials
      vars:
       inventory_name: "{{ eth_inventory_name }}"
       host_name: "{{ eth_host_name }}"
       template_name: "{{ fact_template_value }}"
       job_template_name: "{{ fact_job_name }}"
       playbook_path: "{{ eth_facts_playbook_path }}"
       delete_status: true
      include_tasks: "{{ awx_script_path }}"
     
    - block:
       - name: Validate valid IP and invalid credentials
         assert:
           that:
             - "'failed' in job_status.status"
           success_msg: "{{ success_message }}"
           fail_msg: "{{ fail_case }}"
         changed_when: false
         
    - name: Set credentials back to default
      tower_credential:
        name: "ethernet_credential"
        credential_type: "Machine"
        inputs:
          username: "{{ username }}"
          password: "{{ password }}"