#!/usr/bin/ansible-playbook --module-path=../../../library/
#
# ./oc_adm_manage_node.yml -e "cli_master_test=$OPENSHIFT_MASTER
---
- hosts: "{{ cli_master_test }}"
  gather_facts: no

  tasks:
  - name: get list of nodes
    oc_obj:
      state: list
      kind: node
    register: obj_out

  - name: Set the node to work with
    set_fact:
      node_to_test: "{{ obj_out['module_results']['results'][0]['items'][0]['metadata']['name'] }}"

  - name: list pods from a node
    oc_adm_manage_node:
      list_pods: True
      node:
      - "{{ node_to_test }}"
    register: podout
  - debug: var=podout

  - assert:
      that: "'{{ node_to_test }}' in podout.module_results.nodes"
      msg: Pod data was not returned

  - name: set node to unschedulable
    oc_adm_manage_node:
      schedulable: False
      node:
      - "{{ node_to_test }}"
    register: nodeout
  - debug: var=nodeout

  - name: assert that schedulable=False
    assert:
      that: nodeout.module_results.nodes[0]['schedulable'] == False
      msg: "{{ node_to_test }} schedulable set to True"

  - name: get node scheduable
    oc_obj:
      kind: node
      state: list
      name: "{{ node_to_test }}"
      namespace: None
    register: nodeout

  - debug: var=nodeout

  - name: assert that schedulable=False
    assert:
      that: nodeout.module_results.results[0]['spec']['unschedulable']

  - name: set node to schedulable
    oc_adm_manage_node:
      schedulable: True
      node:
      - "{{ node_to_test }}"
    register: nodeout
  - debug: var=nodeout

  - name: assert that schedulable=False
    assert:
      that: nodeout.module_results.nodes[0]['schedulable']
      msg: "{{ node_to_test }} schedulable set to False"
