Implement new user scripts configsets API

This commit is contained in:
emanuele-f 2019-12-13 17:18:30 +01:00
parent 0ebc64cf71
commit c1cb3b38ca
5 changed files with 242 additions and 0 deletions

View file

@ -0,0 +1,86 @@
--
-- (C) 2019 - ntop.org
--
local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
local json = require("dkjson")
local user_scripts = require("user_scripts")
local action = _GET["action"]
sendHTTPContentTypeHeader('application/json')
if(action == nil) then
traceError(TRACE_ERROR, TRACE_CONSOLE, "Missing 'action' parameter")
return
end
-- ################################################
local result = {}
if(action == "add") then
local name = _GET["confset_name"]
if(name == nil) then
traceError(TRACE_ERROR, TRACE_CONSOLE, "Missing 'confset_name' parameter")
return
end
local confset, err = user_scripts.newConfigset(name)
if(confset == nil) then
result.error = err
end
else
local confid = _GET["confset_id"]
if(confid == nil) then
traceError(TRACE_ERROR, TRACE_CONSOLE, "Missing 'confset_id' parameter")
return
end
if(action == "delete") then
local success, err = user_scripts.deleteConfigset(confid)
if not success then
result.error = err
end
elseif(action == "rename") then
local new_name = _GET["confset_name"]
if(new_name == nil) then
traceError(TRACE_ERROR, TRACE_CONSOLE, "Missing 'newname' parameter")
return
end
local success, err = user_scripts.renameConfigset(confid, new_name)
if not success then
result.error = err
end
elseif(action == "clone") then
local new_name = _GET["confset_name"]
if(new_name == nil) then
traceError(TRACE_ERROR, TRACE_CONSOLE, "Missing 'confset_name' parameter")
return
end
local success, err = user_scripts.cloneConfigset(confid, new_name)
if not success then
result.error = err
end
else
traceError(TRACE_ERROR, TRACE_CONSOLE, "Unknown action '".. action .. "'")
return
end
end
-- ################################################
print(json.encode(result))