-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Host aggregate and Host registration command/event (#21)
* Add Domo and TypedStruct to the mix; import Commanded.AggregateCase support module * Import typed struct formatter config * Add host aggregate and host registration command/event * Add router and wire-up commands * Start commanded when application starts * Add host aggregate tests * Add domo generated code to dialyzer ignore list
- Loading branch information
1 parent
a87b803
commit 86a931d
Showing
11 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
~r/.*domo_generated_code.*/ | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ defmodule Tronto.Commanded do | |
""" | ||
|
||
use Commanded.Application, otp_app: :tronto | ||
|
||
router(Tronto.Router) | ||
end |
17 changes: 17 additions & 0 deletions
17
lib/tronto/monitoring/domain/hosts/commands/register_host.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Tronto.Monitoring.Domain.Commands.RegisterHost do | ||
@moduledoc """ | ||
Register a host to the monitoring system. | ||
""" | ||
|
||
use TypedStruct | ||
use Domo | ||
|
||
typedstruct do | ||
@typedoc "RegisterHost command" | ||
|
||
field :id_host, String.t(), enforce: true | ||
field :hostname, String.t(), enforce: true | ||
field :ip_addresses, [String.t()], enforce: true | ||
field :agent_version, String.t(), enforce: true | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
lib/tronto/monitoring/domain/hosts/events/host_registered.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Tronto.Monitoring.Domain.Events.HostRegistered do | ||
@moduledoc """ | ||
This event is emitted when a host is registered. | ||
""" | ||
|
||
use TypedStruct | ||
|
||
@derive Jason.Encoder | ||
typedstruct do | ||
@typedoc "HostRegistered event" | ||
|
||
field :id_host, String.t(), enforce: true | ||
field :hostname, String.t(), enforce: true | ||
field :ip_addresses, [String.t()], enforce: true | ||
field :agent_version, String.t(), enforce: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
defmodule Tronto.Monitoring.Domain.Host do | ||
@moduledoc false | ||
|
||
alias Tronto.Monitoring.Domain.Host | ||
|
||
alias Tronto.Monitoring.Domain.Commands.{ | ||
RegisterHost | ||
} | ||
|
||
alias Tronto.Monitoring.Domain.Events.{ | ||
HostRegistered | ||
} | ||
|
||
defstruct [ | ||
:id_host, | ||
:hostname, | ||
:ip_addresses, | ||
:agent_version | ||
] | ||
|
||
@type t :: %__MODULE__{ | ||
id_host: String.t(), | ||
hostname: String.t(), | ||
ip_addresses: [String.t()], | ||
agent_version: String.t() | ||
} | ||
|
||
# New host registered | ||
def execute( | ||
%Host{id_host: nil}, | ||
%RegisterHost{ | ||
id_host: id_host, | ||
hostname: hostname, | ||
ip_addresses: ip_addresses, | ||
agent_version: agent_version | ||
} | ||
) do | ||
%HostRegistered{ | ||
id_host: id_host, | ||
hostname: hostname, | ||
ip_addresses: ip_addresses, | ||
agent_version: agent_version | ||
} | ||
end | ||
|
||
def execute( | ||
%Host{id_host: _}, | ||
%RegisterHost{} | ||
) do | ||
[] | ||
end | ||
|
||
def apply( | ||
%Host{} = host, | ||
%HostRegistered{ | ||
id_host: id_host, | ||
hostname: hostname, | ||
ip_addresses: ip_addresses, | ||
agent_version: agent_version | ||
} | ||
) do | ||
%Host{ | ||
host | ||
| id_host: id_host, | ||
hostname: hostname, | ||
ip_addresses: ip_addresses, | ||
agent_version: agent_version | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule Tronto.Router do | ||
use Commanded.Commands.Router | ||
|
||
alias Tronto.Monitoring.Domain.Host | ||
|
||
alias Tronto.Monitoring.Domain.Commands.{ | ||
RegisterHost | ||
} | ||
|
||
identify(Host, by: :id_host) | ||
dispatch(RegisterHost, to: Host) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
defmodule Tronto.Monitoring.HostTest do | ||
use Commanded.AggregateCase, aggregate: Tronto.Monitoring.Domain.Host, async: true | ||
|
||
alias Tronto.Monitoring.Domain.Host | ||
alias Tronto.Monitoring.Domain.Commands.RegisterHost | ||
alias Tronto.Monitoring.Domain.Events.HostRegistered | ||
|
||
describe "host registration" do | ||
test "should register a host" do | ||
id_host = Faker.UUID.v4() | ||
hostname = Faker.StarWars.character() | ||
ip_addresses = [Faker.Internet.ip_v4_address()] | ||
agent_version = Faker.Internet.slug() | ||
|
||
commands = [ | ||
RegisterHost.new!( | ||
id_host: id_host, | ||
hostname: hostname, | ||
ip_addresses: ip_addresses, | ||
agent_version: agent_version | ||
) | ||
] | ||
|
||
assert_events( | ||
commands, | ||
%HostRegistered{ | ||
id_host: id_host, | ||
hostname: hostname, | ||
ip_addresses: ip_addresses, | ||
agent_version: agent_version | ||
} | ||
) | ||
|
||
assert_state( | ||
commands, | ||
%Host{ | ||
id_host: id_host, | ||
hostname: hostname, | ||
ip_addresses: ip_addresses, | ||
agent_version: agent_version | ||
} | ||
) | ||
end | ||
|
||
test "should not register a host if it is already registered" do | ||
id_host = Faker.UUID.v4() | ||
|
||
assert_events( | ||
[ | ||
%HostRegistered{ | ||
id_host: id_host, | ||
hostname: Faker.StarWars.character(), | ||
ip_addresses: [Faker.Internet.ip_v4_address()], | ||
agent_version: Faker.Internet.slug() | ||
} | ||
], | ||
[ | ||
RegisterHost.new!( | ||
id_host: id_host, | ||
hostname: Faker.StarWars.character(), | ||
ip_addresses: [Faker.Internet.ip_v4_address()], | ||
agent_version: Faker.Internet.slug() | ||
) | ||
], | ||
[] | ||
) | ||
end | ||
end | ||
end |