Skip to content

Commit

Permalink
Merge pull request #105 from upmaru/feature/allow-instance-cleanup-re…
Browse files Browse the repository at this point in the history
…start-when-deployment-not-match

Allow cleanup and restart when install for particular deployment
  • Loading branch information
zacksiri authored Oct 16, 2024
2 parents 315cd89 + ab179d8 commit d22269b
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 3 deletions.
42 changes: 39 additions & 3 deletions lib/uplink/packages/instance/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,48 @@ defmodule Uplink.Packages.Instance.Router do
})

nil ->
json(conn, :not_found, %{
error: %{message: "install not available, create a deployment first"}
})
handle_not_found(conn, action, conn.body_params)
end
end

defp handle_not_found(
conn,
action,
%{
"actor" => actor_params,
"installation_id" => instellar_installation_id,
"instance" => instance_params
}
)
when action in ["cleanup", "restart"] do
with {:ok, %Members.Actor{id: actor_id}} <-
Members.get_or_create_actor(actor_params),
%Packages.Install{id: install_id} <-
Packages.latest_install(instellar_installation_id, nil) do
module = get_module(action)

{:ok, %{id: job_id}} =
%{
instance: instance_params,
install_id: install_id,
actor_id: actor_id
}
|> module.new()
|> Oban.insert()

json(conn, :created, %{id: job_id})
else
_ ->
handle_not_found(conn, nil, nil)
end
end

defp handle_not_found(conn, _action, _params) do
json(conn, :not_found, %{
error: %{message: "install not available, create a deployment first"}
})
end

defp get_module(action) do
Map.get(@action_mappings, action) || {:action, :not_found}
end
Expand Down
89 changes: 89 additions & 0 deletions test/uplink/packages/instance/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,93 @@ defmodule Uplink.Packages.Instance.RouterTest do
assert conn.status == 401
end
end

describe "fallback on install not found" do
setup do
valid_body =
Jason.encode!(%{
"actor" => %{
"provider" => "instellar",
"identifier" => "zacksiri",
"id" => "1"
},
"installation_id" => 1,
"deployment" => %{
"hash" => "some-hash-not-existing"
},
"instance" => %{
"slug" => "some-instane-1",
"node" => %{
"slug" => "some-node-1"
}
}
})

{:ok, body: valid_body}
end

test "returns 404 for instance bootstrap", %{body: body} do
signature =
:crypto.mac(:hmac, :sha256, Uplink.Secret.get(), body)
|> Base.encode16()
|> String.downcase()

conn =
conn(:post, "/bootstrap", body)
|> put_req_header("x-uplink-signature-256", "sha256=#{signature}")
|> put_req_header("content-type", "application/json")
|> Router.call(@opts)

assert conn.status == 404
end

test "returns 201 for instance cleanup", %{body: body} do
signature =
:crypto.mac(:hmac, :sha256, Uplink.Secret.get(), body)
|> Base.encode16()
|> String.downcase()

conn =
conn(:post, "/cleanup", body)
|> put_req_header("x-uplink-signature-256", "sha256=#{signature}")
|> put_req_header("content-type", "application/json")
|> Router.call(@opts)

assert conn.status == 201

assert %{"data" => %{"id" => _job_id}} = Jason.decode!(conn.resp_body)
end

test "returns 201 for instance restart", %{body: body} do
signature =
:crypto.mac(:hmac, :sha256, Uplink.Secret.get(), body)
|> Base.encode16()
|> String.downcase()

conn =
conn(:post, "/restart", body)
|> put_req_header("x-uplink-signature-256", "sha256=#{signature}")
|> put_req_header("content-type", "application/json")
|> Router.call(@opts)

assert conn.status == 201

assert %{"data" => %{"id" => _job_id}} = Jason.decode!(conn.resp_body)
end

test "returns 404 for instance upgrade", %{body: body} do
signature =
:crypto.mac(:hmac, :sha256, Uplink.Secret.get(), body)
|> Base.encode16()
|> String.downcase()

conn =
conn(:post, "/upgrade", body)
|> put_req_header("x-uplink-signature-256", "sha256=#{signature}")
|> put_req_header("content-type", "application/json")
|> Router.call(@opts)

assert conn.status == 404
end
end
end

0 comments on commit d22269b

Please sign in to comment.