defmodule RecycledCloud.OpenNebula.VM do @moduledoc """ OpenNebula VM: http://docs.opennebula.io/5.12/integration/system_interfaces/api.html#one-vm-info """ alias RecycledCloud.OpenNebula, as: ONE alias RecycledCloud.OpenNebula.Schema @states [ {:any, -2}, {:any_except_done, -1}, {:init, 0}, {:pending, 1}, {:hold, 2}, {:active, 3}, {:stopped, 4}, {:suspended, 5}, {:done, 6}, {:failure, 7}, {:poweroff, 8}, {:undeployed, 9}, {:cloning, 10}, {:cloning_failure, 11} ] def state_for(state) when is_atom(state) do @states |> Keyword.get(state) end def state_for(state) when is_integer(state) do case Enum.find(@states, fn {atom, value} -> value == state end) do {atom, _value} -> atom nil -> nil end end def get(id) do case ONE.query("one.vm.info", [id]) do {:ok, raw} -> data = raw |> Schema.scan("vm") |> Schema.map_record {:ok, data} {:error, err} -> {:error, err} end end end