meta/lib/recycledcloud/opennebula/vm_pool.ex
2021-02-11 14:34:40 +01:00

53 lines
1.4 KiB
Elixir

defmodule RecycledCloud.OpenNebula.VMPool do
@moduledoc """
OpenNebula VM Pool: http://docs.opennebula.io/5.12/integration/system_interfaces/api.html#one-vmpool-info
"""
alias RecycledCloud.OpenNebula, as: ONE
alias RecycledCloud.OpenNebula.Schema
# Filters ressourses.
# (user) ID >= 0 matches an user's resources.
@filter_flags [
{:user_primary_group, -4},
{:user, -3},
{:all, -2},
{:user_and_groups, -1}
]
def get(%{filter_flag: filter_flag} = params) when is_atom(filter_flag) do
params
|> Map.put(:filter_flag, Keyword.get(@filter_flags, filter_flag))
|> get()
end
def get(%{range_start: :infinite} = params) do
params |> Map.put(:range_start, -1) |> get()
end
def get(%{range_end: :infinite} = params) do
params |> Map.put(:range_end, -1) |> get()
end
def get(%{state_filter: state} = params) when is_atom(state) do
params |> Map.put(:state_filter, ONE.VM.state_for(state)) |> get()
end
def get(%{
filter_flag: filter_flag,
range_start: range_start,
range_end: range_end,
state_filter: state,
kv_filter: kv_filter
}) do
params = [filter_flag, range_start, range_end, state, kv_filter]
case ONE.query("one.vmpool.info", params) do
{:ok, raw} ->
data = raw
|> Schema.scan("vm_pool")
|> Schema.map_record
{:ok, data}
{:error, err} -> {:error, err}
end
end
end