MXS-1220: Use JSON output in dbfwfilter module commands
Added a new module command to the dbfwfilter to allow printing of rules via the REST API. The old module command, `rules`, is now deprecated.
This commit is contained in:
@ -144,6 +144,7 @@ const char* rule_names[] =
|
||||
{
|
||||
"UNDEFINED",
|
||||
"COLUMN",
|
||||
"FUNCTION",
|
||||
"THROTTLE",
|
||||
"PERMISSION",
|
||||
"WILDCARD",
|
||||
@ -802,6 +803,30 @@ bool dbfw_show_rules(const MODULECMD_ARG *argv, json_t** output)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dbfw_show_rules_json(const MODULECMD_ARG *argv, json_t** output)
|
||||
{
|
||||
MXS_FILTER_DEF *filter = argv->argv[0].value.filter;
|
||||
FW_INSTANCE *inst = (FW_INSTANCE*)filter_def_get_instance(filter);
|
||||
|
||||
json_t* arr = json_array();
|
||||
|
||||
if (!thr_rules || !thr_users)
|
||||
{
|
||||
if (!replace_rules(inst))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (RULE *rule = thr_rules; rule; rule = rule->next)
|
||||
{
|
||||
json_array_append_new(arr, rule_to_json(rule));
|
||||
}
|
||||
|
||||
*output = arr;
|
||||
return true;
|
||||
}
|
||||
|
||||
static const MXS_ENUM_VALUE action_values[] =
|
||||
{
|
||||
{"allow", FW_ACTION_ALLOW},
|
||||
@ -838,6 +863,14 @@ MXS_MODULE* MXS_CREATE_MODULE()
|
||||
modulecmd_register_command(MXS_MODULE_NAME, "rules", MODULECMD_TYPE_PASSIVE,
|
||||
dbfw_show_rules, 2, args_rules_show);
|
||||
|
||||
modulecmd_arg_type_t args_rules_show_json[] =
|
||||
{
|
||||
{MODULECMD_ARG_FILTER | MODULECMD_ARG_NAME_MATCHES_DOMAIN, "Filter to inspect"}
|
||||
};
|
||||
|
||||
modulecmd_register_command(MXS_MODULE_NAME, "rules/json", MODULECMD_TYPE_PASSIVE,
|
||||
dbfw_show_rules_json, 1, args_rules_show_json);
|
||||
|
||||
static MXS_FILTER_OBJECT MyObject =
|
||||
{
|
||||
createInstance,
|
||||
|
@ -1717,11 +1717,18 @@ static void callModuleCommand(DCB *dcb, char *domain, char *id, char *v3,
|
||||
|
||||
if (arg)
|
||||
{
|
||||
// TODO: Print output instead of passing a writable DCB to the command
|
||||
if (!modulecmd_call_command(cmd, arg, NULL))
|
||||
json_t* output = NULL;
|
||||
|
||||
if (!modulecmd_call_command(cmd, arg, &output))
|
||||
{
|
||||
dcb_printf(dcb, "Error: %s\n", modulecmd_get_error());
|
||||
}
|
||||
else if (output)
|
||||
{
|
||||
dcb_printf(dcb, "%s\n", json_dumps(output, JSON_INDENT(4)));
|
||||
}
|
||||
|
||||
json_decref(output);
|
||||
modulecmd_arg_free(arg);
|
||||
}
|
||||
else
|
||||
|
Reference in New Issue
Block a user