-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathconsumers.lua
More file actions
107 lines (83 loc) · 2.73 KB
/
consumers.lua
File metadata and controls
107 lines (83 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
local core = require("apisix.core")
local plugins = require("apisix.admin.plugins")
local plugin = require("apisix.plugin")
local pairs = pairs
local _M = {
version = 0.1,
}
local function check_conf(consumer_name, conf)
-- core.log.error(core.json.encode(conf))
if not conf then
return nil, {error_msg = "missing configurations"}
end
local consumer_name = conf.username or consumer_name
if not consumer_name then
return nil, {error_msg = "missing consumer name"}
end
core.log.info("schema: ", core.json.delay_encode(core.schema.consumer))
core.log.info("conf : ", core.json.delay_encode(conf))
local ok, err = core.schema.check(core.schema.consumer, conf)
if not ok then
return nil, {error_msg = "invalid configuration: " .. err}
end
if conf.plugins then
ok, err = plugins.check_schema(conf.plugins)
if not ok then
return nil, {error_msg = "invalid plugins configuration: " .. err}
end
local has_consumer = false
for name, conf in pairs(conf.plugins) do
local plugin_obj = plugin.get(name)
if plugin_obj.type == 'auth' then
if not has_consumer then
has_consumer = true
else
return nil, {error_msg = "only one auth plugin is allowed"}
end
end
end
end
return consumer_name
end
function _M.put(consumer_name, conf)
local consumer_name, err = check_conf(consumer_name, conf)
if not consumer_name then
return 400, err
end
local key = "/consumers/" .. consumer_name
core.log.info("key: ", key)
local res, err = core.etcd.set(key, conf)
if not res then
core.log.error("failed to put consumer[", key, "]: ", err)
return 500, {error_msg = err}
end
return res.status, res.body
end
function _M.get(consumer_name)
local key = "/consumers"
if consumer_name then
key = key .. "/" .. consumer_name
end
local res, err = core.etcd.get(key)
if not res then
core.log.error("failed to get consumer[", key, "]: ", err)
return 500, {error_msg = err}
end
return res.status, res.body
end
function _M.post(consumer_name, conf)
return 400, {error_msg = "not support `POST` method for consumer"}
end
function _M.delete(consumer_name)
if not consumer_name then
return 400, {error_msg = "missing consumer name"}
end
local key = "/consumers/" .. consumer_name
local res, err = core.etcd.delete(key)
if not res then
core.log.error("failed to delete consumer[", key, "]: ", err)
return 500, {error_msg = err}
end
return res.status, res.body
end
return _M