erldns_zone_pipeline behaviour (erldns v13.0.0)
View SourceA pipeline of pipes that the records of a zone go through when the zone is decoded.
A record is an entry/0: a JSON record not yet decoded, or a dns:rr/0. A pipe implements
either or both of two callbacks:
decode_record/3takes one record: the record as the pipeline was given it, and the record as the pipes before it left it. It returns the record, changed or not, or drops it.decode/2takes all the records at once, to relate them to each other, and returns them, changed or not.
The records go one by one through the decode_record/3 of every pipe, in the order of the
pipeline. Then all of them go through the decode/2 of every pipe, in the same order. Both
callbacks can change the options for what comes after them. A record that is neither a JSON object
nor a dns:rr/0 is logged as unsupported and dropped before the pipes. After the last pipe, the
records that aren't a dns:rr/0 are logged as unsupported and dropped too, and the records are
sorted and deduplicated.
Decoding is itself a pipe: erldns_zone_standard turns the JSON records of the types it knows
into dns:rr/0 and leaves the rest. The pipes after it still get each record as it was given, in
JSON, with the keys erldns doesn't use, as the first argument of decode_record/3.
Configuration
The pipeline key of the zones configuration lists the pipes, see erldns_zones.
erldns_zone_context goes first, so that the records it drops aren't decoded, and a pipe that
decodes a custom record type goes after erldns_zone_standard:
{erldns, [
{zones, #{
pipeline => [
erldns_zone_context,
erldns_zone_standard,
sample_custom_zone_pipe,
example_ttl_cap
]
}}
]}Without pipeline, the pipes are erldns_zone_context and erldns_zone_standard.
Types of pipes
- A module that implements this behaviour:
decode_record/3,decode/2or both, and optionallyprepare/1anddeps/0, which work as inerldns_pipeline, andencode/1, see Custom record types. The order thatdeps/0asks for holds among thedecode_record/3callbacks, and among thedecode/2callbacks. - A function of arity 3, called as
decode_record/3, or of arity 2, called asdecode/2.
Custom record types
A pipe placed after erldns_zone_standard gets the JSON records of the types erldns doesn't
know, and can decode them into dns:rr/0. To encode them back to JSON, as
erldns_zones:encode/1,2 and the admin API do, it implements encode/1. The pipes of the
pipeline are tried in order, the first that doesn't return not_implemented encodes the record,
and a record that none encodes is left out.
-module(sample_custom_zone_pipe).
-behaviour(erldns_zone_pipeline).
-include_lib("dns_erlang/include/dns.hrl").
-export([decode_record/3, encode/1]).
-define(DNS_TYPE_SAMPLE, 40000).
decode_record(_, #{~"type" := ~"SAMPLE"} = Json, _Opts) ->
#{~"name" := Name, ~"ttl" := Ttl, ~"data" := #{~"dname" := Dname}} = Json,
#dns_rr{name = Name, type = ?DNS_TYPE_SAMPLE, data = Dname, ttl = Ttl};
decode_record(_, Entry, _Opts) ->
Entry.
encode(#dns_rr{name = Name, type = ?DNS_TYPE_SAMPLE, ttl = Ttl, data = Data}) ->
#{
~"name" => erlang:iolist_to_binary(io_lib:format("~s.", [Name])),
~"type" => ~"SAMPLE",
~"ttl" => Ttl,
~"content" => Data
};
encode(_) ->
not_implemented.Passing on what a record's JSON says
decode/2 gets the records as the pipes left them, mostly decoded into dns:rr/0, which have
no place for the JSON keys erldns doesn't use. A pipe that needs such a key in its decode/2
reads it in its decode_record/3, where the record's JSON is the first argument, and adds what it
needs to its own key of the options, returning {Entry, Opts}. The records after it, and every
decode/2, get the options as it left them. So that the result doesn't depend on the order of
the records, decode_record/3 only adds to its key, and decode/2 acts on it:
-module(example_extra_names).
-behaviour(erldns_zone_pipeline).
-include_lib("dns_erlang/include/dns.hrl").
-export([deps/0, prepare/1, decode_record/3, decode/2]).
deps() ->
#{prerequisites => [erldns_zone_standard]}.
prepare(Opts) ->
Opts#{?MODULE => []}.
decode_record(#{~"extra_names" := Names}, #dns_rr{} = Record, #{?MODULE := Copies} = Opts) ->
{Record, Opts#{?MODULE := [{Record, Names} | Copies]}};
decode_record(_, Entry, _Opts) ->
Entry.
decode(Entries, #{?MODULE := Copies}) ->
Entries ++ [Record#dns_rr{name = Name} || {Record, Names} <- Copies, Name <- Names].Scope
The scope option says whether a pipe sees a whole zone, zone, or a part of one, partial, such
as an RRSet. A pipe passes on the records it doesn't handle, and doesn't assume it sees all the
records of a zone under partial.
Failures
The pipeline doesn't catch exceptions. A pipe that raises, or that returns {error, Reason} or
anything that record_return/0 and return/0 don't list, fails the decode of the zone, so that
a zone is never loaded with a pipe skipped. In this it differs from erldns_pipeline.
Example
A pipe that caps the TTL of the records, once they are decoded:
-module(example_ttl_cap).
-behaviour(erldns_zone_pipeline).
-include_lib("dns_erlang/include/dns.hrl").
-export([deps/0, decode_record/3]).
deps() ->
#{prerequisites => [erldns_zone_standard]}.
decode_record(_, #dns_rr{ttl = Ttl} = Record, _Opts) when Ttl > 86400 ->
Record#dns_rr{ttl = 86400};
decode_record(_, Entry, _Opts) ->
Entry.
Summary
Types
A record going through the pipeline, in JSON or decoded.
A record as it was in the JSON zone.
Options passed to every pipe.
A pipe in the pipeline, see Types of pipes.
A pipeline ready to run: the functions of its pipes, and its options.
The return of decode_record/3.
A function pipe called as decode_record/3.
Whether the records are a whole zone, or a part of one.
Callbacks
Take all the records, after every decode_record/3, and return them. See return/0.
Take a record and return it, changed or not, or drop it, with new options or not. See
record_return/0.
Declare the pipes this one needs before or after it. See erldns_pipeline:deps/0.
Encode a record that this pipe decoded back to JSON, or return not_implemented. See
Custom record types.
Prepare the pipe when the pipeline is prepared, or return disabled to leave the pipe out.
Types
-type entry() :: dns:rr() | json_record().
A record going through the pipeline, in JSON or decoded.
-type json_record() :: #{binary() => json:decode_value()}.
A record as it was in the JSON zone.
-type opts() :: #{zone_name := dns:dname(), version := erldns_zones:version(), scope := scope(), atom() => dynamic()}.
Options passed to every pipe.
The zone's name and version are as the zone was given. Pipes add their own keys in prepare/1,
and both callbacks can change them for what comes after them.
A pipe in the pipeline, see Types of pipes.
A function pipe called as decode/2.
-opaque prepared()
A pipeline ready to run: the functions of its pipes, and its options.
The return of decode_record/3.
- The record, changed or not, for the next pipe.
drop, to leave the record out.{Entry, Opts}or{drop, Opts}, the same with new options for what comes after.{error, Reason}, to fail the decode of the zone.
-type recordfun() :: fun((entry(), entry(), opts()) -> record_return()).
A function pipe called as decode_record/3.
The return of decode/2.
- The records, for the next pipe.
- The records and new options, for the next pipes.
{error, Reason}, to fail the decode of the zone.
-type scope() :: zone | partial.
Whether the records are a whole zone, or a part of one.
Callbacks
Take all the records, after every decode_record/3, and return them. See return/0.
-callback decode_record(Source :: entry(), Entry :: entry(), opts()) -> record_return().
Take a record and return it, changed or not, or drop it, with new options or not. See
record_return/0.
Source is the record as the pipeline was given it, and Entry the record as the pipes before this
one left it.
-callback deps() -> erldns_pipeline:deps().
Declare the pipes this one needs before or after it. See erldns_pipeline:deps/0.
-callback encode(dns:rr()) -> not_implemented | json:encode_value().
Encode a record that this pipe decoded back to JSON, or return not_implemented. See
Custom record types.
Prepare the pipe when the pipeline is prepared, or return disabled to leave the pipe out.
A pipe adds the keys it uses to the options here, so that every decode starts with them and its
callbacks can update them with :=.