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

Broadcast tags and value objects in restore events #1602

Merged
merged 1 commit into from
Jul 10, 2023
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
7 changes: 5 additions & 2 deletions lib/trento/application/projectors/cluster_projector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,17 @@ defmodule Trento.ClusterProjector do

@impl true
def after_update(%ClusterRestored{cluster_id: cluster_id}, _, _) do
cluster = Repo.get!(ClusterReadModel, cluster_id)
cluster =
ClusterReadModel
|> Repo.get!(cluster_id)
|> Repo.preload([:tags])

restored_cluster = enrich_cluster_model(cluster)

TrentoWeb.Endpoint.broadcast(
"monitoring:clusters",
"cluster_registered",
ClusterView.render("cluster_registered.json", cluster: restored_cluster)
ClusterView.render("cluster_restored.json", cluster: restored_cluster)
)
end

Expand Down
7 changes: 5 additions & 2 deletions lib/trento/application/projectors/database_projector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,15 @@ defmodule Trento.DatabaseProjector do
_,
_
) do
database = Repo.get!(DatabaseReadModel, sap_system_id)
database =
DatabaseReadModel
|> Repo.get!(sap_system_id)
|> Repo.preload([:tags])

TrentoWeb.Endpoint.broadcast(
@databases_topic,
"database_registered",
SapSystemView.render("database_registered.json", database: database)
SapSystemView.render("database_restored.json", database: database)
)
end

Expand Down
7 changes: 5 additions & 2 deletions lib/trento/application/projectors/host_projector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,15 @@ defmodule Trento.HostProjector do
_,
_
) do
host = Repo.get!(HostReadModel, id)
host =
HostReadModel
|> Repo.get!(id)
|> Repo.preload([:sles_subscriptions, :tags])

TrentoWeb.Endpoint.broadcast(
"monitoring:hosts",
"host_registered",
jamie-suse marked this conversation as resolved.
Show resolved Hide resolved
HostView.render("host_registered.json", host: host)
HostView.render("host_restored.json", host: host)
)
end

Expand Down
7 changes: 5 additions & 2 deletions lib/trento/application/projectors/sap_system_projector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,15 @@ defmodule Trento.SapSystemProjector do
_,
_
) do
sap_system = Repo.get!(SapSystemReadModel, sap_system_id)
sap_system =
SapSystemReadModel
|> Repo.get!(sap_system_id)
|> Repo.preload([:tags])

TrentoWeb.Endpoint.broadcast(
@sap_systems_topic,
"sap_system_registered",
SapSystemView.render("sap_system_registered.json", sap_system: sap_system)
SapSystemView.render("sap_system_restored.json", sap_system: sap_system)
)
end

Expand Down
4 changes: 4 additions & 0 deletions lib/trento_web/views/v1/host_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ defmodule TrentoWeb.V1.HostView do
|> Map.delete(:tags)
end

def render("host_restored.json", %{host: host}) do
render("host.json", %{host: host})
end

def render("heartbeat_result.json", %{host: %{id: id, hostname: hostname}}) do
%{id: id, hostname: hostname}
end
Expand Down
15 changes: 15 additions & 0 deletions lib/trento_web/views/v1/sap_system_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ defmodule TrentoWeb.V1.SapSystemView do
|> Map.delete(:database_instances)
end

def render("database_restored.json", %{database: database}) do
database
|> Map.from_struct()
|> Map.delete(:__meta__)
|> Map.delete(:database_instances)
end

def render("database_health_changed.json", %{health: health}), do: health

