Skip to content

Commit

Permalink
Add installation_source field to the host telemetry (#728)
Browse files Browse the repository at this point in the history
* Add installation_source field to the host telemetry

* Set the installation_source in a fixed enum

* Upcast version of the changed events

* Add event upcasting tests

* Remove not needed default values from commands ane events

* Split the host event upcasting test to individual files
  • Loading branch information
arbulu89 committed Sep 12, 2022
1 parent 779a3fe commit 824751d
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,22 @@ defmodule Trento.Integration.Discovery.HostDiscoveryPayload do
field :total_memory_mb, :integer
field :socket_count, :integer
field :os_version, :string

field :installation_source, Ecto.Enum,
values: [:community, :suse, :unknown],
default: :unknown
end

def changeset(host, attrs) do
modified_attrs = installation_source_to_downcase(attrs)

host
|> cast(modified_attrs, fields())
|> validate_required_fields(@required_fields)
end

defp installation_source_to_downcase(%{"installation_source" => installation_source} = attrs),
do: %{attrs | "installation_source" => String.downcase(installation_source)}

defp installation_source_to_downcase(attrs), do: attrs
end
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ defmodule Trento.Integration.Discovery.HostPolicy do
cpu_count: cpu_count,
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version
os_version: os_version,
installation_source: installation_source
}),
do:
RegisterHost.new(%{
Expand All @@ -90,7 +91,8 @@ defmodule Trento.Integration.Discovery.HostPolicy do
cpu_count: cpu_count,
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version
os_version: os_version,
installation_source: installation_source
})

defp build_update_provider_command(agent_id, %CloudDiscoveryPayload{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ defmodule Trento.Integration.Telemetry.Suse do
socket_count: &1.socket_count,
total_memory_mb: &1.total_memory_mb,
cloud_provider: &1.provider,
agent_installation_source: &1.installation_source,
time: &1.updated_at
}
)
Expand Down
12 changes: 8 additions & 4 deletions lib/trento/application/projectors/telemetry_projector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ defmodule Trento.TelemetryProjector do
cpu_count: cpu_count,
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: sles_version
os_version: sles_version,
installation_source: installation_source
},
fn multi ->
changeset =
Expand All @@ -34,7 +35,8 @@ defmodule Trento.TelemetryProjector do
cpu_count: cpu_count,
socket_count: socket_count,
total_memory_mb: total_memory_mb,
sles_version: sles_version
sles_version: sles_version,
installation_source: installation_source
})

Ecto.Multi.insert(multi, :host_telemetry, changeset,
Expand All @@ -51,7 +53,8 @@ defmodule Trento.TelemetryProjector do
cpu_count: cpu_count,
socket_count: socket_count,
total_memory_mb: total_memory_mb,
os_version: sles_version
os_version: sles_version,
installation_source: installation_source
},
fn multi ->
changeset =
Expand All @@ -62,7 +65,8 @@ defmodule Trento.TelemetryProjector do
cpu_count: cpu_count,
socket_count: socket_count,
total_memory_mb: total_memory_mb,
sles_version: sles_version
sles_version: sles_version,
installation_source: installation_source
})

