add the driver lua
[msb/apigateway.git] / msb-core / openresty-ext / src / assembly / resources / openresty / nginx / luaext / plugins / driver_manager.lua
1 --[[
2
3     Copyright 2016 2015-2016 OPEN-O. and others. All rights reserved.
4
5     Licensed under the Apache License, Version 2.0 (the "License");
6     you may not use this file except in compliance with the License.
7     You may obtain a copy of the License at
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11     Unless required by applicable law or agreed to in writing, software
12     distributed under the License is distributed on an "AS IS" BASIS,
13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14     See the License for the specific language governing permissions and
15     limitations under the License.
16
17 ]]
18 local _M = {}
19 _M._VERSION = '1.0.0'
20 local _HEADER = "X-Driver-Parameter"
21
22 --extract driver header if present in request
23 local function get_driver_header()
24   local header = ""
25   local driver_header = ngx.req.get_headers()[_HEADER]  
26   if (driver_header ~= nil)
27   then
28     header = driver_header
29   end
30   return header
31 end
32
33 -- generate query url
34 local function get_query_url(x_driver_header)
35   local drivermgr_uri = '/openoapi/drivermgr/v1/drivers'
36   local url = drivermgr_uri.."?".._HEADER.."="..tostring(ngx.escape_uri(x_driver_header)).."&service_url="..ngx.var.uri
37   return url
38 end
39
40 -- generate driver url
41 local function get_driver_url(driver_header)
42   local cjson = require "cjson"
43   local query_url = get_query_url(driver_header)
44   local res = ngx.location.capture(query_url, { method = ngx.HTTP_GET})
45   ngx.log (ngx.ERR, "Driver manager resp url : ", tostring(res.body))
46   if (res.status == 200 and res.body ~= nil and res.body ~= '')
47   then
48     return tostring(cjson.new().decode(res.body).url)
49   else
50     return ''
51   end
52 end
53
54 -- get headers
55 local function get_headers()
56   local headers = {}
57   local h = ngx.req.get_headers()
58   for k, value in pairs(h)
59   do
60      headers[k] = value
61   end
62   return headers
63 end
64
65 local function get_body_params()
66   ngx.req.read_body()
67   local actual_body = ""  
68   local body_param = ngx.req.get_body_data()
69   if(body_param ~= nil)
70   then
71     actual_body = tostring(body_param)
72   end
73   return actual_body
74 end
75
76 function _M.access()
77     ngx.log(ngx.ERR, "DRIVER MANAGER LUA", "***********************")
78
79     -- extract X-Driver-Parameter header param
80     local driver_header = get_driver_header()
81     ngx.log(ngx.ERR, "X-Driver-Parameter: ", driver_header)
82
83
84     -- ignore driver redirection if not driver manager request.
85     if (driver_header ~= "")
86     then 
87       
88       local driver_url = get_driver_url(driver_header)
89       ngx.log (ngx.ERR, "Driver manager URl:: ", driver_url)
90      
91       local http = require "resty.http"
92       local actual_headers = get_headers()
93       local actual_body = get_body_params()
94
95       ngx.log(ngx.ERR, "HTTP request to driver... ", " Request to driver manager")
96       local res, err = http.new():request_uri(driver_url, {
97             method = ngx.req.get_method(),
98             body = actual_body,
99             headers = actual_headers
100           })
101
102           if not res then
103             ngx.say("Request to driver failed : ", err)
104             return
105           end
106      ngx.log(ngx.ERR, "Response from driver : ", tostring(res.body))
107      ngx.say(res.body)
108
109     else
110       ngx.log(ngx.ERR, "X-Driver-Parameter not present", " Redirect to same url")
111     end
112 end
113
114 return _M