From dcb149c85561bdd2a921247288e5eae062c4a41f Mon Sep 17 00:00:00 2001 From: Xabier Arbulu Insausti Date: Thu, 21 Mar 2024 13:49:08 +0100 Subject: [PATCH] Move database events (#2452) * Create new databases context file * Move the database events to its folder --- lib/trento/databases.ex | 68 +++++ lib/trento/databases/database.ex | 30 +-- .../databases/events/database_deregistered.ex | 12 + .../events/database_health_changed.ex | 14 ++ .../events/database_instance_deregistered.ex | 14 ++ .../database_instance_health_changed.ex | 16 ++ .../events/database_instance_marked_absent.ex | 14 ++ .../database_instance_marked_present.ex | 13 + .../events/database_instance_registered.ex | 25 ++ ...ase_instance_system_replication_changed.ex | 15 ++ .../databases/events/database_registered.ex | 15 ++ .../databases/events/database_restored.ex | 14 ++ .../events/legacy}/database_deregistered.ex | 2 +- .../events/legacy}/database_health_changed.ex | 2 +- .../legacy}/database_instance_deregistered.ex | 2 +- .../database_instance_health_changed.ex | 2 +- .../database_instance_marked_absent.ex | 2 +- .../database_instance_marked_present.ex | 2 +- .../legacy}/database_instance_registered.ex | 2 +- ...ase_instance_system_replication_changed.ex | 2 +- .../events/legacy}/database_registered.ex | 2 +- .../events/legacy}/database_restored.ex | 2 +- lib/trento/discovery.ex | 6 +- .../event_handlers/alerts_event_handler.ex | 11 +- .../deregistration_process_manager.ex | 11 +- lib/trento/sap_systems.ex | 53 +--- .../projections/database_projector.ex | 32 +-- .../controllers/v1/sap_system_controller.ex | 9 +- test/support/factory.ex | 25 +- test/trento/databases/database_test.exs | 234 +++++++++--------- test/trento/databases_test.exs | 106 ++++++++ .../stream_roll_up_event_handler_test.exs | 2 +- .../deregistration_process_manager_test.exs | 16 +- .../projections/database_projector_test.exs | 44 ++-- test/trento/sap_systems_test.exs | 95 +------ 35 files changed, 554 insertions(+), 360 deletions(-) create mode 100644 lib/trento/databases.ex create mode 100644 lib/trento/databases/events/database_deregistered.ex create mode 100644 lib/trento/databases/events/database_health_changed.ex create mode 100644 lib/trento/databases/events/database_instance_deregistered.ex create mode 100644 lib/trento/databases/events/database_instance_health_changed.ex create mode 100644 lib/trento/databases/events/database_instance_marked_absent.ex create mode 100644 lib/trento/databases/events/database_instance_marked_present.ex create mode 100644 lib/trento/databases/events/database_instance_registered.ex create mode 100644 lib/trento/databases/events/database_instance_system_replication_changed.ex create mode 100644 lib/trento/databases/events/database_registered.ex create mode 100644 lib/trento/databases/events/database_restored.ex rename lib/trento/{sap_systems/events => databases/events/legacy}/database_deregistered.ex (82%) rename lib/trento/{sap_systems/events => databases/events/legacy}/database_health_changed.ex (80%) rename lib/trento/{sap_systems/events => databases/events/legacy}/database_instance_deregistered.ex (82%) rename lib/trento/{sap_systems/events => databases/events/legacy}/database_instance_health_changed.ex (82%) rename lib/trento/{sap_systems/events => databases/events/legacy}/database_instance_marked_absent.ex (81%) rename lib/trento/{sap_systems/events => databases/events/legacy}/database_instance_marked_present.ex (79%) rename lib/trento/{sap_systems/events => databases/events/legacy}/database_instance_registered.ex (89%) rename lib/trento/{sap_systems/events => databases/events/legacy}/database_instance_system_replication_changed.ex (81%) rename lib/trento/{sap_systems/events => databases/events/legacy}/database_registered.ex (82%) rename lib/trento/{sap_systems/events => databases/events/legacy}/database_restored.ex (81%) create mode 100644 test/trento/databases_test.exs diff --git a/lib/trento/databases.ex b/lib/trento/databases.ex new file mode 100644 index 0000000000..33c1fd3270 --- /dev/null +++ b/lib/trento/databases.ex @@ -0,0 +1,68 @@ +defmodule Trento.Databases do + @moduledoc """ + Provides a set of functions to interact with databases. + """ + + import Ecto.Query + + alias Trento.SapSystems.Projections.{ + DatabaseInstanceReadModel, + DatabaseReadModel + } + + alias Trento.Support.DateService + + alias Trento.Databases.Commands.DeregisterDatabaseInstance + + alias Trento.Repo + + @spec get_all_databases :: [DatabaseReadModel.t()] + def get_all_databases do + DatabaseReadModel + |> where([d], is_nil(d.deregistered_at)) + |> order_by(asc: :sid) + |> Repo.all() + |> Repo.preload([ + :database_instances, + :tags + ]) + end + + @spec get_database_instances_by_host_id(String.t()) :: [DatabaseInstanceReadModel.t()] + def get_database_instances_by_host_id(host_id) do + DatabaseInstanceReadModel + |> where([d], d.host_id == ^host_id) + |> Repo.all() + end + + @spec deregister_database_instance(Ecto.UUID.t(), Ecto.UUID.t(), String.t(), DateService) :: + :ok | {:error, :instance_present} | {:error, :database_instance_not_registered} + def deregister_database_instance( + database_id, + host_id, + instance_number, + date_service \\ DateService + ) do + case Repo.get_by(DatabaseInstanceReadModel, + sap_system_id: database_id, + host_id: host_id, + instance_number: instance_number + ) do + %DatabaseInstanceReadModel{absent_at: nil} -> + {:error, :instance_present} + + _ -> + commanded().dispatch( + DeregisterDatabaseInstance.new!(%{ + database_id: database_id, + host_id: host_id, + instance_number: instance_number, + deregistered_at: date_service.utc_now() + }) + ) + end + end + + defp commanded, + do: Application.fetch_env!(:trento, Trento.Commanded)[:adapter] +end diff --git a/lib/trento/databases/database.ex b/lib/trento/databases/database.ex index 30eee673d1..6245107646 100644 --- a/lib/trento/databases/database.ex +++ b/lib/trento/databases/database.ex @@ -25,7 +25,7 @@ defmodule Trento.Databases.Database do RegisterDatabaseInstance } - alias Trento.SapSystems.Events.{ + alias Trento.Databases.Events.{ DatabaseDeregistered, DatabaseHealthChanged, DatabaseInstanceDeregistered, @@ -84,12 +84,12 @@ defmodule Trento.Databases.Database do ) do [ %DatabaseRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: sid, health: health }, %DatabaseInstanceRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: sid, tenant: tenant, instance_number: instance_number, @@ -139,7 +139,7 @@ defmodule Trento.Databases.Database do when not is_nil(deregistered_at) do [ %DatabaseInstanceRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: sid, tenant: tenant, instance_number: instance_number, @@ -154,7 +154,7 @@ defmodule Trento.Databases.Database do health: health }, %DatabaseRestored{ - sap_system_id: database_id, + database_id: database_id, health: health } ] @@ -206,7 +206,7 @@ defmodule Trento.Databases.Database do %DatabaseInstanceMarkedAbsent{ instance_number: instance_number, host_id: host_id, - sap_system_id: database_id, + database_id: database_id, absent_at: absent_at } @@ -245,7 +245,7 @@ defmodule Trento.Databases.Database do def apply( %Database{database_id: nil}, %DatabaseRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: sid, health: health } @@ -426,7 +426,7 @@ defmodule Trento.Databases.Database do } ) do %DatabaseInstanceRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: sid, tenant: tenant, instance_number: instance_number, @@ -460,7 +460,7 @@ defmodule Trento.Databases.Database do %DatabaseInstanceMarkedPresent{ instance_number: instance_number, host_id: host_id, - sap_system_id: database_id + database_id: database_id } end @@ -482,7 +482,7 @@ defmodule Trento.Databases.Database do when system_replication != new_system_replication or system_replication_status != new_system_replication_status do %DatabaseInstanceSystemReplicationChanged{ - sap_system_id: database_id, + database_id: database_id, host_id: host_id, instance_number: instance_number, system_replication: new_system_replication, @@ -505,7 +505,7 @@ defmodule Trento.Databases.Database do ) when health != new_health do %DatabaseInstanceHealthChanged{ - sap_system_id: database_id, + database_id: database_id, host_id: host_id, instance_number: instance_number, health: new_health @@ -528,7 +528,7 @@ defmodule Trento.Databases.Database do if new_health != health do %DatabaseHealthChanged{ - sap_system_id: database_id, + database_id: database_id, health: new_health } end @@ -555,7 +555,7 @@ defmodule Trento.Databases.Database do _ -> %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, instance_number: instance_number, host_id: host_id, deregistered_at: deregistered_at @@ -571,7 +571,7 @@ defmodule Trento.Databases.Database do }, deregistered_at ) do - %DatabaseDeregistered{sap_system_id: database_id, deregistered_at: deregistered_at} + %DatabaseDeregistered{database_id: database_id, deregistered_at: deregistered_at} end defp maybe_emit_database_deregistered_event( @@ -594,7 +594,7 @@ defmodule Trento.Databases.Database do if has_secondary? and !has_primary? do %DatabaseDeregistered{ - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at } end diff --git a/lib/trento/databases/events/database_deregistered.ex b/lib/trento/databases/events/database_deregistered.ex new file mode 100644 index 0000000000..57f4b7f3b9 --- /dev/null +++ b/lib/trento/databases/events/database_deregistered.ex @@ -0,0 +1,12 @@ +defmodule Trento.Databases.Events.DatabaseDeregistered do + @moduledoc """ + This event is emitted once all database instances belonging to a HANA database have been deregistered (decommissioned). + """ + + use Trento.Support.Event + + defevent do + field :database_id, Ecto.UUID + field :deregistered_at, :utc_datetime_usec + end +end diff --git a/lib/trento/databases/events/database_health_changed.ex b/lib/trento/databases/events/database_health_changed.ex new file mode 100644 index 0000000000..c78c772048 --- /dev/null +++ b/lib/trento/databases/events/database_health_changed.ex @@ -0,0 +1,14 @@ +defmodule Trento.Databases.Events.DatabaseHealthChanged do + @moduledoc """ + This event is emitted when a database health has changed. + """ + + use Trento.Support.Event + + require Trento.Enums.Health, as: Health + + defevent do + field :database_id, Ecto.UUID + field :health, Ecto.Enum, values: Health.values() + end +end diff --git a/lib/trento/databases/events/database_instance_deregistered.ex b/lib/trento/databases/events/database_instance_deregistered.ex new file mode 100644 index 0000000000..c5e9914c32 --- /dev/null +++ b/lib/trento/databases/events/database_instance_deregistered.ex @@ -0,0 +1,14 @@ +defmodule Trento.Databases.Events.DatabaseInstanceDeregistered do + @moduledoc """ + This event is emitted when a database instance is deregistered (decommissioned). + """ + + use Trento.Support.Event + + defevent do + field :instance_number, :string + field :host_id, Ecto.UUID + field :database_id, Ecto.UUID + field :deregistered_at, :utc_datetime_usec + end +end diff --git a/lib/trento/databases/events/database_instance_health_changed.ex b/lib/trento/databases/events/database_instance_health_changed.ex new file mode 100644 index 0000000000..52a4d28905 --- /dev/null +++ b/lib/trento/databases/events/database_instance_health_changed.ex @@ -0,0 +1,16 @@ +defmodule Trento.Databases.Events.DatabaseInstanceHealthChanged do + @moduledoc """ + This event is emitted when a database instance health has changed. + """ + + use Trento.Support.Event + + require Trento.Enums.Health, as: Health + + defevent do + field :database_id, Ecto.UUID + field :host_id, Ecto.UUID + field :instance_number, :string + field :health, Ecto.Enum, values: Health.values() + end +end diff --git a/lib/trento/databases/events/database_instance_marked_absent.ex b/lib/trento/databases/events/database_instance_marked_absent.ex new file mode 100644 index 0000000000..df5015bef2 --- /dev/null +++ b/lib/trento/databases/events/database_instance_marked_absent.ex @@ -0,0 +1,14 @@ +defmodule Trento.Databases.Events.DatabaseInstanceMarkedAbsent do + @moduledoc """ + This event is emitted when a database instance is marked as absent. + """ + + use Trento.Support.Event + + defevent do + field :instance_number, :string + field :host_id, Ecto.UUID + field :database_id, Ecto.UUID + field :absent_at, :utc_datetime_usec + end +end diff --git a/lib/trento/databases/events/database_instance_marked_present.ex b/lib/trento/databases/events/database_instance_marked_present.ex new file mode 100644 index 0000000000..85657a5b15 --- /dev/null +++ b/lib/trento/databases/events/database_instance_marked_present.ex @@ -0,0 +1,13 @@ +defmodule Trento.Databases.Events.DatabaseInstanceMarkedPresent do + @moduledoc """ + This event is emitted when a database instance is marked as present. + """ + + use Trento.Support.Event + + defevent do + field :instance_number, :string + field :host_id, Ecto.UUID + field :database_id, Ecto.UUID + end +end diff --git a/lib/trento/databases/events/database_instance_registered.ex b/lib/trento/databases/events/database_instance_registered.ex new file mode 100644 index 0000000000..756389a4a0 --- /dev/null +++ b/lib/trento/databases/events/database_instance_registered.ex @@ -0,0 +1,25 @@ +defmodule Trento.Databases.Events.DatabaseInstanceRegistered do + @moduledoc """ + This event is emitted when a database instance is registered. + """ + + use Trento.Support.Event + + require Trento.Enums.Health, as: Health + + defevent do + field :database_id, Ecto.UUID + field :sid, :string + field :tenant, :string + field :host_id, Ecto.UUID + field :instance_number, :string + field :instance_hostname, :string + field :features, :string + field :http_port, :integer + field :https_port, :integer + field :start_priority, :string + field :system_replication, :string + field :system_replication_status, :string + field :health, Ecto.Enum, values: Health.values() + end +end diff --git a/lib/trento/databases/events/database_instance_system_replication_changed.ex b/lib/trento/databases/events/database_instance_system_replication_changed.ex new file mode 100644 index 0000000000..a057c96fba --- /dev/null +++ b/lib/trento/databases/events/database_instance_system_replication_changed.ex @@ -0,0 +1,15 @@ +defmodule Trento.Databases.Events.DatabaseInstanceSystemReplicationChanged do + @moduledoc """ + This event is emitted when a database instance system replication has changed. + """ + + use Trento.Support.Event + + defevent do + field :database_id, Ecto.UUID + field :host_id, Ecto.UUID + field :instance_number, :string + field :system_replication, :string + field :system_replication_status, :string + end +end diff --git a/lib/trento/databases/events/database_registered.ex b/lib/trento/databases/events/database_registered.ex new file mode 100644 index 0000000000..a64ed3efde --- /dev/null +++ b/lib/trento/databases/events/database_registered.ex @@ -0,0 +1,15 @@ +defmodule Trento.Databases.Events.DatabaseRegistered do + @moduledoc """ + This event is emitted when a database is registered. + """ + + use Trento.Support.Event + + require Trento.Enums.Health, as: Health + + defevent do + field :database_id, Ecto.UUID + field :sid, :string + field :health, Ecto.Enum, values: Health.values() + end +end diff --git a/lib/trento/databases/events/database_restored.ex b/lib/trento/databases/events/database_restored.ex new file mode 100644 index 0000000000..b1bfe76b7b --- /dev/null +++ b/lib/trento/databases/events/database_restored.ex @@ -0,0 +1,14 @@ +defmodule Trento.Databases.Events.DatabaseRestored do + @moduledoc """ + This event is emitted when a database is restored. + """ + + use Trento.Support.Event + + require Trento.Enums.Health, as: Health + + defevent do + field :database_id, Ecto.UUID + field :health, Ecto.Enum, values: Health.values() + end +end diff --git a/lib/trento/sap_systems/events/database_deregistered.ex b/lib/trento/databases/events/legacy/database_deregistered.ex similarity index 82% rename from lib/trento/sap_systems/events/database_deregistered.ex rename to lib/trento/databases/events/legacy/database_deregistered.ex index 0832ca331c..ce80ac54fe 100644 --- a/lib/trento/sap_systems/events/database_deregistered.ex +++ b/lib/trento/databases/events/legacy/database_deregistered.ex @@ -5,7 +5,7 @@ defmodule Trento.SapSystems.Events.DatabaseDeregistered do use Trento.Support.Event - defevent do + defevent superseded_by: Trento.Databases.Events.DatabaseDeregistered do field :sap_system_id, Ecto.UUID field :deregistered_at, :utc_datetime_usec end diff --git a/lib/trento/sap_systems/events/database_health_changed.ex b/lib/trento/databases/events/legacy/database_health_changed.ex similarity index 80% rename from lib/trento/sap_systems/events/database_health_changed.ex rename to lib/trento/databases/events/legacy/database_health_changed.ex index efd6b6f4de..966cd3809d 100644 --- a/lib/trento/sap_systems/events/database_health_changed.ex +++ b/lib/trento/databases/events/legacy/database_health_changed.ex @@ -7,7 +7,7 @@ defmodule Trento.SapSystems.Events.DatabaseHealthChanged do require Trento.Enums.Health, as: Health - defevent do + defevent superseded_by: Trento.Databases.Events.DatabaseHealthChanged do field :sap_system_id, Ecto.UUID field :health, Ecto.Enum, values: Health.values() end diff --git a/lib/trento/sap_systems/events/database_instance_deregistered.ex b/lib/trento/databases/events/legacy/database_instance_deregistered.ex similarity index 82% rename from lib/trento/sap_systems/events/database_instance_deregistered.ex rename to lib/trento/databases/events/legacy/database_instance_deregistered.ex index f0a907ce58..4ee38d57ec 100644 --- a/lib/trento/sap_systems/events/database_instance_deregistered.ex +++ b/lib/trento/databases/events/legacy/database_instance_deregistered.ex @@ -5,7 +5,7 @@ defmodule Trento.SapSystems.Events.DatabaseInstanceDeregistered do use Trento.Support.Event - defevent do + defevent superseded_by: Trento.Databases.Events.DatabaseInstanceDeregistered do field :instance_number, :string field :host_id, Ecto.UUID field :sap_system_id, Ecto.UUID diff --git a/lib/trento/sap_systems/events/database_instance_health_changed.ex b/lib/trento/databases/events/legacy/database_instance_health_changed.ex similarity index 82% rename from lib/trento/sap_systems/events/database_instance_health_changed.ex rename to lib/trento/databases/events/legacy/database_instance_health_changed.ex index ad8dae35fb..76100ac935 100644 --- a/lib/trento/sap_systems/events/database_instance_health_changed.ex +++ b/lib/trento/databases/events/legacy/database_instance_health_changed.ex @@ -7,7 +7,7 @@ defmodule Trento.SapSystems.Events.DatabaseInstanceHealthChanged do require Trento.Enums.Health, as: Health - defevent do + defevent superseded_by: Trento.Databases.Events.DatabaseInstanceHealthChanged do field :sap_system_id, Ecto.UUID field :host_id, Ecto.UUID field :instance_number, :string diff --git a/lib/trento/sap_systems/events/database_instance_marked_absent.ex b/lib/trento/databases/events/legacy/database_instance_marked_absent.ex similarity index 81% rename from lib/trento/sap_systems/events/database_instance_marked_absent.ex rename to lib/trento/databases/events/legacy/database_instance_marked_absent.ex index 7b687a3124..e0510fb725 100644 --- a/lib/trento/sap_systems/events/database_instance_marked_absent.ex +++ b/lib/trento/databases/events/legacy/database_instance_marked_absent.ex @@ -5,7 +5,7 @@ defmodule Trento.SapSystems.Events.DatabaseInstanceMarkedAbsent do use Trento.Support.Event - defevent do + defevent superseded_by: Trento.Databases.Events.DatabaseInstanceMarkedAbsent do field :instance_number, :string field :host_id, Ecto.UUID field :sap_system_id, Ecto.UUID diff --git a/lib/trento/sap_systems/events/database_instance_marked_present.ex b/lib/trento/databases/events/legacy/database_instance_marked_present.ex similarity index 79% rename from lib/trento/sap_systems/events/database_instance_marked_present.ex rename to lib/trento/databases/events/legacy/database_instance_marked_present.ex index 4c17c3c8f5..24034d4da1 100644 --- a/lib/trento/sap_systems/events/database_instance_marked_present.ex +++ b/lib/trento/databases/events/legacy/database_instance_marked_present.ex @@ -5,7 +5,7 @@ defmodule Trento.SapSystems.Events.DatabaseInstanceMarkedPresent do use Trento.Support.Event - defevent do + defevent superseded_by: Trento.Databases.Events.DatabaseInstanceMarkedPresent do field :instance_number, :string field :host_id, Ecto.UUID field :sap_system_id, Ecto.UUID diff --git a/lib/trento/sap_systems/events/database_instance_registered.ex b/lib/trento/databases/events/legacy/database_instance_registered.ex similarity index 89% rename from lib/trento/sap_systems/events/database_instance_registered.ex rename to lib/trento/databases/events/legacy/database_instance_registered.ex index d2ef596873..1a4e16e7ec 100644 --- a/lib/trento/sap_systems/events/database_instance_registered.ex +++ b/lib/trento/databases/events/legacy/database_instance_registered.ex @@ -7,7 +7,7 @@ defmodule Trento.SapSystems.Events.DatabaseInstanceRegistered do require Trento.Enums.Health, as: Health - defevent do + defevent superseded_by: Trento.Databases.Events.DatabaseInstanceRegistered do field :sap_system_id, Ecto.UUID field :sid, :string field :tenant, :string diff --git a/lib/trento/sap_systems/events/database_instance_system_replication_changed.ex b/lib/trento/databases/events/legacy/database_instance_system_replication_changed.ex similarity index 81% rename from lib/trento/sap_systems/events/database_instance_system_replication_changed.ex rename to lib/trento/databases/events/legacy/database_instance_system_replication_changed.ex index 874611e07e..0c8cfcbb4d 100644 --- a/lib/trento/sap_systems/events/database_instance_system_replication_changed.ex +++ b/lib/trento/databases/events/legacy/database_instance_system_replication_changed.ex @@ -5,7 +5,7 @@ defmodule Trento.SapSystems.Events.DatabaseInstanceSystemReplicationChanged do use Trento.Support.Event - defevent do + defevent superseded_by: Trento.Databases.Events.DatabaseInstanceSystemReplicationChanged do field :sap_system_id, Ecto.UUID field :host_id, Ecto.UUID field :instance_number, :string diff --git a/lib/trento/sap_systems/events/database_registered.ex b/lib/trento/databases/events/legacy/database_registered.ex similarity index 82% rename from lib/trento/sap_systems/events/database_registered.ex rename to lib/trento/databases/events/legacy/database_registered.ex index 4a806e26c6..d6e372b250 100644 --- a/lib/trento/sap_systems/events/database_registered.ex +++ b/lib/trento/databases/events/legacy/database_registered.ex @@ -7,7 +7,7 @@ defmodule Trento.SapSystems.Events.DatabaseRegistered do require Trento.Enums.Health, as: Health - defevent do + defevent superseded_by: Trento.Databases.Events.DatabaseRegistered do field :sap_system_id, Ecto.UUID field :sid, :string field :health, Ecto.Enum, values: Health.values() diff --git a/lib/trento/sap_systems/events/database_restored.ex b/lib/trento/databases/events/legacy/database_restored.ex similarity index 81% rename from lib/trento/sap_systems/events/database_restored.ex rename to lib/trento/databases/events/legacy/database_restored.ex index 7e42f0cd6f..d63c562644 100644 --- a/lib/trento/sap_systems/events/database_restored.ex +++ b/lib/trento/databases/events/legacy/database_restored.ex @@ -7,7 +7,7 @@ defmodule Trento.SapSystems.Events.DatabaseRestored do require Trento.Enums.Health, as: Health - defevent do + defevent superseded_by: Trento.Databases.Events.DatabaseRestored do field :sap_system_id, Ecto.UUID field :health, Ecto.Enum, values: Health.values() end diff --git a/lib/trento/discovery.ex b/lib/trento/discovery.ex index 4661555f39..18c72534e4 100644 --- a/lib/trento/discovery.ex +++ b/lib/trento/discovery.ex @@ -20,7 +20,7 @@ defmodule Trento.Discovery do SapSystemPolicy } - alias Trento.{Clusters, SapSystems} + alias Trento.{Clusters, Databases, SapSystems} @type command :: struct @@ -142,7 +142,7 @@ defmodule Trento.Discovery do defp do_handle(%{"discovery_type" => "saptune_discovery", "agent_id" => agent_id} = event) do current_application_instances = SapSystems.get_application_instances_by_host_id(agent_id) - current_database_instances = SapSystems.get_database_instances_by_host_id(agent_id) + current_database_instances = Databases.get_database_instances_by_host_id(agent_id) sap_running = length(current_application_instances) + length(current_database_instances) > 0 HostPolicy.handle(event, sap_running) @@ -150,7 +150,7 @@ defmodule Trento.Discovery do defp do_handle(%{"discovery_type" => "sap_system_discovery", "agent_id" => agent_id} = event) do current_application_instances = SapSystems.get_application_instances_by_host_id(agent_id) - current_database_instances = SapSystems.get_database_instances_by_host_id(agent_id) + current_database_instances = Databases.get_database_instances_by_host_id(agent_id) cluster_id = Trento.Clusters.get_cluster_id_by_host_id(agent_id) SapSystemPolicy.handle( diff --git a/lib/trento/infrastructure/commanded/event_handlers/alerts_event_handler.ex b/lib/trento/infrastructure/commanded/event_handlers/alerts_event_handler.ex index 20e229c260..610348d587 100644 --- a/lib/trento/infrastructure/commanded/event_handlers/alerts_event_handler.ex +++ b/lib/trento/infrastructure/commanded/event_handlers/alerts_event_handler.ex @@ -11,10 +11,9 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.AlertsEventHandler do alias Trento.Hosts.Events.HostHealthChanged - alias Trento.SapSystems.Events.{ - DatabaseHealthChanged, - SapSystemHealthChanged - } + alias Trento.Databases.Events.DatabaseHealthChanged + + alias Trento.SapSystems.Events.SapSystemHealthChanged alias Trento.Infrastructure.Alerting.Alerting @@ -35,11 +34,11 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.AlertsEventHandler do end def handle( - %DatabaseHealthChanged{sap_system_id: sap_system_id, health: health}, + %DatabaseHealthChanged{database_id: database_id, health: health}, _metadata ) when health == :critical do - Alerting.notify_critical_database_health(sap_system_id) + Alerting.notify_critical_database_health(database_id) end def handle( diff --git a/lib/trento/infrastructure/commanded/process_managers/deregistration_process_manager.ex b/lib/trento/infrastructure/commanded/process_managers/deregistration_process_manager.ex index 174eeef772..ea8a3dba0c 100644 --- a/lib/trento/infrastructure/commanded/process_managers/deregistration_process_manager.ex +++ b/lib/trento/infrastructure/commanded/process_managers/deregistration_process_manager.ex @@ -44,11 +44,14 @@ defmodule Trento.Infrastructure.Commanded.ProcessManagers.DeregistrationProcessM HostRolledUp } + alias Trento.Databases.Events.{ + DatabaseInstanceDeregistered, + DatabaseInstanceRegistered + } + alias Trento.SapSystems.Events.{ ApplicationInstanceDeregistered, ApplicationInstanceRegistered, - DatabaseInstanceDeregistered, - DatabaseInstanceRegistered, SapSystemRolledUp } @@ -190,7 +193,7 @@ defmodule Trento.Infrastructure.Commanded.ProcessManagers.DeregistrationProcessM def apply( %DeregistrationProcessManager{database_instances: database_instances} = state, %DatabaseInstanceRegistered{ - sap_system_id: sap_system_id, + database_id: database_id, instance_number: instance_number } ) do @@ -198,7 +201,7 @@ defmodule Trento.Infrastructure.Commanded.ProcessManagers.DeregistrationProcessM state | database_instances: [ %Instance{ - sap_system_id: sap_system_id, + sap_system_id: database_id, instance_number: instance_number } | database_instances diff --git a/lib/trento/sap_systems.ex b/lib/trento/sap_systems.ex index c035a4d796..51106c79a9 100644 --- a/lib/trento/sap_systems.ex +++ b/lib/trento/sap_systems.ex @@ -1,21 +1,17 @@ defmodule Trento.SapSystems do @moduledoc """ - Provides a set of functions to interact with SAP systems and HANA Databases. + Provides a set of functions to interact with SAP systems. """ import Ecto.Query alias Trento.SapSystems.Projections.{ ApplicationInstanceReadModel, - DatabaseInstanceReadModel, - DatabaseReadModel, SapSystemReadModel } alias Trento.Support.DateService - alias Trento.Databases.Commands.DeregisterDatabaseInstance - alias Trento.SapSystems.Commands.DeregisterApplicationInstance alias Trento.Repo @@ -33,18 +29,6 @@ defmodule Trento.SapSystems do ]) end - @spec get_all_databases :: [DatabaseReadModel.t()] - def get_all_databases do - DatabaseReadModel - |> where([d], is_nil(d.deregistered_at)) - |> order_by(asc: :sid) - |> Repo.all() - |> Repo.preload([ - :database_instances, - :tags - ]) - end - @spec get_application_instances_by_host_id(String.t()) :: [ApplicationInstanceReadModel.t()] def get_application_instances_by_host_id(host_id) do ApplicationInstanceReadModel @@ -52,13 +36,6 @@ defmodule Trento.SapSystems do |> Repo.all() end - @spec get_database_instances_by_host_id(String.t()) :: [DatabaseInstanceReadModel.t()] - def get_database_instances_by_host_id(host_id) do - DatabaseInstanceReadModel - |> where([d], d.host_id == ^host_id) - |> Repo.all() - end - @spec deregister_application_instance(Ecto.UUID.t(), Ecto.UUID.t(), String.t(), DateService) :: :ok | {:error, :instance_present} | {:error, :application_instance_not_registered} def deregister_application_instance( @@ -87,34 +64,6 @@ defmodule Trento.SapSystems do end end - @spec deregister_database_instance(Ecto.UUID.t(), Ecto.UUID.t(), String.t(), DateService) :: - :ok | {:error, :instance_present} | {:error, :database_instance_not_registered} - def deregister_database_instance( - sap_system_id, - host_id, - instance_number, - date_service \\ DateService - ) do - case Repo.get_by(DatabaseInstanceReadModel, - sap_system_id: sap_system_id, - host_id: host_id, - instance_number: instance_number - ) do - %DatabaseInstanceReadModel{absent_at: nil} -> - {:error, :instance_present} - - _ -> - commanded().dispatch( - DeregisterDatabaseInstance.new!(%{ - database_id: sap_system_id, - host_id: host_id, - instance_number: instance_number, - deregistered_at: date_service.utc_now() - }) - ) - end - end - defp commanded, do: Application.fetch_env!(:trento, Trento.Commanded)[:adapter] end diff --git a/lib/trento/sap_systems/projections/database_projector.ex b/lib/trento/sap_systems/projections/database_projector.ex index ec109f06bd..b2c7a474df 100644 --- a/lib/trento/sap_systems/projections/database_projector.ex +++ b/lib/trento/sap_systems/projections/database_projector.ex @@ -15,7 +15,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do alias TrentoWeb.V1.SapSystemView - alias Trento.SapSystems.Events.{ + alias Trento.Databases.Events.{ DatabaseDeregistered, DatabaseHealthChanged, DatabaseInstanceDeregistered, @@ -33,7 +33,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do @databases_topic "monitoring:databases" project( - %DatabaseRegistered{sap_system_id: sap_system_id, sid: sid, health: health}, + %DatabaseRegistered{database_id: sap_system_id, sid: sid, health: health}, fn multi -> changeset = DatabaseReadModel.changeset(%DatabaseReadModel{}, %{ @@ -48,7 +48,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do project( %DatabaseHealthChanged{ - sap_system_id: sap_system_id, + database_id: sap_system_id, health: health }, fn multi -> @@ -63,7 +63,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do project( %DatabaseInstanceRegistered{ - sap_system_id: sap_system_id, + database_id: sap_system_id, sid: sid, instance_number: instance_number, instance_hostname: instance_hostname, @@ -101,7 +101,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do project( %DatabaseInstanceHealthChanged{ - sap_system_id: sap_system_id, + database_id: sap_system_id, host_id: host_id, instance_number: instance_number, health: health @@ -122,7 +122,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do project( %DatabaseInstanceSystemReplicationChanged{ - sap_system_id: sap_system_id, + database_id: sap_system_id, host_id: host_id, instance_number: instance_number, system_replication: system_replication, @@ -149,7 +149,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do %DatabaseInstanceMarkedAbsent{ instance_number: instance_number, host_id: host_id, - sap_system_id: sap_system_id, + database_id: sap_system_id, absent_at: absent_at }, fn multi -> @@ -172,7 +172,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do %DatabaseInstanceMarkedPresent{ instance_number: instance_number, host_id: host_id, - sap_system_id: sap_system_id + database_id: sap_system_id }, fn multi -> changeset = @@ -192,7 +192,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do project( %DatabaseDeregistered{ - sap_system_id: sap_system_id, + database_id: sap_system_id, deregistered_at: deregistered_at }, fn multi -> @@ -207,7 +207,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do project( %DatabaseRestored{ - sap_system_id: sap_system_id, + database_id: sap_system_id, health: health }, fn multi -> @@ -224,7 +224,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do %DatabaseInstanceDeregistered{ instance_number: instance_number, host_id: host_id, - sap_system_id: sap_system_id + database_id: sap_system_id }, fn multi -> deregistered_instance = @@ -355,7 +355,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do %DatabaseInstanceMarkedAbsent{ instance_number: instance_number, host_id: host_id, - sap_system_id: sap_system_id, + database_id: sap_system_id, absent_at: absent_at }, _, @@ -381,7 +381,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do %DatabaseInstanceMarkedPresent{ instance_number: instance_number, host_id: host_id, - sap_system_id: sap_system_id + database_id: sap_system_id }, _, %{database_instance: %DatabaseInstanceReadModel{sid: sid}} @@ -403,7 +403,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do @impl true def after_update( - %DatabaseRestored{sap_system_id: sap_system_id}, + %DatabaseRestored{database_id: sap_system_id}, _, _ ) do @@ -422,7 +422,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do @impl true def after_update( %DatabaseDeregistered{ - sap_system_id: sap_system_id + database_id: sap_system_id }, _, %{ @@ -446,7 +446,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjector do %DatabaseInstanceDeregistered{ instance_number: instance_number, host_id: host_id, - sap_system_id: sap_system_id + database_id: sap_system_id }, _, %{ diff --git a/lib/trento_web/controllers/v1/sap_system_controller.ex b/lib/trento_web/controllers/v1/sap_system_controller.ex index 8653846705..02f6fab778 100644 --- a/lib/trento_web/controllers/v1/sap_system_controller.ex +++ b/lib/trento_web/controllers/v1/sap_system_controller.ex @@ -2,7 +2,10 @@ defmodule TrentoWeb.V1.SapSystemController do use TrentoWeb, :controller use OpenApiSpex.ControllerSpecs - alias Trento.SapSystems + alias Trento.{ + Databases, + SapSystems + } alias TrentoWeb.OpenApi.V1.Schema @@ -41,7 +44,7 @@ defmodule TrentoWeb.V1.SapSystemController do ] def list_databases(conn, _) do - databases = SapSystems.get_all_databases() + databases = Databases.get_all_databases() render(conn, "databases.json", databases: databases) end @@ -117,7 +120,7 @@ defmodule TrentoWeb.V1.SapSystemController do host_id: host_id, instance_number: instance_number }) do - with :ok <- SapSystems.deregister_database_instance(sap_system_id, host_id, instance_number) do + with :ok <- Databases.deregister_database_instance(sap_system_id, host_id, instance_number) do send_resp(conn, 204, "") end end diff --git a/test/support/factory.ex b/test/support/factory.ex index d33a7b43c3..0c5679f6eb 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -42,16 +42,19 @@ defmodule Trento.Factory do SoftwareUpdatesDiscoveryCompleted } - alias Trento.SapSystems.Events.{ - ApplicationInstanceDeregistered, - ApplicationInstanceMarkedAbsent, - ApplicationInstanceRegistered, + alias Trento.Databases.Events.{ DatabaseDeregistered, DatabaseInstanceDeregistered, DatabaseInstanceMarkedAbsent, DatabaseInstanceRegistered, DatabaseRegistered, - DatabaseRestored, + DatabaseRestored + } + + alias Trento.SapSystems.Events.{ + ApplicationInstanceDeregistered, + ApplicationInstanceMarkedAbsent, + ApplicationInstanceRegistered, SapSystemDeregistered, SapSystemRegistered, SapSystemTombstoned @@ -305,7 +308,7 @@ defmodule Trento.Factory do def database_instance_registered_event_factory do %DatabaseInstanceRegistered{ - sap_system_id: Faker.UUID.v4(), + database_id: Faker.UUID.v4(), sid: Faker.UUID.v4(), tenant: Faker.UUID.v4(), instance_number: "00", @@ -325,7 +328,7 @@ defmodule Trento.Factory do DatabaseInstanceMarkedAbsent.new!(%{ instance_number: "00", host_id: Faker.UUID.v4(), - sap_system_id: Faker.UUID.v4(), + database_id: Faker.UUID.v4(), absent_at: DateTime.utc_now() }) end @@ -334,14 +337,14 @@ defmodule Trento.Factory do DatabaseInstanceDeregistered.new!(%{ instance_number: "00", host_id: Faker.UUID.v4(), - sap_system_id: Faker.UUID.v4(), + database_id: Faker.UUID.v4(), deregistered_at: DateTime.utc_now() }) end def database_restored_event_factory do DatabaseRestored.new!(%{ - sap_system_id: Faker.UUID.v4(), + database_id: Faker.UUID.v4(), health: Health.passing() }) end @@ -357,7 +360,7 @@ defmodule Trento.Factory do def database_deregistered_event_factory do DatabaseDeregistered.new!(%{ - sap_system_id: Faker.UUID.v4(), + database_id: Faker.UUID.v4(), deregistered_at: DateTime.utc_now() }) end @@ -406,7 +409,7 @@ defmodule Trento.Factory do def database_registered_event_factory do %DatabaseRegistered{ - sap_system_id: Faker.UUID.v4(), + database_id: Faker.UUID.v4(), sid: Faker.UUID.v4(), health: Health.passing() } diff --git a/test/trento/databases/database_test.exs b/test/trento/databases/database_test.exs index eb6cbd8ea8..ba3be35bdc 100644 --- a/test/trento/databases/database_test.exs +++ b/test/trento/databases/database_test.exs @@ -9,7 +9,7 @@ defmodule Trento.Databases.DatabaseTest do RegisterDatabaseInstance } - alias Trento.SapSystems.Events.{ + alias Trento.Databases.Events.{ DatabaseDeregistered, DatabaseHealthChanged, DatabaseInstanceDeregistered, @@ -68,12 +68,12 @@ defmodule Trento.Databases.DatabaseTest do }), [ %DatabaseRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: sid, health: :passing }, %DatabaseInstanceRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: sid, tenant: tenant, instance_number: instance_number, @@ -139,12 +139,12 @@ defmodule Trento.Databases.DatabaseTest do }), [ %DatabaseRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: sid, health: :passing }, %DatabaseInstanceRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: sid, tenant: tenant, instance_number: instance_number, @@ -189,12 +189,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: sid, tenant: tenant, instance_number: "10" @@ -215,7 +215,7 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: sid, tenant: tenant, instance_number: instance_number, @@ -246,7 +246,7 @@ defmodule Trento.Databases.DatabaseTest do database_instance_registered_event = build( :database_instance_registered_event, - sap_system_id: database_registered_event.sap_system_id + database_id: database_registered_event.database_id ) initial_events = [ @@ -258,7 +258,7 @@ defmodule Trento.Databases.DatabaseTest do initial_events, build( :register_database_instance_command, - database_id: database_registered_event.sap_system_id, + database_id: database_registered_event.database_id, sid: database_instance_registered_event.sid, tenant: database_instance_registered_event.tenant, instance_number: database_instance_registered_event.instance_number, @@ -278,7 +278,7 @@ defmodule Trento.Databases.DatabaseTest do database_instance_registered_event = build( :database_instance_registered_event, - sap_system_id: database_registered_event.sap_system_id, + database_id: database_registered_event.database_id, system_replication: "Secondary", system_replication_status: "" ) @@ -292,7 +292,7 @@ defmodule Trento.Databases.DatabaseTest do initial_events, build( :register_database_instance_command, - database_id: database_registered_event.sap_system_id, + database_id: database_registered_event.database_id, sid: database_instance_registered_event.sid, tenant: database_instance_registered_event.tenant, instance_number: database_instance_registered_event.instance_number, @@ -303,7 +303,7 @@ defmodule Trento.Databases.DatabaseTest do health: :passing ), %DatabaseInstanceSystemReplicationChanged{ - sap_system_id: database_registered_event.sap_system_id, + database_id: database_registered_event.database_id, host_id: database_instance_registered_event.host_id, instance_number: database_instance_registered_event.instance_number, system_replication: "Primary", @@ -323,8 +323,8 @@ defmodule Trento.Databases.DatabaseTest do host_id = Faker.UUID.v4() initial_events = [ - build(:database_registered_event, sap_system_id: database_id), - build(:database_instance_registered_event, sap_system_id: database_id) + build(:database_registered_event, database_id: database_id), + build(:database_instance_registered_event, database_id: database_id) ] assert_events_and_state( @@ -342,7 +342,7 @@ defmodule Trento.Databases.DatabaseTest do [ build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: sid, tenant: tenant, instance_number: instance_number, @@ -351,7 +351,7 @@ defmodule Trento.Databases.DatabaseTest do health: :critical ), %DatabaseHealthChanged{ - sap_system_id: database_id, + database_id: database_id, health: :critical } ], @@ -379,13 +379,13 @@ defmodule Trento.Databases.DatabaseTest do database_instance_registered_event = build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: host_id, instance_number: instance_number ) initial_events = [ - build(:database_registered_event, sap_system_id: database_id), + build(:database_registered_event, database_id: database_id), database_instance_registered_event ] @@ -403,13 +403,13 @@ defmodule Trento.Databases.DatabaseTest do ), [ %DatabaseInstanceHealthChanged{ - sap_system_id: database_id, + database_id: database_id, instance_number: instance_number, host_id: host_id, health: :critical }, %DatabaseHealthChanged{ - sap_system_id: database_id, + database_id: database_id, health: :critical } ], @@ -438,12 +438,12 @@ defmodule Trento.Databases.DatabaseTest do database_instance_registered_event = build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, health: :warning ) initial_events = [ - build(:database_registered_event, sap_system_id: database_id, health: :warning), + build(:database_registered_event, database_id: database_id, health: :warning), database_instance_registered_event ] @@ -474,7 +474,7 @@ defmodule Trento.Databases.DatabaseTest do [ build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: database_instance_registered_event.sid, tenant: database_instance_registered_event.tenant, instance_number: new_instance_number, @@ -517,12 +517,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, sid: db_sid, instance_number: db_instance_number_1, @@ -530,20 +530,20 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_2, system_replication: "Secondary", sid: db_sid ), build(:database_instance_deregistered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, instance_number: db_instance_number_1, deregistered_at: deregistered_at ), build(:database_deregistered_event, - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at ) ] @@ -578,12 +578,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, sid: db_sid, instance_number: db_instance_number_1, @@ -591,20 +591,20 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_2, system_replication: "Secondary", sid: db_sid ), build(:database_instance_deregistered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, instance_number: db_instance_number_1, deregistered_at: deregistered_at ), build(:database_deregistered_event, - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at ) ] @@ -622,7 +622,7 @@ defmodule Trento.Databases.DatabaseTest do command, [ %DatabaseInstanceRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: db_sid, tenant: command.tenant, instance_number: command.instance_number, @@ -637,7 +637,7 @@ defmodule Trento.Databases.DatabaseTest do health: command.health }, %DatabaseRestored{ - sap_system_id: database_id, + database_id: database_id, health: command.health } ], @@ -675,12 +675,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, sid: db_sid, instance_number: db_instance_number_1, @@ -688,26 +688,26 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_2, system_replication: "Secondary", sid: db_sid ), build(:database_instance_deregistered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, instance_number: db_instance_number_1, deregistered_at: deregistered_at ), build(:database_instance_deregistered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_2, deregistered_at: deregistered_at ), build(:database_deregistered_event, - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at ) ] @@ -725,7 +725,7 @@ defmodule Trento.Databases.DatabaseTest do command, [ %DatabaseInstanceRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: db_sid, tenant: command.tenant, instance_number: command.instance_number, @@ -740,7 +740,7 @@ defmodule Trento.Databases.DatabaseTest do health: command.health }, %DatabaseRestored{ - sap_system_id: database_id, + database_id: database_id, health: command.health } ], @@ -780,12 +780,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, sid: db_sid, instance_number: db_instance_number_1, @@ -793,26 +793,26 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_2, system_replication: "Secondary", sid: db_sid ), build(:database_instance_deregistered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, instance_number: db_instance_number_1, deregistered_at: deregistered_at ), build(:database_instance_deregistered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_2, deregistered_at: deregistered_at ), build(:database_deregistered_event, - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at ) ] @@ -830,7 +830,7 @@ defmodule Trento.Databases.DatabaseTest do command, [ %DatabaseInstanceRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: db_sid, tenant: command.tenant, instance_number: command.instance_number, @@ -845,7 +845,7 @@ defmodule Trento.Databases.DatabaseTest do health: command.health }, %DatabaseRestored{ - sap_system_id: database_id, + database_id: database_id, health: command.health } ], @@ -885,12 +885,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, sid: db_sid, instance_number: db_instance_number_1, @@ -898,20 +898,20 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_2, system_replication: "Secondary", sid: db_sid ), build(:database_instance_deregistered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, instance_number: db_instance_number_1, deregistered_at: deregistered_at ), build(:database_deregistered_event, - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at ) ] @@ -929,7 +929,7 @@ defmodule Trento.Databases.DatabaseTest do command, [ %DatabaseInstanceRegistered{ - sap_system_id: database_id, + database_id: database_id, sid: db_sid, tenant: command.tenant, instance_number: command.instance_number, @@ -944,7 +944,7 @@ defmodule Trento.Databases.DatabaseTest do health: command.health }, %DatabaseRestored{ - sap_system_id: database_id, + database_id: database_id, health: command.health } ], @@ -985,12 +985,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, sid: db_sid, instance_number: db_instance_number_1, @@ -998,20 +998,20 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_2, system_replication: "Secondary", sid: db_sid ), build(:database_instance_deregistered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, instance_number: db_instance_number_1, deregistered_at: deregistered_at ), build(:database_deregistered_event, - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at ) ] @@ -1043,12 +1043,12 @@ defmodule Trento.Databases.DatabaseTest do [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, sid: db_sid, instance_number: db_instance_number_1, @@ -1056,7 +1056,7 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_2, system_replication: "Secondary", @@ -1071,13 +1071,13 @@ defmodule Trento.Databases.DatabaseTest do }, [ %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, instance_number: db_instance_number_1, deregistered_at: deregistered_at }, %DatabaseDeregistered{ - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at } ], @@ -1114,12 +1114,12 @@ defmodule Trento.Databases.DatabaseTest do [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, system_replication: "Primary", sid: db_sid, instance_number: instance_number_1, @@ -1127,7 +1127,7 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, system_replication: "Secondary", sid: db_sid, host_id: secondary_db_host_id, @@ -1141,7 +1141,7 @@ defmodule Trento.Databases.DatabaseTest do deregistered_at: deregistered_at }, %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, host_id: secondary_db_host_id, instance_number: instance_number_2, deregistered_at: deregistered_at @@ -1167,12 +1167,12 @@ defmodule Trento.Databases.DatabaseTest do [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: database_host_id, instance_number: db_instance_number_1, system_replication: nil, @@ -1187,13 +1187,13 @@ defmodule Trento.Databases.DatabaseTest do }, [ %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, host_id: database_host_id, instance_number: db_instance_number_1, deregistered_at: deregistered_at }, %DatabaseDeregistered{ - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at } ], @@ -1222,12 +1222,12 @@ defmodule Trento.Databases.DatabaseTest do [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: database_host_id, instance_number: db_instance_number_1, system_replication: nil, @@ -1235,7 +1235,7 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: second_database_host_id, instance_number: db_instance_number_2, system_replication: nil, @@ -1249,7 +1249,7 @@ defmodule Trento.Databases.DatabaseTest do deregistered_at: deregistered_at }, %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, host_id: database_host_id, instance_number: db_instance_number_1, deregistered_at: deregistered_at @@ -1277,12 +1277,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, sid: db_sid, instance_number: db_instance_number_1, @@ -1290,7 +1290,7 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_2, system_replication: "Secondary" @@ -1315,17 +1315,17 @@ defmodule Trento.Databases.DatabaseTest do ], [ %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, instance_number: db_instance_number_1, deregistered_at: deregistered_at }, %DatabaseDeregistered{ - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at }, %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_2, deregistered_at: deregistered_at @@ -1354,12 +1354,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: first_primary_database_host_id, sid: db_sid, instance_number: db_instance_number_1, @@ -1367,7 +1367,7 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: other_primary_database_host_id, sid: db_sid, instance_number: db_instance_number_2, @@ -1375,14 +1375,14 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_3, system_replication: "Secondary" ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: other_secondary_database_host_id, instance_number: db_instance_number_4, system_replication: "Secondary" @@ -1419,29 +1419,29 @@ defmodule Trento.Databases.DatabaseTest do ], [ %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, host_id: first_primary_database_host_id, instance_number: db_instance_number_1, deregistered_at: deregistered_at }, %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, host_id: other_primary_database_host_id, instance_number: db_instance_number_2, deregistered_at: deregistered_at }, %DatabaseDeregistered{ - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at }, %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: db_instance_number_3, deregistered_at: deregistered_at }, %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, host_id: other_secondary_database_host_id, instance_number: db_instance_number_4, deregistered_at: deregistered_at @@ -1465,12 +1465,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, sid: db_sid, instance_number: database_instance_number_1, @@ -1478,7 +1478,7 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, host_id: secondary_database_host_id, instance_number: database_instance_number_2, system_replication: "Secondary" @@ -1497,13 +1497,13 @@ defmodule Trento.Databases.DatabaseTest do ], [ %DatabaseInstanceDeregistered{ - sap_system_id: database_id, + database_id: database_id, host_id: primary_database_host_id, instance_number: database_instance_number_1, deregistered_at: deregistered_at }, %DatabaseDeregistered{ - sap_system_id: database_id, + database_id: database_id, deregistered_at: deregistered_at } ] @@ -1518,12 +1518,12 @@ defmodule Trento.Databases.DatabaseTest do [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid, host_id: UUID.uuid4(), instance_number: "00", @@ -1552,12 +1552,12 @@ defmodule Trento.Databases.DatabaseTest do [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: db_sid, host_id: deregistered_host_id, instance_number: deregistered_instance_number, @@ -1565,7 +1565,7 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_deregistered_event, - sap_system_id: database_id, + database_id: database_id, host_id: deregistered_host_id, instance_number: deregistered_instance_number, deregistered_at: DateTime.utc_now() @@ -1596,12 +1596,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: sid, host_id: host_id, instance_number: absent_db_instance_number, @@ -1610,14 +1610,14 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_marked_absent_event, - sap_system_id: database_id, + database_id: database_id, host_id: host_id, instance_number: absent_db_instance_number, absent_at: absent_db_absent_at ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: sid, host_id: host_id, instance_number: present_db_instance_number, @@ -1648,7 +1648,7 @@ defmodule Trento.Databases.DatabaseTest do %DatabaseInstanceMarkedAbsent{ instance_number: present_db_instance_number, host_id: host_id, - sap_system_id: database_id, + database_id: database_id, absent_at: absent_at } ], @@ -1680,12 +1680,12 @@ defmodule Trento.Databases.DatabaseTest do initial_events = [ build( :database_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: sid ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: sid, host_id: host_id, instance_number: absent_db_instance_number, @@ -1694,14 +1694,14 @@ defmodule Trento.Databases.DatabaseTest do ), build( :database_instance_marked_absent_event, - sap_system_id: database_id, + database_id: database_id, host_id: host_id, instance_number: absent_db_instance_number, absent_at: DateTime.utc_now() ), build( :database_instance_registered_event, - sap_system_id: database_id, + database_id: database_id, sid: sid, host_id: host_id, instance_number: present_db_instance_number, @@ -1730,7 +1730,7 @@ defmodule Trento.Databases.DatabaseTest do %DatabaseInstanceMarkedPresent{ instance_number: absent_db_instance_number, host_id: host_id, - sap_system_id: database_id + database_id: database_id } ], fn state -> diff --git a/test/trento/databases_test.exs b/test/trento/databases_test.exs new file mode 100644 index 0000000000..6896bca371 --- /dev/null +++ b/test/trento/databases_test.exs @@ -0,0 +1,106 @@ +defmodule Trento.DatabasesTest do + use ExUnit.Case + use Trento.DataCase + + import Trento.Factory + import Mox + + alias Trento.Databases + + alias Trento.SapSystems.Projections.DatabaseReadModel + + alias Trento.Databases.Commands.DeregisterDatabaseInstance + + @moduletag :integration + + describe "databases" do + test "should retrieve all the currently registered existing databases and the related instances" do + %DatabaseReadModel{ + id: database_id, + sid: sid + } = insert(:database) + + insert(:database, deregistered_at: DateTime.utc_now()) + + database_instances = + Enum.sort_by( + insert_list(5, :database_instance_without_host, sap_system_id: database_id), + & &1.host_id + ) + + assert [ + %DatabaseReadModel{ + sid: ^sid, + database_instances: ^database_instances + } + ] = Databases.get_all_databases() + end + end + + describe "get_database_instances_by_host_id/1" do + test "should return empty if no database instances were found" do + assert [] == Databases.get_database_instances_by_host_id(UUID.uuid4()) + end + + test "should return all the instances with the matching host_id" do + host_id = UUID.uuid4() + insert_list(10, :database_instance_without_host, host_id: host_id) + insert_list(10, :database_instance_without_host) + + database_instances = Databases.get_database_instances_by_host_id(host_id) + + assert 10 == length(database_instances) + assert Enum.all?(database_instances, &(&1.host_id == host_id)) + end + end + + describe "deregister_database_instance/4" do + test "should dispatch an database deregistration command" do + database_id = Faker.UUID.v4() + host_id = Faker.UUID.v4() + instance_number = "00" + + deregistered_at = DateTime.utc_now() + + expect( + Trento.Support.DateService.Mock, + :utc_now, + fn -> deregistered_at end + ) + + expect( + Trento.Commanded.Mock, + :dispatch, + fn %DeregisterDatabaseInstance{ + database_id: ^database_id, + host_id: ^host_id, + instance_number: ^instance_number, + deregistered_at: ^deregistered_at + } -> + :ok + end + ) + + assert :ok = + Databases.deregister_database_instance( + database_id, + host_id, + instance_number, + Trento.Support.DateService.Mock + ) + end + + test "should not delete a present database instance" do + %{sap_system_id: database_id, host_id: host_id, instance_number: instance_number} = + insert(:database_instance_without_host, absent_at: nil) + + assert {:error, :instance_present} = + Databases.deregister_database_instance( + database_id, + host_id, + instance_number, + Trento.Support.DateService.Mock + ) + end + end +end diff --git a/test/trento/infrastructure/commanded/event_handlers/stream_roll_up_event_handler_test.exs b/test/trento/infrastructure/commanded/event_handlers/stream_roll_up_event_handler_test.exs index e2ebc0c576..27ffe6d2ce 100644 --- a/test/trento/infrastructure/commanded/event_handlers/stream_roll_up_event_handler_test.exs +++ b/test/trento/infrastructure/commanded/event_handlers/stream_roll_up_event_handler_test.exs @@ -100,7 +100,7 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.StreamRollUpEventHandler end) ) - event = build(:database_instance_registered_event, sap_system_id: sap_system_id) + event = build(:application_instance_registered_event, sap_system_id: sap_system_id) expect(Trento.Commanded.Mock, :dispatch, fn %RollUpSapSystem{ sap_system_id: ^sap_system_id diff --git a/test/trento/infrastructure/commanded/process_managers/deregistration_process_manager_test.exs b/test/trento/infrastructure/commanded/process_managers/deregistration_process_manager_test.exs index 18f3232b36..18d514810d 100644 --- a/test/trento/infrastructure/commanded/process_managers/deregistration_process_manager_test.exs +++ b/test/trento/infrastructure/commanded/process_managers/deregistration_process_manager_test.exs @@ -16,11 +16,14 @@ defmodule Trento.Infrastructure.Commanded.ProcessManagers.DeregistrationProcessM HostRolledUp } + alias Trento.Databases.Events.{ + DatabaseInstanceDeregistered, + DatabaseInstanceRegistered + } + alias Trento.SapSystems.Events.{ ApplicationInstanceDeregistered, ApplicationInstanceRegistered, - DatabaseInstanceDeregistered, - DatabaseInstanceRegistered, SapSystemRolledUp } @@ -164,13 +167,13 @@ defmodule Trento.Infrastructure.Commanded.ProcessManagers.DeregistrationProcessM } host_id = UUID.uuid4() - sap_system_id = UUID.uuid4() + database_id = UUID.uuid4() instance_number = "01" events = [ %DatabaseInstanceRegistered{ host_id: host_id, - sap_system_id: sap_system_id, + database_id: database_id, instance_number: instance_number } ] @@ -182,7 +185,7 @@ defmodule Trento.Infrastructure.Commanded.ProcessManagers.DeregistrationProcessM assert %DeregistrationProcessManager{ database_instances: [ %Instance{ - sap_system_id: ^sap_system_id, + sap_system_id: ^database_id, instance_number: ^instance_number } ], @@ -441,6 +444,7 @@ defmodule Trento.Infrastructure.Commanded.ProcessManagers.DeregistrationProcessM test "should remove instance from state when DatabaseInstanceDeregistered event received" do sap_system_id = UUID.uuid4() + database_id = UUID.uuid4() instance_number = "00" initial_state = %DeregistrationProcessManager{ @@ -460,7 +464,7 @@ defmodule Trento.Infrastructure.Commanded.ProcessManagers.DeregistrationProcessM %DatabaseInstanceDeregistered{ instance_number: instance_number, host_id: host_id, - sap_system_id: sap_system_id, + database_id: database_id, deregistered_at: deregistered_at } ] diff --git a/test/trento/sap_systems/projections/database_projector_test.exs b/test/trento/sap_systems/projections/database_projector_test.exs index e36b48ae1e..54180cd693 100644 --- a/test/trento/sap_systems/projections/database_projector_test.exs +++ b/test/trento/sap_systems/projections/database_projector_test.exs @@ -13,7 +13,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do DatabaseReadModel } - alias Trento.SapSystems.Events.{ + alias Trento.Databases.Events.{ DatabaseDeregistered, DatabaseHealthChanged, DatabaseInstanceDeregistered, @@ -41,36 +41,36 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do end test "should project a new database when DatabaseRegistered event is received" do - %{sap_system_id: sap_system_id} = event = build(:database_registered_event) + %{database_id: database_id} = event = build(:database_registered_event) ProjectorTestHelper.project(DatabaseProjector, event, "database_projector") %{sid: sid, health: health} = - database_projection = Repo.get!(DatabaseReadModel, event.sap_system_id) + database_projection = Repo.get!(DatabaseReadModel, event.database_id) - assert event.sap_system_id == database_projection.id + assert event.database_id == database_projection.id assert event.sid == database_projection.sid assert event.health == database_projection.health assert_broadcast "database_registered", %{ health: ^health, - id: ^sap_system_id, + id: ^database_id, sid: ^sid }, 1000 end test "should update the health of a Database when a DatabaseHealthChanged event is received" do - insert(:database, id: sap_system_id = Faker.UUID.v4()) - event = %DatabaseHealthChanged{sap_system_id: sap_system_id, health: :critical} + insert(:database, id: database_id = Faker.UUID.v4()) + event = %DatabaseHealthChanged{database_id: database_id, health: :critical} ProjectorTestHelper.project(DatabaseProjector, event, "database_projector") - projection = Repo.get!(DatabaseReadModel, event.sap_system_id) + projection = Repo.get!(DatabaseReadModel, event.database_id) assert event.health == projection.health - assert_broadcast "database_health_changed", %{health: :critical, id: ^sap_system_id}, 1000 + assert_broadcast "database_health_changed", %{health: :critical, id: ^database_id}, 1000 end test "should project a new database instance when DatabaseInstanceRegistered event is received" do @@ -79,7 +79,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do event = build( :database_instance_registered_event, - sap_system_id: sap_system_id, + database_id: sap_system_id, system_replication: "", system_replication_status: "" ) @@ -95,12 +95,12 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do } = database_instance_projection = Repo.get_by(DatabaseInstanceReadModel, - sap_system_id: event.sap_system_id, + sap_system_id: event.database_id, instance_number: event.instance_number, host_id: event.host_id ) - assert event.sap_system_id == database_instance_projection.sap_system_id + assert event.database_id == database_instance_projection.sap_system_id assert event.sid == database_instance_projection.sid assert event.tenant == database_instance_projection.tenant assert event.instance_number == database_instance_projection.instance_number @@ -133,7 +133,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do event = build( :database_instance_registered_event, - sap_system_id: sap_system_id, + database_id: sap_system_id, system_replication: "Primary", system_replication_status: "ACTIVE" ) @@ -161,7 +161,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do event = build( :database_instance_registered_event, - sap_system_id: sap_system_id, + database_id: sap_system_id, system_replication: "Secondary", system_replication_status: "" ) @@ -193,7 +193,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do system_replication_status: system_replication_status } = event = %DatabaseInstanceSystemReplicationChanged{ - sap_system_id: sap_system_id, + database_id: sap_system_id, instance_number: instance_number, host_id: host_id, system_replication: "Primary", @@ -236,7 +236,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do ) event = %DatabaseInstanceSystemReplicationChanged{ - sap_system_id: sap_system_id, + database_id: sap_system_id, instance_number: instance_number, host_id: host_id, system_replication: nil, @@ -279,7 +279,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do ) event = %DatabaseInstanceHealthChanged{ - sap_system_id: sap_system_id, + database_id: sap_system_id, host_id: host_id, instance_number: instance_number, health: :critical @@ -310,7 +310,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do event = %DatabaseInstanceMarkedAbsent{ instance_number: instance_number, host_id: host_id, - sap_system_id: sap_system_id, + database_id: sap_system_id, absent_at: absent_at } @@ -340,7 +340,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do event = %DatabaseInstanceMarkedPresent{ instance_number: instance_number, host_id: host_id, - sap_system_id: sap_system_id + database_id: sap_system_id } ProjectorTestHelper.project(DatabaseProjector, event, "database_projector") @@ -364,7 +364,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do insert(:database, id: sap_system_id = Faker.UUID.v4()) event = %DatabaseDeregistered{ - sap_system_id: sap_system_id, + database_id: sap_system_id, deregistered_at: deregistered_at } @@ -394,7 +394,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do event = %DatabaseInstanceDeregistered{ instance_number: instance_number, host_id: host_id, - sap_system_id: sap_system_id, + database_id: sap_system_id, deregistered_at: deregistered_at } @@ -439,7 +439,7 @@ defmodule Trento.SapSystems.Projections.DatabaseProjectorTest do insert_list(5, :tag, resource_id: sap_system_id) event = %DatabaseRestored{ - sap_system_id: sap_system_id, + database_id: sap_system_id, health: :passing } diff --git a/test/trento/sap_systems_test.exs b/test/trento/sap_systems_test.exs index 6b13abc5ed..bbe3bff3cb 100644 --- a/test/trento/sap_systems_test.exs +++ b/test/trento/sap_systems_test.exs @@ -7,12 +7,8 @@ defmodule Trento.SapSystemsTest do alias Trento.SapSystems - alias Trento.SapSystems.Projections.{ - DatabaseReadModel, - SapSystemReadModel - } + alias Trento.SapSystems.Projections.SapSystemReadModel - alias Trento.Databases.Commands.DeregisterDatabaseInstance alias Trento.SapSystems.Commands.DeregisterApplicationInstance @moduletag :integration @@ -51,28 +47,6 @@ defmodule Trento.SapSystemsTest do } ] = SapSystems.get_all_sap_systems() end - - test "should retrieve all the currently registered existing databases and the related instances" do - %DatabaseReadModel{ - id: sap_system_id, - sid: sid - } = insert(:database) - - insert(:database, deregistered_at: DateTime.utc_now()) - - database_instances = - Enum.sort_by( - insert_list(5, :database_instance_without_host, sap_system_id: sap_system_id), - & &1.host_id - ) - - assert [ - %DatabaseReadModel{ - sid: ^sid, - database_instances: ^database_instances - } - ] = SapSystems.get_all_databases() - end end describe "get_application_instances_by_host_id/1" do @@ -92,23 +66,6 @@ defmodule Trento.SapSystemsTest do end end - describe "get_database_instances_by_host_id/1" do - test "should return empty if no database instances were found" do - assert [] == SapSystems.get_application_instances_by_host_id(UUID.uuid4()) - end - - test "should return all the instances with the matching host_id" do - host_id = UUID.uuid4() - insert_list(10, :database_instance_without_host, host_id: host_id) - insert_list(10, :database_instance_without_host) - - database_instances = SapSystems.get_database_instances_by_host_id(host_id) - - assert 10 == length(database_instances) - assert Enum.all?(database_instances, &(&1.host_id == host_id)) - end - end - describe "deregister_application_instance/4" do test "should dispatch an application deregistration command" do sap_system_id = Faker.UUID.v4() @@ -158,54 +115,4 @@ defmodule Trento.SapSystemsTest do ) end end - - describe "deregister_database_instance/4" do - test "should dispatch an database deregistration command" do - sap_system_id = Faker.UUID.v4() - host_id = Faker.UUID.v4() - instance_number = "00" - - deregistered_at = DateTime.utc_now() - - expect( - Trento.Support.DateService.Mock, - :utc_now, - fn -> deregistered_at end - ) - - expect( - Trento.Commanded.Mock, - :dispatch, - fn %DeregisterDatabaseInstance{ - database_id: ^sap_system_id, - host_id: ^host_id, - instance_number: ^instance_number, - deregistered_at: ^deregistered_at - } -> - :ok - end - ) - - assert :ok = - SapSystems.deregister_database_instance( - sap_system_id, - host_id, - instance_number, - Trento.Support.DateService.Mock - ) - end - - test "should not delete a present database instance" do - %{sap_system_id: sap_system_id, host_id: host_id, instance_number: instance_number} = - insert(:database_instance_without_host, absent_at: nil) - - assert {:error, :instance_present} = - SapSystems.deregister_database_instance( - sap_system_id, - host_id, - instance_number, - Trento.Support.DateService.Mock - ) - end - end end