Skip to content

Commit

Permalink
Controller refactor due the new openapispex refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco committed Jul 15, 2024
1 parent 486d9ed commit 2fb1833
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 58 deletions.
29 changes: 16 additions & 13 deletions lib/trento/activity_log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,32 @@ defmodule Trento.ActivityLog do
alias Trento.Repo

@spec get_settings() ::
{:ok, Settings.t()} | {:error, :activity_log_settings_not_configured}
{:ok, Settings.t()} | {:error, :not_found}
def get_settings do
case Repo.one(Settings.base_query()) do
%Settings{} = settings -> {:ok, settings}
nil -> {:error, :activity_log_settings_not_configured}
nil -> {:error, :not_found}
end
end

@spec change_retention_period(integer(), RetentionPeriodUnit.t()) ::
{:ok, Settings.t()}
| {:error, :activity_log_settings_not_configured}
| {:error, any()}
def change_retention_period(value, unit) do
with {:ok, settings} <- get_settings() do
settings
|> Settings.changeset(%{
retention_time: %{
value: value,
unit: unit
}
})
|> Repo.update()
|> log_error("Error while updating activity log retention period")
case get_settings() do
{:ok, settings} ->
settings
|> Settings.changeset(%{
retention_time: %{
value: value,
unit: unit
}
})
|> Repo.update()
|> log_error("Error while updating activity log retention period")

{:error, :not_found} ->
{:error, :activity_log_settings_not_configured}
end
end

Expand Down
4 changes: 1 addition & 3 deletions lib/trento_web/controllers/v1/cluster_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ defmodule TrentoWeb.V1.ClusterController do
unprocessable_entity: OpenApiSpex.JsonErrorResponse.response()
]

def select_checks(%{body_params: body_params} = conn, %{cluster_id: cluster_id}) do
%{checks: checks} = body_params

def select_checks(%{body_params: %{checks: checks}} = conn, %{cluster_id: cluster_id}) do
with :ok <- Clusters.select_checks(cluster_id, checks) do
conn
|> put_status(:accepted)
Expand Down
16 changes: 10 additions & 6 deletions lib/trento_web/controllers/v1/discovery_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ defmodule TrentoWeb.V1.DiscoveryController do
]

def collect(
conn,
%{
body_params: %{
agent_id: agent_id,
discovery_type: discovery_type,
payload: payload
}
} = conn,
_
) do
body_params = Map.get(conn, :body_params)

event = %{
"agent_id" => body_params.agent_id,
"discovery_type" => body_params.discovery_type,
"payload" => body_params.payload
"agent_id" => agent_id,
"discovery_type" => discovery_type,
"payload" => payload
}

with :ok <- Discovery.handle(event) do
Expand Down
4 changes: 1 addition & 3 deletions lib/trento_web/controllers/v1/host_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ defmodule TrentoWeb.V1.HostController do
unprocessable_entity: OpenApiSpex.JsonErrorResponse.response()
]

def select_checks(%{body_params: body_params} = conn, %{id: host_id}) do
%{checks: checks} = body_params

def select_checks(%{body_params: %{checks: checks}} = conn, %{id: host_id}) do
with :ok <- Hosts.select_checks(host_id, checks) do
conn
|> put_status(:accepted)
Expand Down
3 changes: 1 addition & 2 deletions lib/trento_web/controllers/v1/profile_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ defmodule TrentoWeb.V1.ProfileController do
forbidden: Schema.Forbidden.response()
]

def confirm_totp_enrollment(%{body_params: body_params} = conn, _) do
def confirm_totp_enrollment(%{body_params: %{totp_code: totp_code}} = conn, _) do
%User{} = user = Pow.Plug.current_user(conn)
totp_code = Map.get(body_params, :totp_code)

with {:ok, %User{totp_enabled_at: totp_enabled_at}} <-
Users.confirm_totp_enrollment(user, totp_code) do
Expand Down
29 changes: 10 additions & 19 deletions lib/trento_web/controllers/v1/settings_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ defmodule TrentoWeb.V1.SettingsController do
not_found: Schema.NotFound.response()
]

