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

  tasks:
  - name: oc delete storageclass (pre-test cleanup)
    oc_storageclass:
      name: testsc
      state: absent
      provisioner: aws-ebs  # Only works for test systems provisioned on AWS

  - name: create a storageclass
    oc_storageclass:
      name: testsc
      parameters:
        type: gp2
      default_storage_class: "true"
      provisioner: aws-ebs  # Only works for test systems provisioned on AWS
      mount_options: []
    register: sc_out
  - debug: var=sc_out

  - assert:
      that:
      - "sc_out.module_results.results[0]['metadata']['name'] == 'testsc'"
      - sc_out.changed
      - "sc_out.module_results.results[0]['parameters']['type'] == 'gp2'"
      msg: storageclass create failed.

  # FIXME: oc_storageclass is not idempotent
  ## Test idempotent create
  #- name: NOOP create the storageclass
  #  oc_storageclass:
  #    name: testsc
  #    parameters:
  #      type: gp2
  #    default_storage_class: "true"
  #    provisioner: aws-ebs  # Only works for test systems provisioned on AWS
  #    mount_options: []
  #  register: sc_out
  #
  #- assert:
  #    that:
  #    - "sc_out.module_results.results[0]['metadata']['name'] == 'testsc'"
  #    - sc_out.changed == False
  #    msg: oc_storageclass is not idempotent

  - name: test list storageclass
    oc_storageclass:
      name: testsc
      state: list
      provisioner: aws-ebs  # Only works for test systems provisioned on AWS
    register: sc_out
  - debug: var=sc_out

  - assert:
      that: "sc_out.module_results[0]['metadata']['name'] == 'testsc'"
      msg: storageclass list failed

  - name: update the storageclass
    oc_storageclass:
      name: testsc
      parameters:
        type: gp2
        encrypted: "true"
      default_storage_class: "true"
      mount_options:
      - debug
      reclaim_policy: Delete
      provisioner: aws-ebs  # Only works for test systems provisioned on AWS
    register: sc_out

  - assert:
      that: "sc_out.module_results.results[0]['parameters']['encrypted'] == 'true'"
      msg: storageclass update failed

  - assert:
      that: "sc_out.module_results.results[0]['reclaimPolicy'] == 'Delete'"
      msg: storageclass update failed

  - assert:
      that: "sc_out.module_results.results[0]['mountOptions'] == ['debug']"
      msg: storageclass update failed

  - name: oc delete storageclass
    oc_storageclass:
      name: testsc
      state: absent
      provisioner: aws-ebs  # Only works for test systems provisioned on AWS
    register: sc_out
  - debug: var=sc_out

  - assert:
      that:
      - "sc_out.module_results['returncode'] == 0"
      - "sc_out.module_results.results == {}"
      msg: storageclass delete failed

  - name: oc get storageclass
    oc_storageclass:
      name: testsc
      state: list
      provisioner: aws-ebs  # Only works for test systems provisioned on AWS
    register: sc_out
  - debug: var=sc_out

  - assert:
      that:
      - sc_out.changed == False
      - "sc_out.module_results == [{}]"
      msg: storageclass get failed
