-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathschema-sync.lua
145 lines (130 loc) · 3.67 KB
/
schema-sync.lua
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
--
-- 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 json = require("json")
-- simulate loading modules to avoid errors that will cause fail to read json schema
local fake_module_list = {
'cjson',
'cjson.safe',
'bit',
'lfs',
'ngx.process',
'ngx.re',
'net.url',
'opentracing.tracer',
'pb',
'prometheus',
'protoc',
'skywalking.tracer',
'resty.cookie',
'resty.core.regex',
'resty.hmac',
'resty.http',
'resty.ipmatcher',
'resty.jit-uuid',
'resty.jwt',
'resty.kafka.producer',
'resty.limit.count',
'resty.limit.conn',
'resty.limit.req',
'resty.logger.socket',
'resty.lock',
'resty.openidc',
'resty.random',
'resty.redis',
'resty.rediscluster',
'resty.signal',
'resty.string',
'apisix.consumer',
'apisix.core.json',
'apisix.core.schema',
'apisix.upstream',
'apisix.utils.log-util',
'apisix.utils.batch-processor',
'apisix.plugin',
'apisix.plugins.skywalking.client',
'apisix.plugins.skywalking.tracer',
'apisix.plugins.zipkin.codec',
'apisix.plugins.zipkin.random_sampler',
'apisix.plugins.zipkin.reporter',
'apisix.timers'
}
for _, name in ipairs(fake_module_list) do
package.loaded[name] = {}
end
local empty_function = function()
end
ngx = {}
ngx.re = {}
ngx.timer = {}
ngx.location = {}
ngx.socket = {}
ngx.thread = {}
ngx.re.gmatch = empty_function
ngx.shared = {
["plugin-api-breaker"] = {}
}
-- additional define for management
local time_def = {
type = "integer",
}
local schema = require("apisix.schema_def")
for _, resource in ipairs({"ssl", "route", "service", "upstream", "consumer"}) do
schema[resource].properties.create_time = time_def
schema[resource].properties.update_time = time_def
end
schema.ssl.properties.validity_start = time_def
schema.ssl.properties.validity_end = time_def
package.loaded["apisix.core"] = {
lrucache = {
new = empty_function
},
schema = schema,
id = {
get = empty_function
},
table = {
insert = empty_function
}
}
function get_plugin_list()
local all = io.popen("ls apisix/plugins");
local list = {};
for filename in all:lines() do
suffix = string.sub(filename, -4)
if suffix == ".lua" then
table.insert(list, string.sub(filename, 1, -5))
end
end
all:close()
return list
end
local schema_all = {}
schema_all.main = schema
schema_all.plugins = {}
local plugins = get_plugin_list()
for idx, plugin_name in pairs(plugins) do
local plugin = require("apisix.plugins." .. plugin_name)
if plugin and type(plugin) == "table" and plugin.schema then
schema_all.plugins[plugin_name]= {
['schema'] = plugin.schema
}
end
if plugin and type(plugin) == "table" and plugin.consumer_schema then
schema_all.plugins[plugin_name]['consumer_schema'] = plugin.consumer_schema
end
end
print(json.encode(schema_all))