Skip to content

Commit

Permalink
Sap system restore domain
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco committed Jun 22, 2023
1 parent cabed2d commit ae41ba9
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/trento/domain/sap_system/events/sap_system_restored.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Trento.Domain.Events.SapSystemRestored do
@moduledoc """
This event is emitted when a sap system is restored.
"""

use Trento.Event

require Trento.Domain.Enums.Health, as: Health

defevent do
field :sap_system_id, Ecto.UUID
field :sid, :string
field :tenant, :string
field :db_host, :string
field :health, Ecto.Enum, values: Health.values()
end
end
55 changes: 55 additions & 0 deletions lib/trento/domain/sap_system/sap_system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ defmodule Trento.Domain.SapSystem do
SapSystemDeregistered,
SapSystemHealthChanged,
SapSystemRegistered,
SapSystemRestored,
SapSystemRolledUp,
SapSystemRollUpRequested,
SapSystemTombstoned,
Expand Down Expand Up @@ -238,6 +239,27 @@ defmodule Trento.Domain.SapSystem do
|> Multi.execute(&maybe_emit_sap_system_health_changed_event/1)
end

# Restore sap system
# Same registration rules
def execute(
%SapSystem{deregistered_at: deregistered_at} = sap_system,
%RegisterApplicationInstance{} = instance
)
when not is_nil(deregistered_at) do
sap_system
|> Multi.new()
|> Multi.execute(fn sap_system ->
emit_application_instance_registered_or_application_instance_health_changed(
sap_system,
instance
)
end)
|> Multi.execute(fn sap_system ->
maybe_emit_sap_system_restored_event(sap_system, instance)
end)
|> Multi.execute(&maybe_emit_sap_system_health_changed_event/1)
end

# SAP system not registered, application already present
# If the instance is not one of MESSAGESERVER or ABAP we discard.
# Otherwise if the instance we want register together with already present instances
Expand Down Expand Up @@ -668,6 +690,18 @@ defmodule Trento.Domain.SapSystem do
}
end

def apply(%SapSystem{} = sap_system, %SapSystemRestored{
sid: sid,
health: health
}) do
%SapSystem{
sap_system
| sid: sid,
health: health,
deregistered_at: nil
}
end

def apply(%SapSystem{} = sap_system, %SapSystemTombstoned{}), do: sap_system

defp maybe_emit_database_instance_registered_event(
Expand Down Expand Up @@ -848,6 +882,27 @@ defmodule Trento.Domain.SapSystem do
end
end

defp maybe_emit_sap_system_restored_event(
%SapSystem{application: %Application{instances: instances}},
%RegisterApplicationInstance{
sap_system_id: sap_system_id,
sid: sid,
tenant: tenant,
db_host: db_host,
health: health
}
) do
if instances_have_abap?(instances) and instances_have_messageserver?(instances) do
%SapSystemRestored{
db_host: db_host,
health: health,
sap_system_id: sap_system_id,
sid: sid,
tenant: tenant
}
end
end

defp maybe_emit_sap_system_registered_or_updated_event(
%SapSystem{sid: nil, application: %Application{instances: instances}},
%RegisterApplicationInstance{
Expand Down
19 changes: 19 additions & 0 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule Trento.Factory do
}

alias Trento.Domain.Events.{
ApplicationInstanceDeregistered,
ApplicationInstanceRegistered,
ClusterDeregistered,
ClusterRegistered,
Expand All @@ -29,6 +30,7 @@ defmodule Trento.Factory do
DatabaseInstanceDeregistered,
DatabaseInstanceRegistered,
DatabaseRegistered,
DatabaseRestored,
HostAddedToCluster,
HostDetailsUpdated,
HostRegistered,
Expand Down Expand Up @@ -267,6 +269,14 @@ defmodule Trento.Factory do
})
end

def database_restored_event_factory do
DatabaseRestored.new!(%{
sap_system_id: Faker.UUID.v4(),
sid: Faker.UUID.v4(),
health: Health.passing()
})
end

def deregister_database_instance_command_factory do
DeregisterDatabaseInstance.new!(%{
sap_system_id: Faker.UUID.v4(),
Expand Down Expand Up @@ -298,6 +308,15 @@ defmodule Trento.Factory do
}
end

def application_instance_deregistered_event_factory do
ApplicationInstanceDeregistered.new!(%{
sap_system_id: Faker.UUID.v4(),
deregistered_at: DateTime.utc_now(),
instance_number: "00",
host_id: Faker.UUID.v4()
})
end

def deregister_application_instance_command_factory do
DeregisterApplicationInstance.new!(%{
sap_system_id: Faker.UUID.v4(),
Expand Down
Loading

0 comments on commit ae41ba9

Please sign in to comment.