spacewander commented on code in PR #8611:
URL: https://github.com/apache/apisix/pull/8611#discussion_r1063037789


##########
apisix/admin/resource.lua:
##########
@@ -0,0 +1,201 @@
+--
+-- 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 utils = require("apisix.admin.utils")
+local setmetatable = setmetatable
+local type = type
+
+
+local _M = {
+    version = 0.2,
+    need_v3_filter = true,
+}
+
+
+local mt = {
+    __index = _M
+}
+
+
+local function get(self, id)
+    local key = "/" .. self.name
+    if id then
+        key = key .. "/" .. id
+    end
+
+    local res, err = core.etcd.get(key, not id)
+    if not res then
+        core.log.error("failed to get " .. self.kind .. "[", key, "] from 
etcd: ", err)

Review Comment:
   The log function will concat the string, no need to do it ourselves



##########
apisix/admin/routes.lua:
##########
@@ -17,13 +17,16 @@
 local expr = require("resty.expr.v1")
 local core = require("apisix.core")
 local apisix_upstream = require("apisix.upstream")
+local resource = require("apisix.admin.resource")
 local schema_plugin = require("apisix.admin.plugins").check_schema
-local utils = require("apisix.admin.utils")
 local tostring = tostring
 local type = type
 local loadstring = loadstring
 
 
+local handler = resource.new("routes", "route")

Review Comment:
   We can pass the check_conf when creating the resource.
   Like 
https://github.com/apache/apisix/blob/030019b5297268b2171df5aa661d84c3c1ab6b92/apisix/upstream.lua#L558
   
   So we can return handler instead of _M directly (note that we need to change 
how we call it in the admin's init.lua)



##########
apisix/admin/routes.lua:
##########
@@ -17,13 +17,16 @@
 local expr = require("resty.expr.v1")
 local core = require("apisix.core")
 local apisix_upstream = require("apisix.upstream")
+local resource = require("apisix.admin.resource")
 local schema_plugin = require("apisix.admin.plugins").check_schema
-local utils = require("apisix.admin.utils")
 local tostring = tostring
 local type = type
 local loadstring = loadstring
 
 
+local handler = resource.new("routes", "route")
+
+
 local _M = {

Review Comment:
   We can remove it?



##########
apisix/admin/resource.lua:
##########
@@ -0,0 +1,201 @@
+--
+-- 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 utils = require("apisix.admin.utils")
+local setmetatable = setmetatable
+local type = type
+
+
+local _M = {
+    version = 0.2,
+    need_v3_filter = true,
+}
+
+
+local mt = {
+    __index = _M
+}
+
+
+local function get(self, id)
+    local key = "/" .. self.name
+    if id then
+        key = key .. "/" .. id
+    end
+
+    local res, err = core.etcd.get(key, not id)
+    if not res then
+        core.log.error("failed to get " .. self.kind .. "[", key, "] from 
etcd: ", err)
+        return 503, {error_msg = err}
+    end
+
+    utils.fix_count(res.body, id)
+    return res.status, res.body
+end
+
+
+local function post(self, check_conf, id, conf, sub_path, args)
+    local id, err = check_conf(id, conf, false)
+    if not id then
+        return 400, err
+    end
+
+    local key = "/" .. self.name
+    utils.inject_timestamp(conf)
+    local res, err = core.etcd.push(key, conf, args.ttl)
+    if not res then
+        core.log.error("failed to post " .. self.kind .. "[", key, "] to etcd: 
", err)
+        return 503, {error_msg = err}
+    end
+
+    return res.status, res.body
+end
+
+
+local function put(self, check_conf, id, conf, sub_path, args)
+    local id, err = check_conf(id, conf, true)
+    if not id then
+        return 400, err
+    end
+
+    local key = "/" .. self.name .. "/" .. id
+
+    local ok, err = utils.inject_conf_with_prev_conf(self.kind, key, conf)
+    if not ok then
+        return 503, {error_msg = err}
+    end
+
+    local res, err = core.etcd.set(key, conf, args.ttl)
+    if not res then
+        core.log.error("failed to put " .. self.kind .. "[", key, "] to etcd: 
", err)
+        return 503, {error_msg = err}
+    end
+
+    return res.status, res.body
+end
+
+
+local function delete(self, id)
+    if not id then
+        return 400, {error_msg = "missing " .. self.kind .. " id"}
+    end
+
+    local key = "/" .. self.name .. "/" .. id
+    local res, err = core.etcd.delete(key)
+    if not res then
+        core.log.error("failed to delete " .. self.kind .. "[", key, "] in 
etcd: ", err)
+        return 503, {error_msg = err}
+    end
+
+    return res.status, res.body
+end
+
+
+local function patch(self, check_conf, id, conf, sub_path, args)
+    if not id then
+        return 400, {error_msg = "missing " .. self.kind .. " id"}
+    end
+
+    if conf == nil then
+        return 400, {error_msg = "missing new configuration"}
+    end
+
+    if not sub_path or sub_path == "" then
+        if type(conf) ~= "table"  then
+            return 400, {error_msg = "invalid configuration"}
+        end
+    end
+
+    local key = "/" .. self.name
+    if id then
+        key = key .. "/" .. id
+    end
+
+    local res_old, err = core.etcd.get(key)
+    if not res_old then
+        core.log.error("failed to get " .. self.kind .. " [", key, "] in etcd: 
", err)
+        return 503, {error_msg = err}
+    end
+
+    if res_old.status ~= 200 then
+        return res_old.status, res_old.body
+    end
+    core.log.info("key: ", key, " old value: ",
+            core.json.delay_encode(res_old, true))

Review Comment:
   Bad indent



##########
apisix/admin/resource.lua:
##########
@@ -0,0 +1,201 @@
+--
+-- 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 utils = require("apisix.admin.utils")
+local setmetatable = setmetatable
+local type = type
+
+
+local _M = {
+    version = 0.2,
+    need_v3_filter = true,
+}
+
+
+local mt = {
+    __index = _M
+}
+
+
+local function get(self, id)
+    local key = "/" .. self.name
+    if id then
+        key = key .. "/" .. id
+    end
+
+    local res, err = core.etcd.get(key, not id)
+    if not res then
+        core.log.error("failed to get " .. self.kind .. "[", key, "] from 
etcd: ", err)
+        return 503, {error_msg = err}
+    end
+
+    utils.fix_count(res.body, id)
+    return res.status, res.body
+end
+
+
+local function post(self, check_conf, id, conf, sub_path, args)
+    local id, err = check_conf(id, conf, false)
+    if not id then
+        return 400, err
+    end
+
+    local key = "/" .. self.name
+    utils.inject_timestamp(conf)
+    local res, err = core.etcd.push(key, conf, args.ttl)
+    if not res then
+        core.log.error("failed to post " .. self.kind .. "[", key, "] to etcd: 
", err)
+        return 503, {error_msg = err}
+    end
+
+    return res.status, res.body
+end
+
+
+local function put(self, check_conf, id, conf, sub_path, args)
+    local id, err = check_conf(id, conf, true)
+    if not id then
+        return 400, err
+    end
+
+    local key = "/" .. self.name .. "/" .. id
+
+    local ok, err = utils.inject_conf_with_prev_conf(self.kind, key, conf)
+    if not ok then
+        return 503, {error_msg = err}
+    end
+
+    local res, err = core.etcd.set(key, conf, args.ttl)
+    if not res then
+        core.log.error("failed to put " .. self.kind .. "[", key, "] to etcd: 
", err)
+        return 503, {error_msg = err}
+    end
+
+    return res.status, res.body
+end
+
+
+local function delete(self, id)
+    if not id then
+        return 400, {error_msg = "missing " .. self.kind .. " id"}
+    end
+
+    local key = "/" .. self.name .. "/" .. id
+    local res, err = core.etcd.delete(key)
+    if not res then
+        core.log.error("failed to delete " .. self.kind .. "[", key, "] in 
etcd: ", err)
+        return 503, {error_msg = err}
+    end
+
+    return res.status, res.body
+end
+
+
+local function patch(self, check_conf, id, conf, sub_path, args)
+    if not id then
+        return 400, {error_msg = "missing " .. self.kind .. " id"}
+    end
+
+    if conf == nil then
+        return 400, {error_msg = "missing new configuration"}
+    end
+
+    if not sub_path or sub_path == "" then
+        if type(conf) ~= "table"  then
+            return 400, {error_msg = "invalid configuration"}
+        end
+    end
+
+    local key = "/" .. self.name
+    if id then
+        key = key .. "/" .. id
+    end
+
+    local res_old, err = core.etcd.get(key)
+    if not res_old then
+        core.log.error("failed to get " .. self.kind .. " [", key, "] in etcd: 
", err)
+        return 503, {error_msg = err}
+    end
+
+    if res_old.status ~= 200 then
+        return res_old.status, res_old.body
+    end
+    core.log.info("key: ", key, " old value: ",
+            core.json.delay_encode(res_old, true))
+
+    local node_value = res_old.body.node.value
+    local modified_index = res_old.body.node.modifiedIndex
+
+    if sub_path and sub_path ~= "" then
+        local code, err, node_val = core.table.patch(node_value, sub_path, 
conf)
+        node_value = node_val
+        if code then
+            return code, err
+        end
+        utils.inject_timestamp(node_value, nil, true)
+    else
+        node_value = core.table.merge(node_value, conf)
+        utils.inject_timestamp(node_value, nil, conf)
+    end
+
+    core.log.info("new conf: ", core.json.delay_encode(node_value, true))
+
+    local id, err = check_conf(id, node_value, true)
+    if not id then
+        return 400, err
+    end
+
+    local res, err = core.etcd.atomic_set(key, node_value, args.ttl, 
modified_index)
+    if not res then
+        core.log.error("failed to set new ".. self.kind .."[", key, "] to 
etcd: ", err)
+        return 503, {error_msg = err}
+    end
+
+    return res.status, res.body
+end
+
+
+function _M.new(name, kind)
+    local obj = setmetatable({
+        name = name,
+        kind = kind
+    }, mt)
+
+    function obj.get(id)

Review Comment:
   We should not define the method in an object. We should define them with 
metatable.



##########
apisix/admin/resource.lua:
##########
@@ -0,0 +1,201 @@
+--
+-- 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 utils = require("apisix.admin.utils")
+local setmetatable = setmetatable
+local type = type
+
+
+local _M = {
+    version = 0.2,

Review Comment:
   We can remove the unused version now



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to