-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document the 'experimental.config.utils.schema' module (#4469)
Resolves #4279. Co-authored-by: Pavel Semyonov <[email protected]> Co-authored-by: Elena Shebunyaeva <[email protected]>
- Loading branch information
1 parent
f0695d1
commit f7d76f6
Showing
33 changed files
with
1,831 additions
and
29 deletions.
There are no files selected for viewing
15 changes: 10 additions & 5 deletions
15
doc/code_snippets/snippets/config/instances.enabled/application_role_cfg/greeter.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 31 additions & 7 deletions
38
doc/code_snippets/snippets/config/instances.enabled/application_role_http_api/http-api.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
doc/code_snippets/snippets/config/instances.enabled/config_schema_annotations/config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
groups: | ||
group001: | ||
replicasets: | ||
replicaset001: | ||
instances: | ||
instance001: | ||
roles: [ http_api ] | ||
roles_cfg: | ||
http_api: | ||
host: '127.0.0.1' | ||
port: 8080 | ||
scheme: 'http' |
56 changes: 56 additions & 0 deletions
56
doc/code_snippets/snippets/config/instances.enabled/config_schema_annotations/http_api.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
-- http_api.lua -- | ||
local log = require('log').new("http_api") | ||
local schema = require('experimental.config.utils.schema') | ||
|
||
local function validate_host(host, w) | ||
local host_pattern = "^(%d+)%.(%d+)%.(%d+)%.(%d+)$" | ||
if not host:match(host_pattern) then | ||
w.error("'host' should be a string containing a valid IP address, got %q", host) | ||
end | ||
end | ||
|
||
local function validate_port(port, w) | ||
if port <= 1 or port >= 65535 then | ||
w.error("'port' should be between 1 and 65535, got %d", port) | ||
end | ||
end | ||
|
||
local listen_address_schema = schema.new('listen_address', schema.record({ | ||
scheme = schema.scalar({ | ||
type = 'string', | ||
allowed_values = { 'http', 'https' }, | ||
default = 'http', | ||
}), | ||
host = schema.scalar({ | ||
type = 'string', | ||
validate = validate_host, | ||
default = '127.0.0.1', | ||
}), | ||
port = schema.scalar({ | ||
type = 'integer', | ||
validate = validate_port, | ||
default = 8080, | ||
}), | ||
})) | ||
|
||
local function validate(cfg) | ||
listen_address_schema:validate(cfg) | ||
end | ||
|
||
local function apply(cfg) | ||
local cfg_with_defaults = listen_address_schema:apply_default(cfg) | ||
local scheme = listen_address_schema:get(cfg_with_defaults, 'scheme') | ||
local host = listen_address_schema:get(cfg_with_defaults, 'host') | ||
local port = listen_address_schema:get(cfg_with_defaults, 'port') | ||
log.info("HTTP API endpoint: %s://%s:%d", scheme, host, port) | ||
end | ||
|
||
local function stop() | ||
log.info("The 'http_api' role is stopped") | ||
end | ||
|
||
return { | ||
validate = validate, | ||
apply = apply, | ||
stop = stop, | ||
} |
1 change: 1 addition & 0 deletions
1
doc/code_snippets/snippets/config/instances.enabled/config_schema_annotations/instances.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
instance001: |
51 changes: 51 additions & 0 deletions
51
...ode_snippets/snippets/config/instances.enabled/config_schema_computed_annotations/api.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
-- api.lua -- | ||
local log = require('log').new("api") | ||
local schema = require('experimental.config.utils.schema') | ||
|
||
local listen_address = schema.record({ | ||
scheme = schema.enum({ 'http', 'https' }), | ||
host = schema.scalar({ type = 'string' }), | ||
port = schema.scalar({ type = 'integer' }) | ||
}, { | ||
validate = function(data, w) | ||
local protocol = w.schema.computed.annotations.protocol | ||
if protocol == 'iproto' and data.scheme ~= nil then | ||
w.error("iproto doesn't support 'scheme'") | ||
end | ||
end, | ||
}) | ||
|
||
local http_listen_address_schema = schema.new('http_listen_address', schema.record({ | ||
name = schema.scalar({ type = 'string' }), | ||
listen_address = listen_address, | ||
}, { | ||
protocol = 'http', | ||
})) | ||
|
||
local iproto_listen_address_schema = schema.new('iproto_listen_address', schema.record({ | ||
name = schema.scalar({ type = 'string' }), | ||
listen_address = listen_address, | ||
}, { | ||
protocol = 'iproto', | ||
})) | ||
|
||
local function validate(cfg) | ||
http_listen_address_schema:validate(cfg) | ||
end | ||
|
||
local function apply(cfg) | ||
local scheme = http_listen_address_schema:get(cfg, 'listen_address.scheme') | ||
local host = http_listen_address_schema:get(cfg, 'listen_address.host') | ||
local port = http_listen_address_schema:get(cfg, 'listen_address.port') | ||
log.info("API endpoint: %s://%s:%d", scheme, host, port) | ||
end | ||
|
||
local function stop() | ||
log.info("The 'api' role is stopped") | ||
end | ||
|
||
return { | ||
validate = validate, | ||
apply = apply, | ||
stop = stop, | ||
} |
14 changes: 14 additions & 0 deletions
14
...snippets/snippets/config/instances.enabled/config_schema_computed_annotations/config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
groups: | ||
group001: | ||
replicasets: | ||
replicaset001: | ||
instances: | ||
instance001: | ||
roles: [ api ] | ||
roles_cfg: | ||
api: | ||
name: 'HTTP listen address' | ||
listen_address: | ||
host: '127.0.0.1' | ||
port: 8080 | ||
scheme: 'http' |
1 change: 1 addition & 0 deletions
1
...ippets/snippets/config/instances.enabled/config_schema_computed_annotations/instances.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
instance001: |
12 changes: 12 additions & 0 deletions
12
doc/code_snippets/snippets/config/instances.enabled/config_schema_fromenv/config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
groups: | ||
group001: | ||
replicasets: | ||
replicaset001: | ||
instances: | ||
instance001: | ||
roles: [ http_api ] | ||
roles_cfg: | ||
http_api: | ||
host: '127.0.0.1' | ||
port: 8080 | ||
scheme: 'http' |
43 changes: 43 additions & 0 deletions
43
doc/code_snippets/snippets/config/instances.enabled/config_schema_fromenv/http_api.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
-- http_api.lua -- | ||
local log = require('log').new("http_api") | ||
local schema = require('experimental.config.utils.schema') | ||
|
||
local listen_address_schema = schema.new('listen_address', schema.record({ | ||
scheme = schema.enum({ 'http', 'https' }, { env = 'HTTP_SCHEME' }), | ||
host = schema.scalar({ type = 'string', env = 'HTTP_HOST' }), | ||
port = schema.scalar({ type = 'integer', env = 'HTTP_PORT' }) | ||
})) | ||
|
||
local function collect_env_cfg() | ||
local res = {} | ||
for _, w in listen_address_schema:pairs() do | ||
local env_var = w.schema.env | ||
if env_var ~= nil then | ||
local value = schema.fromenv(env_var, os.getenv(env_var), w.schema) | ||
listen_address_schema:set(res, w.path, value) | ||
end | ||
end | ||
return res | ||
end | ||
|
||
local function validate(cfg) | ||
local env_cfg = collect_env_cfg() | ||
local result_cfg = listen_address_schema:merge(cfg, env_cfg) | ||
listen_address_schema:validate(result_cfg) | ||
end | ||
|
||
local function apply(cfg) | ||
local env_cfg = collect_env_cfg() | ||
local result_cfg = listen_address_schema:merge(cfg, env_cfg) | ||
log.info("HTTP API endpoint: %s://%s:%d", result_cfg.scheme, result_cfg.host, result_cfg.port) | ||
end | ||
|
||
local function stop() | ||
log.info("The 'http_api' role is stopped") | ||
end | ||
|
||
return { | ||
validate = validate, | ||
apply = apply, | ||
stop = stop, | ||
} |
1 change: 1 addition & 0 deletions
1
doc/code_snippets/snippets/config/instances.enabled/config_schema_fromenv/instances.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
instance001: |
12 changes: 12 additions & 0 deletions
12
doc/code_snippets/snippets/config/instances.enabled/config_schema_methods/config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
groups: | ||
group001: | ||
replicasets: | ||
replicaset001: | ||
instances: | ||
instance001: | ||
roles: [ http_api ] | ||
roles_cfg: | ||
http_api: | ||
host: '127.0.0.1' | ||
port: 8080 | ||
scheme: 'http' |
40 changes: 40 additions & 0 deletions
40
doc/code_snippets/snippets/config/instances.enabled/config_schema_methods/http_api.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
-- http_api.lua -- | ||
local log = require('log').new("http_api") | ||
local schema = require('experimental.config.utils.schema') | ||
|
||
local listen_address_schema = schema.new( | ||
"listen_address", | ||
schema.record( | ||
{ | ||
scheme = schema.enum({ "http", "https" }), | ||
host = schema.scalar({ type = "string" }), | ||
port = schema.scalar({ type = "integer" }) | ||
} | ||
), | ||
{ | ||
methods = { | ||
format = function(_self, url) | ||
return string.format("%s://%s:%d", url.scheme, url.host, url.port) | ||
end | ||
} | ||
} | ||
) | ||
|
||
local function validate(cfg) | ||
listen_address_schema:validate(cfg) | ||
end | ||
|
||
local function apply(cfg) | ||
local url_string = listen_address_schema:format(cfg) | ||
log.info("HTTP API endpoint: %s", url_string) | ||
end | ||
|
||
local function stop() | ||
log.info("The 'http_api' role is stopped") | ||
end | ||
|
||
return { | ||
validate = validate, | ||
apply = apply, | ||
stop = stop, | ||
} |
1 change: 1 addition & 0 deletions
1
doc/code_snippets/snippets/config/instances.enabled/config_schema_methods/instances.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
instance001: |
15 changes: 15 additions & 0 deletions
15
doc/code_snippets/snippets/config/instances.enabled/config_schema_nodes_array/config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
groups: | ||
group001: | ||
replicasets: | ||
replicaset001: | ||
instances: | ||
instance001: | ||
roles: [ http-api ] | ||
roles_cfg: | ||
http-api: | ||
- host: '127.0.0.1' | ||
port: 8080 | ||
scheme: 'http' | ||
- host: '127.0.0.1' | ||
port: 8443 | ||
scheme: 'https' |
34 changes: 34 additions & 0 deletions
34
doc/code_snippets/snippets/config/instances.enabled/config_schema_nodes_array/http-api.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
-- http_api.lua -- | ||
local log = require('log').new("http_api") | ||
local schema = require('experimental.config.utils.schema') | ||
|
||
local listen_address_schema = schema.new('listen_address', schema.array({ | ||
items = schema.record({ | ||
scheme = schema.enum({ 'http', 'https' }), | ||
host = schema.scalar({ type = 'string' }), | ||
port = schema.scalar({ type = 'integer' }) | ||
}) | ||
})) | ||
|
||
local function validate(cfg) | ||
listen_address_schema:validate(cfg) | ||
end | ||
|
||
local function apply(cfg) | ||
for _, uri in ipairs(cfg) do | ||
local scheme = uri.scheme | ||
local host = uri.host | ||
local port = uri.port | ||
log.info("HTTP API endpoint: %s://%s:%d", scheme, host, port) | ||
end | ||
end | ||
|
||
local function stop() | ||
log.info("The 'http_api' role is stopped") | ||
end | ||
|
||
return { | ||
validate = validate, | ||
apply = apply, | ||
stop = stop, | ||
} |
1 change: 1 addition & 0 deletions
1
doc/code_snippets/snippets/config/instances.enabled/config_schema_nodes_array/instances.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
instance001: |
Oops, something went wrong.