Skip to content

Commit

Permalink
Add a POC for cloud discovery decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
dottorblaster committed May 24, 2022
1 parent 8b062ae commit 1dd2bb6
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
defmodule Trento.Integration.Discovery.CloudDiscoveryPayload do
@moduledoc """
Cloud discovery integration event payload
"""

@required_fields []

use Trento.Type
import PolymorphicEmbed, only: [cast_polymorphic_embed: 3]

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

field :metadata, PolymorphicEmbed,
types: [azure: __MODULE__.AzureMetadata],
on_type_not_found: :nilify,
on_replace: :update
end

def changeset(event, attrs) do
enriched_attrs = enrich_metadata_type(attrs)

event
|> cast(enriched_attrs, [:provider])
|> cast_polymorphic_embed(:metadata, required: false)
end

defp enrich_metadata_type(%{"provider" => provider, "metadata" => %{} = metadata} = attrs),
do: %{attrs | "metadata" => Map.put(metadata, "__type__", provider)}

defp enrich_metadata_type(attrs), do: attrs

defmodule AzureMetadata do
@moduledoc nil

@required_fields nil
use Trento.Type

deftype do
embeds_one :compute, Compute do
field :name, :string
field :resource_group_name, :string
field :location, :string
field :vm_size, :string
field :offer, :string
field :sku, :string

embeds_one :os_profile, OsProfile do
field :admin_username, :string
end

embeds_one :storage_profile, StorageProfile do
field :data_disks, {:array, :map}
end
end
end

def changeset(event, attrs) do
event
|> cast(attrs, [])
|> cast_embed(:compute,
with: fn event, attrs ->
event
|> cast(attrs, [
:name,
:resource_group_name,
:location,
:vm_size,
:offer,
:sku
])
|> cast_embed(:os_profile,
with: fn event, attrs -> event |> cast(attrs, [:admin_username]) end
)
|> cast_embed(:storage_profile,
with: fn event, attrs -> event |> cast(attrs, [:data_disks]) end
)
end
)
|> validate_required_fields(@required_fields)
end
end
end
124 changes: 52 additions & 72 deletions lib/trento/application/integration/discovery/policies/host_policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Trento.Integration.Discovery.HostPolicy do
UpdateSlesSubscriptions
}

alias Trento.Integration.Discovery.HostDiscoveryPayload
alias Trento.Integration.Discovery.{CloudDiscoveryPayload, HostDiscoveryPayload}

@spec handle(map) ::
{:ok, RegisterHost.t() | UpdateProvider.t() | UpdateSlesSubscriptions.t()}
Expand All @@ -28,44 +28,15 @@ defmodule Trento.Integration.Discovery.HostPolicy do
def handle(%{
"discovery_type" => "cloud_discovery",
"agent_id" => agent_id,
"payload" =>
%{
"Provider" => "azure"
} = payload
}) do
UpdateProvider.new(%{
host_id: agent_id,
provider: :azure,
provider_data: parse_azure_data(payload)
})
end

def handle(%{
"discovery_type" => "cloud_discovery",
"agent_id" => agent_id,
"payload" => %{
"Provider" => "aws"
}
}) do
UpdateProvider.new(%{
host_id: agent_id,
provider: :aws,
provider_data: nil
})
end

def handle(%{
"discovery_type" => "cloud_discovery",
"agent_id" => agent_id,
"payload" => %{
"Provider" => "gcp"
}
"payload" => payload
}) do
UpdateProvider.new(%{
host_id: agent_id,
provider: :gcp,
provider_data: nil
})
payload
|> ProperCase.to_snake_case()
|> CloudDiscoveryPayload.new()
|> case do
{:ok, decoded_payload} -> build_update_provider_command(agent_id, decoded_payload)
error -> error
end
end

def handle(%{
Expand Down Expand Up @@ -113,6 +84,49 @@ defmodule Trento.Integration.Discovery.HostPolicy do
os_version: os_version
})

