Consul API
Consul KV helpers are available under ptool.consul and p.consul.
ptool.consul.connect
Unreleased- Introduced.
ptool.consul.connect(options) opens a Consul connection and returns a
Connection object.
options fields:
address(string, optional): The Consul HTTP address such ashttp://127.0.0.1:8500or127.0.0.1:8500.token(string, optional): The Consul ACL token sent through thex-consul-tokenheader.datacenter(string, optional): Defaultdcquery parameter for requests sent through this connection.timeout_ms(integer, optional): Default request timeout in milliseconds.
Environment fallback:
addressfalls back toCONSUL_HTTP_ADDR.tokenfalls back toCONSUL_HTTP_TOKEN.- Environment fallback uses
ptool's runtime environment view, so values set throughp.os.setenv(...)are also visible toptool.consul.connect(...).
Example:
local consul = ptool.consul.connect({
address = "127.0.0.1:8500",
token = p.os.getenv("CONSUL_HTTP_TOKEN"),
datacenter = "dc1",
timeout_ms = 10_000,
})
Connection
Unreleased- Introduced.
Connection represents an open Consul connection returned by
ptool.consul.connect().
It is implemented as a Lua userdata.
Methods:
conn:get(key[, options])->table | nilconn:put(key, value[, options])->booleanconn:delete(key[, options])->booleanconn:list(prefix[, options])->tableconn:request(options)->Response
KV entry table shape:
key(string): The Consul key.value(string | nil): The decoded raw value as a Lua string.flags(integer): The Consul KV flags value.create_index(integer): The create index.modify_index(integer): The modify index.lock_index(integer): The lock index.session(string | nil): The lock session when present.namespace(string | nil): The Consul Enterprise namespace when present.partition(string | nil): The Consul Enterprise partition when present.
Read options:
consistent(boolean, optional): Adds theconsistentquery flag.stale(boolean, optional): Adds thestalequery flag.wait_index(integer, optional): Adds the blocking-queryindexparameter.wait_time(string, optional): Adds the blocking-querywaitparameter such as"30s"or"5m".
get
Unreleased- Introduced.
Canonical API name: ptool.consul.Connection:get.
conn:get(key[, options]) reads a single Consul KV entry.
key(string, required): The Consul key.options(table, optional): Read options.- Returns:
table | nil.
Behavior:
- Returns
nilwhen the key does not exist. - The returned
valuefield is the decoded raw bytes as a Lua string.
Example:
local consul = ptool.consul.connect({ address = "127.0.0.1:8500" })
local item = consul:get("apps/api/config.json")
if item then
local config = p.json.parse(item.value)
print(item.modify_index, config.port)
end
put
Unreleased- Introduced.
Canonical API name: ptool.consul.Connection:put.
conn:put(key, value[, options]) writes a Consul KV entry.
key(string, required): The Consul key.value(string, required): The raw bytes to store.options(table, optional):flags(integer, optional): Consul KV flags.cas(integer, optional): Check-and-set modify index.
- Returns:
boolean.
Behavior:
- Returns the boolean result produced by Consul.
Example:
local consul = ptool.consul.connect({ address = "127.0.0.1:8500" })
local item = consul:get("apps/api/version")
local ok = consul:put("apps/api/version", "v2\n", {
cas = item and item.modify_index or 0,
})
print(ok)
delete
Unreleased- Introduced.
Canonical API name: ptool.consul.Connection:delete.
conn:delete(key[, options]) deletes a Consul KV entry or subtree.
key(string, required): The Consul key or prefix.options(table, optional):cas(integer, optional): Check-and-set modify index.recurse(boolean, optional): Whentrue, delete the whole subtree.
- Returns:
boolean.
Notes:
casandrecurseare mutually exclusive.
list
Unreleased- Introduced.
Canonical API name: ptool.consul.Connection:list.
conn:list(prefix[, options]) lists Consul KV entries under a prefix.
prefix(string, required): The prefix to list.options(table, optional): Read options.- Returns:
table.
Behavior:
- Returns a dense Lua array of KV entry tables.
- Returns an empty array when the prefix does not exist.
Example:
local consul = ptool.consul.connect({ address = "127.0.0.1:8500" })
local items = consul:list("apps/api/")
for _, item in ipairs(items) do
print(item.key, item.modify_index)
end
request
Unreleased- Introduced.
Canonical API name: ptool.consul.Connection:request.
conn:request(options) sends a raw HTTP request through the current Consul
connection defaults and returns the same Response object shape as
ptool.http.request(...).
options supports the same request fields as ptool.http.request(options),
plus:
path(string, optional): A Consul-relative path such as"/v1/status/leader".url(string, optional): An explicit absolute URL.
Notes:
- Exactly one of
pathorurlis required. - Connection defaults automatically add
x-consul-token,dc, andtimeout_mswhen they are configured and not overridden by the request.
Example:
local consul = ptool.consul.connect({ address = "127.0.0.1:8500" })
local resp = consul:request({
path = "/v1/status/leader",
fail_on_http_error = true,
})
print(resp:text())