# Test code for the UCS modules
# Copyright 2018, David Soper (@dsoper2)

- name: Test that we have a UCS host, UCS username, and UCS password
  fail:
    msg: 'Please define the following variables: ucs_hostname, ucs_username and ucs_password.'
  when: ucs_hostname is not defined or ucs_username is not defined or ucs_password is not defined
  vars:
    login_info: &login_info
      hostname: "{{ ucs_hostname }}"
      username: "{{ ucs_username }}"
      password: "{{ ucs_password }}"

# Setup (clean environment)
- name: Remove Network Control Policy
  ucs_managed_objects: &managed_objects_absent
    <<: *login_info
    objects:
    - module: ucsmsdk.mometa.nwctrl.NwctrlDefinition
      class: NwctrlDefinition
      properties:
        parent_mo_or_dn: org-root
        name: Enable-CDP-LLDP
    state: absent


# Test present (check_mode)
- name: Configure Network Control Policy
  ucs_managed_objects: &managed_objects_present
    <<: *login_info
    objects:
    - module: ucsmsdk.mometa.nwctrl.NwctrlDefinition
      class: NwctrlDefinition
      properties:
        parent_mo_or_dn: org-root
        cdp: enabled
        descr: ''
        lldp_receive: enabled
        lldp_transmit: enabled
        name: Enable-CDP-LLDP
  check_mode: yes
  register: cm_managed_objects_present


# Present (normal mode)
- name: Managed Objects present (normal mode)
  ucs_managed_objects: *managed_objects_present
  register: nm_managed_objects_present


# Test present again (idempotent)
- name: Managed Objects present again (check_mode)
  ucs_managed_objects: *managed_objects_present
  check_mode: yes
  register: cm_managed_objects_present_again


# Present again (normal mode)
- name: Managed Objects present again (normal mode)
  ucs_managed_objects: *managed_objects_present
  register: nm_managed_objects_present_again


# Verfiy present
- name: Verify Managed Objects present results
  assert:
    that:
    - cm_managed_objects_present.changed == nm_managed_objects_present.changed == true
    - cm_managed_objects_present_again.changed == nm_managed_objects_present_again.changed == false


# Test change (check_mode)
- name: Managed Objects change (check_mode)
  ucs_managed_objects: &managed_objects_change
    <<: *login_info
    objects:
    - module: ucsmsdk.mometa.nwctrl.NwctrlDefinition
      class: NwctrlDefinition
      properties:
        parent_mo_or_dn: org-root
        cdp: enabled
        descr: Testing Ansible
        lldp_receive: enabled
        lldp_transmit: enabled
        name: Enable-CDP-LLDP
  check_mode: yes
  register: cm_managed_objects_change


# Change (normal mode)
- name: Managed Objects change (normal mode)
  ucs_managed_objects: *managed_objects_change
  register: nm_managed_objects_change


# Test change again (idempotent)
- name: Managed Objects again (check_mode)
  ucs_managed_objects: *managed_objects_change
  check_mode: yes
  register: cm_managed_objects_change_again


# Change again (normal mode)
- name: Managed Objects change again (normal mode)
  ucs_managed_objects: *managed_objects_change
  register: nm_managed_objects_change_again


# Verfiy change
- name: Verify Managed Objects change results
  assert:
    that:
    - cm_managed_objects_change.changed == nm_managed_objects_change.changed == true
    - cm_managed_objects_change_again.changed == nm_managed_objects_change_again.changed == false


# Teardown (clean environment)
- name: Managed Objects absent (check_mode)
  ucs_managed_objects: *managed_objects_absent
  check_mode: yes
  register: cm_managed_objects_absent


# Absent (normal mode)
- name: Managed Objects absent (normal mode)
  ucs_managed_objects: *managed_objects_absent
  register: nm_managed_objects_absent


# Test absent again (idempotent)
- name: Managed Objects absent again (check_mode)
  ucs_managed_objects: *managed_objects_absent
  check_mode: yes
  register: cm_managed_objects_absent_again


# Absent again (normal mode)
- name: Managed Objects absent again (normal mode)
  ucs_managed_objects: *managed_objects_absent
  register: nm_managed_objects_absent_again


# Verfiy absent
- name: Verify Managed Objects absent results
  assert:
    that:
    - cm_managed_objects_absent.changed == nm_managed_objects_absent.changed == true
    - cm_managed_objects_absent_again.changed == nm_managed_objects_absent_again.changed == false


# Setup Boot Policy (clean environment)
- name: Remove Boot Policy
  ucs_managed_objects: &boot_managed_objects_absent
    <<: *login_info
    objects:
    - {
          "module": "ucsmsdk.mometa.lsboot.LsbootPolicy",
          "class": "LsbootPolicy",
          "properties": {
              "parent_mo_or_dn": "org-root",
              "name": "Python_SDS"
          }
      }
    state: absent


