Skip to content

Commit

Permalink
Set deployment when viewing device
Browse files Browse the repository at this point in the history
  • Loading branch information
nshoes committed Jan 2, 2025
1 parent 65acdf4 commit 04553a1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
24 changes: 24 additions & 0 deletions lib/nerves_hub_web/live/devices/show.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule NervesHubWeb.Live.Devices.Show do
alias NervesHub.Deployments
use NervesHubWeb, :updated_live_view

require Logger
Expand Down Expand Up @@ -54,6 +55,7 @@ defmodule NervesHubWeb.Live.Devices.Show do
|> schedule_health_check_timer()
|> assign(:fwup_progress, nil)
|> audit_log_assigns(1)
|> assign(:eligible_deployments, Deployments.matching_deployments(device))
|> ok()
end

Expand Down Expand Up @@ -256,6 +258,28 @@ defmodule NervesHubWeb.Live.Devices.Show do
{:noreply, clear_flash(socket, String.to_existing_atom(key_str))}
end

def handle_event(
"set-deployment",
%{"deployment_id" => deployment_id},
%{assigns: %{user: user, device: device, eligible_deployments: eligible_deployments}} =
socket
) do
deployment = Enum.find(eligible_deployments, &(&1.id == String.to_integer(deployment_id)))
device = Devices.update_deployment(device, deployment)

AuditLogs.audit!(
user,
device,
"#{user.name} set #{device.identifier}'s deployment to #{deployment.name}"
)

socket
|> assign(:device, device)
|> assign(:deployment, deployment)
|> put_flash(:info, "Deployment successfully set")
|> noreply()
end

def handle_event("push-update", %{"uuid" => uuid}, socket) do
authorized!(:"device:push-update", socket.assigns.org_user)

Expand Down
21 changes: 19 additions & 2 deletions lib/nerves_hub_web/live/devices/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
<div>
<h3 class="mb-2">Deployments</h3>
<div class="display-box">
<div>
<div :if={@device.deployment_id}>
<div class="help-text mb-1">
Assigned Deployment
</div>
Expand All @@ -235,7 +235,24 @@
<span :if={is_nil(@deployment)} class="color-white-50">No Assigned Deployment</span>
</div>

<div :if={@update_information.update_available} class="mt-4">
<div :if={Enum.any?(@eligible_deployments) && is_nil(@device.deployment_id)}>
<div class="help-text mb-1">
Eligible Deployments
</div>
<form phx-submit="set-deployment">
<div class="flex-row justify-content-between">
<select name="deployment_id" class="form-control">
<option value="">Select a deployment</option>
<%= for deployment <- @eligible_deployments do %>
<option value={deployment.id}><%= deployment.name %></option>
<% end %>
</select>
<button class="btn btn-secondary ml-2">Set</button>
</div>
</form>
</div>

<div :if={@update_information.update_available && @device.deployment_id} class="mt-4">
<div class="help-text mb-1 tooltip-label help-tooltip">
<span>Update available</span>
<span class="tooltip-info"></span>
Expand Down
26 changes: 25 additions & 1 deletion test/nerves_hub_web/live/devices/show_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ defmodule NervesHubWeb.Live.Devices.ShowTest do
conn
|> visit("/org/#{org.name}/#{product.name}/devices/#{device.identifier}")
|> click_button("Remove From Deployment")
|> assert_has("span", text: "No Assigned Deployment")

refute Repo.reload(device) |> Map.get(:deployment_id)
end

test "cannot clear deployment if no deployment is set", %{
Expand All @@ -452,6 +453,29 @@ defmodule NervesHubWeb.Live.Devices.ShowTest do
end
end

describe "setting deployment" do
test "sets deployment and creates audit", %{
conn: conn,
org: org,
product: product,
device: device,
deployment: deployment
} do
assert length(AuditLogs.logs_for(device)) == 0

conn
|> visit("/org/#{org.name}/#{product.name}/devices/#{device.identifier}")
|> assert_has("div", text: "Eligible Deployment")
|> unwrap(fn view ->
render_change(view, "set-deployment", %{"deployment_id" => deployment.id})
end)
|> assert_has("div", text: "Assigned Deployment")

assert Repo.reload(device) |> Map.get(:deployment_id)
assert length(AuditLogs.logs_for(device)) == 1
end
end

def device_show_path(%{device: device, org: org, product: product}) do
~p"/org/#{org.name}/#{product.name}/devices/#{device.identifier}"
end
Expand Down

0 comments on commit 04553a1

Please sign in to comment.