Skip to content

Commit

Permalink
202
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza committed Oct 16, 2024
1 parent 0a704bb commit 445a9a0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ defimpl Trento.Infrastructure.Commanded.Middleware.Enrichable,
alias Trento.Repo
import Ecto.Query

require Logger

@spec enrich(RegisterApplicationInstance.t(), map) :: {:ok, map} | {:error, any}
def enrich(%RegisterApplicationInstance{db_host: db_host, tenant: tenant} = command, _) do
def enrich(
%RegisterApplicationInstance{db_host: db_host, tenant: tenant, sid: sid} = command,
_
) do
query =
from d in DatabaseReadModel,
join: di in DatabaseInstanceReadModel,
Expand All @@ -36,7 +41,11 @@ defimpl Trento.Infrastructure.Commanded.Middleware.Enrichable,
}}

nil ->
{:error, :database_not_registered}
Logger.warning(
"database instance associated to application instance #{sid} not registered in Trento. Please make sure that Trento agent is running on database instances associated to this SAP system"
)

{:error, :associated_database_not_found}
end
end
end
7 changes: 7 additions & 0 deletions lib/trento_web/controllers/fallback_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ defmodule TrentoWeb.FallbackController do
|> render(:"422", changeset: changeset)
end

def call(conn, {:error, :associated_database_not_found}) do
conn
|> put_status(:accepted)
|> put_view(json: ErrorJSON)
|> render(:"202")
end

def call(conn, {:error, {:validation, _} = reason}) do
conn
|> put_status(:unprocessable_entity)
Expand Down
24 changes: 23 additions & 1 deletion test/trento_web/controllers/v1/discovery_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule TrentoWeb.V1.DiscoveryControllerTest do
|> json_response(202)
end

test "collect action discards application instance registrations when the associated database does not exists",
test "collect action discards application instance registrations when the dispatch fails",
%{conn: conn} do
body =
load_discovery_event_fixture("sap_system_discovery_application")
Expand All @@ -62,5 +62,27 @@ defmodule TrentoWeb.V1.DiscoveryControllerTest do
assert %DiscardedDiscoveryEvent{payload: ^body, reason: "[:any_error]"} =
discarded_event
end

test "collect action discards application instance registrations when the associated database does not exists",
%{conn: conn} do
body =
load_discovery_event_fixture("sap_system_discovery_application")

expect(Trento.Commanded.Mock, :dispatch, fn _ ->
{:error, :associated_database_not_found}
end)

%{status: status} =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/collect", body)

assert status == 202

[discarded_event] = Discovery.get_discarded_discovery_events(1)

assert %DiscardedDiscoveryEvent{payload: ^body, reason: "[:associated_database_not_found]"} =
discarded_event
end
end
end

0 comments on commit 445a9a0

Please sign in to comment.