def render("database_instance_health_changed.json", %{
Expand Down Expand Up @@ -124,6 +131,14 @@ defmodule TrentoWeb.V1.SapSystemView do
|> Map.delete(:tags)
end

def render("sap_system_restored.json", %{sap_system: sap_system}) do
sap_system
|> Map.from_struct()
|> Map.delete(:__meta__)
|> Map.delete(:database_instances)
|> Map.delete(:application_instances)
end

def render("sap_system_updated.json", %{id: id, ensa_version: ensa_version}),
do: %{id: id, ensa_version: ensa_version}

Expand Down
4 changes: 4 additions & 0 deletions lib/trento_web/views/v2/cluster_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ defmodule TrentoWeb.V2.ClusterView do
Map.delete(render("cluster.json", %{cluster: cluster}), :tags)
end

def render("cluster_restored.json", %{cluster: cluster}) do
render("cluster.json", %{cluster: cluster})
end

def render("cluster_details_updated.json", %{data: data}) do
data
|> Map.from_struct()
Expand Down
13 changes: 10 additions & 3 deletions test/trento/application/projectors/cluster_projector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,22 @@ defmodule Trento.ClusterProjectorTest do
test "should set deregistered_at field to nil when ClusterRestored is received" do
%{id: cluster_id, name: name, type: type} =
insert(:cluster,
id: Faker.UUID.v4(),
name: "deregistered_cluster",
selected_checks: [],
deregistered_at: DateTime.utc_now()
)

insert_list(5, :tag, resource_id: cluster_id)

event = ClusterRestored.new!(%{cluster_id: cluster_id})

ProjectorTestHelper.project(ClusterProjector, event, "cluster_projector")
cluster_projection = Repo.get!(ClusterReadModel, event.cluster_id)

%{tags: tags} =
cluster_projection =
ClusterReadModel
|> Repo.get!(event.cluster_id)
|> Repo.preload([:tags])

assert nil == cluster_projection.deregistered_at

Expand All @@ -228,7 +234,8 @@ defmodule Trento.ClusterProjectorTest do
cib_last_written: nil,
id: ^cluster_id,
name: ^name,
type: ^type
type: ^type,
tags: ^tags
},
1000
end
Expand Down
24 changes: 16 additions & 8 deletions test/trento/application/projectors/database_projector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,14 @@ defmodule Trento.DatabaseProjectorTest do
end

test "should restore a deregistered database when DatabaseRestored is received" do
insert(:database,
id: sap_system_id = Faker.UUID.v4(),
sid: "NWD",
deregistered_at: DateTime.utc_now(),
health: :critical
)
%{id: sap_system_id} =
insert(:database,
sid: "NWD",
deregistered_at: DateTime.utc_now(),
health: :critical
)

insert_list(5, :tag, resource_id: sap_system_id)

event = %DatabaseRestored{
sap_system_id: sap_system_id,
Expand All @@ -322,15 +324,21 @@ defmodule Trento.DatabaseProjectorTest do

ProjectorTestHelper.project(DatabaseProjector, event, "database_projector")

projection = Repo.get(DatabaseReadModel, sap_system_id)
%{tags: tags} =
projection =
DatabaseReadModel
|> Repo.get(sap_system_id)
|> Repo.preload([:tags])

assert nil == projection.deregistered_at
assert :passing == projection.health

assert_broadcast "database_registered",
%{
health: :passing,
id: ^sap_system_id,
sid: "NWD"
sid: "NWD",
tags: ^tags
},
1000
end
Expand Down
23 changes: 18 additions & 5 deletions test/trento/application/projectors/host_projector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,14 @@ defmodule Trento.HostProjectorTest do
end

test "should set deregistered_at to nil when HostRestored is received" do
host_id = UUID.uuid4()
insert(:host, id: host_id, deregistered_at: DateTime.utc_now())
%{id: host_id} =
insert(
:host,
deregistered_at: DateTime.utc_now()
)

insert_list(5, :tag, resource_id: host_id)
insert_list(5, :sles_subscription, host_id: host_id)

event = %HostRestored{
host_id: host_id
Expand All @@ -432,8 +438,13 @@ defmodule Trento.HostProjectorTest do
cluster_id: cluster_id,
provider: provider,
provider_data: provider_data,
deregistered_at: deregistered_at
} = Repo.get!(HostReadModel, host_id)
deregistered_at: deregistered_at,
sles_subscriptions: sles_subscriptions,
tags: tags
} =
HostReadModel
|> Repo.get!(host_id)
|> Repo.preload([:sles_subscriptions, :tags])

assert nil == deregistered_at

Expand All @@ -446,7 +457,9 @@ defmodule Trento.HostProjectorTest do
id: ^id,
ip_addresses: ^ip_addresses,
provider: ^provider,
provider_data: ^provider_data
provider_data: ^provider_data,
sles_subscriptions: ^sles_subscriptions,
tags: ^tags
},
1000
end
Expand Down
11 changes: 9 additions & 2 deletions test/trento/application/projectors/sap_system_projector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ defmodule Trento.SapSystemProjectorTest do
%{tenant: tenant, id: sap_system_id, sid: sid} =
insert(:sap_system, deregistered_at: DateTime.utc_now())

insert_list(5, :tag, resource_id: sap_system_id)

new_db_host = Faker.Internet.ip_v4_address()
new_health = :passing

Expand All @@ -252,7 +254,11 @@ defmodule Trento.SapSystemProjectorTest do

ProjectorTestHelper.project(SapSystemProjector, event, "sap_system_projector")

projection = Repo.get(SapSystemReadModel, sap_system_id)
%{tags: tags} =
projection =
SapSystemReadModel
|> Repo.get(sap_system_id)
|> Repo.preload([:tags])

assert_broadcast(
"sap_system_registered",
Expand All @@ -261,7 +267,8 @@ defmodule Trento.SapSystemProjectorTest do
health: ^new_health,
id: ^sap_system_id,
sid: ^sid,
tenant: ^tenant
tenant: ^tenant,
tags: ^tags
},
1000
)
Expand Down