Support IP Hash LB policy
[msb/apigateway.git] / openresty-ext / src / assembly / resources / openresty / nginx / luaext / lib / utils / svc_util.lua
1 --[[\r
2 \r
3     Copyright (C) 2017-2018 ZTE, Inc. and others. All rights reserved. (ZTE)\r
4 \r
5     Licensed under the Apache License, Version 2.0 (the "License");\r
6     you may not use this file except in compliance with the License.\r
7     You may obtain a copy of the License at\r
8 \r
9             http://www.apache.org/licenses/LICENSE-2.0\r
10 \r
11     Unless required by applicable law or agreed to in writing, software\r
12     distributed under the License is distributed on an "AS IS" BASIS,\r
13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14     See the License for the specific language governing permissions and\r
15     limitations under the License.\r
16 \r
17 --]]\r
18 \r
19 local _M = {}\r
20 _M._VERSION = '1.0.0'\r
21 \r
22 local msbConf= require('conf.msbinit')\r
23 local svcConf   =  require('conf.svcconf')\r
24 local log_util  =  require('lib.utils.log_util')\r
25 local bit = require("bit")\r
26 \r
27 local log = log_util.log\r
28 local useconsultemplate = msbConf.systemConf.useconsultemplate\r
29 local urlfieldMap = svcConf.urlfieldMap\r
30 local apiRelatedTypes = svcConf.apiRelatedTypes\r
31 \r
32 local SYS_SCENARIO_FLAG = {  -- cos    router\r
33         ["ROUTER"]  = 1,         -- 0      1\r
34         ["COS"]     = 2          -- 1      0\r
35 }\r
36 \r
37 function _M.isactive(svcinfo)\r
38         if svcinfo["status"] == "1" then\r
39                 return true\r
40         else\r
41                 return false\r
42         end\r
43 end\r
44 \r
45 function _M.use_own_upstream(svcinfo)\r
46         if useconsultemplate and svcinfo.spec.useOwnUpstream == "1" then\r
47                 log("useOwnUpstream",true)\r
48                 return true\r
49         else\r
50                 return false\r
51         end\r
52 end\r
53 \r
54 function _M.get_url(svcinfo,svc_type)\r
55         return svcinfo.spec[urlfieldMap[svc_type]]\r
56 end\r
57 \r
58 function _M.get_backend_protocol(svcinfo)\r
59         local svc_enable_ssl = svcinfo.spec["enable_ssl"]\r
60         if svc_enable_ssl then\r
61                 return "https"\r
62         else\r
63                 return "http"\r
64         end\r
65 end\r
66 \r
67 function _M.is_api_related_types(svc_type)\r
68         if(apiRelatedTypes[svc_type]) then\r
69                 return true\r
70         else\r
71                 return false\r
72         end\r
73 end\r
74 \r
75 function _M.get_connect_timeout(svcinfo)\r
76         local connect_timeout = svcinfo.spec["connect_timeout"]\r
77         if connect_timeout then\r
78                 connect_timeout = tonumber(connect_timeout)\r
79                 if connect_timeout and connect_timeout<=0 then \r
80                         ngx.log(ngx.WARN, ngx.var.request_id.." ".."bad connect timeout!Zero and negative timeout values are not allowed.Input value:"..connect_timeout)\r
81                         return nil\r
82                 else\r
83                         return connect_timeout\r
84                 end\r
85         else\r
86                 return nil\r
87         end\r
88 end\r
89 \r
90 function _M.get_send_timeout(svcinfo)\r
91         local send_timeout = svcinfo.spec["send_timeout"]\r
92         if send_timeout then\r
93                 send_timeout = tonumber(send_timeout)\r
94                 if send_timeout and send_timeout<=0 then \r
95                         ngx.log(ngx.WARN, ngx.var.request_id.." ".."bad send timeout!Zero and negative timeout values are not allowed.Input value:"..send_timeout)\r
96                         return nil\r
97                 else\r
98                         return send_timeout\r
99                 end\r
100         else\r
101                 return nil\r
102         end\r
103 end\r
104 \r
105 function _M.get_read_timeout(svcinfo)\r
106         local read_timeout = svcinfo.spec["read_timeout"]\r
107         if read_timeout then\r
108                 read_timeout = tonumber(read_timeout)\r
109                 if read_timeout and read_timeout <= 0 then \r
110                         ngx.log(ngx.WARN, ngx.var.request_id.." ".."bad send timeout!Zero and negative timeout values are not allowed.Input value:"..read_timeout)\r
111                         return nil\r
112                 else\r
113                         return read_timeout\r
114                 end\r
115         else\r
116                 return nil\r
117         end\r
118 end\r
119 \r
120 function _M.enable_refer_match(svcinfo)\r
121         local enable_refer_match = svcinfo.spec["enable_refer_match"]\r
122         --Be compatible with the old service info. If the field is not filled, the refer match is enabled by default.\r
123         if enable_refer_match == nil or enable_refer_match then\r
124                 return true\r
125         else\r
126                 return false\r
127         end\r
128 end\r
129 \r
130 function _M.is_allow_access(system_tag,svcinfo)\r
131         local scenario = svcinfo.spec["scenario"] or 1\r
132         local ok,res = pcall(function() return bit.band(SYS_SCENARIO_FLAG[system_tag], scenario) end)\r
133         if ok and res==SYS_SCENARIO_FLAG[system_tag] then \r
134                 return true\r
135         else\r
136                 return false\r
137         end\r
138 end\r
139 return _M\r