Fix validation errors when creating objects with the API

refs #9101
This commit is contained in:
Gunnar Beutner 2015-08-14 20:01:12 +02:00
parent e2290d5012
commit ce2735f10b
3 changed files with 46 additions and 21 deletions

View File

@ -264,6 +264,17 @@ void ConfigItem::Register(void)
m_UnnamedItems.push_back(this); m_UnnamedItems.push_back(this);
} else { } else {
boost::mutex::scoped_lock lock(m_Mutex); boost::mutex::scoped_lock lock(m_Mutex);
ItemMap::const_iterator it = m_Items[m_Type].find(m_Name);
if (it != m_Items[m_Type].end()) {
std::ostringstream msgbuf;
msgbuf << "A configuration item of type '" << GetType()
<< "' and name '" << GetName() << "' already exists ("
<< it->second->GetDebugInfo() << "), new declaration: " << GetDebugInfo();
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str()));
}
m_Items[m_Type][m_Name] = this; m_Items[m_Type][m_Name] = this;
} }
} }
@ -447,7 +458,7 @@ bool ConfigItem::CommitItems(WorkQueue& upq)
BOOST_FOREACH(const ItemCountMap::value_type& kv, itemCounts) { BOOST_FOREACH(const ItemCountMap::value_type& kv, itemCounts) {
Log(LogInformation, "ConfigItem") Log(LogInformation, "ConfigItem")
<< "Instantiated " << kv.second << " " << kv.first->GetPluralName() << "."; << "Instantiated " << kv.second << " " << (kv.second != 1 ? kv.first->GetPluralName() : kv.first->GetName()) << ".";
} }
return true; return true;

View File

@ -104,15 +104,19 @@ int IcingaApplication::Main(void)
l_RetentionTimer->Start(); l_RetentionTimer->Start();
/* restore modified attributes */ /* restore modified attributes */
Expression *expression = ConfigCompiler::CompileFile(GetModAttrPath()); if (Utility::PathExists(GetModAttrPath())) {
Expression *expression = ConfigCompiler::CompileFile(GetModAttrPath());
if (expression) { if (expression) {
try { try {
ScriptFrame frame; ScriptFrame frame;
expression->Evaluate(frame); expression->Evaluate(frame);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
Log(LogCritical, "config", DiagnosticInformation(ex)); Log(LogCritical, "config", DiagnosticInformation(ex));
}
} }
delete expression;
} }
RunEventLoop(); RunEventLoop();

View File

@ -89,28 +89,38 @@ bool CreateObjectHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& r
builder->AddExpression(expr); builder->AddExpression(expr);
} }
} }
ConfigItem::Ptr item = builder->Compile();
item->Register();
WorkQueue upq;
Dictionary::Ptr result1 = new Dictionary(); Dictionary::Ptr result1 = new Dictionary();
int code; int code;
String status; String status;
if (!ConfigItem::CommitItems(upq) || !ConfigItem::ActivateItems(upq, false)) { try {
ConfigItem::Ptr item = builder->Compile();
item->Register();
WorkQueue upq;
if (!ConfigItem::CommitItems(upq) || !ConfigItem::ActivateItems(upq, false)) {
code = 500;
status = "Object could not be created.";
Array::Ptr errors = new Array();
BOOST_FOREACH(const boost::exception_ptr& ex, upq.GetExceptions())
{
errors->Add(DiagnosticInformation(ex));
}
result1->Set("errors", errors);
} else {
code = 200;
status = "Object created";
}
} catch (const std::exception& ex) {
code = 500; code = 500;
status = "Object could not be created."; status = "Object could not be created.";
Array::Ptr errors = new Array(); Array::Ptr errors = new Array();
BOOST_FOREACH(const boost::exception_ptr& ex, upq.GetExceptions()) { errors->Add(DiagnosticInformation(ex));
errors->Add(DiagnosticInformation(ex));
}
result1->Set("errors", errors); result1->Set("errors", errors);
} else {
code = 200;
status = "Object created";
} }
result1->Set("code", code); result1->Set("code", code);