1 /******************************************************************************
3 * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "remote/configobjectutility.hpp"
21 #include "remote/configmoduleutility.hpp"
22 #include "config/configitembuilder.hpp"
23 #include "config/configitem.hpp"
24 #include "config/configwriter.hpp"
25 #include "base/exception.hpp"
26 #include "base/serializer.hpp"
27 #include <boost/algorithm/string/split.hpp>
28 #include <boost/algorithm/string/classification.hpp>
29 #include <boost/algorithm/string/case_conv.hpp>
31 using namespace icinga;
33 String ConfigObjectUtility::GetConfigDir(void)
35 return ConfigModuleUtility::GetModuleDir() + "/_api/" +
36 ConfigModuleUtility::GetActiveStage("_api");
39 String ConfigObjectUtility::EscapeName(const String& name)
41 return Utility::EscapeString(name, "<>:\"/\\|?*", true);
44 bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& fullName,
45 const Array::Ptr& templates, const Dictionary::Ptr& attrs, const Array::Ptr& errors)
47 NameComposer *nc = dynamic_cast<NameComposer *>(type.get());
48 Dictionary::Ptr nameParts;
52 nameParts = nc->ParseName(fullName);
53 name = nameParts->Get("name");
57 ConfigItemBuilder::Ptr builder = new ConfigItemBuilder();
58 builder->SetType(type->GetName());
59 builder->SetName(name);
60 builder->SetScope(ScriptGlobal::GetGlobals());
61 builder->SetModule("_api");
64 ObjectLock olock(templates);
65 BOOST_FOREACH(const String& tmpl, templates) {
66 ImportExpression *expr = new ImportExpression(MakeLiteral(tmpl));
67 builder->AddExpression(expr);
72 ObjectLock olock(nameParts);
73 BOOST_FOREACH(const Dictionary::Pair& kv, nameParts) {
74 SetExpression *expr = new SetExpression(MakeIndexer(ScopeThis, kv.first), OpSetLiteral, MakeLiteral(kv.second));
75 builder->AddExpression(expr);
80 ObjectLock olock(attrs);
81 BOOST_FOREACH(const Dictionary::Pair& kv, attrs) {
82 std::vector<String> tokens;
83 boost::algorithm::split(tokens, kv.first, boost::is_any_of("."));
85 Expression *expr = new GetScopeExpression(ScopeThis);
87 BOOST_FOREACH(const String& val, tokens) {
88 expr = new IndexerExpression(expr, MakeLiteral(val));
91 SetExpression *aexpr = new SetExpression(expr, OpSetLiteral, MakeLiteral(kv.second));
92 builder->AddExpression(aexpr);
97 ConfigItem::Ptr item = builder->Compile();
102 if (!ConfigItem::CommitItems(upq) || !ConfigItem::ActivateItems(upq, false)) {
104 BOOST_FOREACH(const boost::exception_ptr& ex, upq.GetExceptions()) {
105 errors->Add(DiagnosticInformation(ex));
111 } catch (const std::exception& ex) {
113 errors->Add(DiagnosticInformation(ex));
118 if (!ConfigModuleUtility::ModuleExists("_api")) {
119 ConfigModuleUtility::CreateModule("_api");
121 String stage = ConfigModuleUtility::CreateStage("_api");
122 ConfigModuleUtility::ActivateStage("_api", stage);
125 String typeDir = type->GetPluralName();
126 boost::algorithm::to_lower(typeDir);
128 String path = GetConfigDir() + "/conf.d/" + typeDir;
129 Utility::MkDirP(path, 0700);
131 path += "/" + EscapeName(fullName) + ".conf";
133 Dictionary::Ptr allAttrs = new Dictionary();
134 attrs->CopyTo(allAttrs);
135 nameParts->CopyTo(allAttrs);
136 allAttrs->Remove("name");
138 ConfigWriter::Ptr cw = new ConfigWriter(path);
139 cw->EmitConfigItem(type->GetName(), name, false, templates, allAttrs);
145 bool ConfigObjectUtility::DeleteObject(const ConfigObject::Ptr& object, const Array::Ptr& errors)
147 if (object->GetModule() != "_api") {
149 errors->Add("Object cannot be deleted because it was not created using the API.");
154 Type::Ptr type = object->GetReflectionType();
156 ConfigItem::Ptr item = ConfigItem::GetObject(type->GetName(), object->GetName());
159 object->Deactivate();
164 object->Unregister();
166 } catch (const std::exception& ex) {
168 errors->Add(DiagnosticInformation(ex));
173 String typeDir = type->GetPluralName();
174 boost::algorithm::to_lower(typeDir);
176 String path = GetConfigDir() + "/conf.d/" + typeDir +
177 "/" + EscapeName(object->GetName()) + ".conf";
179 if (Utility::PathExists(path)) {
180 if (unlink(path.CStr()) < 0) {
181 BOOST_THROW_EXCEPTION(posix_error()
182 << boost::errinfo_api_function("unlink")
183 << boost::errinfo_errno(errno)
184 << boost::errinfo_file_name(path));