Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkopliku committed Aug 24, 2023
1 parent 7781b10 commit d228142
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ defmodule Trento.StreamRollUpEventHandler do
Trento.Domain.Events.HostDetailsUpdated,
Trento.Domain.Events.HostRegistered,
Trento.Domain.Events.ProviderUpdated,
Trento.Domain.Events.SlesSubscriptionsUpdated
Trento.Domain.Events.SlesSubscriptionsUpdated,
Trento.Domain.Events.HostChecksSelected
]

@sap_system_events [
Expand Down
21 changes: 16 additions & 5 deletions lib/trento/application/integration/checks/amqp/processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,28 @@ defmodule Trento.Integration.Checks.AMQP.Processor do
defp handle(%ExecutionStarted{
execution_id: execution_id,
group_id: group_id,
targets: targets
targets: targets,
target_type: target_type
}) do
Logger.debug("Targets for execution #{inspect(targets)}")

# TODO: this needs to broadcast either a cluster execution started or a host execution started
TrentoWeb.Endpoint.broadcast("monitoring:executions", "execution_started", %{
group_id: group_id,
execution_id: execution_id,
targets: map_targets(targets)
targets: map_targets(targets),
target_type: target_type
})
end

defp handle(%ExecutionCompleted{
execution_id: execution_id,
group_id: group_id,
result: result
result: result,
target_type: target_type
}) do
with :ok <-
with "cluster" <- target_type,
:ok <-
commanded().dispatch(
CompleteChecksExecution.new!(%{
cluster_id: group_id,
Expand All @@ -57,8 +62,14 @@ defmodule Trento.Integration.Checks.AMQP.Processor do
correlation_id: execution_id
) do
TrentoWeb.Endpoint.broadcast("monitoring:executions", "execution_completed", %{
group_id: group_id
group_id: group_id,
target_type: target_type
})
else
_zs ->
IO.inspect(_zs)

Check warning on line 70 in lib/trento/application/integration/checks/amqp/processor.ex

View workflow job for this annotation

GitHub Actions / API bc check (V1)

the underscored variable "_zs" is used after being set. A leading underscore indicates that the value of the variable should be ignored. If this is intended please rename the variable to remove the underscore

Check warning on line 70 in lib/trento/application/integration/checks/amqp/processor.ex

View workflow job for this annotation

GitHub Actions / API bc check (V2)

the underscored variable "_zs" is used after being set. A leading underscore indicates that the value of the variable should be ignored. If this is intended please rename the variable to remove the underscore

Check warning on line 70 in lib/trento/application/integration/checks/amqp/processor.ex

View workflow job for this annotation

GitHub Actions / End to end tests

the underscored variable "_zs" is used after being set. A leading underscore indicates that the value of the variable should be ignored. If this is intended please rename the variable to remove the underscore

Check warning on line 70 in lib/trento/application/integration/checks/amqp/processor.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.14.3, OTP 25)

the underscored variable "_zs" is used after being set. A leading underscore indicates that the value of the variable should be ignored. If this is intended please rename the variable to remove the underscore

Check warning on line 70 in lib/trento/application/integration/checks/amqp/processor.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.13.4, OTP 22)

the underscored variable "_zs" is used after being set. A leading underscore indicates that the value of the variable should be ignored. If this is intended please rename the variable to remove the underscore

Check warning on line 70 in lib/trento/application/integration/checks/amqp/processor.ex

View workflow job for this annotation

GitHub Actions / Static Code Analysis

There should be no calls to IO.inspect/1.
# host execution completion to be implemented
:ok
end
end

Expand Down
54 changes: 47 additions & 7 deletions lib/trento/application/integration/checks/checks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Trento.Integration.Checks do
Checks runner service integration
"""

alias Trento.HostReadModel
alias Trento.Infrastructure.Messaging

alias Trento.Checks.V1.{
Expand All @@ -11,20 +12,53 @@ defmodule Trento.Integration.Checks do
}

alias Trento.Integration.Checks.ClusterExecutionEnv
alias Trento.Integration.Checks.HostExecutionEnv

require Logger

@spec request_execution(String.t(), String.t(), ClusterExecutionEnv.t(), [map], [String.t()]) ::
@type target_type :: :host | :cluster

@supported_targets [

Check warning on line 21 in lib/trento/application/integration/checks/checks.ex

View workflow job for this annotation

GitHub Actions / API bc check (V1)

module attribute @supported_targets was set but never used

Check warning on line 21 in lib/trento/application/integration/checks/checks.ex

View workflow job for this annotation

GitHub Actions / API bc check (V2)

module attribute @supported_targets was set but never used

Check warning on line 21 in lib/trento/application/integration/checks/checks.ex

View workflow job for this annotation

GitHub Actions / End to end tests

module attribute @supported_targets was set but never used

Check warning on line 21 in lib/trento/application/integration/checks/checks.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.14.3, OTP 25)

module attribute @supported_targets was set but never used

Check warning on line 21 in lib/trento/application/integration/checks/checks.ex

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.13.4, OTP 22)

module attribute @supported_targets was set but never used
:host,
:cluster
]

@spec request_host_execution(
String.t(),
String.t(),
HostExecutionEnv.t(),
[String.t()]
) ::
:ok | {:error, :any}
def request_host_execution(execution_id, host_id, env, selected_checks) do
request_execution(execution_id, host_id, env, [%{host_id: host_id}], selected_checks, :host)
end

@spec request_cluster_execution(
String.t(),
String.t(),
ClusterExecutionEnv.t(),
[HostReadModel.t()],
[String.t()]
) ::
:ok | {:error, :any}
def request_execution(execution_id, cluster_id, env, hosts, selected_checks) do
def request_cluster_execution(execution_id, cluster_id, env, hosts, selected_checks) do
request_execution(execution_id, cluster_id, env, hosts, selected_checks, :cluster)
end

defp request_execution(execution_id, target_id, env, targets, selected_checks, target_type) do
execution_requested = %ExecutionRequested{
execution_id: execution_id,
group_id: cluster_id,
group_id: target_id,
targets:
Enum.map(hosts, fn %{host_id: host_id} ->
%Target{agent_id: host_id, checks: selected_checks}
end),
env: build_env(env)
Enum.map(
targets,
fn %{host_id: host_id} ->
%Target{agent_id: host_id, checks: selected_checks}
end
),
env: build_env(env),
target_type: Atom.to_string(target_type)
}

case Messaging.publish("executions", execution_requested) do
Expand All @@ -44,4 +78,10 @@ defmodule Trento.Integration.Checks do
"provider" => %{kind: {:string_value, Atom.to_string(provider)}}
}
end

defp build_env(%HostExecutionEnv{provider: provider}) do
%{
"provider" => %{kind: {:string_value, Atom.to_string(provider)}}
}
end
end
14 changes: 14 additions & 0 deletions lib/trento/application/integration/checks/host_executions_env.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Trento.Integration.Checks.HostExecutionEnv do
@moduledoc """
Host checks execution env map
"""

@required_fields :all
use Trento.Type

require Trento.Domain.Enums.Provider, as: Provider

deftype do
field :provider, Ecto.Enum, values: Provider.values()
end
end
2 changes: 1 addition & 1 deletion lib/trento/application/usecases/clusters/clusters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ defmodule Trento.Clusters do
where: h.cluster_id == ^cluster_id and is_nil(h.deregistered_at)
)

Checks.request_execution(
Checks.request_cluster_execution(
UUID.uuid4(),
cluster_id,
%Checks.ClusterExecutionEnv{
Expand Down
39 changes: 39 additions & 0 deletions lib/trento/application/usecases/hosts/hosts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ defmodule Trento.Hosts do
SelectHostChecks
}

alias Trento.Integration.Checks

alias Trento.Repo

@spec get_all_hosts :: [HostReadModel.t()]
Expand Down Expand Up @@ -67,6 +69,43 @@ defmodule Trento.Hosts do
end
end

@spec request_checks_execution(String.t()) :: :ok | {:error, any}
def request_checks_execution(host_id) do
query =
from(h in HostReadModel,
where: is_nil(h.deregistered_at) and h.id == ^host_id
)

case Repo.one(query) do
%HostReadModel{} = host ->
Logger.debug("Requesting checks execution, host: #{host_id}")

maybe_request_checks_execution(host)

nil ->
Logger.error("Requested checks execution for a non-existing host: #{host_id}")

{:error, :not_found}
end
end

defp maybe_request_checks_execution(%{selected_checks: []}), do: {:error, :no_checks_selected}

defp maybe_request_checks_execution(%{
id: host_id,
selected_checks: selected_checks,
provider: provider
}) do
Checks.request_host_execution(
UUID.uuid4(),
host_id,
%Checks.HostExecutionEnv{provider: provider},
selected_checks
)
end

# defp maybe_request_checks_execution(error), do: error

@spec deregister_host(Ecto.UUID.t(), DateService) ::
:ok | {:error, :host_alive} | {:error, :host_not_registered}
def deregister_host(host_id, date_service \\ DateService) do
Expand Down
2 changes: 1 addition & 1 deletion lib/trento_web/controllers/v1/cluster_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule TrentoWeb.V1.ClusterController do
]
],
responses: [
accepted: "The Command has been accepted and the Requested execution is scheduled",
accepted: "The Command has been accepted and the Requested Cluster execution is scheduled",
not_found: Schema.NotFound.response(),
bad_request: Schema.BadRequest.response(),
unprocessable_entity: OpenApiSpex.JsonErrorResponse.response()
Expand Down
26 changes: 26 additions & 0 deletions lib/trento_web/controllers/v1/host_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,30 @@ defmodule TrentoWeb.V1.HostController do
|> json(%{})
end
end

operation :request_checks_execution,
summary: "Request Checks Execution for a Host",
tags: ["Checks"],
description: "Trigger execution of the latest Checks Selection on the target infrastructure",
parameters: [
id: [
in: :path,
required: true,
type: %OpenApiSpex.Schema{type: :string, format: :uuid}
]
],
responses: [
accepted: "The Command has been accepted and the Requested Host execution is scheduled",
not_found: Schema.NotFound.response(),
bad_request: Schema.BadRequest.response(),
unprocessable_entity: OpenApiSpex.JsonErrorResponse.response()
]

def request_checks_execution(conn, %{id: host_id}) do
with :ok <- Hosts.request_checks_execution(host_id) do
conn
|> put_status(:accepted)
|> json(%{})
end
end
end
4 changes: 4 additions & 0 deletions lib/trento_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ defmodule TrentoWeb.Router do
ClusterController,
:request_checks_execution

post "/hosts/:id/checks/request_execution",
HostController,
:request_checks_execution

post "/hosts/:id/tags", TagsController, :add_tag,
assigns: %{resource_type: :host},
as: :hosts_tagging
Expand Down
2 changes: 1 addition & 1 deletion test/trento/application/integration/checks/checks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule Trento.Integration.ChecksTest do
end)

assert :ok =
Checks.request_execution(
Checks.request_cluster_execution(
execution_id,
group_id,
env,
Expand Down
6 changes: 4 additions & 2 deletions test/trento/application/integration/checks/processor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ defmodule Trento.Integration.Checks.AMQP.ProcessorTest do
Contracts.to_event(%ExecutionCompleted{
execution_id: execution_id,
group_id: group_id,
result: :PASSING
result: :PASSING,
target_type: "cluster"
})

message = %GenRMQ.Message{payload: execution_completed, attributes: %{}, channel: nil}
Expand All @@ -94,7 +95,8 @@ defmodule Trento.Integration.Checks.AMQP.ProcessorTest do
Contracts.to_event(%ExecutionCompleted{
execution_id: UUID.uuid4(),
group_id: group_id,
result: :PASSING
result: :PASSING,
target_type: "cluster"
})

message = %GenRMQ.Message{payload: execution_completed, attributes: %{}, channel: nil}
Expand Down

0 comments on commit d228142

Please sign in to comment.