Skip to content

Commit

Permalink
Schema validation: samples
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyaksenov committed Aug 29, 2024
1 parent 1c97c28 commit 3647d9c
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 0 deletions.
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'
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 pairs(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,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
instance001:
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'
query_params:
skip: 0
limit: 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- http_api.lua --
log = require('log').new("http_api")
schema = require('experimental.config.utils.schema')

listen_address_schema = schema.new('listen_address', schema.record({
scheme = schema.enum({ 'http', 'https' }),
host = schema.scalar({ type = 'string' }),
port = schema.scalar({ type = 'integer' }),
query_params = schema.map({ key = schema.scalar({ type = 'string' }),
value = schema.scalar({ type = 'integer' }) })
}))

local function validate(cfg)
listen_address_schema:validate(cfg)
end

local function apply(cfg)
test_cfg = cfg
local scheme = listen_address_schema:get(cfg, 'scheme')
local host = listen_address_schema:get(cfg, 'host')
local port = listen_address_schema:get(cfg, 'port')
local query_params = listen_address_schema:get(cfg, 'query_params')
local query_string = ''
for name, value in pairs(query_params) do
query_string = query_string .. name .. '=' .. value .. '&'
end
local query_string_without_amp = string.gsub(query_string, "&$", "")
log.info("HTTP API endpoint: %s://%s:%d?%s", scheme, host, port, query_string_without_amp)
end

local function stop()
log.info("The 'http_api' role is stopped")
end

return {
validate = validate,
apply = apply,
stop = stop,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
instance001:
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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- 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' })
}))

local function validate(cfg)
listen_address_schema:validate(cfg)
end

local function apply(cfg)
local scheme = listen_address_schema:get(cfg, 'scheme')
local host = listen_address_schema:get(cfg, 'host')
local port = listen_address_schema:get(cfg, '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,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
instance001:
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
groups:
group001:
replicasets:
replicaset001:
instances:
instance001:
roles: [ http_api ]
roles_cfg:
http_api: 'http://127.0.0.1:8080'
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- http_api.lua --
local log = require('log').new("http_api")
local schema = require('experimental.config.utils.schema')

local http_api_schema = schema.new('http_api', schema.scalar({ type = 'string' }))

local function validate(cfg)
http_api_schema:validate(cfg)
end

local function apply(cfg)
local http_api_cfg = http_api_schema:get(cfg)
log.info("HTTP API endpoint: %s", http_api_cfg)
end

local function stop()
log.info("The 'http_api' role is stopped")
end

return {
validate = validate,
apply = apply,
stop = stop,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
instance001:

0 comments on commit 3647d9c

Please sign in to comment.