Skip to content

Commit

Permalink
feat: allow workflow config without case (#11787)
Browse files Browse the repository at this point in the history
  • Loading branch information
Revolyssup authored Nov 29, 2024
1 parent 9f80311 commit ee6a04c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 8 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
3 changes: 2 additions & 1 deletion docs/en/latest/plugins/workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The `workflow` plugin is used to introduce [lua-resty-expr](https://github.com/a

| Name | Type | Required | Default | Valid values | Description |
| ---------------------------- | ------------- | -------- | ------- | ------------ | ------------------------------------------------------------ |
| rules.case | array[array] | True | | | List of variables to match for filtering requests for conditional traffic split. It is in the format `{variable operator value}`. For example, `{"arg_name", "==", "json"}`. The variables here are consistent with NGINX internal variables. For details on supported operators, you can refer to [lua-resty-expr](https://github.com/api7/lua-resty-expr#operator-list). |
| rules.case | array[array] | False | | | List of variables to match for filtering requests for conditional traffic split. It is in the format `{variable operator value}`. For example, `{"arg_name", "==", "json"}`. The variables here are consistent with NGINX internal variables. For details on supported operators, you can refer to [lua-resty-expr](https://github.com/api7/lua-resty-expr#operator-list). |
| rules.actions | array[object] | True | | | The action to be performed when the case matches successfully. Currently, only one element is supported in actions. The first child element of the actions' only element can be `return` or `limit-count`. |

### `actions` Attributes
Expand All @@ -58,6 +58,7 @@ The `workflow` plugin is used to introduce [lua-resty-expr](https://github.com/a
:::note

In `rules`, match `case` in order according to the index of the `rules`, and execute `actions` directly if `case` match.
If `case` is missing, the default behavior is to match.

:::

Expand Down
85 changes: 85 additions & 0 deletions t/plugin/workflow-without-case.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#
# 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 ee6a04c

Please sign in to comment.