---

- name: Converge
  hosts: localhost
  connection: local
  vars:
    ansible_python_interpreter: '{{ ansible_playbook_python }}'
    deploy_dir: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/deploy"
    image_name: smartgateway.infra.watch/smart-gateway-operator:testing
    custom_resource: "{{ lookup('file', '/'.join([deploy_dir, 'crds/smartgateway_v1alpha1_smartgateway_cr.yaml'])) | from_yaml }}"
  tasks:
  - name: Create the smartgateway.infra.watch/v1alpha1.SmartGateway
    k8s:
      namespace: '{{ namespace }}'
      definition: "{{ lookup('file', '/'.join([deploy_dir, 'crds/smartgateway_v1alpha1_smartgateway_cr.yaml'])) }}"

  - name: Get the newly created Custom Resource
    debug:
      msg: "{{ lookup('k8s', group='smartgateway.infra.watch', api_version='v1alpha1', kind='SmartGateway', namespace=namespace, resource_name=custom_resource.metadata.name) }}"

  - name: Wait 40s for reconciliation to run
    k8s_facts:
      api_version: 'v1alpha1'
      kind: 'SmartGateway'
      namespace: '{{ namespace }}'
      name: '{{ custom_resource.metadata.name }}'
    register: reconcile_cr
    until:
    - "'Successful' in (reconcile_cr | json_query('resources[].status.conditions[].reason'))"
    delay: 4
    retries: 10

- import_playbook: "{{ playbook_dir }}/../default/asserts.yml"
