|
| 1 | +defmodule Console.Deployments.Rtc.Utils do |
| 2 | + alias Console.Repo |
| 3 | + alias Console.Schema.{Pipeline, Service, Cluster, DeploymentSettings} |
| 4 | + alias Console.Deployments.Settings |
| 5 | + |
| 6 | + def audience(%Pipeline{} = pipe) do |
| 7 | + %{read_bindings: bindings} = Repo.preload(pipe, [:read_bindings]) |
| 8 | + settings = Settings.fetch() |
| 9 | + collect(bindings, settings) |
| 10 | + end |
| 11 | + |
| 12 | + def audience(%Service{} = service) do |
| 13 | + %{read_bindings: bindings, cluster: cluster} = Repo.preload(service, [:read_bindings, cluster: :read_bindings]) |
| 14 | + collect(bindings, audience(cluster)) |
| 15 | + end |
| 16 | + |
| 17 | + def audience(%Cluster{} = cluster) do |
| 18 | + %{read_bindings: bindings} = Repo.preload(cluster, [:read_bindings]) |
| 19 | + settings = Settings.fetch() |
| 20 | + collect(bindings, settings) |
| 21 | + end |
| 22 | + |
| 23 | + defp collect([_ | _] = bindings, %DeploymentSettings{read_bindings: [_ | _] = globals}), |
| 24 | + do: do_collect(bindings ++ globals) |
| 25 | + defp collect([_ | _] = bindings, {_, _} = acc), do: do_collect(bindings, acc) |
| 26 | + defp collect([_ | _] = bindings, _), do: do_collect(bindings) |
| 27 | + defp collect(_, _), do: {[], []} |
| 28 | + |
| 29 | + defp do_collect(bindings, acc \\ {[], []}) do |
| 30 | + Enum.reduce(bindings, acc, fn |
| 31 | + %{user_id: uid}, {users, groups} when is_binary(uid) -> {[uid | users], groups} |
| 32 | + %{group_id: gid}, {users, groups} when is_binary(gid) -> {users, [gid | groups]} |
| 33 | + _, acc -> acc |
| 34 | + end) |
| 35 | + end |
| 36 | +end |
| 37 | + |
1 | 38 | ## I need to find a way to provide authorization for this that's not a major perf drag |
2 | 39 |
|
3 | 40 | # defimpl Console.PubSub.Rtc, for: [ |
4 | 41 | # Console.PubSub.ServiceUpdated, |
5 | 42 | # Console.PubSub.ClusterUpdated, |
6 | | -# Console.PubSub.ProviderUpdated, |
7 | | -# Console.PubSub.GitRepositoryUpdated, |
| 43 | +# Console.PubSub.PipelineUpdated, |
| 44 | +# Console.PubSub.ServiceDeleted, |
| 45 | +# Console.PubSub.ServiceComponentsUpdated, |
8 | 46 | # ] do |
9 | | -# def deliver(%{item: item}), do: {item, :update} |
| 47 | +# alias Console.Deployments.Rtc.Utils |
| 48 | +# def deliver(%{item: item}) do |
| 49 | +# {users, groups} = Utils.audience(item) |
| 50 | +# channel = channel(item) |
| 51 | +# topics = |
| 52 | +# Enum.map(users, & "#{channel}:user:#{&1}") ++ |
| 53 | +# Enum.map(groups, & "#{channel}:group:#{&1}") |
| 54 | +# {item, topics, :update} |
| 55 | +# end |
| 56 | + |
| 57 | +# defp channel(%Console.Schema.Service{}), do: "service" |
| 58 | +# defp channel(%Console.Schema.Cluster{}), do: "cluster" |
| 59 | +# defp channel(%Console.Schema.Pipeline{}), do: "pipeline" |
10 | 60 | # end |
11 | 61 |
|
12 | 62 | # defimpl Console.PubSub.Rtc, for: [ |
13 | 63 | # Console.PubSub.ServiceCreated, |
14 | | -# Console.PubSub.ServiceDeleted, |
15 | 64 | # Console.PubSub.ClusterCreated, |
16 | | -# Console.PubSub.ClusterDeleted, |
17 | | -# Console.PubSub.ProviderCreated, |
18 | | -# Console.PubSub.GitRepositoryCreated |
| 65 | +# Console.PubSub.PipelineCreated, |
| 66 | +# ] do |
| 67 | +# alias Console.Deployments.Rtc.Utils |
| 68 | +# def deliver(%{item: item}) do |
| 69 | +# {users, groups} = Utils.audience(item) |
| 70 | +# channel = channel(item) |
| 71 | +# topics = |
| 72 | +# Enum.map(users, & "#{channel}:user:#{&1}") ++ |
| 73 | +# Enum.map(groups, & "#{channel}:group:#{&1}") |
| 74 | +# {item, topics, :create} |
| 75 | +# end |
| 76 | + |
| 77 | +# defp channel(%Console.Schema.Service{}), do: "service" |
| 78 | +# defp channel(%Console.Schema.Cluster{}), do: "cluster" |
| 79 | +# defp channel(%Console.Schema.Pipeline{}), do: "pipeline" |
| 80 | +# end |
| 81 | + |
| 82 | +# defimpl Console.PubSub.Rtc, for: [ |
| 83 | +# Console.PubSub.ServiceHardDeleted, |
| 84 | +# Console.PubSub.ClusterCreated, |
| 85 | +# Console.PubSub.PipelineCreated |
19 | 86 | # ] do |
20 | | -# def deliver(%{item: item}), do: {item, :create} |
| 87 | +# alias Console.Deployments.Rtc.Utils |
| 88 | +# def deliver(%{item: item}) do |
| 89 | +# {users, groups} = Utils.audience(item) |
| 90 | +# channel = channel(item) |
| 91 | +# topics = |
| 92 | +# Enum.map(users, & "#{channel}:user:#{&1}") ++ |
| 93 | +# Enum.map(groups, & "#{channel}:group:#{&1}") |
| 94 | +# {item, topics, :create} |
| 95 | +# end |
| 96 | + |
| 97 | +# defp channel(%Console.Schema.Service{}), do: "service" |
| 98 | +# defp channel(%Console.Schema.Cluster{}), do: "cluster" |
| 99 | +# defp channel(%Console.Schema.Pipeline{}), do: "pipeline" |
21 | 100 | # end |
0 commit comments