Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Add HardwareIndex and MotherboardIndex on ServerBootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Dec 15, 2017
1 parent 7af42f7 commit 3fecf8d
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/network/model/network/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule Helix.Network.Model.Network.Connection do
|> validate_required(@required_fields)
end

@spec update_nic(t, Component.nic) ::
@spec update_nic(t, Component.nic | nil) ::
changeset
def update_nic(nc = %__MODULE__{}, nic = %Component{type: :nic}) do
nc
Expand Down
2 changes: 0 additions & 2 deletions lib/server/action/flow/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ defmodule Helix.Server.Action.Flow.Server do

defp update_server_mobo(server = %Server{motherboard_id: mobo_id}, mobo_id),
do: {:ok, server}
defp update_server_mobo(server, nil),
do: ServerAction.detach(server)
defp update_server_mobo(server, mobo_id),
do: ServerAction.attach(server, mobo_id)
end
21 changes: 17 additions & 4 deletions lib/server/action/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,39 @@ defmodule Helix.Server.Action.Server do

@spec attach(Server.t, Motherboard.id) ::
{:ok, Server.t}
| {:error, Ecto.Changeset.t}
| {:error, :internal}
@doc """
Attaches a motherboard to the server
This function will fail if either the `motherboard_id` or the `server`
are already attached
"""
def attach(server, motherboard_id) do
ServerInternal.attach(server, motherboard_id)
case ServerInternal.attach(server, motherboard_id) do
{:ok, server} ->
{:ok, server}

{:error, _} ->
{:error, :internal}
end
end

@spec detach(Server.t) ::
:ok
{:ok, Server.t}
| {:error, :internal}
@doc """
Detaches the motherboard linked to server
This function is idempotent
"""
def detach(server) do
ServerInternal.detach(server)
case ServerInternal.detach(server) do
{:ok, server} ->
{:ok, server}

{:error, _} ->
{:error, :internal}
end
end

@spec delete(Server.t) ::
Expand Down
15 changes: 9 additions & 6 deletions lib/server/internal/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,18 @@ defmodule Helix.Server.Internal.Server do
end

@spec detach(Server.t) ::
:ok
repo_return
def detach(server = %Server{}) do
server
|> Server.detach_motherboard()
|> Repo.update!()
result =
server
|> Server.detach_motherboard()
|> Repo.update()

CacheAction.update_server(server)
with {:ok, _} <- result do
CacheAction.update_server(server)
end

:ok
result
end

@spec delete(Server.t) ::
Expand Down
2 changes: 1 addition & 1 deletion lib/server/model/motherboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ defmodule Helix.Server.Model.Motherboard do

alias Helix.Server.Model.Component

@spec by_motherboard(Queryable.t, Motherboard.idt) ::
@spec by_motherboard(Queryable.t, Motherboard.idt, [eager: boolean]) ::
Queryable.t
def by_motherboard(query \\ Motherboard, motherboard_id, eager?)
def by_motherboard(query, mobo = %Component{type: :mobo}, eager?),
Expand Down
10 changes: 9 additions & 1 deletion lib/server/public/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule Helix.Server.Public.Index do
alias Helix.Software.Model.Storage
alias Helix.Software.Public.Index, as: FileIndex
alias Helix.Server.Model.Server
alias Helix.Server.Public.Index.Hardware, as: HardwareIndex
alias Helix.Server.Query.Server, as: ServerQuery

@type index ::
Expand Down Expand Up @@ -72,6 +73,7 @@ defmodule Helix.Server.Public.Index do
logs: LogIndex.index,
main_storage: Storage.id,
storages: FileIndex.index,
hardware: HardwareIndex.index,
processes: ProcessIndex.index,
tunnels: NetworkIndex.index,
}
Expand All @@ -84,6 +86,7 @@ defmodule Helix.Server.Public.Index do
logs: LogIndex.rendered_index,
main_storage: String.t,
storages: FileIndex.rendered_index,
hardware: HardwareIndex.rendered_index,
processes: ProcessIndex.index,
tunnels: NetworkIndex.rendered_index,
}
Expand All @@ -93,7 +96,8 @@ defmodule Helix.Server.Public.Index do
nips: [Network.nip],
logs: LogIndex.index,
main_storage: Storage.id,
storages: FileIndex.rendered_index,
storages: FileIndex.index,
hardware: HardwareIndex.index,
processes: ProcessIndex.index,
tunnels: NetworkIndex.index
}
Expand All @@ -104,6 +108,7 @@ defmodule Helix.Server.Public.Index do
logs: LogIndex.rendered_index,
main_storage: String.t,
storages: FileIndex.rendered_index,
hardware: HardwareIndex.rendered_index,
processes: ProcessIndex.index,
tunnels: NetworkIndex.rendered_index
}
Expand Down Expand Up @@ -280,6 +285,7 @@ defmodule Helix.Server.Public.Index do
filesystem_index = FileIndex.index(server.server_id)
tunnel_index = NetworkIndex.index(server.server_id)
process_index = ProcessIndex.index(server.server_id, entity_id)
hardware_index = HardwareIndex.index(server)

