diff --git a/backend/lib/edgehog/astarte/device/create_container_request/request_data.ex b/backend/lib/edgehog/astarte/device/create_container_request/request_data.ex index cbc306d76..815897e42 100644 --- a/backend/lib/edgehog/astarte/device/create_container_request/request_data.ex +++ b/backend/lib/edgehog/astarte/device/create_container_request/request_data.ex @@ -28,6 +28,7 @@ defmodule Edgehog.Astarte.Device.CreateContainerRequest.RequestData do :id, :imageId, :image, + :deploymentId, :networkIds, :hostname, :restartPolicy, @@ -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(), diff --git a/backend/lib/edgehog/astarte/device/create_image_request/request_data.ex b/backend/lib/edgehog/astarte/device/create_image_request/request_data.ex index 044aa3042..5a409f5bc 100644 --- a/backend/lib/edgehog/astarte/device/create_image_request/request_data.ex +++ b/backend/lib/edgehog/astarte/device/create_image_request/request_data.ex @@ -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() } diff --git a/backend/lib/edgehog/astarte/device/create_network_request/request_data.ex b/backend/lib/edgehog/astarte/device/create_network_request/request_data.ex index 6e7f2c483..299717bb2 100644 --- a/backend/lib/edgehog/astarte/device/create_network_request/request_data.ex +++ b/backend/lib/edgehog/astarte/device/create_network_request/request_data.ex @@ -23,6 +23,7 @@ defmodule Edgehog.Astarte.Device.CreateNetworkRequest.RequestData do defstruct [ :id, + :deploymentId, :driver, :internal, :enableIpv6, @@ -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(), diff --git a/backend/lib/edgehog/containers/manual_actions/send_deploy_request.ex b/backend/lib/edgehog/containers/manual_actions/send_deploy_request.ex index 619c51e41..cd637276f 100644 --- a/backend/lib/edgehog/containers/manual_actions/send_deploy_request.ex +++ b/backend/lib/edgehog/containers/manual_actions/send_deploy_request.ex @@ -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 diff --git a/backend/lib/edgehog/devices/device/device.ex b/backend/lib/edgehog/devices/device/device.ex index 7fcc85df9..e4374e143 100644 --- a/backend/lib/edgehog/devices/device/device.ex +++ b/backend/lib/edgehog/devices/device/device.ex @@ -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 @@ -269,10 +275,17 @@ 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 @@ -280,10 +293,17 @@ defmodule Edgehog.Devices.Device do 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 diff --git a/backend/lib/edgehog/devices/device/manual_actions/send_create_container.ex b/backend/lib/edgehog/devices/device/manual_actions/send_create_container.ex index 1d368d670..557b92d5c 100644 --- a/backend/lib/edgehog/devices/device/manual_actions/send_create_container.ex +++ b/backend/lib/edgehog/devices/device/manual_actions/send_create_container.ex @@ -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 @@ -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, diff --git a/backend/lib/edgehog/devices/device/manual_actions/send_create_image_request.ex b/backend/lib/edgehog/devices/device/manual_actions/send_create_image_request.ex index 682fff81c..16d141148 100644 --- a/backend/lib/edgehog/devices/device/manual_actions/send_create_image_request.ex +++ b/backend/lib/edgehog/devices/device/manual_actions/send_create_image_request.ex @@ -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 } diff --git a/backend/lib/edgehog/devices/device/manual_actions/send_create_network.ex b/backend/lib/edgehog/devices/device/manual_actions/send_create_network.ex index 474b96c8f..963f7c28f 100644 --- a/backend/lib/edgehog/devices/device/manual_actions/send_create_network.ex +++ b/backend/lib/edgehog/devices/device/manual_actions/send_create_network.ex @@ -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, diff --git a/backend/lib/edgehog/devices/devices.ex b/backend/lib/edgehog/devices/devices.ex index 7e44bae68..1da208cba 100644 --- a/backend/lib/edgehog/devices/devices.ex +++ b/backend/lib/edgehog/devices/devices.ex @@ -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,