#!/usr/bin/ansible-playbook --module-path=../../../library/:../../../../lib_utils/library

---
- hosts: "{{ cli_master_test }}"
  gather_facts: no

  vars:
    template_name: mysql-ephemeral
    ns_name: test

  tasks:
  - name: get the mysql-ephemeral template
    oc_obj:
      name: mysql-ephemeral
      state: list
      namespace: openshift
      kind: template
    register: mysqltempl

  - name: fix namespace
    yedit:
      src: /tmp/mysql-template
      key: metadata.namespace
      value: test
      backup: false
      content: "{{ mysqltempl.module_results.results[0] | to_yaml }}"

  - name: create the test namespace
    oc_obj:
      name: test
      state: present
      namespace: test
      kind: namespace
      content:
        path: /tmp/ns_test
        data:
          apiVersion: v1
          kind: Namespace
          metadata:
            name: test
          spec:
            finalizers:
            - openshift.io/origin
            - kubernetes

  - name: create the mysql-ephemeral template
    oc_obj:
      name: mysql-ephemeral
      state: present
      namespace: test
      kind: template
      files:
      - /tmp/mysql-template
      delete_after: True

  - name: process mysql-ephemeral
    oc_process:
      template_name: mysql-ephemeral
      namespace: test
      params:
        NAMESPACE: test
        DATABASE_SERVICE_NAME: testdb
      create: False
      reconcile: false
    register: procout

  # These assertions could fail due to changes in item order
  - assert:
      that:
      - not procout.changed
      - procout.module_results.results['items'][0]['metadata']['name'] == 'testdb'
      - procout.module_results.results['items'][0]['kind'] == 'Secret'
      - procout.module_results.results['items'][2]['metadata']['name'] == 'testdb'
      - procout.module_results.results['items'][2]['kind'] == 'DeploymentConfig'
      msg: process failed on template

  - name: remove namespace test
    oc_obj:
      kind: namespace
      name: test
      namespace: test
      state: absent