defp build_update_provider_command(agent_id, %CloudDiscoveryPayload{
provider: provider,
metadata: metadata
}) do
UpdateProvider.new(%{
host_id: agent_id,
provider: provider,
provider_data:
case metadata do
%{
compute: %{
name: name,
resource_group_name: resource_group,
location: location,
vm_size: vm_size,
storage_profile: storage_profile,
offer: offer,
sku: sku,
os_profile: %{admin_username: admin_username}
}
} ->
%{
vm_name: name,
resource_group: resource_group,
location: location,
vm_size: vm_size,
data_disk_number:
case storage_profile do
%{data_disks: nil} -> 0
%{data_disks: data_disks} -> length(data_disks)
_ -> 0
end,
offer: offer,
sku: sku,
admin_username: admin_username
}

generic_metadata ->
generic_metadata
end
})
end

@spec is_non_loopback_ipv4?(String.t()) :: boolean
defp is_non_loopback_ipv4?("127.0.0.1"), do: false

Expand Down Expand Up @@ -164,38 +178,4 @@ defmodule Trento.Integration.Discovery.HostPolicy do
version: version
}
end

@spec parse_azure_data(map) :: map
defp parse_azure_data(%{
"Metadata" => %{
"compute" => %{
"name" => name,
"resourceGroupName" => resource_group,
"location" => location,
"vmSize" => vm_size,
"storageProfile" => storage_profile,
"offer" => offer,
"sku" => sku,
"osProfile" => %{
"adminUsername" => admin_username
}
}
}
}) do
%{
vm_name: name,
resource_group: resource_group,
location: location,
vm_size: vm_size,
data_disk_number: parse_data_disk_number(storage_profile),
offer: offer,
sku: sku,
admin_username: admin_username
}
end

@spec parse_data_disk_number(map) :: non_neg_integer()
defp parse_data_disk_number(%{"dataDisks" => data_disks}), do: length(data_disks)

defp parse_data_disk_number(_), do: 0
end
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ defmodule Trento.MixProject do
{:swoosh, "~> 1.3"},
{:telemetry_metrics, "~> 0.6"},
{:telemetry_poller, "~> 1.0"},
{:timex, "~> 3.7.7"}
{:timex, "~> 3.7.7"},
{:proper_case, "~> 1.3.1"},
{:polymorphic_embed, "~> 1.9.0"}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"polymorphic_embed": {:hex, :polymorphic_embed, "1.9.0", "6863169a34f489f1674d265c35b3723a0ad03a75b6e5457df48997998985e565", [:mix], [{:ecto, "~> 3.6", [hex: :ecto, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "7ccb0e942b8f0191036ccd256a067de7e6fdce023ce9fa9ffc2b99ed9a91d241"},
"postgrex": {:hex, :postgrex, "0.16.3", "fac79a81a9a234b11c44235a4494d8565303fa4b9147acf57e48978a074971db", [:mix], [{:connection, "~> 1.1", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "aeaae1d2d1322da4e5fe90d241b0a564ce03a3add09d7270fb85362166194590"},
"pow": {:hex, :pow, "1.0.27", "867556f8474203023c86cbdf15545dc32b18baf7cd19858d136a0549df830667", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.3.0 and < 1.7.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, ">= 2.0.0 and < 4.0.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:plug, ">= 1.5.0 and < 2.0.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "3ec645c2487c5479d7af7025261506ad5ce6ea3431c3633ed64dc6f553b71ae8"},
"proper_case": {:hex, :proper_case, "1.3.1", "5f51cabd2d422a45f374c6061b7379191d585b5154456b371432d0fa7cb1ffda", [:mix], [], "hexpm", "6cc715550cc1895e61608060bbe67aef0d7c9cf55d7ddb013c6d7073036811dd"},
"quantum": {:hex, :quantum, "3.5.0", "8d2c5ba68c55991e8975aca368e3ab844ba01f4b87c4185a7403280e2c99cf34", [:mix], [{:crontab, "~> 1.1", [hex: :crontab, repo: "hexpm", optional: false]}, {:gen_stage, "~> 0.14 or ~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_registry, "~> 0.2", [hex: :telemetry_registry, repo: "hexpm", optional: false]}], "hexpm", "cab737d1d9779f43cb1d701f46dd05ea58146fd96238d91c9e0da662c1982bb6"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
Expand Down

0 comments on commit 1dd2bb6

Please sign in to comment.