diff --git a/lib/trento/domain/host/host.ex b/lib/trento/domain/host/host.ex index 91f66d3b49..f699d80a1d 100644 --- a/lib/trento/domain/host/host.ex +++ b/lib/trento/domain/host/host.ex @@ -31,6 +31,7 @@ defmodule Trento.Domain.Host do alias Trento.Domain.Commands.{ RegisterHost, RollUpHost, + SelectHostChecks, UpdateHeartbeat, UpdateProvider, UpdateSlesSubscriptions @@ -39,6 +40,7 @@ defmodule Trento.Domain.Host do alias Trento.Domain.Events.{ HeartbeatFailed, HeartbeatSucceded, + HostChecksSelected, HostDetailsUpdated, HostRegistered, HostRolledUp, @@ -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 @@ -267,6 +270,27 @@ defmodule Trento.Domain.Host do } end + def execute( + %Host{host_id: nil}, + %SelectHostChecks{} + ) do + {:error, :host_not_registered} + 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{ @@ -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 diff --git a/lib/trento/infrastructure/router.ex b/lib/trento/infrastructure/router.ex index 0b4f5c8524..94127474b4 100644 --- a/lib/trento/infrastructure/router.ex +++ b/lib/trento/infrastructure/router.ex @@ -19,6 +19,7 @@ defmodule Trento.Router do RollUpHost, RollUpSapSystem, SelectChecks, + SelectHostChecks, UpdateHeartbeat, UpdateProvider, UpdateSlesSubscriptions @@ -33,7 +34,8 @@ defmodule Trento.Router do UpdateHeartbeat, UpdateProvider, UpdateSlesSubscriptions, - RollUpHost + RollUpHost, + SelectHostChecks ], to: Host, lifespan: Host.Lifespan diff --git a/test/trento/domain/host/host_test.exs b/test/trento/domain/host/host_test.exs index 6ac022564c..30881a301e 100644 --- a/test/trento/domain/host/host_test.exs +++ b/test/trento/domain/host/host_test.exs @@ -6,6 +6,7 @@ defmodule Trento.HostTest do alias Trento.Domain.Commands.{ RegisterHost, RollUpHost, + SelectHostChecks, UpdateHeartbeat, UpdateProvider, UpdateSlesSubscriptions @@ -14,6 +15,7 @@ defmodule Trento.HostTest do alias Trento.Domain.Events.{ HeartbeatFailed, HeartbeatSucceded, + HostChecksSelected, HostDetailsUpdated, HostRegistered, HostRolledUp, @@ -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()