Skip to content

Commit

Permalink
Host views aligned with openapi spex
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco committed Feb 21, 2024
1 parent e40c993 commit 474219e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
34 changes: 30 additions & 4 deletions lib/trento_web/views/v1/host_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,40 @@ defmodule TrentoWeb.V1.HostView do
render_many(hosts, __MODULE__, "host.json")
end

def render("host.json", %{host: host}) do
def render("host.json", %{host: %{sles_subscriptions: sles_subscriptions} = host}) do
sles_subscriptions =
render_many(sles_subscriptions, __MODULE__, "sles_subscription.json",
as: :sles_subscription
)

host
|> Map.from_struct()
|> Map.put(:sles_subscriptions, sles_subscriptions)
|> Map.delete(:fully_qualified_domain_name)
|> Map.delete(:health)
|> Map.delete(:selected_checks)
|> Map.delete(:inserted_at)
|> Map.delete(:__meta__)
end

def render("sles_subscription.json", %{sles_subscription: sles_subscription}) do
sles_subscription
|> Map.from_struct()
|> Map.delete(:__meta__)
|> Map.delete(:inserted_at)
|> Map.delete(:updated_at)
end

def render("sles_subscription.json", _), do: nil

def render("broadcast_host.json", %{host: host}) do
host
|> Map.from_struct()
|> Map.delete(:__meta__)
end

def render("host_details_updated.json", %{host: host}) do
render("host.json", %{host: host})
render("broadcast_host.json", %{host: host})
|> Map.delete(:sles_subscriptions)
|> Map.delete(:tags)
|> Map.delete(:cluster_id)
Expand All @@ -22,13 +48,13 @@ defmodule TrentoWeb.V1.HostView do
end

def render("host_registered.json", %{host: host}) do
render("host.json", %{host: host})
render("broadcast_host.json", %{host: host})
|> Map.delete(:sles_subscriptions)
|> Map.delete(:tags)
end

def render("host_restored.json", %{host: host}) do
render("host.json", %{host: host})
render("broadcast_host.json", %{host: host})
end

def render("heartbeat_result.json", %{host: %{id: id, hostname: hostname}}) do
Expand Down
11 changes: 11 additions & 0 deletions test/trento_web/views/v1/host_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ defmodule TrentoWeb.V1.HostViewTest do
render(HostView, "host_health_changed.json", %{host: host})
end

test "should render single host information" do
host = build(:host, sles_subscriptions: build_list(1, :sles_subscription))

rendered_host = render(HostView, "host.json", %{host: host})

refute Access.get(rendered_host, "fully_qualified_domain_name")
refute Access.get(rendered_host, "health")
refute Access.get(rendered_host, "selected_checks")
refute Access.get(rendered_host, "inserted_at")
end

test "should render host details relevant information" do
host = build(:host)

Expand Down

0 comments on commit 474219e

Please sign in to comment.