Skip to content

Commit

Permalink
GraphQL: add deleteBaseImage mutation
Browse files Browse the repository at this point in the history
Allow removing an existing base image.

Update CHANGELOG since Base Image support is now complete.

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
  • Loading branch information
rbino committed Jan 30, 2023
1 parent 8c8d2f7 commit ddf1c86
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow creating and managing groups based on selectors.
- Add support for device's `network_interfaces` ([#231](https://github.com/edgehog-device-manager/edgehog/pull/231))
- Add support for base image collections ([#229](https://github.com/edgehog-device-manager/edgehog/pull/229), [#230](https://github.com/edgehog-device-manager/edgehog/pull/230)).
- Add support for base images ([#240](https://github.com/edgehog-device-manager/edgehog/pull/240))

### Changed
- Handle Device part numbers for nonexistent system models
Expand Down
7 changes: 7 additions & 0 deletions backend/lib/edgehog_web/resolvers/base_images.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ defmodule EdgehogWeb.Resolvers.BaseImages do
end
end

def delete_base_image(args, _resolution) do
with {:ok, %BaseImage{} = base_image} <- BaseImages.fetch_base_image(args.base_image_id),
{:ok, %BaseImage{} = base_image} <- BaseImages.delete_base_image(base_image) do
{:ok, %{base_image: base_image}}
end
end

# TODO: consider extracting all this functions dealing with locale wrapping/unwrapping
# in a dedicated resolver/helper module

Expand Down
17 changes: 17 additions & 0 deletions backend/lib/edgehog_web/schema/base_images_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,22 @@ defmodule EdgehogWeb.Schema.BaseImagesTypes do

resolve &Resolvers.BaseImages.update_base_image/2
end

@desc "Deletes a base image."
payload field :delete_base_image do
input do
@desc "The ID of the base image to be deleted."
field :base_image_id, non_null(:id)
end

output do
@desc "The deleted base image."
field :base_image, non_null(:base_image)
end

middleware Absinthe.Relay.Node.ParseIDs, base_image_id: :base_image

resolve &Resolvers.BaseImages.delete_base_image/2
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#
# This file is part of Edgehog.
#
# Copyright 2023 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 EdgehogWeb.Schema.Mutation.DeleteBaseImageTest do
use EdgehogWeb.ConnCase
use Edgehog.BaseImagesStorageMockCase

alias Edgehog.BaseImages
import Edgehog.BaseImagesFixtures

describe "deleteBaseImage mutation" do
setup do
{:ok, base_image: base_image_fixture()}
end

test "deletes existing base image", %{
conn: conn,
api_path: api_path,
base_image: base_image
} do
response = delete_base_image_mutation(conn, api_path, base_image.id)
assert response["data"]["deleteBaseImage"]["baseImage"]["version"] == base_image.version
assert BaseImages.fetch_base_image(base_image.id) == {:error, :not_found}
end

test "fails with non-existing base image", %{
conn: conn,
api_path: api_path
} do
response = delete_base_image_mutation(conn, api_path, "123456")
assert %{"errors" => [%{"status_code" => 404, "code" => "not_found"}]} = response
end
end

@query """
mutation DeleteBaseImage($input: DeleteBaseImageInput!) {
deleteBaseImage(input: $input) {
baseImage {
id
version
url
startingVersionRequirement
description
releaseDisplayName
baseImageCollection {
handle
}
}
}
}
"""
defp delete_base_image_mutation(conn, api_path, db_id) do
base_image_id = Absinthe.Relay.Node.to_global_id(:base_image, db_id, EdgehogWeb.Schema)

variables = %{input: %{base_image_id: base_image_id}}

conn = post(conn, api_path, query: @query, variables: variables)

json_response(conn, 200)
end
end

0 comments on commit ddf1c86

Please sign in to comment.