From a6ffb62bf2e0a0e4288e21a4e86e89274d85ebf3 Mon Sep 17 00:00:00 2001 From: Simone Mainardi Date: Fri, 10 Jul 2020 16:32:49 +0200 Subject: [PATCH] Implements unbind of configsets across all pools instances --- scripts/lua/edit_scripts_configsets.lua | 7 ++- scripts/lua/modules/pools/base_pools.lua | 27 +++++++++ scripts/lua/modules/pools/pools_lua_utils.lua | 60 +++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 scripts/lua/modules/pools/pools_lua_utils.lua diff --git a/scripts/lua/edit_scripts_configsets.lua b/scripts/lua/edit_scripts_configsets.lua index e7ec2debda..54c780b806 100644 --- a/scripts/lua/edit_scripts_configsets.lua +++ b/scripts/lua/edit_scripts_configsets.lua @@ -4,6 +4,7 @@ local dirs = ntop.getDirs() package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path +package.path = dirs.installdir .. "/scripts/lua/modules/pools/?.lua;" .. package.path require "lua_utils" local json = require("dkjson") @@ -40,7 +41,11 @@ if(action == "delete") then result.success = success if not success then - result.error = err + result.error = err + else + -- Unbind confid from all pools which are currently using it + local pools_lua_utils = require "pools_lua_utils" + pools_lua_utils.unbind_all_configset_id(confid) end elseif(action == "rename") then local new_name = _POST["confset_name"] diff --git a/scripts/lua/modules/pools/base_pools.lua b/scripts/lua/modules/pools/base_pools.lua index b48a9f6e26..059455abaf 100644 --- a/scripts/lua/modules/pools/base_pools.lua +++ b/scripts/lua/modules/pools/base_pools.lua @@ -470,6 +470,33 @@ end -- ############################################## +-- @brief Unbind a `configset_id` from all pools which are currently using it, and sets them the defauls configset. +function base_pools:unbind_all_configset_id(configset_id) + configset_id = tonumber(configset_id) + + if not configset_id then + -- Invalid argument + return + end + + local locked = self:_lock() + + if locked then + local all_pools = self:get_all_pools() + + for _, pool in pairs(all_pools) do + if pool["configset_id"] == configset_id then + -- Rewrite the pool using the default configset id + self:_persist(pool["pool_id"], pool["name"], pool["members"], user_scripts.DEFAULT_CONFIGSET_ID) + end + end + + self:_unlock() + end +end + +-- ############################################## + -- @brief Returns available confset ids which can be added to a pool function base_pools:get_available_configset_ids() -- Currently, confset_ids are shared across pools of all types diff --git a/scripts/lua/modules/pools/pools_lua_utils.lua b/scripts/lua/modules/pools/pools_lua_utils.lua new file mode 100644 index 0000000000..f926f8b42f --- /dev/null +++ b/scripts/lua/modules/pools/pools_lua_utils.lua @@ -0,0 +1,60 @@ +-- +-- (C) 2017-20 - ntop.org +-- +local dirs = ntop.getDirs() +package.path = dirs.installdir .. "/scripts/lua/modules/pools/?.lua;" .. package.path + +require "lua_utils" +local json = require "dkjson" +local os_utils = require "os_utils" +local base_pools = require "base_pools" + +-- ############################################## + +local pools_lua_utils = {} + +-- ############################################## + +-- @brief Returns an array of pool Lua class instances, for all available pools +-- e.g., {interface_pools:create(), local_network_pools:create(), snmp_device_pools:create(), ...} +-- +-- This method is useful to perform operations (such as the deletion of a configset id) which are +-- global and affect all the pool instances. Indeed a configset id is shared across all pools +local function all_pool_instances_factory() + local pools_dir = os_utils.fixPath(dirs.installdir .. "/scripts/lua/modules/pools/") + local res = {} + + for pool_file in pairs(ntop.readdir(pools_dir)) do + if pool_file:match("%.lua$") then + local pool_file_path = os_utils.fixPath(string.format("%s/%s", pools_dir, pool_file)) + + local pool = dofile(pool_file_path) + + -- Make sure pool is actually a pool Lua class by checking if it has method create + -- this is to avoid instantiating modules such as pool_lua_utils.lua and pool_rest_utils.lua + -- which are not classes and thus cannot be instantiated + if pool.create then + -- If it has a method create, then we can instantiate it and add it to the result + local instance = pool:create() + res[#res + 1] = instance + end + end + end + + return res +end + +-- ############################################## + +-- @brief Call `instance:unbind_all_configset_id` for every available pools `instance` +function pools_lua_utils.unbind_all_configset_id(configset_id) + local all_instances = all_pool_instances_factory() + + for _, instance in pairs(all_instances) do + instance:unbind_all_configset_id(configset_id) + end +end + +-- ############################################## + +return pools_lua_utils