diff --git a/lib/polar/machines.ex b/lib/polar/machines.ex index 8454c2a..414532a 100644 --- a/lib/polar/machines.ex +++ b/lib/polar/machines.ex @@ -1,6 +1,10 @@ defmodule Polar.Machines do alias __MODULE__.Cluster + defdelegate list_clusters(scope), + to: Cluster.Manager, + as: :list + defdelegate create_cluster(params), to: Cluster.Manager, as: :create diff --git a/lib/polar/machines/cluster/connect.ex b/lib/polar/machines/cluster/connect.ex index ceb4ff2..6ba574f 100644 --- a/lib/polar/machines/cluster/connect.ex +++ b/lib/polar/machines/cluster/connect.ex @@ -11,8 +11,6 @@ defmodule Polar.Machines.Cluster.Connect do user = Repo.get(User, user_id) %{credential: credential} = cluster = Repo.get(Cluster, cluster_id) - IO.inspect(credential) - client = Lexdee.create_client( credential["endpoint"], diff --git a/lib/polar/machines/cluster/manager.ex b/lib/polar/machines/cluster/manager.ex index 28d86fd..ed8e0a9 100644 --- a/lib/polar/machines/cluster/manager.ex +++ b/lib/polar/machines/cluster/manager.ex @@ -2,6 +2,14 @@ defmodule Polar.Machines.Cluster.Manager do alias Polar.Repo alias Polar.Machines.Cluster + import Ecto.Query, only: [where: 3] + + def list(:for_testing) do + Cluster + |> where([c], c.current_state == "healthy") + |> Repo.all() + end + def create(params) do %Cluster{} |> Cluster.changeset(params) diff --git a/lib/polar_web/controllers/publish/testing/assessment_controller.ex b/lib/polar_web/controllers/publish/testing/assessment_controller.ex new file mode 100644 index 0000000..e69de29 diff --git a/lib/polar_web/controllers/publish/testing/assessment_json.ex b/lib/polar_web/controllers/publish/testing/assessment_json.ex new file mode 100644 index 0000000..e69de29 diff --git a/lib/polar_web/controllers/publish/testing/cluster_controller.ex b/lib/polar_web/controllers/publish/testing/cluster_controller.ex new file mode 100644 index 0000000..c191370 --- /dev/null +++ b/lib/polar_web/controllers/publish/testing/cluster_controller.ex @@ -0,0 +1,11 @@ +defmodule PolarWeb.Publish.Testing.ClusterController do + use PolarWeb, :controller + + alias Polar.Machines + + def index(conn, _params) do + clusters = Machines.list_clusters(:for_testing) + + render(conn, :index, %{clusters: clusters}) + end +end diff --git a/lib/polar_web/controllers/publish/testing/cluster_json.ex b/lib/polar_web/controllers/publish/testing/cluster_json.ex new file mode 100644 index 0000000..0cea9c9 --- /dev/null +++ b/lib/polar_web/controllers/publish/testing/cluster_json.ex @@ -0,0 +1,17 @@ +defmodule PolarWeb.Publish.Testing.ClusterJSON do + alias Polar.Machines.Cluster + + def index(%{clusters: clusters}) do + %{ + data: Enum.map(clusters, &data/1) + } + end + + def data(%Cluster{} = cluster) do + %{ + id: cluster.id, + credential: cluster.credential, + current_state: cluster.current_state + } + end +end diff --git a/lib/polar_web/router.ex b/lib/polar_web/router.ex index b84e5b8..eae81b2 100644 --- a/lib/polar_web/router.ex +++ b/lib/polar_web/router.ex @@ -99,6 +99,10 @@ defmodule PolarWeb.Router do scope "/" do pipe_through :publish + scope "/testing", Testing, as: :testing do + resources "/clusters", ClusterController, only: [:index] + end + resources "/storage", StorageController, only: [:show], singleton: true resources "/products", ProductController, only: [:show] do diff --git a/test/polar_web/controllers/publish/testing/cluster_controller_test.exs b/test/polar_web/controllers/publish/testing/cluster_controller_test.exs new file mode 100644 index 0000000..58edc96 --- /dev/null +++ b/test/polar_web/controllers/publish/testing/cluster_controller_test.exs @@ -0,0 +1,67 @@ +defmodule PolarWeb.Publish.Testing.ClusterControllerTest do + use PolarWeb.ConnCase + + alias Polar.Accounts + alias Polar.Machines + + import Polar.AccountsFixtures + + setup do + password = Accounts.generate_automation_password() + + bot = bot_fixture(%{password: password}) + + user = Accounts.get_user_by_email_and_password(bot.email, password) + + session_token = + Accounts.generate_user_session_token(user) + |> Base.encode64() + + conn = + build_conn() + |> put_req_header("authorization", session_token) + + {:ok, cluster} = + Machines.create_cluster(%{ + name: "example", + type: "lxd", + arch: "amd64", + credential_endpoint: "some.cluster.com:8443", + credential_password: "sometoken", + credential_password_confirmation: "sometoken" + }) + + {:ok, _created_cluster} = + Machines.create_cluster(%{ + name: "example2", + type: "lxd", + arch: "amd64", + credential_endpoint: "some.cluster.com:8443", + credential_password: "sometoken", + credential_password_confirmation: "sometoken" + }) + + {:ok, conn: conn, cluster: cluster, user: user} + end + + describe "GET /publish/testing/clusters" do + setup %{cluster: cluster, user: user} do + {:ok, %{resource: connecting_cluster}} = + Eventful.Transit.perform(cluster, user, "connect") + + {:ok, %{resource: _healthy_cluster}} = + Eventful.Transit.perform(connecting_cluster, user, "healthy") + + {:ok, cluster: cluster} + end + + test "list healthy clusters", %{conn: conn, cluster: cluster} do + conn = + get(conn, "/publish/testing/clusters") + + assert %{"data" => data} = json_response(conn, 200) + + assert cluster.id in Enum.map(data, & &1["id"]) + end + end +end