mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-27 07:34:15 +02:00
Move some checks. Add macros parameters to ExecuteCommand message
This commit is contained in:
parent
edbd8688fb
commit
ee2f7bafdd
@ -556,10 +556,31 @@ Dictionary::Ptr ApiActions::GenerateTicket(const ConfigObject::Ptr&,
|
|||||||
Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
|
Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
|
||||||
const Dictionary::Ptr& params)
|
const Dictionary::Ptr& params)
|
||||||
{
|
{
|
||||||
|
ApiListener::Ptr listener = ApiListener::GetInstance();
|
||||||
|
if (!listener)
|
||||||
|
BOOST_THROW_EXCEPTION(std::invalid_argument("No ApiListener instance configured."));
|
||||||
|
|
||||||
|
/* Get command_type */
|
||||||
|
String command_type = "EventCommand";
|
||||||
|
if (params->Contains("command_type"))
|
||||||
|
command_type = HttpUtility::GetLastParameter(params, "command_type");
|
||||||
|
|
||||||
|
/* Validate command_type */
|
||||||
|
if (command_type != "EventCommand" && command_type != "CheckCommand" && command_type != "NotificationCommand")
|
||||||
|
return ApiActions::CreateResult(400, "Invalid command_type '" + command_type + "'.");
|
||||||
|
|
||||||
Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
|
Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
|
||||||
if (!checkable)
|
if (!checkable)
|
||||||
return ApiActions::CreateResult(404, "Can't start a command execution for a non-existent object.");
|
return ApiActions::CreateResult(404, "Can't start a command execution for a non-existent object.");
|
||||||
|
|
||||||
|
/* Get TTL param */
|
||||||
|
if (!params->Contains("ttl"))
|
||||||
|
return ApiActions::CreateResult(400, "Parameter ttl is required.");
|
||||||
|
|
||||||
|
double ttl = HttpUtility::GetLastParameter(params, "ttl");
|
||||||
|
if (ttl <= 0)
|
||||||
|
return ApiActions::CreateResult(400, "Parameter ttl must be greater than 0.");
|
||||||
|
|
||||||
ObjectLock oLock (checkable);
|
ObjectLock oLock (checkable);
|
||||||
|
|
||||||
Host::Ptr host;
|
Host::Ptr host;
|
||||||
@ -571,10 +592,13 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
|
|||||||
endpoint = HttpUtility::GetLastParameter(params, "endpoint");
|
endpoint = HttpUtility::GetLastParameter(params, "endpoint");
|
||||||
|
|
||||||
MacroProcessor::ResolverList resolvers;
|
MacroProcessor::ResolverList resolvers;
|
||||||
|
Dictionary::Ptr macros;
|
||||||
if (params->Contains("macros")) {
|
if (params->Contains("macros")) {
|
||||||
Value macros = HttpUtility::GetLastParameter(params, "macros");
|
Value vmacros = HttpUtility::GetLastParameter(params, "macros");
|
||||||
if (macros.IsObjectType<Dictionary>())
|
if (vmacros.IsObjectType<Dictionary>()) {
|
||||||
resolvers.emplace_back("override", macros);
|
resolvers.emplace_back("override", vmacros);
|
||||||
|
macros = vmacros;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
return ApiActions::CreateResult(400, "Parameter macros must be a dictionary.");
|
return ApiActions::CreateResult(400, "Parameter macros must be a dictionary.");
|
||||||
}
|
}
|
||||||
@ -594,15 +618,6 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
|
|||||||
if (!endpointPtr)
|
if (!endpointPtr)
|
||||||
return ApiActions::CreateResult(404, "Can't find a valid endpoint for '" + resolved_endpoint + "'.");
|
return ApiActions::CreateResult(404, "Can't find a valid endpoint for '" + resolved_endpoint + "'.");
|
||||||
|
|
||||||
/* Get command_type */
|
|
||||||
String command_type = "EventCommand";
|
|
||||||
if (params->Contains("command_type"))
|
|
||||||
command_type = HttpUtility::GetLastParameter(params, "command_type");
|
|
||||||
|
|
||||||
/* Validate command_type */
|
|
||||||
if (command_type != "EventCommand" && command_type != "CheckCommand" && command_type != "NotificationCommand")
|
|
||||||
return ApiActions::CreateResult(400, "Invalid command_type '" + command_type + "'.");
|
|
||||||
|
|
||||||
/* Get command */
|
/* Get command */
|
||||||
String command;
|
String command;
|
||||||
if (!params->Contains("command")) {
|
if (!params->Contains("command")) {
|
||||||
@ -635,14 +650,6 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
|
|||||||
return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'.");
|
return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get TTL param */
|
|
||||||
if (!params->Contains("ttl"))
|
|
||||||
return ApiActions::CreateResult(400, "Parameter ttl is required.");
|
|
||||||
|
|
||||||
double ttl = HttpUtility::GetLastParameter(params, "ttl");
|
|
||||||
if (ttl <= 0)
|
|
||||||
return ApiActions::CreateResult(400, "Parameter ttl must be greater than 0.");
|
|
||||||
|
|
||||||
/* This generates a UUID */
|
/* This generates a UUID */
|
||||||
String uuid = Utility::NewUniqueID();
|
String uuid = Utility::NewUniqueID();
|
||||||
|
|
||||||
@ -660,15 +667,13 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
|
|||||||
checkable->SetExecutions(executions);
|
checkable->SetExecutions(executions);
|
||||||
|
|
||||||
/* Broadcast the update */
|
/* Broadcast the update */
|
||||||
ApiListener::Ptr listener = ApiListener::GetInstance();
|
Dictionary::Ptr executionsToBroadcast = new Dictionary();
|
||||||
if (!listener)
|
executionsToBroadcast->Set(uuid, pending_execution);
|
||||||
return ApiActions::CreateResult(404, "No ApiListener instance available.");
|
|
||||||
|
|
||||||
Dictionary::Ptr updateParams = new Dictionary();
|
Dictionary::Ptr updateParams = new Dictionary();
|
||||||
updateParams->Set("host", host->GetName());
|
updateParams->Set("host", host->GetName());
|
||||||
if (service)
|
if (service)
|
||||||
updateParams->Set("service", service->GetShortName());
|
updateParams->Set("service", service->GetShortName());
|
||||||
updateParams->Set("executions", executions);
|
updateParams->Set("executions", executionsToBroadcast);
|
||||||
|
|
||||||
Dictionary::Ptr updateMessage = new Dictionary();
|
Dictionary::Ptr updateMessage = new Dictionary();
|
||||||
updateMessage->Set("jsonrpc", "2.0");
|
updateMessage->Set("jsonrpc", "2.0");
|
||||||
@ -695,6 +700,8 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
|
|||||||
|
|
||||||
execParams->Set("source", uuid);
|
execParams->Set("source", uuid);
|
||||||
execParams->Set("deadline", deadline);
|
execParams->Set("deadline", deadline);
|
||||||
|
if (macros)
|
||||||
|
execParams->Set("macros", macros);
|
||||||
|
|
||||||
/* Execute command */
|
/* Execute command */
|
||||||
bool local = endpointPtr == Endpoint::GetLocalEndpoint();
|
bool local = endpointPtr == Endpoint::GetLocalEndpoint();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user