meta/lib/recycledcloud/opennebula.ex

45 lines
1.1 KiB
Elixir

defmodule RecycledCloud.OpenNebula do
@moduledoc """
OpenNebula XML-RPC Interface.
See http://docs.opennebula.io/5.12/integration/system_interfaces/api.html for details.
"""
# OpenNebula daemon.
@endpoint "/RPC2"
defp get_opennebula_config(key) do
Application.get_env(:recycledcloud, :opennebula, []) |> Keyword.get(key)
end
##
# Related to XML-RPC calls
defp get_auth_string() do
"#{get_opennebula_config(:user)}:#{get_opennebula_config(:password)}"
end
defp post!(call, endpoint) do
url = get_opennebula_config(:server) <> endpoint
opts = []
headers = []
body = call |> XMLRPC.encode!
response = HTTPoison.post!(url, body, headers, opts).body |> XMLRPC.decode!
case response do
%{fault_code: _, fault_string: err} -> {:error, err}
%{param: [false, err | _]} -> {:error, err}
%{param: [true, result | _]} -> {:ok, result}
end
end
def query(method, params) do
call = %XMLRPC.MethodCall{
method_name: method,
params: [get_auth_string() | params]
}
call |> post!(@endpoint)
end
end