Ecto.Multi.insert(multi, :host_telemetry, changeset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ defmodule Trento.HostTelemetryReadModel do
field :socket_count, :integer
field :total_memory_mb, :integer
field :sles_version, :string

field :installation_source, Ecto.Enum,
values: [:community, :suse, :unknown],
default: :unknown

field :provider, Ecto.Enum, values: [:azure, :aws, :gcp, :unknown]

timestamps()
Expand Down
2 changes: 2 additions & 0 deletions lib/trento/domain/host/commands/register_host.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ defmodule Trento.Domain.Commands.RegisterHost do
field :total_memory_mb, :integer
field :socket_count, :integer
field :os_version, :string, default: "Unknown"

field :installation_source, Ecto.Enum, values: [:community, :suse, :unknown]
end
end
6 changes: 5 additions & 1 deletion lib/trento/domain/host/events/host_details_updated.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Trento.Domain.Events.HostDetailsUpdated do

use Trento.Event

defevent do
defevent version: 2 do
field :host_id, Ecto.UUID
field :hostname, :string
field :ip_addresses, {:array, :string}
Expand All @@ -15,5 +15,9 @@ defmodule Trento.Domain.Events.HostDetailsUpdated do
field :total_memory_mb, :integer
field :socket_count, :integer
field :os_version, :string

field :installation_source, Ecto.Enum, values: [:community, :suse, :unknown]
end

def upcast(params, _, 2), do: Map.put(params, "installation_source", :unknown)
end
7 changes: 6 additions & 1 deletion lib/trento/domain/host/events/host_registered.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Trento.Domain.Events.HostRegistered do

use Trento.Event

defevent do
defevent version: 2 do
field :host_id, Ecto.UUID
field :hostname, :string
field :ip_addresses, {:array, :string}
Expand All @@ -15,6 +15,11 @@ defmodule Trento.Domain.Events.HostRegistered do
field :total_memory_mb, :integer
field :socket_count, :integer
field :os_version, :string

field :installation_source, Ecto.Enum, values: [:community, :suse, :unknown]

field :heartbeat, Ecto.Enum, values: [:unknown]
end

def upcast(params, _, 2), do: Map.put(params, "installation_source", :unknown)
end
26 changes: 19 additions & 7 deletions lib/trento/domain/host/host.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ defmodule Trento.Domain.Host do
:total_memory_mb,
:socket_count,
:os_version,
:installation_source,
:heartbeat,
:subscriptions,
:provider_data
Expand All @@ -53,6 +54,7 @@ defmodule Trento.Domain.Host do
total_memory_mb: non_neg_integer(),
socket_count: non_neg_integer(),
os_version: String.t(),
installation_source: :community | :suse | :unknown,
subscriptions: [SlesSubscription.t()],
provider_data: AwsProvider.t() | AzureProvider.t() | GcpProvider.t() | nil,
heartbeat: :passing | :critical | :unknown
Expand All @@ -70,7 +72,8 @@ defmodule Trento.Domain.Host do
cpu_count: cpu_count,
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version
os_version: os_version,
installation_source: installation_source
}
) do
%HostRegistered{
Expand All @@ -83,6 +86,7 @@ defmodule Trento.Domain.Host do
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version,
installation_source: installation_source,
heartbeat: :unknown
}
end
Expand All @@ -97,7 +101,8 @@ defmodule Trento.Domain.Host do
cpu_count: cpu_count,
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version
os_version: os_version,
installation_source: installation_source
},
%RegisterHost{
hostname: hostname,
Expand All @@ -107,7 +112,8 @@ defmodule Trento.Domain.Host do
cpu_count: cpu_count,
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version
os_version: os_version,
installation_source: installation_source
}
) do
[]
Expand All @@ -124,7 +130,8 @@ defmodule Trento.Domain.Host do
cpu_count: cpu_count,
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version
os_version: os_version,
installation_source: installation_source
}
) do
%HostDetailsUpdated{
Expand All @@ -136,7 +143,8 @@ defmodule Trento.Domain.Host do
cpu_count: cpu_count,
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version
os_version: os_version,
installation_source: installation_source
}
end

Expand Down Expand Up @@ -232,6 +240,7 @@ defmodule Trento.Domain.Host do
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version,
installation_source: installation_source,
heartbeat: heartbeat
}
) do
Expand All @@ -246,6 +255,7 @@ defmodule Trento.Domain.Host do
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version,
installation_source: installation_source,
heartbeat: heartbeat
}
end
Expand All @@ -260,7 +270,8 @@ defmodule Trento.Domain.Host do
cpu_count: cpu_count,
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version
os_version: os_version,
installation_source: installation_source
}
) do
%Host{
Expand All @@ -272,7 +283,8 @@ defmodule Trento.Domain.Host do
cpu_count: cpu_count,
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version
os_version: os_version,
installation_source: installation_source
}
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Trento.Repo.Migrations.AddInstallationSourceHostTelemetryReadModel do
use Ecto.Migration

