mirror of
https://github.com/outbackdingo/firezone.git
synced 2026-01-27 18:18:55 +00:00
Why: * In order to manage a large number of Firezone Sites, Resources, Policies, etc... a REST API is needed as clicking through the UI is too time consuming, as well as prone to error. By providing a REST API Firezone customers will be able to manage things within their Firezone accounts with code.
120 lines
3.2 KiB
Elixir
120 lines
3.2 KiB
Elixir
defmodule API.PolicyController do
|
|
use API, :controller
|
|
use OpenApiSpex.ControllerSpecs
|
|
alias API.Pagination
|
|
alias Domain.Policies
|
|
|
|
action_fallback API.FallbackController
|
|
|
|
tags ["Policies"]
|
|
|
|
operation :index,
|
|
summary: "List Policies",
|
|
parameters: [
|
|
limit: [in: :query, description: "Limit Policies returned", type: :integer, example: 10],
|
|
page_cursor: [in: :query, description: "Next/Prev page cursor", type: :string]
|
|
],
|
|
responses: [
|
|
ok: {"Policy Response", "application/json", API.Schemas.Policy.ListResponse}
|
|
]
|
|
|
|
# List Policies
|
|
def index(conn, params) do
|
|
list_opts = Pagination.params_to_list_opts(params)
|
|
|
|
with {:ok, policies, metadata} <- Policies.list_policies(conn.assigns.subject, list_opts) do
|
|
render(conn, :index, policies: policies, metadata: metadata)
|
|
end
|
|
end
|
|
|
|
operation :show,
|
|
summary: "Show Policy",
|
|
parameters: [
|
|
id: [
|
|
in: :path,
|
|
description: "Policy ID",
|
|
type: :string,
|
|
example: "00000000-0000-0000-0000-000000000000"
|
|
]
|
|
],
|
|
responses: [
|
|
ok: {"Policy Response", "application/json", API.Schemas.Policy.Response}
|
|
]
|
|
|
|
# Show a specific Policy
|
|
def show(conn, %{"id" => id}) do
|
|
with {:ok, policy} <- Policies.fetch_policy_by_id(id, conn.assigns.subject) do
|
|
render(conn, :show, policy: policy)
|
|
end
|
|
end
|
|
|
|
operation :create,
|
|
summary: "Create Policy",
|
|
parameters: [],
|
|
request_body:
|
|
{"Policy Attributes", "application/json", API.Schemas.Policy.Request, required: true},
|
|
responses: [
|
|
ok: {"Policy Response", "application/json", API.Schemas.Policy.Response}
|
|
]
|
|
|
|
# Create a new Policy
|
|
def create(conn, %{"policy" => params}) do
|
|
with {:ok, policy} <- Policies.create_policy(params, conn.assigns.subject) do
|
|
conn
|
|
|> put_status(:created)
|
|
|> put_resp_header("location", ~p"/policies/#{policy}")
|
|
|> render(:show, policy: policy)
|
|
end
|
|
end
|
|
|
|
def create(_conn, _params) do
|
|
{:error, :bad_request}
|
|
end
|
|
|
|
operation :update,
|
|
summary: "Update a Policy",
|
|
request_body:
|
|
{"Policy Attributes", "application/json", API.Schemas.Policy.Request, required: true},
|
|
responses: [
|
|
ok: {"Policy Response", "application/json", API.Schemas.Policy.Response}
|
|
]
|
|
|
|
# Update a Policy
|
|
def update(conn, %{"id" => id, "policy" => params}) do
|
|
subject = conn.assigns.subject
|
|
|
|
with {:ok, policy} <- Policies.fetch_policy_by_id(id, subject),
|
|
{:ok, policy} <- Policies.update_policy(policy, params, subject) do
|
|
render(conn, :show, policy: policy)
|
|
end
|
|
end
|
|
|
|
def update(_conn, _params) do
|
|
{:error, :bad_request}
|
|
end
|
|
|
|
operation :delete,
|
|
summary: "Delete a Policy",
|
|
parameters: [
|
|
id: [
|
|
in: :path,
|
|
description: "Policy ID",
|
|
type: :string,
|
|
example: "00000000-0000-0000-0000-000000000000"
|
|
]
|
|
],
|
|
responses: [
|
|
ok: {"Policy Response", "application/json", API.Schemas.Policy.Response}
|
|
]
|
|
|
|
# Delete a Policy
|
|
def delete(conn, %{"id" => id}) do
|
|
subject = conn.assigns.subject
|
|
|
|
with {:ok, policy} <- Policies.fetch_policy_by_id(id, subject),
|
|
{:ok, policy} <- Policies.delete_policy(policy, subject) do
|
|
render(conn, :show, policy: policy)
|
|
end
|
|
end
|
|
end
|