# Test present (check_mode)
- name: Configure Boot Policy
  ucs_managed_objects: &boot_managed_objects_present
    <<: *login_info
    objects:
    - {
          "module": "ucsmsdk.mometa.lsboot.LsbootPolicy",
          "class": "LsbootPolicy",
          "properties": {
              "parent_mo_or_dn": "org-root",
              "name": "Python_SDS",
              "enforce_vnic_name": "yes",
              "boot_mode": "legacy",
              "reboot_on_update": "no"
          },
          "children": [
              {
                  "module": "ucsmsdk.mometa.lsboot.LsbootVirtualMedia",
                  "class": "LsbootVirtualMedia",
                  "properties": {
                      "access": "read-only-local",
                      "lun_id": "0",
                      "order": "2"
                  }
              },
              {
                  "module": "ucsmsdk.mometa.lsboot.LsbootStorage",
                  "class": "LsbootStorage",
                  "properties": {
                      "order": "1"
                  },
                  "children": [
                      {
                          "module": "ucsmsdk.mometa.lsboot.LsbootLocalStorage",
                          "class": "LsbootLocalStorage",
                          "properties": {},
                          "children": [
                              {
                                  "module": "ucsmsdk.mometa.lsboot.LsbootDefaultLocalImage",
                                  "class": "LsbootDefaultLocalImage",
                                  "properties": {
                                      "order": "1"
                                  }
                              }
                          ]
                      }
                  ]
              }
          ]
      }
  check_mode: yes
  register: cm_boot_managed_objects_present


# Present (normal mode)
- name: Boot Managed Objects present (normal mode)
  ucs_managed_objects: *boot_managed_objects_present
  register: nm_boot_managed_objects_present


# Test present again (idempotent)
- name: Boot Managed Objects present again (check_mode)
  ucs_managed_objects: *boot_managed_objects_present
  check_mode: yes
  register: cm_boot_managed_objects_present_again


# Present again (normal mode)
- name: Boot Managed Objects present again (normal mode)
  ucs_managed_objects: *boot_managed_objects_present
  register: nm_boot_managed_objects_present_again


# Verfiy present
- name: Verify Boot Managed Objects present results
  assert:
    that:
    - cm_boot_managed_objects_present.changed == nm_boot_managed_objects_present.changed == true
    - cm_boot_managed_objects_present_again.changed == nm_boot_managed_objects_present_again.changed == false


# Test change (check_mode)
- name: Boot Managed Objects change (check_mode)
  ucs_managed_objects: &boot_managed_objects_change
    <<: *login_info
    objects:
    - {
          "module": "ucsmsdk.mometa.lsboot.LsbootPolicy",
          "class": "LsbootPolicy",
          "properties": {
              "parent_mo_or_dn": "org-root",
              "name": "Python_SDS",
              "enforce_vnic_name": "yes",
              "boot_mode": "legacy",
              "reboot_on_update": "yes"
          },
          "children": [
              {
                  "module": "ucsmsdk.mometa.lsboot.LsbootVirtualMedia",
                  "class": "LsbootVirtualMedia",
                  "properties": {
                      "access": "read-only-local",
                      "lun_id": "0",
                      "order": "2"
                  }
              },
              {
                  "module": "ucsmsdk.mometa.lsboot.LsbootStorage",
                  "class": "LsbootStorage",
                  "properties": {
                      "order": "1"
                  },
                  "children": [
                      {
                          "module": "ucsmsdk.mometa.lsboot.LsbootLocalStorage",
                          "class": "LsbootLocalStorage",
                          "properties": {},
                          "children": [
                              {
                                  "module": "ucsmsdk.mometa.lsboot.LsbootDefaultLocalImage",
                                  "class": "LsbootDefaultLocalImage",
                                  "properties": {
                                      "order": "1"
                                  }
                              }
                          ]
                      }
                  ]
              }
          ]
      }
  check_mode: yes
  register: cm_boot_managed_objects_change


# Change (normal mode)
- name: Boot Managed Objects change (normal mode)
  ucs_managed_objects: *boot_managed_objects_change
  register: nm_boot_managed_objects_change


# Test change again (idempotent)
- name: Boot Managed Objects again (check_mode)
  ucs_managed_objects: *boot_managed_objects_change
  check_mode: yes
  register: cm_boot_managed_objects_change_again


# Change again (normal mode)
- name: Boot Managed Objects change again (normal mode)
  ucs_managed_objects: *boot_managed_objects_change
  register: nm_boot_managed_objects_change_again


# Verfiy change
- name: Verify Boot Managed Objects change results
  assert:
    that:
    - cm_boot_managed_objects_change.changed == nm_boot_managed_objects_change.changed == true
    - cm_boot_managed_objects_change_again.changed == nm_boot_managed_objects_change_again.changed == false


# Teardown (clean environment)
- name: Boot Managed Objects absent (check_mode)
  ucs_managed_objects: *boot_managed_objects_absent
  check_mode: yes
  register: cm_boot_managed_objects_absent


# Absent (normal mode)
- name: Boot Managed Objects absent (normal mode)
  ucs_managed_objects: *boot_managed_objects_absent
  register: nm_boot_managed_objects_absent


# Test absent again (idempotent)
- name: Boot Managed Objects absent again (check_mode)
  ucs_managed_objects: *boot_managed_objects_absent
  check_mode: yes
  register: cm_boot_managed_objects_absent_again


# Absent again (normal mode)
- name: Boot Managed Objects absent again (normal mode)
  ucs_managed_objects: *boot_managed_objects_absent
  register: nm_boot_managed_objects_absent_again


# Verfiy absent
- name: Verify Boot Managed Objects absent results
  assert:
    that:
    - cm_boot_managed_objects_absent.changed == nm_boot_managed_objects_absent.changed == true
    - cm_boot_managed_objects_absent_again.changed == nm_boot_managed_objects_absent_again.changed == false
