-
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 a POC for cloud discovery decoding
- Loading branch information
1 parent
8b062ae
commit 1dd2bb6
Showing
4 changed files
with
139 additions
and
73 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
lib/trento/application/integration/discovery/payloads/cloud_discovery_payload.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,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 |
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