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

Add Host Check Selection Domain logic #1524

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions lib/trento/domain/host/commands/select_host_checks.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Trento.Domain.Commands.SelectHostChecks do
@moduledoc """
Select the checks to be executed on a host.
"""

@required_fields :all

use Trento.Command

defcommand do
field :host_id, Ecto.UUID
field :checks, {:array, :string}
end
end
12 changes: 12 additions & 0 deletions lib/trento/domain/host/events/host_checks_selected.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Trento.Domain.Events.HostChecksSelected do
@moduledoc """
Event of the checks selected for a host.
"""

use Trento.Event

defevent do
field :host_id, Ecto.UUID
field :checks, {:array, :string}
end
end
36 changes: 36 additions & 0 deletions lib/trento/domain/host/host.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule Trento.Domain.Host do
alias Trento.Domain.Commands.{
RegisterHost,
RollUpHost,
SelectHostChecks,
UpdateHeartbeat,
UpdateProvider,
UpdateSlesSubscriptions
Expand All @@ -39,6 +40,7 @@ defmodule Trento.Domain.Host do
alias Trento.Domain.Events.{
HeartbeatFailed,
HeartbeatSucceded,
HostChecksSelected,
HostDetailsUpdated,
HostRegistered,
HostRolledUp,
Expand Down Expand Up @@ -66,6 +68,7 @@ defmodule Trento.Domain.Host do
field :installation_source, Ecto.Enum, values: [:community, :suse, :unknown]
field :heartbeat, Ecto.Enum, values: [:passing, :critical, :unknown]
field :rolling_up, :boolean, default: false
field :selected_checks, {:array, :string}, default: []

embeds_many :subscriptions, SlesSubscription

Expand Down Expand Up @@ -267,6 +270,27 @@ defmodule Trento.Domain.Host do
}
end

def execute(
%Host{host_id: nil},
%SelectHostChecks{}
) do
{:error, :host_not_registered}
Copy link
Member Author

@nelsonkopliku nelsonkopliku Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this clause to keep the domain consistent.
However, this will be likely replaced by what has been implemented in deregistration

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it will cause conflicts

end

def execute(
%Host{
host_id: host_id
},
%SelectHostChecks{
checks: selected_checks
}
) do
%HostChecksSelected{
host_id: host_id,
checks: selected_checks
}
end

def apply(
%Host{} = host,
%HostRegistered{
Expand Down Expand Up @@ -373,4 +397,16 @@ defmodule Trento.Domain.Host do
}) do
snapshot
end

def apply(
%Host{} = host,
%HostChecksSelected{
checks: selected_checks
}
) do
%Host{
host
| selected_checks: selected_checks
}
end
end
4 changes: 3 additions & 1 deletion lib/trento/infrastructure/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule Trento.Router do
RollUpHost,
RollUpSapSystem,
SelectChecks,
EMaksy marked this conversation as resolved.
Show resolved Hide resolved
SelectHostChecks,
UpdateHeartbeat,
UpdateProvider,
UpdateSlesSubscriptions
Expand All @@ -33,7 +34,8 @@ defmodule Trento.Router do
UpdateHeartbeat,
UpdateProvider,
UpdateSlesSubscriptions,
RollUpHost
RollUpHost,
SelectHostChecks
],
to: Host,
lifespan: Host.Lifespan
Expand Down
42 changes: 42 additions & 0 deletions test/trento/domain/host/host_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Trento.HostTest do
alias Trento.Domain.Commands.{
RegisterHost,
RollUpHost,
SelectHostChecks,
UpdateHeartbeat,
UpdateProvider,
UpdateSlesSubscriptions
Expand All @@ -14,6 +15,7 @@ defmodule Trento.HostTest do
alias Trento.Domain.Events.{
HeartbeatFailed,
HeartbeatSucceded,
HostChecksSelected,
HostDetailsUpdated,
HostRegistered,
HostRolledUp,
Expand Down Expand Up @@ -154,6 +156,46 @@ defmodule Trento.HostTest do
end
end

describe "host checks selection" do
test "should select desired checks for host" do
host_id = Faker.UUID.v4()
selected_host_checks = Enum.map(0..4, fn _ -> Faker.Cat.name() end)
host_registered_event = build(:host_registered_event, host_id: host_id)

assert_events_and_state(
host_registered_event,
SelectHostChecks.new!(%{
host_id: host_id,
checks: selected_host_checks
}),
[
%HostChecksSelected{
host_id: host_id,
checks: selected_host_checks
}
],
fn host ->
assert %Host{
selected_checks: ^selected_host_checks
} = host
end
)
end

test "should not accept checks selection if a host is not registered yet" do
host_id = Faker.UUID.v4()
selected_host_checks = Enum.map(0..4, fn _ -> Faker.Cat.name() end)

assert_error(
SelectHostChecks.new!(%{
host_id: host_id,
checks: selected_host_checks
}),
{:error, :host_not_registered}
)
end
end

describe "heartbeat" do
test "should emit an HeartbeatSucceded event if the Host never received an heartbeat already" do
host_id = Faker.UUID.v4()
Expand Down