From 9489838a5fbdb98309b0b0738005f1e55a8739c6 Mon Sep 17 00:00:00 2001 From: Kirill Solomko Date: Thu, 10 Aug 2017 17:04:57 +0200 Subject: [PATCH] TT#18850 add lua ngcp.api_client * enables internal provisioning requests to the NGCP API Change-Id: I84cff201d573d5a2ce65c56b4ceb8657aded4769 --- debian/control | 4 ++ ngcp/api_client.lua | 99 +++++++++++++++++++++++++++++++++++++++ t/Dockerfile | 2 +- tests/ngcp_api_client.lua | 89 +++++++++++++++++++++++++++++++++++ 4 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 ngcp/api_client.lua create mode 100644 tests/ngcp_api_client.lua diff --git a/debian/control b/debian/control index e9545dd..b5052b2 100644 --- a/debian/control +++ b/debian/control @@ -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, diff --git a/ngcp/api_client.lua b/ngcp/api_client.lua new file mode 100644 index 0000000..ee55da7 --- /dev/null +++ b/ngcp/api_client.lua @@ -0,0 +1,99 @@ +-- +-- Copyright 2017 SipWise Team +-- +-- 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 . +-- . +-- 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 diff --git a/t/Dockerfile b/t/Dockerfile index 985c683..d0767a0 100644 --- a/t/Dockerfile +++ b/t/Dockerfile @@ -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 diff --git a/tests/ngcp_api_client.lua b/tests/ngcp_api_client.lua new file mode 100644 index 0000000..466145b --- /dev/null +++ b/tests/ngcp_api_client.lua @@ -0,0 +1,89 @@ +-- +-- Copyright 2014 SipWise Team +-- +-- 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 . +-- . +-- 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