Skip to content

Commit

Permalink
Add collector endpoint, host discovery integration event handler and …
Browse files Browse the repository at this point in the history
…monitoring context (#34)

* Add discovery integration event handler

* Add monitoring context

* Add discovery API controller

* Add /api/collect route

Co-authored-by: Alessio Biancalana <[email protected]>
  • Loading branch information
fabriziosestito and dottorblaster authored Dec 30, 2021
1 parent 6b9f21a commit b362faa
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/tronto/monitoring/integration/discovery.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Tronto.Monitoring.Integration.Discovery do
@moduledoc """
This module contains functions to handle integration events
from the discovery bounded-context
"""

alias Tronto.Monitoring.Domain.Commands.RegisterHost

@spec handle_discovery_event(map) :: {:error, any} | {:ok, struct}
def handle_discovery_event(%{
"discovery_type" => "host_discovery",
"agent_id" => agent_id,
"payload" => %{
"hostname" => hostname,
"ip_addresses" => ip_addresses,
"agent_version" => agent_version
}
}) do
RegisterHost.new(
id_host: agent_id,
hostname: hostname,
ip_addresses: ip_addresses,
agent_version: agent_version
)
end

def handle_discovery_event(_) do
{:error, :invalid_payload}
end
end
17 changes: 17 additions & 0 deletions lib/tronto/monitoring/monitoring.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Tronto.Monitoring do
@moduledoc """
This module encapuslates the access to the monitoring bounded context
"""

alias Tronto.Monitoring.Integration.Discovery

def handle_discovery_event(event) do
case Discovery.handle_discovery_event(event) do
{:ok, command} ->
Tronto.Commanded.dispatch(command)

{:error, _} = error ->
error
end
end
end
20 changes: 20 additions & 0 deletions lib/tronto_web/controllers/discovery_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule TrontoWeb.DiscoveryController do
use TrontoWeb, :controller

alias Tronto.Monitoring

@spec collect(Plug.Conn.t(), map) :: Plug.Conn.t()
def collect(conn, event) do
case Monitoring.handle_discovery_event(event) do
:ok ->
conn
|> put_status(:accepted)
|> json(%{})

{:error, reason} ->
conn
|> put_status(:bad_request)
|> json(%{error: reason})
end
end
end
5 changes: 5 additions & 0 deletions lib/tronto_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ defmodule TrontoWeb.Router do
get "/", PageController, :index
end

scope "/api", TrontoWeb do
pipe_through :api
post "/collect", DiscoveryController, :collect
end

# Other scopes may use custom stacks.
# scope "/api", TrontoWeb do
# pipe_through :api
Expand Down

0 comments on commit b362faa

Please sign in to comment.