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

Forbid not registered instances deregistration #1733

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
92 changes: 74 additions & 18 deletions lib/trento/domain/sap_system/sap_system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -305,21 +305,14 @@ defmodule Trento.Domain.SapSystem do
def execute(
%SapSystem{sap_system_id: sap_system_id} = sap_system,
%DeregisterDatabaseInstance{
instance_number: instance_number,
host_id: host_id,
sap_system_id: sap_system_id,
deregistered_at: deregistered_at
}
} = instance
) do
sap_system
|> Multi.new()
|> Multi.execute(fn _ ->
%DatabaseInstanceDeregistered{
sap_system_id: sap_system_id,
host_id: host_id,
instance_number: instance_number,
deregistered_at: deregistered_at
}
maybe_emit_database_instance_deregistered_event(sap_system, instance)
end)
|> Multi.execute(fn sap_system ->
maybe_emit_database_deregistered_event(sap_system, deregistered_at)
Expand All @@ -337,21 +330,14 @@ defmodule Trento.Domain.SapSystem do
sap_system_id: sap_system_id
} = sap_system,
%DeregisterApplicationInstance{
instance_number: instance_number,
sap_system_id: sap_system_id,
host_id: host_id,
deregistered_at: deregistered_at
}
} = instance
) do
sap_system
|> Multi.new()
|> Multi.execute(fn _ ->
%ApplicationInstanceDeregistered{
sap_system_id: sap_system_id,
instance_number: instance_number,
host_id: host_id,
deregistered_at: deregistered_at
}
maybe_emit_application_instance_deregistered_event(sap_system, instance)
end)
|> Multi.execute(fn sap_system ->
maybe_emit_sap_system_deregistered_event(
Expand Down Expand Up @@ -1034,6 +1020,76 @@ defmodule Trento.Domain.SapSystem do
end
end

defp maybe_emit_application_instance_deregistered_event(
%SapSystem{application: nil},
%DeregisterApplicationInstance{}
),
do: {:error, :application_instance_not_registered}

defp maybe_emit_application_instance_deregistered_event(
%SapSystem{application: %Application{instances: []}},
%DeregisterApplicationInstance{}
),
do: {:error, :application_instance_not_registered}

defp maybe_emit_application_instance_deregistered_event(
%SapSystem{application: %Application{instances: instances}},
%DeregisterApplicationInstance{
sap_system_id: sap_system_id,
host_id: host_id,
instance_number: instance_number,
deregistered_at: deregistered_at
}
) do
case get_instance(instances, host_id, instance_number) do
nil ->
{:error, :application_instance_not_registered}

_ ->
%ApplicationInstanceDeregistered{
sap_system_id: sap_system_id,
instance_number: instance_number,
host_id: host_id,
deregistered_at: deregistered_at
}
end
end

defp maybe_emit_database_instance_deregistered_event(
%SapSystem{database: nil},
%DeregisterDatabaseInstance{}
),
do: {:error, :database_instance_not_registered}

defp maybe_emit_database_instance_deregistered_event(
%SapSystem{database: %Database{instances: []}},
%DeregisterDatabaseInstance{}
),
do: {:error, :database_instance_not_registered}

defp maybe_emit_database_instance_deregistered_event(
%SapSystem{database: %Database{instances: instances}},
%DeregisterDatabaseInstance{
sap_system_id: sap_system_id,
host_id: host_id,
instance_number: instance_number,
deregistered_at: deregistered_at
}
) do
case get_instance(instances, host_id, instance_number) do
nil ->
{:error, :database_instance_not_registered}

_ ->
%DatabaseInstanceDeregistered{
sap_system_id: sap_system_id,
instance_number: instance_number,
host_id: host_id,
deregistered_at: deregistered_at
}
end
end

defp maybe_emit_sap_system_deregistered_event(
%SapSystem{sid: nil},
_deregistered_at
Expand Down
170 changes: 168 additions & 2 deletions test/trento/domain/sap_system/sap_system_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2923,8 +2923,6 @@ defmodule Trento.SapSystemTest do
commands_to_accept = [
build(:register_database_instance_command),
build(:register_application_instance_command),
build(:deregister_database_instance_command, sap_system_id: sap_system_id),
build(:deregister_application_instance_command, sap_system_id: sap_system_id),
build(:rollup_sap_system_command)
]

Expand Down Expand Up @@ -4034,6 +4032,174 @@ defmodule Trento.SapSystemTest do
]
)
end

test "should not deregister a not registered application instance" do
sap_system_id = UUID.uuid4()
db_sid = fake_sid()

assert_error(
[
build(
:database_registered_event,
sap_system_id: sap_system_id,
sid: db_sid
),
build(
:database_instance_registered_event,
sap_system_id: sap_system_id,
sid: db_sid,
host_id: UUID.uuid4(),
instance_number: "00",
system_replication: "Primary"
)
],
[
%DeregisterApplicationInstance{
sap_system_id: sap_system_id,
host_id: UUID.uuid4(),
instance_number: "01",
deregistered_at: DateTime.utc_now()
}
],
{:error, :application_instance_not_registered}
)
end

test "should not deregister an already deregistered application instance" do
sap_system_id = UUID.uuid4()
db_sid = fake_sid()
application_sid = fake_sid()
deregistered_host_id = UUID.uuid4()
deregistered_instance_number = "02"

assert_error(
[
build(
:database_registered_event,
sap_system_id: sap_system_id,
sid: db_sid
),
build(
:database_instance_registered_event,
sap_system_id: sap_system_id,
sid: db_sid,
host_id: UUID.uuid4(),
instance_number: "00",
system_replication: "Primary"
),
build(
:application_instance_registered_event,
sap_system_id: sap_system_id,
features: "MESSAGESERVER|ENQUE",
sid: application_sid,
host_id: UUID.uuid4(),
instance_number: "01"
),
build(
:application_instance_registered_event,
sap_system_id: sap_system_id,
features: "ABAP|GATEWAY|ICMAN|IGS",
sid: application_sid,
host_id: deregistered_host_id,
instance_number: deregistered_instance_number
),
build(
:sap_system_registered_event,
sap_system_id: sap_system_id,
sid: application_sid
),
build(
:application_instance_deregistered_event,
sap_system_id: sap_system_id,
host_id: deregistered_host_id,
instance_number: deregistered_instance_number,
deregistered_at: DateTime.utc_now()
)
],
[
%DeregisterApplicationInstance{
sap_system_id: sap_system_id,
host_id: deregistered_host_id,
instance_number: deregistered_instance_number,
deregistered_at: DateTime.utc_now()
}
],
{:error, :application_instance_not_registered}
)
end

test "should not deregister a not registered database instance" do
sap_system_id = UUID.uuid4()
db_sid = fake_sid()

assert_error(
[
build(
:database_registered_event,
sap_system_id: sap_system_id,
sid: db_sid
),
build(
:database_instance_registered_event,
sap_system_id: sap_system_id,
sid: db_sid,
host_id: UUID.uuid4(),
instance_number: "00",
system_replication: "Primary"
)
],
[
%DeregisterDatabaseInstance{
sap_system_id: sap_system_id,
host_id: UUID.uuid4(),
instance_number: "01",
deregistered_at: DateTime.utc_now()
}
],
{:error, :database_instance_not_registered}
)
end

test "should not deregister an already deregistered database instance" do
sap_system_id = UUID.uuid4()
db_sid = fake_sid()
deregistered_host_id = UUID.uuid4()
deregistered_instance_number = "01"

assert_error(
[
build(
:database_registered_event,
sap_system_id: sap_system_id,
sid: db_sid
),
build(
:database_instance_registered_event,
sap_system_id: sap_system_id,
sid: db_sid,
host_id: deregistered_host_id,
instance_number: deregistered_instance_number,
system_replication: "Primary"
),
build(
:database_instance_deregistered_event,
sap_system_id: sap_system_id,
host_id: deregistered_host_id,
instance_number: deregistered_instance_number,
deregistered_at: DateTime.utc_now()
)
],
[
%DeregisterDatabaseInstance{
sap_system_id: sap_system_id,
host_id: deregistered_host_id,
instance_number: deregistered_instance_number,
deregistered_at: DateTime.utc_now()
}
],
{:error, :database_instance_not_registered}
)
end
end

defp fake_sid,
Expand Down