def update_api_key_settings(%{body_params: body_params} = conn, _) do
%{expire_at: expire_at} = body_params

def update_api_key_settings(%{body_params: %{expire_at: expire_at}} = conn, _) do
with {:ok, updated_settings} <- Settings.update_api_key_settings(expire_at) do
api_key =
Map.put(
Expand All @@ -116,10 +114,11 @@ defmodule TrentoWeb.V1.SettingsController do
unprocessable_entity: Schema.UnprocessableEntity.response()
]

def update_activity_log_settings(%{body_params: body_params} = conn, _) do
%{retention_time: %{value: retention_period, unit: retention_period_unit}} =
body_params

def update_activity_log_settings(
%{body_params: %{retention_time: %{value: retention_period, unit: retention_period_unit}}} =
conn,
_
) do
with {:ok, updated_settings} <-
ActivityLog.change_retention_period(retention_period, retention_period_unit) do
render(conn, "activity_log_settings.json", %{
Expand All @@ -139,18 +138,10 @@ defmodule TrentoWeb.V1.SettingsController do
]

def get_activity_log_settings(conn, _) do
case ActivityLog.get_settings() do
{:ok, settings} ->
render(conn, "activity_log_settings.json", %{
activity_log_settings: settings
})

_ ->
# Here we rebind the error returned by the ActivityLog.get_settings/0 function
# to a "not_found" in order to disambiguate from a similar error being returned by the
# ActivityLog.change_retention_period/2 function. This error gets picked up by the FallbackController
# and leads to dispatching to the appropriate error response.
{:error, :not_found}
with {:ok, settings} <- ActivityLog.get_settings() do
render(conn, "activity_log_settings.json", %{
activity_log_settings: settings
})
end
end

Expand Down
11 changes: 2 additions & 9 deletions lib/trento_web/controllers/v1/suma_credentials_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ defmodule TrentoWeb.V1.SUMACredentialsController do

@spec create(Plug.Conn.t(), any) :: Plug.Conn.t()
def create(%{body_params: body_params} = conn, _) do
attrs = decode_body(body_params)

with {:ok, saved_settings} <- SoftwareUpdates.save_settings(attrs) do
with {:ok, saved_settings} <- SoftwareUpdates.save_settings(body_params) do
conn
|> put_status(:created)
|> render("suma_credentials.json", %{settings: saved_settings})
Expand All @@ -73,9 +71,7 @@ defmodule TrentoWeb.V1.SUMACredentialsController do

@spec update(Plug.Conn.t(), any) :: Plug.Conn.t()
def update(%{body_params: body_params} = conn, _) do
attrs = decode_body(body_params)

with {:ok, saved_settings} <- SoftwareUpdates.change_settings(attrs) do
with {:ok, saved_settings} <- SoftwareUpdates.change_settings(body_params) do
conn
|> put_status(:ok)
|> render("suma_credentials.json", %{settings: saved_settings})
Expand Down Expand Up @@ -116,7 +112,4 @@ defmodule TrentoWeb.V1.SUMACredentialsController do
end

def get_policy_resource(_), do: Trento.SoftwareUpdates.Settings

defp decode_body(body) when is_struct(body), do: Map.from_struct(body)
defp decode_body(body), do: body
end
5 changes: 3 additions & 2 deletions lib/trento_web/controllers/v1/tags_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ defmodule TrentoWeb.V1.TagsController do
%{
assigns: %{
resource_type: resource_type
},
body_params: %{
value: value
}
} = conn,
%{id: id}
) do
%{value: value} = Map.get(conn, :body_params)

with {:ok, %Tag{value: value}} <- Tags.add_tag(value, id, resource_type) do
conn
|> put_status(:created)
Expand Down
2 changes: 1 addition & 1 deletion test/trento/activity_log_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Trento.ActivityLogTest do

describe "retrieving activity log settings" do
test "should return an error when settings are not available" do
assert {:error, :activity_log_settings_not_configured} == ActivityLog.get_settings()
assert {:error, :not_found} == ActivityLog.get_settings()
end

test "should return settings" do
Expand Down

0 comments on commit 2fb1833

Please sign in to comment.