-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(update_channel): better errors
When creating and updating an update channel, target group ids are validated to be available, meaning: - they are present in the DB - they are not related to any other update channel closes #529 Signed-off-by: Luca Zaninotto <[email protected]>
- Loading branch information
Showing
6 changed files
with
154 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...nd/lib/edgehog/update_campaigns/update_channel/validations/target_groups_are_available.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# | ||
# This file is part of Edgehog. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
defmodule Edgehog.UpdateCampaigns.UpdateChannel.Validations.TargetGroupsAreAvailable do | ||
@moduledoc false | ||
|
||
use Ash.Resource.Validation | ||
|
||
alias Ash.Error.Changes.InvalidArgument | ||
alias Edgehog.Groups.DeviceGroup | ||
|
||
@impl Ash.Resource.Validation | ||
def validate(changeset, _opts, context) do | ||
%{tenant: tenant} = context | ||
|
||
{:ok, target_group_ids} = Ash.Changeset.fetch_argument(changeset, :target_group_ids) | ||
|
||
{:ok, target_groups} = | ||
DeviceGroup | ||
|> Ash.Query.set_tenant(tenant) | ||
|> Ash.Query.filter(id in ^target_group_ids) | ||
|> Ash.read() | ||
|
||
read_groups_ids = Enum.map(target_groups, & &1.id) | ||
|
||
not_found_groups = | ||
Enum.reject(target_group_ids, &(&1 in read_groups_ids)) | ||
|
||
if Enum.empty?(not_found_groups), | ||
do: :ok, | ||
else: | ||
{:error, | ||
InvalidArgument.exception( | ||
field: :target_group_ids, | ||
message: "some target groups were not found: #{inspect(not_found_groups, charlists: :as_list)}" | ||
)} | ||
end | ||
end |
53 changes: 53 additions & 0 deletions
53
...nd/lib/edgehog/update_campaigns/update_channel/validations/target_groups_are_unrelated.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# | ||
# This file is part of Edgehog. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
defmodule Edgehog.UpdateCampaigns.UpdateChannel.Validations.TargetGroupsAreUnrelated do | ||
@moduledoc false | ||
use Ash.Resource.Validation | ||
|
||
alias Ash.Error.Changes.InvalidArgument | ||
alias Edgehog.Groups.DeviceGroup | ||
|
||
@impl Ash.Resource.Validation | ||
def validate(changeset, _opts, context) do | ||
%{tenant: tenant} = context | ||
|
||
{:ok, target_group_ids} = Ash.Changeset.fetch_argument(changeset, :target_group_ids) | ||
|
||
{:ok, related_target_groups} = | ||
DeviceGroup | ||
|> Ash.Query.set_tenant(tenant) | ||
|> Ash.Query.filter(id in ^target_group_ids) | ||
|> Ash.Query.filter(not is_nil(update_channel_id)) | ||
|> Ash.read() | ||
|
||
related_target_groups_ids = Enum.map(related_target_groups, & &1.id) | ||
|
||
if Enum.empty?(related_target_groups_ids), | ||
do: :ok, | ||
else: | ||
{:error, | ||
InvalidArgument.exception( | ||
field: :target_group_ids, | ||
message: | ||
"some target groups are already associated with an update channel: #{inspect(related_target_groups_ids, charlists: :as_list)}" | ||
)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters