Alternera.Plugin.Host behaviour (alternera_plugin_sdk v0.9.0)

Copy Markdown View Source

What the plugin SDK asks of the application around it. Resolving a template needs two things only the running pipelines know: the tenant's variables and the methods the tenant's pipelines allow for a request. Reading a body needs the application's in-memory limit, and work on a body loaded whole a claim on the application's memory. A call out of the proxy (Plugin.Provisional.Outbound) goes through the application's forward side, and a saved output's fingerprint (Plugin.Provisional.SavedOutput) is keyed with the application's secret. A rate (Plugin.Provisional.Rate) is counted by the application's counter, and a stamped secret reference is shown unstamped by the application's rule (Outbound.unstamp/1). A response cache step's lookup, single flight and fill (Plugin.Provisional.ResponseCache) are the application's response cache. The credentials a request is sent with (Plugin.Provisional.Credentials) and the signed-in caller an upstream is told of (Plugin.Provisional.Identity) are the application's. A step's settings checked against the flow (Plugin.Provisional.Checks) are checked with the application's own conditions and cache vocabulary. A file a user uploads into a step (Plugin.Provisional.Files) is kept in the application's storage. A map is written as YAML (write_yaml/1) by the application's writer. A resource record is read by its lookup key (resource_get/3) and a resource's action is run (resource_run/5), both optional callbacks, by the application's resources. The SDK asks through this behaviour, so it names no module of the application. The application names the implementation under :host:

config :alternera_plugin_sdk, :host, MyApp.PluginHost

Without one the SDK uses Alternera.Plugin.Test.Host, the stand-in a plugin's own tests run on.

Summary

Callbacks

Functions

The methods the tenant's pipelines allow for request, as an Allow header lists them.

The most bytes of a body the tenant's pipelines inspect (body_inspect_max_bytes), or nil for the application's default. What a step keeps of a body for an error (Plugin.Error.max_body_bytes/1) is bounded by it.

Claims room in the application's memory, in the calling process, for work on input_bytes of bodies and values loaded whole: a JSON path read of a spooled body (Plugin.Location.read/2), a plugin that runs in a heap of its own (the Script sandbox). The application claims factor bytes per byte (Plugin.memory_factor/1; nil for its default) and waits a moment for room. {:error, :busy, message} when it has none now, {:error, :size, message} when the claim could never fit; the claim goes back with release_held_bytes/1.

The step error a refused claim of kind (claim_held_bytes/2) is in phase, as the application answers its own refusals: its type (memory_limit for the application's load, body_too_large for a claim that could never fit), its status and, for a request the application turned away for its load, when to try again (retry_after_ms).

The body a spool of open_body_spool/2 holds, its file closed: inline or a file.

Gives a spool of open_body_spool/2 up: its file and its disk claim go.

The most bytes of a body the application holds in memory at once.

The size over which the application holds a body on disk rather than in memory, and over which a step on it runs in a process of its own under a heap limit and is metered (Provisional.HeapPeak).

A new spool for a body step that writes the body it leaves a piece at a time (the Script step run per record), in the calling process: in memory up to the exchange's spool threshold or 64 KiB, whichever is lower (what a step keeps there is its own claim's to count), past it in a file of the step's own beside the held body's, which the application's disk budget for held bodies claims as it grows. spool is the step's context.body_spool (nil outside a body phase that spools: there is none to open, {:error, message}), name a tag unique to the step's run, which remove_body_spool/2 takes too. The spool is written (write_body_spool/2) and closed (close_body_spool/1) in the process that opened it; the body the close gives is the step's to return as its body, and the application releases it once the exchange no longer needs it. A spool given up is discard_body_spool/1ed.

Removes what is left of a spool open_body_spool(spool, name) opened in a process that ended before it closed or discarded it (a call killed at its time or memory limit), with its disk claim.

text with its tenant variable references resolved against scope, literal applied to the text around them. Raises on a reference that does not resolve.

The record of resource name ("<package>.<resource>", spec 069) whose lookup key is key, in tenant_id: {:ok, record}, the record's public attributes as a map with string keys and JSON-ready values; :not_found when the tenant has no such record; or {:error, reason}: :unknown_resource (no such resource in this build), :no_lookup_key (the resource has no lookup key), :unlicensed (the licence does not include resources) or the reason a read failed, which the application logs. An optional callback: a host without it answers {:error, :unsupported}.

Runs the generic action action of resource name ("<package>.<resource>") in tenant_id with input (argument name to value) on the pipeline's behalf: {:ok, result}, the result JSON-ready (string keys, decimals and dates as text), or {:error, reason}: :unknown_resource, {:unknown_action, open} (no such action open to the API; open lists the ones that are), :unlicensed, {:invalid, message} (the action refused the input or failed, the message says why) or the reason a run failed, which the application logs. Only the generic actions the resource's expose api: lists run here. With dry_run?: true (a preview, a dry run) an action that may write, one not in the resource's read_only_actions, is not run: {:ok, :not_run}. An optional callback: a host without it answers {:error, :unsupported}.

The id of the tenant whose slug is slug: {:ok, id}; {:error, :not_found} when no tenant has it; {:error, :disabled} when the tenant is disabled (its hosts do not start); {:error, :not_started} while the application cannot read its tenants yet (it starts after the plugin packages), no error; or {:error, reason} when the read failed, which the application logs. A plugin host entry names its tenant by slug (Alternera.PluginHosts).

Whether text holds a tenant variable reference, {{NAME}} or {{secret.NAME}}.

Appends bytes to a spool of open_body_spool/2: {:error, :disk_budget, message} when the disk budget has no room for them, {:error, :spool_failed, message} when the file could not be written; either way the spool is spent and its file gone.

map as YAML text, as the application writes YAML: {:ok, text}, or {:error, reason} for a map it cannot write. A Jason.OrderedObject anywhere in map is written with its keys in the order it has.

Callbacks

allowed_method_list(arg1)

@callback allowed_method_list(map() | nil) :: [String.t()]

allowed_methods(map)

@callback allowed_methods(map()) :: String.t()

authorization(map, arg2, arg3)

@callback authorization(map(), String.t() | nil, String.t() | nil) ::
  {:ok,
   %{
     value: String.t(),
     profile: String.t(),
     directives: map(),
     outbound_us: non_neg_integer()
   }}
  | {:token_request, String.t()}
  | {:error, String.t()}
  | {:error, String.t(), non_neg_integer()}

body_inspect_max_bytes(arg1)

@callback body_inspect_max_bytes(String.t() | nil) :: pos_integer() | nil

cache_get(t, term)

@callback cache_get(String.t(), term()) :: {:ok, term()} | :miss

cache_key_reads_body?(term)

@callback cache_key_reads_body?(term()) :: boolean()

cache_put(t, term, term, pos_integer)

@callback cache_put(String.t(), term(), term(), pos_integer()) :: :ok

cache_settings_errors(map)

@callback cache_settings_errors(map()) :: [String.t()]

call_authorization(map, t, keyword)

@callback call_authorization(map(), String.t(), keyword()) ::
  {:ok,
   %{value: String.t(), profile: String.t(), outbound_us: non_neg_integer()}}
  | {:error, String.t()}
  | {:error, String.t(), non_neg_integer()}

caller_claims(map, t, pos_integer)

@callback caller_claims(map(), String.t(), pos_integer()) :: {:ok, map()} | :anonymous

claim_held_bytes(non_neg_integer, arg2)

@callback claim_held_bytes(non_neg_integer(), pos_integer() | nil) ::
  {:ok, term()} | {:error, :busy | :size, String.t()}

claim_refusal(arg1, atom)

@callback claim_refusal(:busy | :size, atom()) :: %{
  :type => String.t(),
  :status => pos_integer(),
  optional(:retry_after_ms) => pos_integer()
}

close_body_spool(term)

@callback close_body_spool(term()) ::
  {:ok, Alternera.Plugin.Body.t()} | {:error, :spool_failed, String.t()}

condition_errors(list, arg2)

@callback condition_errors([term()], :request | :response) :: [String.t()]

discard_body_spool(term)

@callback discard_body_spool(term()) :: :ok

error_map(term)

@callback error_map(term()) :: %{required(String.t()) => term()}

fingerprint_key()

@callback fingerprint_key() :: binary()

in_memory_hold_max_bytes()

@callback in_memory_hold_max_bytes() :: pos_integer()

inline_threshold_bytes()

@callback inline_threshold_bytes() :: non_neg_integer()

max_header_size_bytes()

@callback max_header_size_bytes() :: pos_integer()

open_body_spool(term, t)

@callback open_body_spool(term(), String.t()) :: {:ok, term()} | {:error, String.t()}

outbound_call(map, map, keyword)

@callback outbound_call(map(), map(), keyword()) ::
  {:ok, map()}
  | {:error, String.t(), String.t()}
  | {:error, String.t(), String.t(), map()}

plugin_file_get(arg1, t, t, arg4)

@callback plugin_file_get(String.t() | nil, String.t(), String.t(), :info | :bytes) ::
  {:ok, map() | binary()} | :not_found | {:error, String.t()}

plugin_file_put(t, t, binary, map, keyword)

@callback plugin_file_put(String.t(), String.t(), binary(), map(), keyword()) ::
  {:ok, %{digest: String.t(), size: non_neg_integer(), new?: boolean()}}
  | {:error, :quota, %{used: non_neg_integer(), limit: non_neg_integer()}}
  | {:error, String.t()}

plugin_files_list(t, t)

@callback plugin_files_list(String.t(), String.t()) ::
  {:ok, [map()]} | {:error, String.t()}

plugin_files_reclaim(t)

@callback plugin_files_reclaim(String.t()) ::
  {:ok, %{removed: non_neg_integer(), bytes: non_neg_integer()}}
  | {:error, String.t()}

plugin_files_used(t, t)

@callback plugin_files_used(String.t(), String.t()) ::
  {:ok, non_neg_integer()} | {:error, String.t()}

rate_check(arg1, t, t, map)

@callback rate_check(String.t() | nil, String.t(), String.t(), map()) ::
  :ok | {:refuse, pos_integer()}

read_tenant_vars(t, map)

@callback read_tenant_vars(String.t(), %{plain: [String.t()], secret: [String.t()]}) ::
  {:ok, %{required(String.t()) => String.t()}}
  | {:error, [String.t()]}
  | :unreadable

read_timeout_ms(arg1)

@callback read_timeout_ms(String.t() | nil) :: pos_integer()

recorded_body(map, arg2)

@callback recorded_body(map(), :request | :response) ::
  {:ok, binary()} | {:error, :none} | {:error, String.t()}

release_held_bytes(term)

@callback release_held_bytes(term()) :: :ok

remove_body_spool(term, t)

@callback remove_body_spool(term(), String.t()) :: :ok

resolve_tenant_vars!(t, arg2, keyword, function)

@callback resolve_tenant_vars!(String.t(), map() | nil, keyword(), (String.t() ->
                                                            String.t())) ::
  String.t()

resource_get(t, t, term)

(optional)
@callback resource_get(String.t(), String.t(), term()) ::
  {:ok, map()} | :not_found | {:error, term()}

resource_run(t, t, t, map, keyword)

(optional)
@callback resource_run(String.t(), String.t(), String.t(), map(), keyword()) ::
  {:ok, term()} | {:error, term()}

response_cache_give_up(term)

@callback response_cache_give_up(term()) :: :ok

response_cache_keep(term, map, tuple)

@callback response_cache_keep(term(), map(), {non_neg_integer() | nil, term()}) ::
  {:keep, {module(), term()}, map()} | {:bypass, String.t(), map()}

response_cache_key_template(t)

@callback response_cache_key_template(String.t()) :: term()

response_cache_look_up(map, map, map)

@callback response_cache_look_up(map(), map(), map()) ::
  {:hit, map(), map()}
  | {:fill, term(), map()}
  | {:miss, String.t(), map()}
  | {:bypass, String.t(), map()}

reveal_secret(t, map)

@callback reveal_secret(String.t(), map()) :: {:ok, String.t()} | {:error, String.t()}

saved_body_max_bytes()

@callback saved_body_max_bytes() :: pos_integer()

script_cache_max_entries(arg1)

@callback script_cache_max_entries(String.t() | nil) :: pos_integer()

sign_caller(map, map)

@callback sign_caller(map(), map()) :: {:ok, String.t()} | {:error, String.t()}

store_delete(t, t, t, keyword)

@callback store_delete(String.t(), String.t(), String.t(), keyword()) ::
  {:ok, boolean()} | {:error, atom()}

store_entries(t, t, keyword)

@callback store_entries(String.t(), String.t(), keyword()) ::
  {:ok, [map()], %{truncated: boolean()}} | {:error, atom()}

store_get(t, t, t)

@callback store_get(String.t(), String.t(), String.t()) ::
  {:ok, map()} | :miss | {:error, atom()}

store_invalidate(map, term, map, keyword)

@callback store_invalidate(
  map(),
  term(),
  %{required(String.t()) => String.t()},
  keyword()
) ::
  {:ok, %{deleted: boolean(), key: String.t()}} | :unresolved | {:error, atom()}

store_plain?(term)

@callback store_plain?(term()) :: boolean()

store_put(t, t, t, term, keyword)

@callback store_put(String.t(), String.t(), String.t(), term(), keyword()) ::
  :ok | {:error, atom()}

store_secret?(t, t)

@callback store_secret?(String.t(), String.t()) :: boolean()

store_update(t, t, t, term, keyword)

@callback store_update(String.t(), String.t(), String.t(), term(), keyword()) ::
  {:ok, term(), %{created: boolean()}} | {:error, atom()}

store_valid_name?(term)

@callback store_valid_name?(term()) :: boolean()

tenant_id_for_slug(t)

@callback tenant_id_for_slug(String.t()) ::
  {:ok, String.t()} | {:error, :not_found | :disabled | :not_started | term()}

tenant_var_names(arg1)

@callback tenant_var_names(String.t() | nil) :: %{
  plain: [String.t()],
  secret: [String.t()]
}

tenant_var_references?(t)

@callback tenant_var_references?(String.t()) :: boolean()

tenant_var_value(arg1, t, arg3, keyword)

@callback tenant_var_value(:plain | :secret, String.t(), map() | nil, keyword()) ::
  {:ok, String.t()} | {:error, String.t()}

tenant_var_values(arg1, map)

@callback tenant_var_values(String.t() | nil, %{plain: [String.t()], secret: [String.t()]}) ::
  {%{required(String.t()) => String.t()}, [String.t()]}

unstamp(t)

@callback unstamp(String.t()) :: String.t()

write_body_spool(term, iodata)

@callback write_body_spool(term(), iodata()) ::
  {:ok, term()} | {:error, :disk_budget | :spool_failed, String.t()}

write_yaml(map)

@callback write_yaml(map()) :: {:ok, String.t()} | {:error, String.t()}

Functions

allowed_methods(request)

@spec allowed_methods(map()) :: String.t()

The methods the tenant's pipelines allow for request, as an Allow header lists them.

body_inspect_max_bytes(tenant_id)

@spec body_inspect_max_bytes(String.t() | nil) :: pos_integer() | nil

The most bytes of a body the tenant's pipelines inspect (body_inspect_max_bytes), or nil for the application's default. What a step keeps of a body for an error (Plugin.Error.max_body_bytes/1) is bounded by it.

claim_held_bytes(input_bytes, factor \\ nil)

@spec claim_held_bytes(non_neg_integer(), pos_integer() | nil) ::
  {:ok, term()} | {:error, :busy | :size, String.t()}

Claims room in the application's memory, in the calling process, for work on input_bytes of bodies and values loaded whole: a JSON path read of a spooled body (Plugin.Location.read/2), a plugin that runs in a heap of its own (the Script sandbox). The application claims factor bytes per byte (Plugin.memory_factor/1; nil for its default) and waits a moment for room. {:error, :busy, message} when it has none now, {:error, :size, message} when the claim could never fit; the claim goes back with release_held_bytes/1.

claim_refusal(kind, phase)

@spec claim_refusal(:busy | :size, atom()) :: %{
  :type => String.t(),
  :status => pos_integer(),
  optional(:retry_after_ms) => pos_integer()
}

The step error a refused claim of kind (claim_held_bytes/2) is in phase, as the application answers its own refusals: its type (memory_limit for the application's load, body_too_large for a claim that could never fit), its status and, for a request the application turned away for its load, when to try again (retry_after_ms).

close_body_spool(spool)

@spec close_body_spool(term()) ::
  {:ok, Alternera.Plugin.Body.t()} | {:error, :spool_failed, String.t()}

The body a spool of open_body_spool/2 holds, its file closed: inline or a file.

discard_body_spool(spool)

@spec discard_body_spool(term()) :: :ok

Gives a spool of open_body_spool/2 up: its file and its disk claim go.

in_memory_hold_max_bytes()

@spec in_memory_hold_max_bytes() :: pos_integer()

The most bytes of a body the application holds in memory at once.

inline_threshold_bytes()

@spec inline_threshold_bytes() :: non_neg_integer()

The size over which the application holds a body on disk rather than in memory, and over which a step on it runs in a process of its own under a heap limit and is metered (Provisional.HeapPeak).

open_body_spool(spool, name)

@spec open_body_spool(term(), String.t()) :: {:ok, term()} | {:error, String.t()}

A new spool for a body step that writes the body it leaves a piece at a time (the Script step run per record), in the calling process: in memory up to the exchange's spool threshold or 64 KiB, whichever is lower (what a step keeps there is its own claim's to count), past it in a file of the step's own beside the held body's, which the application's disk budget for held bodies claims as it grows. spool is the step's context.body_spool (nil outside a body phase that spools: there is none to open, {:error, message}), name a tag unique to the step's run, which remove_body_spool/2 takes too. The spool is written (write_body_spool/2) and closed (close_body_spool/1) in the process that opened it; the body the close gives is the step's to return as its body, and the application releases it once the exchange no longer needs it. A spool given up is discard_body_spool/1ed.

