Skip to content

Instantly share code, notes, and snippets.

@alexey-kirdin-ezlo
Last active June 27, 2023 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexey-kirdin-ezlo/823e061fb4baabcd91167850828f392d to your computer and use it in GitHub Desktop.
Save alexey-kirdin-ezlo/823e061fb4baabcd91167850828f392d to your computer and use it in GitHub Desktop.
extract_ip.lua
-- Oleksii.Kirdin@ezlo.com, 2023-06-25
-- Anonymous Plugin (aka Lua script) for Ezlogic.
-- Result: saves extracted string value to the target variable.
local logging = require "logging"
local json = assert(require "json", "Could not load json library")
local scenes = require "scenes"
local variables = {
source = "myIP",
target = "WANIP"
}
local path_to_extract = {"ip"}
local function extract_by_path(data, path)
local value = data
for _, key in ipairs(path) do
logging.debug("key: " .. tostring(key))
value = value[key]
logging.trace(value)
end
return value
end
local function extract_and_save(variable_from, variable_to, path)
logging.debug("HTTP Response variable: " .. variables.source)
local http_response_variable = scenes.get_expression_value(variable_from)
if http_response_variable.value ~= nil then
logging.debug("Source variable has value: " .. tostring(http_response_variable.value))
local data = http_response_variable.value
local res = json.decode(data)
logging.debug("After json.decode")
logging.trace(res)
local found_str = extract_by_path(res, path)
logging.debug(found_str)
-- fetch ONLY in full mode, not in compact:
local success, saved_value = pcall(scenes.get_expression_value, variable_to, {compact = false})
logging.trace("pcall.success: " .. tostring(success))
logging.trace("pcall.saved_value: " .. json.encode(saved_value))
local target_value = {
value = found_str,
value_type = saved_value.value_type or "string",
metadata = saved_value.metadata or {}
}
logging.debug("target_value: " .. json.encode(target_value))
logging.debug("Save to target variable: " .. variable_to)
scenes.set_variable(variable_to, target_value)
return true
else
logging.error("Source variable has no value")
if http_response_variable.error ~= nil then
logging.error("Variable.error: " .. tostring(http_response_variable.error))
end
return false
end
end
return extract_and_save(variables.source, variables.target, path_to_extract)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment