You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/11/17 01:36:42 UTC

[GitHub] [apisix] spacewander commented on a change in pull request #5479: feat(plugin): azure serverless functions

spacewander commented on a change in pull request #5479:
URL: https://github.com/apache/apisix/pull/5479#discussion_r750816376



##########
File path: t/plugin/azure-functions.t
##########
@@ -0,0 +1,226 @@
+#
+# 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 (!defined $block->additional_http_config) {

Review comment:
       We can use `http_config` to inject HTTP config, see https://github.com/apache/apisix/blob/master/t/discovery/consul_kv.t
   
   And I suggest you put `azure-demo` as https://github.com/apache/apisix/blob/2c12b3695302f580a1498333aaafb68da26a3096/t/lib/server.lua#L33
   
   There is no need to introduce a new server.

##########
File path: conf/config-default.yaml
##########
@@ -340,6 +340,7 @@ plugins:                          # plugin list (sorted by priority)
   - response-rewrite               # priority: 899
   #- dubbo-proxy                   # priority: 507
   - grpc-transcode                 # priority: 506
+  - azure-functions                # priority: 505

Review comment:
       Better to put it ahead of serverless-post-function

##########
File path: apisix/plugins/azure-functions.lua
##########
@@ -0,0 +1,104 @@
+--
+-- 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.
+
+local core = require("apisix.core")
+local http = require("resty.http")
+local ngx  = ngx
+local getenv = os.getenv
+local plugin_name = "azure-functions"
+
+local env_key = {
+    API = "AZURE_FUNCTIONS_APIKEY",
+    CLIENT_ID = "AZURE_FUNCTIONS_CLIENTID"
+}
+
+local schema = {
+    type = "object",
+    properties = {
+        function_uri = {type = "string"},
+        authorization = {
+            type = "object",
+            properties = {
+                apikey = {type = "string"},
+                clientid = {type = "string"}
+            }
+        },
+        timeout = {type = "integer", minimum = 1000, default = 3000},
+        ssl_verify = {type = "boolean", default = true},
+        keepalive = {type = "boolean", default = true},
+        keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+        keepalive_pool = {type = "integer", minimum = 1, default = 5}
+    },
+    required = {"function_uri"}
+}
+
+local _M = {
+    version = 0.1,
+    priority = 505,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+function _M.access(conf, ctx)
+    local uri_args = core.request.get_uri_args(ctx)
+    local headers = core.request.headers(ctx) or {}
+    local req_body, _ = core.request.get_body()
+
+
+    -- set authorization headers
+    if conf.authorization then
+        headers["x-functions-key"] = conf.authorization.apikey or ""
+        headers["x-functions-clientid"] = conf.authorization.clientid or ""
+    else
+        headers["x-functions-key"] = getenv(env_key.API)
+        headers["x-functions-clientid"] = getenv(env_key.CLIENT_ID)
+    end
+
+    local params = {
+        method = ngx.req.get_method(),
+        body = req_body,
+        query = uri_args,
+        headers = headers,
+        keepalive = conf.keepalive,
+        ssl_verify = conf.ssl_verify
+    }
+
+    -- Keepalive options
+    if conf.keepalive then
+        params.keepalive_timeout = conf.keepalive_timeout
+        params.keepalive_pool = conf.keepalive_pool
+    end
+
+    local httpc = http.new()
+    httpc:set_timeout(conf.timeout)
+
+    local res, err = httpc:request_uri(conf.function_uri, params)
+
+    if not res or err then
+        return core.response.exit(500, "failed to process azure function, err: " .. err)

Review comment:
       ```suggestion
           core.log.error("failed to process azure function, err: " .. err)
           return 503
   ```
   I think it will be safer not to return an internal error to the client.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org