def change do
alter table(:hosts_telemetry) do
add :installation_source, :string
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"agent_id": "779cdd70-e9e2-58ca-b18a-bf3eb3f71244",
"discovery_type": "host_discovery",
"payload": {
"ssh_address": "10.2.2.22",
"os_version": "15-SP2",
"ip_addresses": ["10.1.1.4", "10.1.1.5", "10.1.1.6"],
"hostname": "suse",
"cpu_count": 2,
"socket_count": 1,
"total_memory_mb": 4096,
"agent_version": "0.1.0",
"installation_source": "Community"
}
}
7 changes: 5 additions & 2 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ defmodule Trento.Factory do
total_memory_mb: Enum.random(1..128),
socket_count: Enum.random(1..16),
os_version: Faker.App.semver(),
installation_source: Enum.random([:community, :suse, :unknown]),
heartbeat: :unknown
}
end
Expand All @@ -78,7 +79,8 @@ defmodule Trento.Factory do
cpu_count: Enum.random(1..16),
total_memory_mb: Enum.random(1..128),
socket_count: Enum.random(1..16),
os_version: Faker.App.semver()
os_version: Faker.App.semver(),
installation_source: Enum.random([:community, :suse, :unknown])
}
end

Expand Down Expand Up @@ -157,7 +159,8 @@ defmodule Trento.Factory do
cpu_count: Enum.random(0..100),
socket_count: Enum.random(0..100),
total_memory_mb: Enum.random(0..100),
sles_version: Faker.App.version()
sles_version: Faker.App.version(),
installation_source: Enum.random([:community, :suse, :unknown])
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,31 @@ defmodule Trento.Integration.Discovery.HostPolicyTest do
agent_version: "0.1.0",
host_id: "779cdd70-e9e2-58ca-b18a-bf3eb3f71244",
hostname: "suse",
ip_addresses: ["10.1.1.4", "10.1.1.5", "10.1.1.6"]
ip_addresses: ["10.1.1.4", "10.1.1.5", "10.1.1.6"],
installation_source: :unknown
}
} =
"host_discovery"
|> load_discovery_event_fixture()
|> HostPolicy.handle()
end

test "should return the expected commands when a host_discovery_with_installation_source payload is handled" do
assert {
:ok,
%RegisterHost{
agent_version: "0.1.0",
host_id: "779cdd70-e9e2-58ca-b18a-bf3eb3f71244",
hostname: "suse",
ip_addresses: ["10.1.1.4", "10.1.1.5", "10.1.1.6"],
installation_source: :community
}
} =
"host_discovery_with_installation_source"
|> load_discovery_event_fixture()
|> HostPolicy.handle()
end

test "should return the expected commands when a cloud_discovery payload with an azure provider is handled" do
assert {
:ok,
Expand Down
46 changes: 46 additions & 0 deletions test/trento/domain/host/events/host_details_updated_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
defmodule Trento.Domain.Events.HostDetailsUpdatedTest do
use Trento.AggregateCase, aggregate: Trento.Domain.Host, async: true

alias Trento.Domain.Events.HostDetailsUpdated

describe "HostDetailsUpdated event upcasting" do
test "should upcast HostDetailsUpdated event properly from version 1" do
host_id = Faker.UUID.v4()
hostname = Faker.StarWars.character()
ip_addresses = [Faker.Internet.ip_v4_address()]
ssh_address = Faker.Internet.ip_v4_address()
agent_version = Faker.Internet.slug()
cpu_count = Enum.random(1..16)
total_memory_mb = Enum.random(1..128)
socket_count = Enum.random(1..16)
os_version = Faker.App.version()

assert %HostDetailsUpdated{
version: 2,
host_id: host_id,
hostname: hostname,
ip_addresses: ip_addresses,
ssh_address: ssh_address,
agent_version: agent_version,
cpu_count: cpu_count,
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version,
installation_source: :unknown
} ==
%{
"host_id" => host_id,
"hostname" => hostname,
"ip_addresses" => ip_addresses,
"ssh_address" => ssh_address,
"agent_version" => agent_version,
"cpu_count" => cpu_count,
"total_memory_mb" => total_memory_mb,
"socket_count" => socket_count,
"os_version" => os_version
}
|> HostDetailsUpdated.upcast(%{})
|> HostDetailsUpdated.new!()
end
end
end

0 comments on commit 824751d

Please sign in to comment.