Skip to content

Commit

Permalink
feat: add management fields for stream_route and proto in schema (#3966)
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-chen authored and spacewander committed Apr 2, 2021
1 parent 7886a4e commit f1fe04b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
10 changes: 10 additions & 0 deletions apisix/admin/proto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
local type = type
local ipairs = ipairs
local core = require("apisix.core")
local utils = require("apisix.admin.utils")
local get_routes = require("apisix.router").http_routes
local get_services = require("apisix.http.service").services
local tostring = tostring
Expand Down Expand Up @@ -63,6 +64,12 @@ function _M.put(id, conf)
end

local key = "/proto/" .. id

local ok, err = utils.inject_conf_with_prev_conf("proto", key, conf)
if not ok then
return 500, {error_msg = err}
end

local res, err = core.etcd.set(key, conf)
if not res then
core.log.error("failed to put proto[", key, "]: ", err)
Expand Down Expand Up @@ -96,6 +103,9 @@ function _M.post(id, conf)
end

local key = "/proto"

utils.inject_timestamp(conf)

-- core.log.info("key: ", key)
local res, err = core.etcd.push("/proto", conf)
if not res then
Expand Down
10 changes: 10 additions & 0 deletions apisix/admin/stream_routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-- limitations under the License.
--
local core = require("apisix.core")
local utils = require("apisix.admin.utils")
local schema_plugin = require("apisix.admin.plugins").stream_check_schema
local tostring = tostring

Expand Down Expand Up @@ -86,6 +87,12 @@ function _M.put(id, conf)
end

local key = "/stream_routes/" .. id

local ok, err = utils.inject_conf_with_prev_conf("stream_routes", key, conf)
if not ok then
return 500, {error_msg = err}
end

local res, err = core.etcd.set(key, conf)
if not res then
core.log.error("failed to put stream route[", key, "]: ", err)
Expand Down Expand Up @@ -119,6 +126,9 @@ function _M.post(id, conf)
end

local key = "/stream_routes"

utils.inject_timestamp(conf)

local res, err = core.etcd.push("/stream_routes", conf)
if not res then
core.log.error("failed to post stream route[", key, "]: ", err)
Expand Down
7 changes: 7 additions & 0 deletions apisix/schema_def.lua
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ _M.ssl = {
_M.proto = {
type = "object",
properties = {
id = id_schema,
desc = desc_def,
create_time = timestamp_def,
update_time = timestamp_def,
content = {
type = "string", minLength = 1, maxLength = 1024*1024
}
Expand All @@ -690,6 +694,9 @@ _M.stream_route = {
type = "object",
properties = {
id = id_schema,
desc = desc_def,
create_time = timestamp_def,
update_time = timestamp_def,
remote_addr = remote_addr_def,
server_addr = {
description = "server IP",
Expand Down
20 changes: 20 additions & 0 deletions t/admin/schema.t
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,23 @@ GET /apisix/admin/schema/global_rule
qr/("update_time":\{"type":"integer"\}.*"create_time":\{"type":"integer"\}|"create_time":\{"type":"integer"\}.*"update_time":\{"type":"integer"\})/
--- no_error_log
[error]
=== TEST 16: get proto schema to check if it contains `create_time` and `update_time`
--- request
GET /apisix/admin/schema/proto
--- response_body eval
qr/("update_time":\{"type":"integer"\}.*"create_time":\{"type":"integer"\}|"create_time":\{"type":"integer"\}.*"update_time":\{"type":"integer"\})/
--- no_error_log
[error]
=== TEST 17: get stream_route schema to check if it contains `create_time` and `update_time`
--- request
GET /apisix/admin/schema/stream_route
--- response_body eval
qr/("update_time":\{"type":"integer"\}.*"create_time":\{"type":"integer"\}|"create_time":\{"type":"integer"\}.*"update_time":\{"type":"integer"\})/
--- no_error_log
[error]
22 changes: 22 additions & 0 deletions t/admin/stream-routes.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ __DATA__
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local etcd = require("apisix.core.etcd")
local code, body = t('/apisix/admin/stream_routes/1',
ngx.HTTP_PUT,
[[{
"remote_addr": "127.0.0.1",
"desc": "test-desc",
"upstream": {
"nodes": {
"127.0.0.1:8080": 1
Expand All @@ -47,6 +49,7 @@ __DATA__
"node": {
"value": {
"remote_addr": "127.0.0.1",
"desc": "test-desc",
"upstream": {
"nodes": {
"127.0.0.1:8080": 1
Expand All @@ -63,6 +66,13 @@ __DATA__
ngx.status = code
ngx.say(body)
local res = assert(etcd.get('/stream_routes/1'))
local create_time = res.body.node.value.create_time
assert(create_time ~= nil, "create_time is nil")
local update_time = res.body.node.value.update_time
assert(update_time ~= nil, "update_time is nil")
}
}
--- request
Expand Down Expand Up @@ -142,6 +152,7 @@ GET /t
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local etcd = require("apisix.core.etcd")
local code, message, res = t('/apisix/admin/stream_routes',
ngx.HTTP_POST,
[[{
Expand Down Expand Up @@ -180,6 +191,13 @@ GET /t
ngx.say("[push] code: ", code, " message: ", message)
local id = string.sub(res.node.key, #"/apisix/stream_routes/" + 1)
local ret = assert(etcd.get('/stream_routes/' .. id))
local create_time = ret.body.node.value.create_time
assert(create_time ~= nil, "create_time is nil")
local update_time = ret.body.node.value.update_time
assert(update_time ~= nil, "update_time is nil")
code, message = t('/apisix/admin/stream_routes/' .. id,
ngx.HTTP_DELETE,
nil,
Expand Down Expand Up @@ -497,7 +515,11 @@ GET /t
res = json.decode(res)
assert(res.count ~= nil)
assert(res.node.value.create_time ~= nil)
assert(res.node.value.update_time ~= nil)
res.count = nil
res.node.value.create_time = nil
res.node.value.update_time = nil
ngx.say(json.encode(res))
}
}
Expand Down
8 changes: 8 additions & 0 deletions t/plugin/grpc-transcode.t
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ __DATA__
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local etcd = require("apisix.core.etcd")
local code, body = t('/apisix/admin/proto/1',
ngx.HTTP_PUT,
[[{
Expand All @@ -62,6 +63,13 @@ __DATA__
ngx.status = code
end
ngx.say(body)
local res = assert(etcd.get('/proto/1'))
local create_time = res.body.node.value.create_time
assert(create_time ~= nil, "create_time is nil")
local update_time = res.body.node.value.update_time
assert(update_time ~= nil, "update_time is nil")
}
}
--- request
Expand Down

0 comments on commit f1fe04b

Please sign in to comment.