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,6 +104,7 @@ int IcingaApplication::Main(void)
l_RetentionTimer->Start(); l_RetentionTimer->Start();
/* restore modified attributes */ /* restore modified attributes */
if (Utility::PathExists(GetModAttrPath())) {
Expression *expression = ConfigCompiler::CompileFile(GetModAttrPath()); Expression *expression = ConfigCompiler::CompileFile(GetModAttrPath());
if (expression) { if (expression) {
@ -115,6 +116,9 @@ int IcingaApplication::Main(void)
} }
} }
delete expression;
}
RunEventLoop(); RunEventLoop();
Log(LogInformation, "IcingaApplication", "Icinga has shut down."); Log(LogInformation, "IcingaApplication", "Icinga has shut down.");

View File

@ -90,21 +90,23 @@ bool CreateObjectHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& r
} }
} }
Dictionary::Ptr result1 = new Dictionary();
int code;
String status;
try {
ConfigItem::Ptr item = builder->Compile(); ConfigItem::Ptr item = builder->Compile();
item->Register(); item->Register();
WorkQueue upq; WorkQueue upq;
Dictionary::Ptr result1 = new Dictionary();
int code;
String status;
if (!ConfigItem::CommitItems(upq) || !ConfigItem::ActivateItems(upq, false)) { if (!ConfigItem::CommitItems(upq) || !ConfigItem::ActivateItems(upq, false)) {
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()) { 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);
@ -112,6 +114,14 @@ bool CreateObjectHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& r
code = 200; code = 200;
status = "Object created"; status = "Object created";
} }
} catch (const std::exception& ex) {
code = 500;
status = "Object could not be created.";
Array::Ptr errors = new Array();
errors->Add(DiagnosticInformation(ex));
result1->Set("errors", errors);
}
result1->Set("code", code); result1->Set("code", code);
result1->Set("status", status); result1->Set("status", status);