release_held_bytes(claim)

@spec release_held_bytes(term()) :: :ok

Gives back a claim of claim_held_bytes/2.

remove_body_spool(spool, name)

@spec remove_body_spool(term(), String.t()) :: :ok

Removes what is left of a spool open_body_spool(spool, name) opened in a process that ended before it closed or discarded it (a call killed at its time or memory limit), with its disk claim.

resolve_tenant_vars!(text, scope, opts, literal)

@spec resolve_tenant_vars!(String.t(), map() | nil, keyword(), (String.t() ->
                                                            String.t())) ::
  String.t()

text with its tenant variable references resolved against scope, literal applied to the text around them. Raises on a reference that does not resolve.

resource_get(tenant_id, name, key)

@spec resource_get(String.t(), String.t(), term()) ::
  {:ok, map()} | :not_found | {:error, term()}

The record of resource name ("<package>.<resource>", spec 069) whose lookup key is key, in tenant_id: {:ok, record}, the record's public attributes as a map with string keys and JSON-ready values; :not_found when the tenant has no such record; or {:error, reason}: :unknown_resource (no such resource in this build), :no_lookup_key (the resource has no lookup key), :unlicensed (the licence does not include resources) or the reason a read failed, which the application logs. An optional callback: a host without it answers {:error, :unsupported}.

resource_run(tenant_id, name, action, input, opts \\ [])

@spec resource_run(String.t(), String.t(), String.t(), map(), keyword()) ::
  {:ok, term()} | {:error, term()}

Runs the generic action action of resource name ("<package>.<resource>") in tenant_id with input (argument name to value) on the pipeline's behalf: {:ok, result}, the result JSON-ready (string keys, decimals and dates as text), or {:error, reason}: :unknown_resource, {:unknown_action, open} (no such action open to the API; open lists the ones that are), :unlicensed, {:invalid, message} (the action refused the input or failed, the message says why) or the reason a run failed, which the application logs. Only the generic actions the resource's expose api: lists run here. With dry_run?: true (a preview, a dry run) an action that may write, one not in the resource's read_only_actions, is not run: {:ok, :not_run}. An optional callback: a host without it answers {:error, :unsupported}.

tenant_id_for_slug(slug)

@spec tenant_id_for_slug(String.t()) ::
  {:ok, String.t()} | {:error, :not_found | :disabled | :not_started | term()}

The id of the tenant whose slug is slug: {:ok, id}; {:error, :not_found} when no tenant has it; {:error, :disabled} when the tenant is disabled (its hosts do not start); {:error, :not_started} while the application cannot read its tenants yet (it starts after the plugin packages), no error; or {:error, reason} when the read failed, which the application logs. A plugin host entry names its tenant by slug (Alternera.PluginHosts).

tenant_var_references?(text)

@spec tenant_var_references?(String.t()) :: boolean()

Whether text holds a tenant variable reference, {{NAME}} or {{secret.NAME}}.

write_body_spool(spool, bytes)

@spec write_body_spool(term(), iodata()) ::
  {:ok, term()} | {:error, :disk_budget | :spool_failed, String.t()}

Appends bytes to a spool of open_body_spool/2: {:error, :disk_budget, message} when the disk budget has no room for them, {:error, :spool_failed, message} when the file could not be written; either way the spool is spent and its file gone.

write_yaml(map)

@spec write_yaml(map()) :: {:ok, String.t()} | {:error, String.t()}

map as YAML text, as the application writes YAML: {:ok, text}, or {:error, reason} for a map it cannot write. A Jason.OrderedObject anywhere in map is written with its keys in the order it has.