Skip to content

Commit f5e34b9

Browse files
Add buckets/domains to stack install apis and pluralContext gql query (#171)
1 parent 823fc5c commit f5e34b9

File tree

7 files changed

+51
-21
lines changed

7 files changed

+51
-21
lines changed

lib/console/deployer.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ defmodule Console.Deployer do
184184
], storage)
185185
end
186186

187-
defp perform(storage, %Build{type: :install, context: %{"configuration" => conf, "bundles" => bs}, message: message} = build) do
187+
defp perform(storage, %Build{type: :install, context: %{"configuration" => conf, "bundles" => bs} = ctx, message: message} = build) do
188188
with_build(build, [
189189
{storage, :init, []},
190-
{Context, :merge, [conf, Enum.map(bs, fn b -> %Context.Bundle{repository: b["repository"], name: b["name"]} end)]},
190+
{Context, :merge, [conf, Enum.map(bs, fn b -> %Context.Bundle{repository: b["repository"], name: b["name"]} end), ctx["buckets"], ctx["domains"]]},
191191
{Plural, :build, []},
192192
{Plural, :install, [Enum.map(bs, & &1["repository"])]},
193193
{storage, :revise, [message]},

lib/console/graphql/plural.ex

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ defmodule Console.GraphQl.Plural do
1111
field :user, :string
1212
end
1313

14+
input_object :context_attributes do
15+
field :buckets, list_of(:string)
16+
field :domain, list_of(:string)
17+
field :configuration, non_null(:map)
18+
end
19+
1420
object :smtp do
1521
field :server, :string
1622
field :port, :integer
@@ -98,6 +104,12 @@ defmodule Console.GraphQl.Plural do
98104
field :context, :map
99105
end
100106

107+
object :plural_context do
108+
field :buckets, list_of(:string)
109+
field :domains, list_of(:string)
110+
field :configuration, non_null(:map)
111+
end
112+
101113
connection node_type: :installation
102114
connection node_type: :repository
103115
connection node_type: :recipe
@@ -138,10 +150,18 @@ defmodule Console.GraphQl.Plural do
138150

139151
field :context, list_of(:repository_context) do
140152
middleware Authenticated
153+
middleware Rbac, perm: :deploy, arg: :id
141154

142155
resolve &Plural.resolve_context/2
143156
end
144157

158+
field :plural_context, :plural_context do
159+
middleware Authenticated
160+
middleware Rbac, perm: :deploy, arg: :id
161+
162+
resolve &Plural.resolve_plural_context/2
163+
end
164+
145165
field :recipe, :recipe do
146166
middleware Authenticated
147167
arg :id, non_null(:id)
@@ -186,7 +206,7 @@ defmodule Console.GraphQl.Plural do
186206
middleware Rbac, perm: :deploy, arg: :id
187207

188208
arg :name, non_null(:string)
189-
arg :context, non_null(:map)
209+
arg :context, non_null(:context_attributes)
190210
arg :oidc, :boolean
191211

192212
safe_resolve &Plural.install_stack/2

lib/console/graphql/resolvers/plural.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ defmodule Console.GraphQl.Resolvers.Plural do
5858
end
5959
end
6060

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

65-
def install_stack(%{name: name, context: context} = args, %{context: %{current_user: user}}) do
66-
Plural.install_stack(name, context, !!args[:oidc], user)
67-
end
63+
def install_recipe(%{id: id, context: context} = args, %{context: %{current_user: user}}),
64+
do: Plural.install_recipe(id, context, !!args[:oidc], user)
65+
66+
def install_stack(%{name: name, context: ctx} = args, %{context: %{current_user: user}}),
67+
do: Plural.install_stack(name, ctx, !!args[:oidc], user)
6868

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

lib/console/plural/context.ex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Console.Plural.Context do
22
import Console
33
alias Console.Deployer
44

5-
defstruct [:configuration, :bundles, :smtp]
5+
defstruct [:configuration, :bundles, :smtp, :buckets, :domains]
66

77
defmodule Smtp do
88
defstruct [:user, :password, :server, :port, :sender]
@@ -43,15 +43,21 @@ defmodule Console.Plural.Context do
4343
end
4444
end
4545

46-
def merge(ctx, new_bundles) do
46+
def merge(ctx, new_bundles, buckets \\ [], domains \\ []) do
4747
with {:ok, %{configuration: config, bundles: bundles} = context} <- get() do
4848
updated = DeepMerge.deep_merge(config, ctx)
49-
write(%{context | configuration: updated, bundles: merge_bundles(new_bundles, bundles)})
49+
%{context | configuration: updated, bundles: merge_list(new_bundles, bundles)}
50+
|> add_meta(buckets, domains)
51+
|> write()
5052
end
5153
end
5254

53-
defp merge_bundles([_ | _] = new, bundles), do: Enum.uniq(new ++ bundles)
54-
defp merge_bundles(b, bundles), do: merge_bundles([b], bundles)
55+
defp add_meta(%__MODULE__{buckets: buckets, domains: domains} = ctx, new_buckets, new_domains) do
56+
%{ctx | buckets: merge_list(buckets || [], new_buckets || []), domains: merge_list(domains || [], new_domains || [])}
57+
end
58+
59+
defp merge_list(new, bundles) when is_list(new), do: Enum.uniq(new ++ bundles)
60+
defp merge_list(b, bundles), do: merge_list([b], bundles)
5561

5662
def write(%__MODULE__{bundles: bundles, configuration: conf, smtp: smtp} = context) do
5763
sanitized = %{

lib/console/services/plural.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,18 @@ defmodule Console.Services.Plural do
6565
end
6666
end
6767

68-
def install_stack(name, context, oidc, %User{} = user) do
68+
def install_stack(name, %{configuration: ctx} = context, oidc, %User{} = user) do
6969
with {:ok, [recipe | _] = recipes} <- Repositories.install_stack(name),
70-
{:ok, _} <- oidc_for_stack(recipes, context, oidc) do
70+
{:ok, _} <- oidc_for_stack(recipes, ctx, oidc) do
7171
repos = Enum.map(recipes, & &1.repository.name)
7272
Builds.create(%{
7373
type: :install,
7474
repository: recipe.repository.name,
7575
message: "Installed stack #{name} with repositories #{Enum.join(repos, ", ")}",
7676
context: %{
77-
configuration: context,
77+
configuration: ctx,
78+
domains: Map.get(context, :domains, []),
79+
buckets: Map.get(context, :buckets, []),
7880
bundles: Enum.map(recipes, & %{name: &1.name, repository: &1.repository.name}),
7981
},
8082
}, user)

test/console/graphql/mutations/plural_mutations_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ defmodule Console.GraphQl.PluralMutationsTest do
6767
user = insert(:user, roles: %{admin: true})
6868

6969
{:ok, %{data: %{"installStack" => build}}} = run_query("""
70-
mutation Install($name: Name!, $context: Map!) {
70+
mutation Install($name: Name!, $context: ContextAttributes!) {
7171
installStack(name: $name, context: $context) {
7272
id
7373
type
7474
creator { id }
7575
}
7676
}
77-
""", %{"name" => "id", "context" => Jason.encode!(%{"repo" => %{"some" => "val"}})}, %{current_user: user})
77+
""", %{"name" => "id", "context" => %{"configuration" => Jason.encode!(%{"repo" => %{"some" => "val"}})}}, %{current_user: user})
7878

7979
assert build["type"] == "INSTALL"
8080
assert build["creator"]["id"] == user.id

test/console/services/plural_test.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,17 @@ defmodule Console.Services.PluralTest do
261261
user = insert(:user)
262262
{:ok, build} = Plural.install_stack(
263263
"id",
264-
%{"repo" => %{"domain" => "domain.com"}},
264+
%{configuration: %{"repo" => %{"domain" => "domain.com"}}},
265265
true,
266266
user
267267
)
268268

269269
assert build.type == :install
270270
assert build.context == %{
271271
configuration: %{"repo" => %{"domain" => "domain.com"}},
272-
bundles: [%{repository: "repo", name: "name"}]
272+
bundles: [%{repository: "repo", name: "name"}],
273+
buckets: [],
274+
domains: []
273275
}
274276
assert build.creator_id == user.id
275277
end

0 commit comments

Comments
 (0)