Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(containers): send deploymentId on resource requests #729

Draft
wants to merge 2 commits into
base: feature/application-management
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Edgehog.Astarte.Device.CreateContainerRequest.RequestData do
:id,
:imageId,
:image,
:deploymentId,
:networkIds,
:hostname,
:restartPolicy,
Expand All @@ -43,6 +44,7 @@ defmodule Edgehog.Astarte.Device.CreateContainerRequest.RequestData do
id: String.t(),
imageId: String.t(),
image: String.t(),
deploymentId: String.t(),
volumeIds: list(String.t()),
hostname: String.t(),
restartPolicy: String.t(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ defmodule Edgehog.Astarte.Device.CreateImageRequest.RequestData do

defstruct [
:id,
:deploymentId,
:reference,
:registryAuth
]

@type t() :: %__MODULE__{
id: String.t(),
deploymentId: String.t(),
reference: String.t(),
registryAuth: String.t()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Edgehog.Astarte.Device.CreateNetworkRequest.RequestData do

defstruct [
:id,
:deploymentId,
:driver,
:internal,
:enableIpv6,
Expand All @@ -31,6 +32,7 @@ defmodule Edgehog.Astarte.Device.CreateNetworkRequest.RequestData do

@type t() :: %__MODULE__{
id: String.t(),
deploymentId: String.t(),
driver: String.t(),
internal: boolean(),
enableIpv6: boolean(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,36 @@ defmodule Edgehog.Containers.ManualActions.SendDeployRequest do
|> Enum.flat_map(& &1.networks)
|> Enum.uniq_by(& &1.id)

with :ok <- send_create_image_requests(device, images),
:ok <- send_create_container_requests(device, containers),
:ok <- send_create_network_requests(device, networks),
with :ok <- send_create_image_requests(device, deployment, images),
:ok <- send_create_container_requests(device, deployment, containers),
:ok <- send_create_network_requests(device, deployment, networks),
{:ok, _device} <- Devices.send_create_deployment_request(device, deployment) do
{:ok, deployment}
end
end
end

defp send_create_network_requests(device, networks) do
defp send_create_network_requests(device, deployment, networks) do
Enum.reduce_while(networks, :ok, fn network, _acc ->
case Devices.send_create_network_request(device, network) do
case Devices.send_create_network_request(device, network, deployment) do
{:ok, _device} -> {:cont, :ok}
{:error, reason} -> {:halt, {:error, reason}}
end
end)
end

defp send_create_image_requests(device, images) do
defp send_create_image_requests(device, deployment, images) do
Enum.reduce_while(images, :ok, fn image, _acc ->
case Devices.send_create_image_request(device, image) do
case Devices.send_create_image_request(device, image, deployment) do
{:ok, _device} -> {:cont, :ok}
{:error, reason} -> {:halt, {:error, reason}}
end
end)
end

defp send_create_container_requests(device, containers) do
defp send_create_container_requests(device, deployment, containers) do
Enum.reduce_while(containers, :ok, fn container, _acc ->
case Devices.send_create_container_request(device, container) do
case Devices.send_create_container_request(device, container, deployment) do
{:ok, _device} -> {:cont, :ok}
{:error, reason} -> {:halt, {:error, reason}}
end
Expand Down
40 changes: 30 additions & 10 deletions backend/lib/edgehog/devices/device/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,18 @@ defmodule Edgehog.Devices.Device do
manual ManualActions.SetLedBehavior
end

update :send_create_image do
update :send_create_image_request do
description "Sends a create image request to the device."

argument :image, :struct do
constraints instance_of: Image
description "The image the device will pull."
description "The image the device will create."
allow_nil? false
end

argument :deployment, :struct do
constraints instance_of: Deployment
description "The deployment which contains the image."
allow_nil? false
end

Expand All @@ -269,21 +275,35 @@ defmodule Edgehog.Devices.Device do
update :send_create_container_request do
description "Sends a create container request to the device."

argument :container, :struct,
constraints: [instance_of: Edgehog.Containers.Container],
description: "The Container the device has to initiate.",
allow_nil?: false
argument :container, :struct do
constraints instance_of: Edgehog.Containers.Container
description "The Container the device has to create."
allow_nil? false
end

argument :deployment, :struct do
constraints instance_of: Deployment
description "The deployment which contains the container."
allow_nil? false
end

manual ManualActions.SendCreateContainer
end

update :send_create_network_request do
description "Sends a create network request to the device."

argument :network, :struct,
constraints: [instance_of: Edgehog.Containers.Network],
description: "The Network the device has to create.",
allow_nil?: false
argument :network, :struct do
constraints instance_of: Edgehog.Containers.Network
description "The Network the device has to create."
allow_nil? false
end

argument :deployment, :struct do
constraints instance_of: Deployment
description "The deployment which contains the network."
allow_nil? false
end

manual ManualActions.SendCreateNetwork
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ defmodule Edgehog.Devices.Device.ManualActions.SendCreateContainer do
device = changeset.data

with {:ok, container} <- Ash.Changeset.fetch_argument(changeset, :container),
{:ok, deployment} <- Ash.Changeset.fetch_argument(changeset, :deployment),
{:ok, container} <- Ash.load(container, [:env_encoding, :image, :networks]),
{:ok, device} <- Ash.load(device, :appengine_client) do
env_encoding = container.env_encoding
Expand All @@ -45,6 +46,7 @@ defmodule Edgehog.Devices.Device.ManualActions.SendCreateContainer do
id: container.id,
imageId: container.image_id,
image: image.reference,
deploymentId: deployment.id,
volumeIds: [],
hostname: container.hostname,
restartPolicy: restart_policy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ defmodule Edgehog.Devices.Device.ManualActions.SendCreateImageRequest do
device = changeset.data

with {:ok, image} <- Ash.Changeset.fetch_argument(changeset, :image),
{:ok, deployment} <- Ash.Changeset.fetch_argument(changeset, :deployment),
{:ok, image} <- Ash.load(image, credentials: [:base64_json]),
{:ok, device} <- Ash.load(device, :appengine_client) do
credentials = image.credentials.base64_json |> get_in() |> to_string()

data = %RequestData{
id: image.id,
deploymentId: deployment.id,
reference: image.reference,
registryAuth: credentials
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ defmodule Edgehog.Devices.Device.ManualActions.SendCreateNetwork do
device = changeset.data

with {:ok, network} <- Ash.Changeset.fetch_argument(changeset, :network),
{:ok, deployment} <- Ash.Changeset.fetch_argument(changeset, :deployment),
{:ok, device} <- Ash.load(device, :appengine_client) do
data = %RequestData{
id: network.id,
deploymentId: deployment.id,
driver: network.driver,
internal: network.internal,
enableIpv6: network.enable_ipv6,
Expand Down
8 changes: 4 additions & 4 deletions backend/lib/edgehog/devices/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ defmodule Edgehog.Devices do
define :fetch_device, action: :read, get_by: [:id]

define :send_create_image_request,
action: :send_create_image,
args: [:image]
action: :send_create_image_request,
args: [:image, :deployment]

define :send_create_container_request,
action: :send_create_container_request,
args: [:container]
args: [:container, :deployment]

define :send_create_network_request,
action: :send_create_network_request,
args: [:network]
args: [:network, :deployment]

define :send_create_deployment_request,
action: :send_create_deployment_request,
Expand Down
Loading