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

feat(containers): support Application resource queries #705

Merged
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
17 changes: 15 additions & 2 deletions backend/lib/edgehog/containers/container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@
defmodule Edgehog.Containers.Container do
@moduledoc false
use Edgehog.MultitenantResource,
domain: Edgehog.Containers
domain: Edgehog.Containers,
extensions: [AshGraphql.Resource]

alias Edgehog.Containers.Container.EnvEncoding
alias Edgehog.Containers.Image
alias Edgehog.Containers.Types.RestartPolicy

graphql do
type :container
paginate_relationship_with networks: :relay
end

actions do
defaults [
:read,
Expand All @@ -39,20 +45,25 @@ defmodule Edgehog.Containers.Container do
attributes do
uuid_primary_key :id

attribute :restart_policy, RestartPolicy
attribute :restart_policy, RestartPolicy do
public? true
end

attribute :hostname, :string do
constraints allow_empty?: true
default ""
allow_nil? false
public? true
end

attribute :env, :map do
default %{}
public? true
end

attribute :privileged, :boolean do
default false
public? true
end

timestamps()
Expand All @@ -63,6 +74,7 @@ defmodule Edgehog.Containers.Container do
source_attribute :image_id
attribute_type :uuid
allow_nil? false
public? true
end

many_to_many :releases, Edgehog.Containers.Release do
Expand All @@ -71,6 +83,7 @@ defmodule Edgehog.Containers.Container do

many_to_many :networks, Edgehog.Containers.Network do
through Edgehog.Containers.ContainerNetwork
public? true
end
end

Expand Down
5 changes: 5 additions & 0 deletions backend/lib/edgehog/containers/containers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Edgehog.Containers do
alias Edgehog.Containers.Application
alias Edgehog.Containers.Deployment
alias Edgehog.Containers.ImageCredentials
alias Edgehog.Containers.Release

graphql do
root_level_errors? true
Expand All @@ -48,6 +49,10 @@ defmodule Edgehog.Containers do
get Application, :application, :read do
description "Returns the desired application."
end

get Release, :release, :read do
description "Returns the desired release."
end
end

mutations do
Expand Down
9 changes: 8 additions & 1 deletion backend/lib/edgehog/containers/image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@
defmodule Edgehog.Containers.Image do
@moduledoc false
use Edgehog.MultitenantResource,
domain: Edgehog.Containers
domain: Edgehog.Containers,
extensions: [AshGraphql.Resource]

alias Edgehog.Containers.ImageCredentials

graphql do
type :image
end

actions do
defaults [:read, :destroy, create: [:reference, :image_credentials_id]]
end
Expand All @@ -34,6 +39,7 @@ defmodule Edgehog.Containers.Image do

attribute :reference, :string do
allow_nil? false
public? true
end

timestamps()
Expand All @@ -43,6 +49,7 @@ defmodule Edgehog.Containers.Image do
belongs_to :credentials, ImageCredentials do
source_attribute :image_credentials_id
attribute_type :uuid
public? true
end
end

Expand Down
11 changes: 10 additions & 1 deletion backend/lib/edgehog/containers/network.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
defmodule Edgehog.Containers.Network do
@moduledoc false
use Edgehog.MultitenantResource,
domain: Edgehog.Containers
domain: Edgehog.Containers,
extensions: [AshGraphql.Resource]

graphql do
type :network
end

actions do
defaults [:read, :destroy, create: [:driver, :check_duplicate, :internal, :enable_ipv6]]
Expand All @@ -33,21 +38,25 @@ defmodule Edgehog.Containers.Network do
attribute :driver, :string do
default "bridge"
allow_nil? false
public? true
end

attribute :check_duplicate, :boolean do
default false
allow_nil? false
public? true
end

attribute :internal, :boolean do
default false
allow_nil? false
public? true
end

attribute :enable_ipv6, :boolean do
default false
allow_nil? false
public? true
end

timestamps()
Expand Down
2 changes: 2 additions & 0 deletions backend/lib/edgehog/containers/release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Edgehog.Containers.Release do

graphql do
type :release
paginate_relationship_with containers: :relay
end

actions do
Expand Down Expand Up @@ -64,6 +65,7 @@ defmodule Edgehog.Containers.Release do

many_to_many :containers, Edgehog.Containers.Container do
through Edgehog.Containers.ReleaseContainers
public? true
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ defmodule EdgehogWeb.Schema.Query.ApplicationsTest do
document = Keyword.get(opts, :document, default_document)

id = Keyword.fetch!(opts, :id)
variables = dbg(%{"id" => id})
variables = %{"id" => id}

Absinthe.run!(document, EdgehogWeb.Schema, variables: variables, context: %{tenant: tenant})
end

def extract_result!(result) do
dbg(result)
refute :errors in Map.keys(result)
assert %{data: data} = result
assert data != nil
Expand Down
102 changes: 102 additions & 0 deletions backend/test/edgehog_web/schema/query/release_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#
# 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 EdgehogWeb.Schema.Query.ReleaseTest do
use EdgehogWeb.GraphqlCase, async: true

import Edgehog.ContainersFixtures

alias Edgehog.Containers.ContainerNetwork
alias Edgehog.Containers.ReleaseContainers

test "can access containers and netowkrs trough relationship", %{tenant: tenant} do
app = application_fixture(tenant: tenant)
release = release_fixture(application_id: app.id, tenant: tenant)

container = container_fixture(tenant: tenant)
network = network_fixture(tenant: tenant)

params = %{container_id: container.id, release_id: release.id}
Ash.create!(ReleaseContainers, params, tenant: tenant)

params = %{container_id: container.id, network_id: network.id}
Ash.create!(ContainerNetwork, params, tenant: tenant)

id = AshGraphql.Resource.encode_relay_id(release)

release = [tenant: tenant, id: id] |> get_release() |> extract_result!()

assert get_in(release, ["release", "id"]) == id

container_result =
release
|> get_in(["release", "containers", "edges"])
|> Enum.map(& &1["node"])
|> hd()

assert container_result["id"] == AshGraphql.Resource.encode_relay_id(container)

network_result =
container_result |> get_in(["networks", "edges"]) |> Enum.map(& &1["node"]) |> hd()

assert network_result["id"] == AshGraphql.Resource.encode_relay_id(network)
end

defp get_release(opts) do
default_document =
"""
query ($id: ID!) {
release(id: $id) {
id
containers {
edges {
node {
id
networks {
edges {
node {
id
}
}
}
}
}
}
}
}
"""

{tenant, opts} = Keyword.pop!(opts, :tenant)
document = Keyword.get(opts, :document, default_document)

id = Keyword.fetch!(opts, :id)
variables = %{"id" => id}

Absinthe.run!(document, EdgehogWeb.Schema, variables: variables, context: %{tenant: tenant})
end

def extract_result!(result) do
refute :errors in Map.keys(result)
assert %{data: data} = result
assert data != nil

data
end
end
9 changes: 9 additions & 0 deletions backend/test/support/fixtures/containers_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule Edgehog.ContainersFixtures do
alias Edgehog.Containers.Container
alias Edgehog.Containers.Deployment
alias Edgehog.Containers.Image
alias Edgehog.Containers.Network
alias Edgehog.Containers.Release
alias Edgehog.Containers.ReleaseContainers

Expand Down Expand Up @@ -82,6 +83,14 @@ defmodule Edgehog.ContainersFixtures do
|> Ash.create!()
end

def network_fixture(opts \\ []) do
{tenant, opts} = Keyword.pop!(opts, :tenant)

params = Map.new(opts)

Ash.create!(Network, params, tenant: tenant)
end

def image_fixture(opts \\ []) do
{tenant, opts} = Keyword.pop!(opts, :tenant)

Expand Down
Loading