TT#18850 add lua ngcp.api_client

* enables internal provisioning requests to the NGCP API

Change-Id: I84cff201d573d5a2ce65c56b4ceb8657aded4769
changes/46/14846/6
Kirill Solomko 8 years ago
parent c71a2df31c
commit 9489838a5f

4
debian/control vendored

@ -13,6 +13,8 @@ Section: libs
Architecture: any
Depends:
lua-argparse,
lua-cjson,
lua-curl,
lua-sql-mysql,
lua5.1,
ngcp-system-tools-ce | ngcp-system-tools,
@ -29,6 +31,8 @@ Package: lua-ngcp-kamailio-dev
Section: libs
Architecture: any
Depends:
lua-cjson,
lua-curl,
lua-lemock,
lua-logging,
lua-sql-mysql,

@ -0,0 +1,99 @@
--
-- Copyright 2017 SipWise Team <development@sipwise.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This package is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- .
-- On Debian systems, the complete text of the GNU General
-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
--
local NGCPAPIClient = {
__class__ = 'NGCPAPIClient'
}
require('curl')
local utils = require 'ngcp.utils'
local utable = utils.table
_ENV = NGCPAPIClient
-- class NGCPAPIClient
local NGCPAPIClient_MT = { __index = NGCPAPIClient }
NGCPAPIClient_MT.__tostring = function (t)
return string.format("config:%s", utable.tostring(t.config))
end
function NGCPAPIClient.new()
local t = NGCPAPIClient.init();
setmetatable( t, NGCPAPIClient_MT )
return t;
end
function NGCPAPIClient.init()
local t = {
config = {
ip = '127.0.0.1',
port = 1442,
user = 'system',
pass = 'password',
},
c = curl.easy_init(),
j = require 'cjson'
};
return t;
end
function NGCPAPIClient:request(method, request)
local result = {}
local ipport = self.config.ip .. ':' .. self.config.port
local userpass = self.config.user .. ':' .. self.config.pass
local headers = {
'Content-Type: application/json',
'Prefer: return=internal',
'NGCP-UserAgent: NGCP::API::Client'
}
self.c:setopt(curl.OPT_VERBOSE, 0)
self.c:setopt(curl.OPT_SSL_VERIFYHOST, 0)
self.c:setopt(curl.OPT_SSL_VERIFYPEER, 0)
self.c:setopt(curl.OPT_URL, 'https://' .. ipport .. '/api/' .. request)
self.c:setopt(curl.OPT_HTTPAUTH, curl.AUTH_BASIC)
self.c:setopt(curl.OPT_USERPWD, userpass)
self.c:setopt(curl.OPT_WRITEFUNCTION,
function(param,buf)
table.insert(result, param)
return buf
end
)
self.c:setopt(curl.OPT_CUSTOMREQUEST, method)
self.c:setopt(curl.OPT_HTTPHEADER, headers)
local res, msg = self.c:perform()
if curl.close then
self.c:close()
end
local res_data = table.concat(result)
if (res_data == nil or res_data == '') then
return "{}"
end
return self.j.decode(table.concat(result))
end
return NGCPAPIClient

@ -12,7 +12,7 @@ COPY t/sources.list.d/builddeps.list /etc/apt/sources.list.d/
COPY t/sources.list.d/preferences /etc/apt/preferences.d/
RUN apt-get update && apt-get install --assume-yes \
lua5.1 lua-unit lua-lemock lua-logging lua-argparse python-xmlrunner
lua5.1 lua-unit lua-cjson lua-curl lua-lemock lua-logging lua-argparse python-xmlrunner
RUN echo './t/testrunner' >>/root/.bash_history

@ -0,0 +1,89 @@
--
-- Copyright 2014 SipWise Team <development@sipwise.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This package is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- .
-- On Debian systems, the complete text of the GNU General
-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
--
require('luaunit')
local lemock = require('lemock')
local srMock = require 'mocks.sr'
sr = srMock:new()
local mc
-- luacheck: ignore TestNGCPAPIClient
TestNGCPAPIClient = {} --class
function TestNGCPAPIClient:setUp()
mc = lemock.controller()
self.c = mc:mock()
self.j = mc:mock()
local NGCPAPIClient = require 'ngcp.api_client'
self.client = NGCPAPIClient.new()
assertTrue(self.client)
self.client.c = self.c;
self.client.j = self.j;
end
function TestNGCPAPIClient:test_api_request()
local method = 'GET'
local request = 'domains'
local result = {}
local ipport = self.client.config.ip .. ':' .. self.client.config.port
local userpass = self.client.config.user .. ':' .. self.client.config.pass
local headers = {
'Content-Type: application/json',
'Prefer: return=internal',
'NGCP-UserAgent: NGCP::API::Client'
}
self.c:setopt(curl.OPT_VERBOSE, 0) ;mc :returns(true)
self.c:setopt(curl.OPT_SSL_VERIFYHOST, 0) ;mc :returns(true)
self.c:setopt(curl.OPT_SSL_VERIFYPEER, 0) ;mc :returns(true)
self.c:setopt(curl.OPT_URL, 'https://' .. ipport .. '/api/' .. request) ;mc :returns(true)
self.c:setopt(curl.OPT_HTTPAUTH, curl.AUTH_BASIC) ;mc :returns(true)
self.c:setopt(curl.OPT_USERPWD, userpass) ;mc :returns(true)
self.c:setopt(curl.OPT_WRITEFUNCTION, mc.ANYARGS)
self.c:setopt(curl.OPT_CUSTOMREQUEST, method) ;mc :returns(true)
self.c:setopt(curl.OPT_HTTPHEADER, mc.ANYARGS) ;mc :returns(true)
local res, msg = self.c:perform() ;mc :returns(0)
if curl.close then
self.c:close() ;mc :returns(true)
end
mc:replay()
local res = self.client:request(method, request)
mc:verify()
assertTrue(res)
assertIs(self.client.c, self.c)
end
-- class TestNGCAPIClient
--EOF
Loading…
Cancel
Save