meta/lib/meta_web/controllers/virtual_machine_hosting_con...

60 lines
1.9 KiB
Elixir
Raw Normal View History

defmodule MetaWeb.VirtualMachineHostingController do
use MetaWeb, :controller
2021-04-07 17:23:48 +02:00
alias Meta.OpenNebula, as: ONE
2021-04-07 17:23:48 +02:00
def index(conn, _params) do
username = conn.assigns.current_user.username
2021-04-09 11:36:33 +02:00
vms = ONE.get_vms_for_user(username)
2021-04-07 17:23:48 +02:00
render(conn, "index.html", vms: vms)
end
2021-04-09 11:36:33 +02:00
# def start(conn, %{"id" => id}) do
# case VM.execute(String.to_integer(id), "resume") do
# :ok ->
# conn
# |> put_flash(:info, "Start request sent to VMM.")
# |> redirect(to: Routes.virtual_machine_hosting_path(conn, :index))
# {:error, err} ->
# conn
# |> put_flash(:error, "Something went wrong: #{inspect(err)}")
# |> redirect(to: Routes.virtual_machine_hosting_path(conn, :index))
# end
# end
#
# def stop(conn, %{"id" => id}) do
# case VM.execute(String.to_integer(id), "poweroff") do
# :ok ->
# conn
# |> put_flash(:stop, "Stop request sent to VMM.")
# |> redirect(to: Routes.virtual_machine_hosting_path(conn, :index))
# {:error, err} ->
# conn
# |> put_flash(:error, "Something went wrong: #{inspect(err)}")
# |> redirect(to: Routes.virtual_machine_hosting_path(conn, :index))
# end
# end
2021-04-07 17:23:48 +02:00
2021-04-09 11:36:33 +02:00
def show(conn, %{"location" => location, "id" => id}) do
case ONE.get_vm(location, String.to_integer(id)) do
2021-04-07 17:23:48 +02:00
{:error, err} ->
conn
|> put_flash(:error, "Could not fetch VM details: #{inspect(err)}")
|> redirect(to: Routes.virtual_machine_hosting_path(conn, :index))
{:ok, vm} ->
owner = vm |> Map.get(:UNAME) |> List.to_string
if owner == conn.assigns.current_user.username do
conn
2021-04-09 11:36:33 +02:00
|> assign(:location, location)
2021-04-07 17:23:48 +02:00
|> assign(:vm, vm)
|> render("show.html")
else
conn
|> put_flash(:error, "You are not the owner of this machine.")
|> redirect(to: Routes.virtual_machine_hosting_path(conn, :index))
end
end
end
end