Skip to content

Commit

Permalink
feat: allow workflow config without case
Browse files Browse the repository at this point in the history
  • Loading branch information
Revolyssup committed Nov 28, 2024
1 parent a7524c0 commit 0a5ab15
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
18 changes: 11 additions & 7 deletions apisix/plugins/workflow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ local schema = {
}
}
},
required = {"case", "actions"}
required = {"actions"}
}
}
},
Expand Down Expand Up @@ -117,9 +117,11 @@ function _M.check_schema(conf)
end

for idx, rule in ipairs(conf.rules) do
local ok, err = expr.new(rule.case)
if not ok then
return false, "failed to validate the 'case' expression: " .. err
if rule.case then
local ok, err = expr.new(rule.case)
if not ok then
return false, "failed to validate the 'case' expression: " .. err
end
end

local actions = rule.actions
Expand All @@ -143,10 +145,12 @@ end


function _M.access(conf, ctx)
local match_result
for _, rule in ipairs(conf.rules) do
local expr, _ = expr.new(rule.case)
match_result = expr:eval(ctx.var)
local match_result = true
if rule.case then
local expr, _ = expr.new(rule.case)
match_result = expr:eval(ctx.var)
end
if match_result then
-- only one action is currently supported
local action = rule.actions[1]
Expand Down
86 changes: 86 additions & 0 deletions t/plugin/workflow-without-case.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
use t::APISIX 'no_plan';

repeat_each(1);
no_long_string();
no_root_location();
no_shuffle();
add_block_preprocessor(sub {
my ($block) = @_;

if (!$block->request) {
$block->set_value("request", "GET /t");
}
});

run_tests();


__DATA__
=== TEST 1: set plugin
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"workflow": {
"rules": [
{
"actions": [
[
"return",
{
"code": 403
}
]
]
}
]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 2: trigger workflow
--- request
GET /hello
--- error_code: 403

0 comments on commit 0a5ab15

Please sign in to comment.