NFTables.Query (NFTables v0.8.2)
View SourceCommand builders for querying nftables resources.
This module provides pure functions that build nftables JSON commands for read operations. Commands are meant to be piped through NFTables.Local for execution and Decoder for transformation.
Pipeline Architecture
Query.list_tables(family: :inet) # Build command (pure function)
|> NFTables.Local.submit(pid: pid) # Execute & JSON decode
|> Decoder.decode() # Transform to idiomatic ElixirExamples
# List all tables
{:ok, %{tables: tables}} =
Query.list_tables(family: :inet)
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
# List rules in a specific chain
{:ok, %{rules: rules}} =
Query.list_rules("filter", "INPUT")
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
# List entire ruleset
{:ok, %{tables: tables, chains: chains, rules: rules, sets: sets}} =
Query.list_ruleset(family: :inet)
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
# Build command for remote execution
cmd = Query.list_tables(family: :inet)
MyTransport.send_to_node("firewall-1", cmd)
Summary
Functions
Delete elements from a set.
Build a command map to flush ruleset.
Build a command map to list chains.
Build a command map to list rules.
Build a command map to list the entire ruleset.
Build a command map to list set elements.
Build a command map to list sets.
Build a command map to list tables.
Types
Functions
@spec delete_set_elements(pid(), String.t(), String.t(), [String.t()], keyword()) :: :ok | {:error, term()}
Delete elements from a set.
Parameters
pid- NFTables process pidtable- Table nameset_name- Set nameelements- List of element values (strings)opts- Keyword list options::family- Protocol family (default::inet):timeout- Operation timeout in ms (default: 5000)
Example
:ok = NFTables.Query.delete_set_elements(pid, "filter", "blocked_ips", ["192.168.1.100"])
Build a command map to flush ruleset.
Returns a map that can be piped to NFTables.Local.submit/2.
Options
:family- Protocol family (optional, default: flush all families)
Examples
# Flush entire ruleset
Query.flush_ruleset()
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
#=> :ok
# Flush only specific family
Query.flush_ruleset(family: :inet)
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
#=> :ok
Build a command map to list chains.
Returns a map that can be piped to NFTables.Local.submit/2.
Options
:family- Protocol family (optional)
Examples
Query.list_chains(family: :inet)
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
Build a command map to list rules.
Returns a map that can be piped to NFTables.Local.submit/2.
Parameters
opts- Keyword list options::family- Protocol family (default::inet)
Or:
table- Table name (string)chain- Chain name (string)opts- Keyword list options::family- Protocol family (default::inet)
Examples
# List all rules for a family
Query.list_rules(family: :inet)
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
# List rules in a specific chain
Query.list_rules("filter", "INPUT")
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
# With options
Query.list_rules("filter", "INPUT", family: :inet6)
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
Build a command map to list the entire ruleset.
Returns a map that can be piped to NFTables.Local.submit/2.
Options
:family- Protocol family (optional, default: list all families)
Examples
# List entire ruleset
Query.list_ruleset()
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
#=> {:ok, %{
# tables: [...],
# chains: [...],
# rules: [...],
# sets: [...]
# }}
# List ruleset for specific family
Query.list_ruleset(family: :inet)
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
Build a command map to list set elements.
Returns a map that can be piped to NFTables.Local.submit/2.
Parameters
table- Table name (string)set_name- Set name (string)opts- Keyword list options::family- Protocol family (default::inet)
Examples
Query.list_set_elements("filter", "blocklist")
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
Query.list_set_elements("filter", "blocklist", family: :inet6)
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
Build a command map to list sets.
Returns a map that can be piped to NFTables.Local.submit/2.
Options
:family- Protocol family (optional)
Examples
Query.list_sets(family: :inet)
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
Build a command map to list tables.
Returns a map that can be piped to NFTables.Local.submit/2.
Options
:family- Protocol family (optional)
Examples
# List all tables
Query.list_tables()
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()
# List tables for specific family
Query.list_tables(family: :inet)
|> NFTables.Local.submit(pid: pid)
|> Decoder.decode()