MCPcopy Create free account
hub / github.com/Icinga/icinga2 / AddComment

Method AddComment

lib/icinga/comment.cpp:134–189  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

132}
133
134Comment::Ptr Comment::AddComment(const Checkable::Ptr& checkable, CommentType entryType, const String& author,
135 const String& text, bool persistent, double expireTime, bool sticky, const String& id)
136{
137 String fullName;
138
139 if (id.IsEmpty())
140 fullName = checkable->GetName() + "!" + Utility::NewUniqueID();
141 else
142 fullName = id;
143
144 Dictionary::Ptr attrs = new Dictionary();
145
146 attrs->Set("author", author);
147 attrs->Set("text", text);
148 attrs->Set("persistent", persistent);
149 attrs->Set("expire_time", expireTime);
150 attrs->Set("entry_type", entryType);
151 attrs->Set("sticky", sticky);
152 attrs->Set("entry_time", Utility::GetTime());
153
154 Host::Ptr host;
155 Service::Ptr service;
156 tie(host, service) = GetHostService(checkable);
157
158 attrs->Set("host_name", host->GetName());
159 if (service)
160 attrs->Set("service_name", service->GetShortName());
161
162 String zone = checkable->GetZoneName();
163
164 if (!zone.IsEmpty())
165 attrs->Set("zone", zone);
166
167 String config = ConfigObjectUtility::CreateObjectConfig(Comment::TypeInstance, fullName, true, nullptr, attrs);
168
169 Array::Ptr errors = new Array();
170
171 if (!ConfigObjectUtility::CreateObject(Comment::TypeInstance, fullName, config, errors, nullptr)) {
172 ObjectLock olock(errors);
173 for (String error : errors) {
174 Log(LogCritical, "Comment", error);
175 }
176
177 BOOST_THROW_EXCEPTION(std::runtime_error("Could not create comment."));
178 }
179
180 Comment::Ptr comment = Comment::GetByName(fullName);
181
182 if (!comment)
183 BOOST_THROW_EXCEPTION(std::runtime_error("Could not create comment."));
184
185 Log(LogNotice, "Comment")
186 << "Added comment '" << comment->GetName() << "'.";
187
188 return comment;
189}
190
191void Comment::RemoveComment(const String& id, bool removedManually, const String& removedBy)

Callers

nothing calls this directly

Calls 4

LogClass · 0.85
IsEmptyMethod · 0.45
GetNameMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected