]> granicus.if.org Git - icinga2/blob - lib/remote/configobjectutility.cpp
Add missing attributes for dependent objects
[icinga2] / lib / remote / configobjectutility.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
30
31 using namespace icinga;
32
33 String ConfigObjectUtility::GetConfigDir(void)
34 {
35         return ConfigModuleUtility::GetModuleDir() + "/_api/" +
36             ConfigModuleUtility::GetActiveStage("_api");
37 }
38
39 String ConfigObjectUtility::EscapeName(const String& name)
40 {
41         return Utility::EscapeString(name, "<>:\"/\\|?*", true);
42 }
43
44 bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& fullName,
45     const Array::Ptr& templates, const Dictionary::Ptr& attrs, const Array::Ptr& errors)
46 {
47         NameComposer *nc = dynamic_cast<NameComposer *>(type.get());
48         Dictionary::Ptr nameParts;
49         String name;
50
51         if (nc) {
52                 nameParts = nc->ParseName(fullName);
53                 name = nameParts->Get("name");
54         } else
55                 name = fullName;
56
57         ConfigItemBuilder::Ptr builder = new ConfigItemBuilder();
58         builder->SetType(type->GetName());
59         builder->SetName(name);
60         builder->SetScope(ScriptGlobal::GetGlobals());
61         builder->SetModule("_api");
62
63         if (templates) {
64                 ObjectLock olock(templates);
65                 BOOST_FOREACH(const String& tmpl, templates) {
66                         ImportExpression *expr = new ImportExpression(MakeLiteral(tmpl));
67                         builder->AddExpression(expr);
68                 }
69         }
70
71         if (nameParts) {
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);
76                 }
77         }
78
79         if (attrs) {
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("."));
84                         
85                         Expression *expr = new GetScopeExpression(ScopeThis);
86                         
87                         BOOST_FOREACH(const String& val, tokens) {
88                                 expr = new IndexerExpression(expr, MakeLiteral(val));
89                         }
90                         
91                         SetExpression *aexpr = new SetExpression(expr, OpSetLiteral, MakeLiteral(kv.second));
92                         builder->AddExpression(aexpr);
93                 }
94         }
95         
96         try {
97                 ConfigItem::Ptr item = builder->Compile();
98                 item->Register();
99
100                 WorkQueue upq;
101
102                 if (!ConfigItem::CommitItems(upq) || !ConfigItem::ActivateItems(upq, false)) {
103                         if (errors) {
104                                 BOOST_FOREACH(const boost::exception_ptr& ex, upq.GetExceptions()) {
105                                         errors->Add(DiagnosticInformation(ex));
106                                 }
107                         }
108                         
109                         return false;
110                 }
111         } catch (const std::exception& ex) {
112                 if (errors)
113                         errors->Add(DiagnosticInformation(ex));
114                         
115                 return false;
116         }
117         
118         if (!ConfigModuleUtility::ModuleExists("_api")) {
119                 ConfigModuleUtility::CreateModule("_api");
120         
121                 String stage = ConfigModuleUtility::CreateStage("_api");
122                 ConfigModuleUtility::ActivateStage("_api", stage);
123         } 
124         
125         String typeDir = type->GetPluralName();
126         boost::algorithm::to_lower(typeDir);
127         
128         String path = GetConfigDir() + "/conf.d/" + typeDir;
129         Utility::MkDirP(path, 0700);
130
131         path += "/" + EscapeName(fullName) + ".conf";
132         
133         Dictionary::Ptr allAttrs = new Dictionary();
134         attrs->CopyTo(allAttrs);
135         nameParts->CopyTo(allAttrs);
136         allAttrs->Remove("name");
137
138         ConfigWriter::Ptr cw = new ConfigWriter(path);
139         cw->EmitConfigItem(type->GetName(), name, false, templates, allAttrs);
140         cw->EmitRaw("\n");
141         
142         return true;
143 }
144         
145 bool ConfigObjectUtility::DeleteObject(const ConfigObject::Ptr& object, const Array::Ptr& errors)
146 {
147         if (object->GetModule() != "_api") {
148                 if (errors)
149                         errors->Add("Object cannot be deleted because it was not created using the API.");
150                         
151                 return false;
152         }
153
154         Type::Ptr type = object->GetReflectionType();
155         
156         ConfigItem::Ptr item = ConfigItem::GetObject(type->GetName(), object->GetName());
157
158         try {
159                 object->Deactivate();
160
161                 if (item)
162                         item->Unregister();
163                 else
164                         object->Unregister();
165
166         } catch (const std::exception& ex) {
167                 if (errors)
168                         errors->Add(DiagnosticInformation(ex));
169                         
170                 return false;
171         }
172         
173         String typeDir = type->GetPluralName();
174         boost::algorithm::to_lower(typeDir);
175         
176         String path = GetConfigDir() + "/conf.d/" + typeDir +
177             "/" + EscapeName(object->GetName()) + ".conf";
178         
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));
185                 }
186         }
187
188         return true;
189 }
190