Skip to content

Commit

Permalink
Add cluster controller to publish namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Jun 24, 2024
1 parent 7779de8 commit d5bb35b
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/polar/machines.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/polar/machines/cluster/connect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
8 changes: 8 additions & 0 deletions lib/polar/machines/cluster/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Empty file.
Empty file.
11 changes: 11 additions & 0 deletions lib/polar_web/controllers/publish/testing/cluster_controller.ex
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions lib/polar_web/controllers/publish/testing/cluster_json.ex
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/polar_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d5bb35b

Please sign in to comment.