---
- name: Get state of VirtualMachine
  redhat.openshift_virtualization.kubevirt_vm_info:
    name: "{{ item }}"
    namespace: "{{ vm_namespace }}"
  register: state

- name: List snapshots
  kubernetes.core.k8s_info:
    api_version: snapshot.kubevirt.io/v1alpha1
    kind: VirtualMachineSnapshot
    namespace: "{{ vm_namespace }}"
  register: snapshot

- name: Set snapshot name for {{ item }}
  ansible.builtin.set_fact:
    latest_snapshot: "{{ snapshot.resources | selectattr('spec.source.name', 'equalto', item) | sort(attribute='metadata.creationTimestamp') | first }}"

- name: Stop VirtualMachine
  redhat.openshift_virtualization.kubevirt_vm:
    name: "{{ item }}"
    namespace: "{{ vm_namespace }}"
    running: false
    wait: true
  when: state.resources.0.spec.running

- name: Restore a VirtualMachineSnapshot
  kubernetes.core.k8s:
    definition:
      apiVersion: snapshot.kubevirt.io/v1alpha1
      kind: VirtualMachineRestore
      metadata:
        generateName: "{{ latest_snapshot.metadata.generateName }}"
        namespace: "{{ vm_namespace }}"
      spec:
        target:
          apiGroup: kubevirt.io
          kind: VirtualMachine
          name: "{{ item }}"
        virtualMachineSnapshotName: "{{ latest_snapshot.metadata.name }}"
    wait: true
    wait_condition:
      type: Ready

- name: Start VirtualMachine
  redhat.openshift_virtualization.kubevirt_vm:
    name: "{{ item }}"
    namespace: "{{ vm_namespace }}"
    running: true
    wait: true
  when: state.resources.0.spec.running
