| -- |
| -- 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 schema_plugin = require("apisix.admin.plugins").stream_check_schema |
| local tostring = tostring |
| |
| |
| local _M = { |
| version = 0.1, |
| } |
| |
| |
| local function check_conf(id, conf, need_id) |
| if not conf then |
| return nil, {error_msg = "missing configurations"} |
| end |
| |
| id = id or conf.id |
| if need_id and not id then |
| return nil, {error_msg = "missing stream route id"} |
| end |
| |
| if not need_id and id then |
| return nil, {error_msg = "wrong stream route id, do not need it"} |
| end |
| |
| if need_id and conf.id and tostring(conf.id) ~= tostring(id) then |
| return nil, {error_msg = "wrong stream route id"} |
| end |
| |
| conf.id = id |
| |
| core.log.info("schema: ", core.json.delay_encode(core.schema.stream_route)) |
| core.log.info("conf : ", core.json.delay_encode(conf)) |
| local ok, err = core.schema.check(core.schema.stream_route, conf) |
| if not ok then |
| return nil, {error_msg = "invalid configuration: " .. err} |
| end |
| |
| local upstream_id = conf.upstream_id |
| if upstream_id then |
| local key = "/upstreams/" .. upstream_id |
| local res, err = core.etcd.get(key) |
| if not res then |
| return nil, {error_msg = "failed to fetch upstream info by " |
| .. "upstream id [" .. upstream_id .. "]: " |
| .. err} |
| end |
| |
| if res.status ~= 200 then |
| return nil, {error_msg = "failed to fetch upstream info by " |
| .. "upstream id [" .. upstream_id .. "], " |
| .. "response code: " .. res.status} |
| end |
| end |
| |
| if conf.plugins then |
| local ok, err = schema_plugin(conf.plugins) |
| if not ok then |
| return nil, {error_msg = err} |
| end |
| end |
| |
| return need_id and id or true |
| end |
| |
| |
| function _M.put(id, conf) |
| local id, err = check_conf(id, conf, true) |
| if not id then |
| return 400, err |
| 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) |
| return 500, {error_msg = err} |
| end |
| |
| return res.status, res.body |
| end |
| |
| |
| function _M.get(id) |
| local key = "/stream_routes" |
| 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 stream route[", key, "]: ", err) |
| return 500, {error_msg = err} |
| end |
| |
| return res.status, res.body |
| end |
| |
| |
| function _M.post(id, conf) |
| local id, err = check_conf(id, conf, false) |
| if not id then |
| return 400, err |
| 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) |
| return 500, {error_msg = err} |
| end |
| |
| return res.status, res.body |
| end |
| |
| |
| function _M.delete(id) |
| if not id then |
| return 400, {error_msg = "missing stream route id"} |
| end |
| |
| local key = "/stream_routes/" .. id |
| -- core.log.info("key: ", key) |
| local res, err = core.etcd.delete(key) |
| if not res then |
| core.log.error("failed to delete stream route[", key, "]: ", err) |
| return 500, {error_msg = err} |
| end |
| |
| return res.status, res.body |
| end |
| |
| |
| return _M |