mirror of
https://github.com/outbackdingo/firezone.git
synced 2026-03-22 03:41:56 +00:00
Why: * Rather than using a persistent_id field in Resources/Policies, it was decided that we should allow "breaking changes" to these entities. This means that Resources/Policies will now be able to update all fields on the schema without changing the primary key ID of the entity. * This change will greatly help the API and Terraform provider development. @jamilbk, would you like me to put a migration in this PR to actually get rid of all of the existing soft deleted entities? @thomaseizinger, I tagged you on this, because I wanted to make sure that these changes weren't going to break any expectations in the client and/or gateways. --------- Signed-off-by: Brian Manifold <bmanifold@users.noreply.github.com> Co-authored-by: Jamil <jamilbk@users.noreply.github.com>
137 lines
3.7 KiB
Elixir
137 lines
3.7 KiB
Elixir
defmodule API.ResourceController do
|
|
use API, :controller
|
|
use OpenApiSpex.ControllerSpecs
|
|
alias API.Pagination
|
|
alias Domain.Resources
|
|
|
|
action_fallback API.FallbackController
|
|
|
|
tags ["Resources"]
|
|
|
|
operation :index,
|
|
summary: "List Resources",
|
|
parameters: [
|
|
limit: [in: :query, description: "Limit Resources returned", type: :integer, example: 10],
|
|
page_cursor: [in: :query, description: "Next/Prev page cursor", type: :string]
|
|
],
|
|
responses: [
|
|
ok: {"Resource Response", "application/json", API.Schemas.Resource.ListResponse}
|
|
]
|
|
|
|
def index(conn, params) do
|
|
list_opts = Pagination.params_to_list_opts(params)
|
|
|
|
with {:ok, resources, metadata} <-
|
|
Resources.list_resources(conn.assigns.subject, list_opts) do
|
|
render(conn, :index, resources: resources, metadata: metadata)
|
|
end
|
|
end
|
|
|
|
operation :show,
|
|
summary: "Show Resource",
|
|
parameters: [
|
|
id: [
|
|
in: :path,
|
|
description: "Resource ID",
|
|
type: :string,
|
|
example: "00000000-0000-0000-0000-000000000000"
|
|
]
|
|
],
|
|
responses: [
|
|
ok: {"Resource Response", "application/json", API.Schemas.Resource.Response}
|
|
]
|
|
|
|
def show(conn, %{"id" => id}) do
|
|
with {:ok, resource} <-
|
|
Resources.fetch_resource_by_id_or_persistent_id(id, conn.assigns.subject) do
|
|
render(conn, :show, resource: resource)
|
|
end
|
|
end
|
|
|
|
operation :create,
|
|
summary: "Create Resource",
|
|
parameters: [],
|
|
request_body:
|
|
{"Resource Attributes", "application/json", API.Schemas.Resource.Request, required: true},
|
|
responses: [
|
|
ok: {"Resource Response", "application/json", API.Schemas.Resource.Response}
|
|
]
|
|
|
|
def create(conn, %{"resource" => params}) do
|
|
attrs = set_param_defaults(params)
|
|
|
|
with {:ok, resource} <- Resources.create_resource(attrs, conn.assigns.subject) do
|
|
conn
|
|
|> put_status(:created)
|
|
|> put_resp_header("location", ~p"/resources/#{resource}")
|
|
|> render(:show, resource: resource)
|
|
end
|
|
end
|
|
|
|
def create(_conn, _params) do
|
|
{:error, :bad_request}
|
|
end
|
|
|
|
operation :update,
|
|
summary: "Update Resource",
|
|
parameters: [
|
|
id: [
|
|
in: :path,
|
|
description: "Resource ID",
|
|
type: :string,
|
|
example: "00000000-0000-0000-0000-000000000000"
|
|
]
|
|
],
|
|
request_body:
|
|
{"Resource Attributes", "application/json", API.Schemas.Resource.Request, required: true},
|
|
responses: [
|
|
ok: {"Resource Response", "application/json", API.Schemas.Resource.Response}
|
|
]
|
|
|
|
def update(conn, %{"id" => id, "resource" => params}) do
|
|
subject = conn.assigns.subject
|
|
attrs = set_param_defaults(params)
|
|
|
|
with {:ok, resource} <- Resources.fetch_active_resource_by_id_or_persistent_id(id, subject) do
|
|
case Resources.update_resource(resource, attrs, subject) do
|
|
{:updated, updated_resource} ->
|
|
render(conn, :show, resource: updated_resource)
|
|
|
|
{:error, reason} ->
|
|
{:error, reason}
|
|
end
|
|
end
|
|
end
|
|
|
|
def update(_conn, _params) do
|
|
{:error, :bad_request}
|
|
end
|
|
|
|
operation :delete,
|
|
summary: "Delete Resource",
|
|
parameters: [
|
|
id: [
|
|
in: :path,
|
|
description: "Resource ID",
|
|
type: :string,
|
|
example: "00000000-0000-0000-0000-000000000000"
|
|
]
|
|
],
|
|
responses: [
|
|
ok: {"Resource Response", "application/json", API.Schemas.Resource.Response}
|
|
]
|
|
|
|
def delete(conn, %{"id" => id}) do
|
|
subject = conn.assigns.subject
|
|
|
|
with {:ok, resource} <- Resources.fetch_resource_by_id_or_persistent_id(id, subject),
|
|
{:ok, resource} <- Resources.delete_resource(resource, subject) do
|
|
render(conn, :show, resource: resource)
|
|
end
|
|
end
|
|
|
|
defp set_param_defaults(params) do
|
|
Map.put_new(params, "filters", %{})
|
|
end
|
|
end
|