Skip to content

Commit

Permalink
Add buckets/domains to stack install apis and pluralContext gql query (
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Jan 6, 2023
1 parent 823fc5c commit f5e34b9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lib/console/deployer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ defmodule Console.Deployer do
], storage)
end

defp perform(storage, %Build{type: :install, context: %{"configuration" => conf, "bundles" => bs}, message: message} = build) do
defp perform(storage, %Build{type: :install, context: %{"configuration" => conf, "bundles" => bs} = ctx, message: message} = build) do
with_build(build, [
{storage, :init, []},
{Context, :merge, [conf, Enum.map(bs, fn b -> %Context.Bundle{repository: b["repository"], name: b["name"]} end)]},
{Context, :merge, [conf, Enum.map(bs, fn b -> %Context.Bundle{repository: b["repository"], name: b["name"]} end), ctx["buckets"], ctx["domains"]]},
{Plural, :build, []},
{Plural, :install, [Enum.map(bs, & &1["repository"])]},
{storage, :revise, [message]},
Expand Down
22 changes: 21 additions & 1 deletion lib/console/graphql/plural.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ defmodule Console.GraphQl.Plural do
field :user, :string
end

input_object :context_attributes do
field :buckets, list_of(:string)
field :domain, list_of(:string)
field :configuration, non_null(:map)
end

object :smtp do
field :server, :string
field :port, :integer
Expand Down Expand Up @@ -98,6 +104,12 @@ defmodule Console.GraphQl.Plural do
field :context, :map
end

object :plural_context do
field :buckets, list_of(:string)
field :domains, list_of(:string)
field :configuration, non_null(:map)
end

connection node_type: :installation
connection node_type: :repository
connection node_type: :recipe
Expand Down Expand Up @@ -138,10 +150,18 @@ defmodule Console.GraphQl.Plural do

field :context, list_of(:repository_context) do
middleware Authenticated
middleware Rbac, perm: :deploy, arg: :id

resolve &Plural.resolve_context/2
end

field :plural_context, :plural_context do
middleware Authenticated
middleware Rbac, perm: :deploy, arg: :id

resolve &Plural.resolve_plural_context/2
end

field :recipe, :recipe do
middleware Authenticated
arg :id, non_null(:id)
Expand Down Expand Up @@ -186,7 +206,7 @@ defmodule Console.GraphQl.Plural do
middleware Rbac, perm: :deploy, arg: :id

arg :name, non_null(:string)
arg :context, non_null(:map)
arg :context, non_null(:context_attributes)
arg :oidc, :boolean

safe_resolve &Plural.install_stack/2
Expand Down
12 changes: 6 additions & 6 deletions lib/console/graphql/resolvers/plural.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ defmodule Console.GraphQl.Resolvers.Plural do
end
end

def install_recipe(%{id: id, context: context} = args, %{context: %{current_user: user}}) do
Plural.install_recipe(id, context, !!args[:oidc], user)
end
def resolve_plural_context(_, _), do: Console.Plural.Context.get()

def install_stack(%{name: name, context: context} = args, %{context: %{current_user: user}}) do
Plural.install_stack(name, context, !!args[:oidc], user)
end
def install_recipe(%{id: id, context: context} = args, %{context: %{current_user: user}}),
do: Plural.install_recipe(id, context, !!args[:oidc], user)

def install_stack(%{name: name, context: ctx} = args, %{context: %{current_user: user}}),
do: Plural.install_stack(name, ctx, !!args[:oidc], user)

def update_smtp(%{smtp: smtp}, _), do: Plural.update_smtp(smtp)

Expand Down
16 changes: 11 additions & 5 deletions lib/console/plural/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Console.Plural.Context do
import Console
alias Console.Deployer

defstruct [:configuration, :bundles, :smtp]
defstruct [:configuration, :bundles, :smtp, :buckets, :domains]

defmodule Smtp do
defstruct [:user, :password, :server, :port, :sender]
Expand Down Expand Up @@ -43,15 +43,21 @@ defmodule Console.Plural.Context do
end
end

def merge(ctx, new_bundles) do
def merge(ctx, new_bundles, buckets \\ [], domains \\ []) do
with {:ok, %{configuration: config, bundles: bundles} = context} <- get() do
updated = DeepMerge.deep_merge(config, ctx)
write(%{context | configuration: updated, bundles: merge_bundles(new_bundles, bundles)})
%{context | configuration: updated, bundles: merge_list(new_bundles, bundles)}
|> add_meta(buckets, domains)
|> write()
end
end

defp merge_bundles([_ | _] = new, bundles), do: Enum.uniq(new ++ bundles)
defp merge_bundles(b, bundles), do: merge_bundles([b], bundles)
defp add_meta(%__MODULE__{buckets: buckets, domains: domains} = ctx, new_buckets, new_domains) do
%{ctx | buckets: merge_list(buckets || [], new_buckets || []), domains: merge_list(domains || [], new_domains || [])}
end

defp merge_list(new, bundles) when is_list(new), do: Enum.uniq(new ++ bundles)
defp merge_list(b, bundles), do: merge_list([b], bundles)

def write(%__MODULE__{bundles: bundles, configuration: conf, smtp: smtp} = context) do
sanitized = %{
Expand Down
8 changes: 5 additions & 3 deletions lib/console/services/plural.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,18 @@ defmodule Console.Services.Plural do
end
end

def install_stack(name, context, oidc, %User{} = user) do
def install_stack(name, %{configuration: ctx} = context, oidc, %User{} = user) do
with {:ok, [recipe | _] = recipes} <- Repositories.install_stack(name),
{:ok, _} <- oidc_for_stack(recipes, context, oidc) do
{:ok, _} <- oidc_for_stack(recipes, ctx, oidc) do
repos = Enum.map(recipes, & &1.repository.name)
Builds.create(%{
type: :install,
repository: recipe.repository.name,
message: "Installed stack #{name} with repositories #{Enum.join(repos, ", ")}",
context: %{
configuration: context,
configuration: ctx,
domains: Map.get(context, :domains, []),
buckets: Map.get(context, :buckets, []),
bundles: Enum.map(recipes, & %{name: &1.name, repository: &1.repository.name}),
},
}, user)
Expand Down
4 changes: 2 additions & 2 deletions test/console/graphql/mutations/plural_mutations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ defmodule Console.GraphQl.PluralMutationsTest do
user = insert(:user, roles: %{admin: true})

{:ok, %{data: %{"installStack" => build}}} = run_query("""
mutation Install($name: Name!, $context: Map!) {
mutation Install($name: Name!, $context: ContextAttributes!) {
installStack(name: $name, context: $context) {
id
type
creator { id }
}
}
""", %{"name" => "id", "context" => Jason.encode!(%{"repo" => %{"some" => "val"}})}, %{current_user: user})
""", %{"name" => "id", "context" => %{"configuration" => Jason.encode!(%{"repo" => %{"some" => "val"}})}}, %{current_user: user})

assert build["type"] == "INSTALL"
assert build["creator"]["id"] == user.id
Expand Down
6 changes: 4 additions & 2 deletions test/console/services/plural_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,17 @@ defmodule Console.Services.PluralTest do
user = insert(:user)
{:ok, build} = Plural.install_stack(
"id",
%{"repo" => %{"domain" => "domain.com"}},
%{configuration: %{"repo" => %{"domain" => "domain.com"}}},
true,
user
)

assert build.type == :install
assert build.context == %{
configuration: %{"repo" => %{"domain" => "domain.com"}},
bundles: [%{repository: "repo", name: "name"}]
bundles: [%{repository: "repo", name: "name"}],
buckets: [],
domains: []
}
assert build.creator_id == user.id
end
Expand Down

0 comments on commit f5e34b9

Please sign in to comment.