Skip to content

Commit

Permalink
Fix calculations of Astarte values for container resources
Browse files Browse the repository at this point in the history
The calculations defined in the Devices domain do not correctly load the
calculation's dependencies, so the calculations cannot be computed.
The proposed change is to just load the calculations with Ash.load,
which was already done in the addressed code anyway.

Signed-off-by: Davide Briani <[email protected]>
  • Loading branch information
davidebriani committed Nov 26, 2024
1 parent 8790a98 commit fcbe974
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,17 @@ defmodule Edgehog.Containers.Deployment.Changes.CheckContainers do
@moduledoc false
use Ash.Resource.Change

alias Edgehog.Devices

@impl Ash.Resource.Change
def change(changeset, _opts, context) do
%{tenant: tenant} = context
def change(changeset, _opts, _context) do
deployment = changeset.data

with {:ok, :created_networks} <- Ash.Changeset.fetch_argument_or_change(changeset, :status),
{:ok, deployment} <-
Ash.load(deployment, [:device, release: [:containers]], reuse_values?: true),
{:ok, available_containers} <-
Devices.available_containers(deployment.device, tenant: tenant) do
available_containers = Enum.map(available_containers, & &1.id)
Ash.load(deployment, [device: :available_containers, release: [:containers]], reuse_values?: true) do
available_container_ids = Enum.map(deployment.device.available_containers, & &1.id)

missing_containers =
Enum.reject(deployment.release.containers, &(&1.id in available_containers))
Enum.reject(deployment.release.containers, &(&1.id in available_container_ids))

if missing_containers == [] do
Ash.Changeset.change_attribute(changeset, :status, :created_containers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,19 @@ defmodule Edgehog.Containers.Deployment.Changes.CheckDeployments do
use Ash.Resource.Change

alias Edgehog.Containers
alias Edgehog.Devices

@impl Ash.Resource.Change
def change(changeset, _opts, context) do
%{tenant: tenant} = context
def change(changeset, _opts, _context) do
deployment = changeset.data

with {:ok, :created_containers} <- Ash.Changeset.fetch_argument_or_change(changeset, :status),
{:ok, deployment} <- Ash.load(deployment, :device),
{:ok, available_deployments_statuses} <-
Devices.available_deployments(deployment.device, tenant: tenant) do
deployment_status =
Enum.find(available_deployments_statuses, &(&1.id == deployment.id))
{:ok, deployment} <- Ash.load(deployment, device: :available_deployments) do
available_deployment =
Enum.find(deployment.device.available_deployments, &(&1.id == deployment.id))

if deployment_status do
if available_deployment do
changeset
|> Ash.Changeset.change_attribute(:status, deployment_status.status)
|> Ash.Changeset.change_attribute(:status, available_deployment.status)
|> Ash.Changeset.after_transaction(fn _changeset, transaction_result ->
with {:ok, deployment} <- transaction_result do
Containers.run_ready_actions(deployment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@ defmodule Edgehog.Containers.Deployment.Changes.CheckImages do
@moduledoc false
use Ash.Resource.Change

alias Edgehog.Devices

@impl Ash.Resource.Change
def change(changeset, _opts, context) do
%{tenant: tenant} = context
def change(changeset, _opts, _context) do
deployment = changeset.data

with :sent <- deployment.status,
{:ok, deployment} <-
Ash.load(deployment, [:device, release: [containers: [:image]]], reuse_values?: true),
{:ok, available_images_statuses} <-
Devices.available_images(deployment.device, tenant: tenant) do
Ash.load(deployment, [device: :available_images, release: [containers: [:image]]], reuse_values?: true) do
available_images_ids =
Enum.map(available_images_statuses, & &1.id)
Enum.map(deployment.device.available_images, & &1.id)

missing_images =
deployment.release.containers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,22 @@ defmodule Edgehog.Containers.Deployment.Changes.CheckNetworks do
@moduledoc false
use Ash.Resource.Change

alias Edgehog.Devices

@impl Ash.Resource.Change
def change(changeset, _opts, context) do
%{tenant: tenant} = context
def change(changeset, _opts, _context) do
deployment = changeset.data

with {:ok, :created_images} <- Ash.Changeset.fetch_argument_or_change(changeset, :status),
{:ok, deployment} <-
Ash.load(deployment, [:device, release: [containers: [:networks]]], reuse_values?: true),
{:ok, available_networks} <-
Devices.available_networks(deployment.device, tenant: tenant) do
available_networks =
Enum.map(available_networks, & &1.id)
Ash.load(deployment, [device: :available_networks, release: [containers: [:networks]]], reuse_values?: true) do
available_network_ids =
Enum.map(deployment.device.available_networks, & &1.id)

missing_networks =
deployment.release.containers
|> Enum.flat_map(& &1.networks)
|> Enum.map(& &1.id)
|> Enum.uniq()
|> Enum.reject(&(&1 in available_networks))
|> Enum.reject(&(&1 in available_network_ids))

if missing_networks == [] do
Ash.Changeset.change_attribute(changeset, :status, :created_networks)
Expand Down
5 changes: 0 additions & 5 deletions backend/lib/edgehog/devices/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ defmodule Edgehog.Devices do
args: [:release, :command]

define :update_application, action: :update_application, args: [:from, :to]

define_calculation :available_images, args: [:_record]
define_calculation :available_containers, args: [:_record]
define_calculation :available_networks, args: [:_record]
define_calculation :available_deployments, args: [:_record]
end

resource HardwareType
Expand Down

0 comments on commit fcbe974

Please sign in to comment.