main_storage_id =
server.server_id
Expand All @@ -291,6 +297,7 @@ defmodule Helix.Server.Public.Index do
logs: log_index,
main_storage: main_storage_id,
storages: filesystem_index,
hardware: hardware_index,
processes: process_index,
tunnels: tunnel_index
}
Expand All @@ -311,6 +318,7 @@ defmodule Helix.Server.Public.Index do
logs: LogIndex.render_index(server.logs),
main_storage: server.main_storage |> to_string(),
storages: FileIndex.render_index(server.storages),
hardware: HardwareIndex.render_index(server.hardware),
processes: server.processes,
tunnels: NetworkIndex.render_index(server.tunnels)
}
Expand Down
31 changes: 31 additions & 0 deletions lib/server/public/index/hardware.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Helix.Server.Public.Index.Hardware do

alias Helix.Server.Model.Server
alias Helix.Server.Public.Index.Motherboard, as: MotherboardIndex

@type index ::
%{
motherboard: MotherboardIndex.index | nil
}

@type rendered_index ::
%{
motherboard: MotherboardIndex.rendered_index | nil
}

@spec index(Server.t) ::
index
def index(server = %Server{}) do
%{
motherboard: MotherboardIndex.index(server)
}
end

@spec render_index(index) ::
rendered_index
def render_index(index) do
%{
motherboard: MotherboardIndex.render_index(index.motherboard)
}
end
end
97 changes: 97 additions & 0 deletions lib/server/public/index/motherboard.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
defmodule Helix.Server.Public.Index.Motherboard do

alias Helix.Network.Query.Network, as: NetworkQuery
alias Helix.Server.Model.Motherboard
alias Helix.Server.Model.Server
alias Helix.Server.Query.Motherboard, as: MotherboardQuery

@type index :: term
@type rendered_index :: term

def index(%Server{motherboard_id: nil}) do
%{
motherboard_id: nil,
slots: %{},
network_connections: %{}
}
end

def index(server = %Server{}) do
motherboard = MotherboardQuery.fetch(server.motherboard_id)

network_connections =
motherboard
|> MotherboardQuery.get_nics()
|> Enum.reduce([], fn nic, acc ->

if nic do
nc = NetworkQuery.Connection.fetch_by_nic(nic)

acc ++ [nc]
else
acc
end
end)

%{
motherboard: motherboard,
network_connections: network_connections,
}
end

def render_index(index = %{motherboard_id: nil}),
do: index

def render_index(index) do
%{
motherboard_id: to_string(index.motherboard.motherboard_id),
slots: render_slots(index.motherboard),
network_connections: render_network_connections(index.network_connections)
}
end

defp render_slots(motherboard = %Motherboard{}) do
used_slots =
motherboard.slots
|> Enum.map(fn {slot_id, component} ->
comp_data =
%{
type: to_string(component.type),
component_id: to_string(component.component_id)
}

{slot_id, comp_data}
end)
|> Enum.into(%{})

free_slots =
motherboard
|> MotherboardQuery.get_free_slots()
|> Enum.reduce(%{}, fn {comp_type, free_slots}, acc ->
free_slots
|> Enum.map(fn slot_id ->

{slot_id, %{type: to_string(comp_type), component_id: nil}}
end)
|> Enum.into(%{})
|> Map.merge(acc)
end)

Map.merge(used_slots, free_slots)
end

defp render_network_connections(network_connections) do
network_connections
|> Enum.reduce(%{}, fn nc, acc ->
client_nip =
%{
network_id: to_string(nc.network_id),
ip: nc.ip
}

%{}
|> Map.put(to_string(nc.nic_id), client_nip)
|> Map.merge(acc)
end)
end
end
5 changes: 5 additions & 0 deletions lib/server/query/motherboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ defmodule Helix.Server.Query.Motherboard do
to: MotherboardInternal
defdelegate get_rams(motherboard),
to: MotherboardInternal

defdelegate get_free_slots(motherboard),
to: MotherboardInternal
defdelegate get_free_slots(mobo, motherboard),
to: MotherboardInternal
end
Loading

0 comments on commit 3fecf8d

Please sign in to comment.