]> granicus.if.org Git - icinga2/commitdiff
Cli: Repository should validate if object exists before add/remove
authorMichael Friedrich <michael.friedrich@netways.de>
Fri, 31 Oct 2014 20:08:11 +0000 (21:08 +0100)
committerMichael Friedrich <michael.friedrich@netways.de>
Fri, 31 Oct 2014 20:08:11 +0000 (21:08 +0100)
fixes #7461

lib/cli/repositoryutility.cpp
lib/cli/repositoryutility.hpp

index 06702be8806f0813a34772bf46958d8a6bb7b2b8..af1398755006972e2601a0bb37073fef80d2a0e1 100644 (file)
@@ -155,13 +155,6 @@ void RepositoryUtility::PrintObjects(std::ostream& fp, const String& type)
                }
 
                fp << "\n";
-
-               /*
-               Dictionary::Ptr obj = GetObjectFromRepository(object); //TODO: config parser not implemented yet!
-
-               if (obj)
-                       fp << JsonEncode(obj);
-               */
        }
 }
 
@@ -259,6 +252,15 @@ bool RepositoryUtility::AddObject(const String& name, const String& type, const
                        return false;
        }
 
+       if (CheckChangeExists(change)) {
+               Log(LogWarning, "cli")
+                   << "Change '" << change->Get("command") << "' for type '"
+                   << change->Get("type") << "' and name '" << change->Get("name")
+                   << "' already exists.";
+
+               return false;
+       }
+
        return WriteObjectToRepositoryChangeLog(path, change);
 }
 
@@ -275,6 +277,15 @@ bool RepositoryUtility::RemoveObject(const String& name, const String& type, con
        change->Set("command", "remove");
        change->Set("attrs", attrs); //required for service->host_name
 
+       if (CheckChangeExists(change)) {
+               Log(LogWarning, "cli")
+                   << "Change '" << change->Get("command") << "' for type '"
+                   << change->Get("type") << "' and name '" << change->Get("name")
+                   << "' already exists.";
+
+               return false;
+       }
+
        return WriteObjectToRepositoryChangeLog(path, change);
 }
 
@@ -284,6 +295,32 @@ bool RepositoryUtility::SetObjectAttribute(const String& name, const String& typ
        return true;
 }
 
+bool RepositoryUtility::CheckChangeExists(const Dictionary::Ptr& change)
+{
+       Array::Ptr changelog = make_shared<Array>();
+
+       GetChangeLog(boost::bind(RepositoryUtility::CollectChange, _1, boost::ref(changelog)));
+
+       ObjectLock olock(changelog);
+       BOOST_FOREACH(const Dictionary::Ptr& entry, changelog) {
+               if (entry->Get("type") != change->Get("type"))
+                       continue;
+
+               if (entry->Get("name") != change->Get("name"))
+                       continue;
+
+               if (entry->Get("command") != change->Get("command"))
+                       continue;
+
+               /* only works for add/remove commands (no set) */
+               if (change->Get("command") == "add" || change->Get("command") == "remove") {
+                       return true;
+               }
+       }
+
+       return false;
+}
+
 bool RepositoryUtility::ClearChangeLog(void)
 {
        GetChangeLog(boost::bind(RepositoryUtility::ClearChange, _1, _2));
@@ -369,6 +406,10 @@ bool RepositoryUtility::RemoveObjectInternal(const String& name, const String& t
 
        bool success = RemoveObjectFileInternal(path);
 
+       if (success)
+               Log(LogInformation, "cli")
+                   << "Removing config object '" << name << "' in file '" << path << "'";
+
        /* special treatment for hosts -> remove the services too */
        if (type == "Host") {
                path = GetRepositoryObjectConfigPath(type, attrs) + "/" + name;
@@ -446,7 +487,7 @@ bool RepositoryUtility::SetObjectAttributeInternal(const String& name, const Str
 bool RepositoryUtility::WriteObjectToRepository(const String& path, const String& name, const String& type, const Dictionary::Ptr& item)
 {
        Log(LogInformation, "cli")
-           << "Dumping config object '" << name << "' to file '" << path << "'";
+           << "Writing config object '" << name << "' to file '" << path << "'";
 
        Utility::MkDirP(Utility::DirName(path), 0755);
 
@@ -607,9 +648,7 @@ void RepositoryUtility::FormatChangelogEntry(std::ostream& fp, const Dictionary:
        Dictionary::Ptr attrs = change->Get("attrs");
 
        fp << " " << ConsoleColorTag(Console_ForegroundMagenta | Console_Bold) << type << ConsoleColorTag(Console_Normal) << " '";
-       fp << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << change->Get("name") << ConsoleColorTag(Console_Normal) << "'";
-
-       fp << ": \n";
+       fp << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << change->Get("name") << ConsoleColorTag(Console_Normal) << "'\n";
 
        BOOST_FOREACH(const Dictionary::Pair& kv, attrs) {
                /* skip the name */
index c999599406f9b48e42ac1fcd0004f670c7712460..64d64392d74e04c2586f6c9f18a30ab255487e54 100644 (file)
@@ -53,6 +53,8 @@ public:
        static bool AddObject(const String& name, const String& type, const Dictionary::Ptr& attrs);
        static bool RemoveObject(const String& name, const String& type, const Dictionary::Ptr& attrs);
 
+       static bool CheckChangeExists(const Dictionary::Ptr& change);
+
        static bool SetObjectAttribute(const String& name, const String& type, const String& attr, const Value& val);
 
        static bool CommitChangeLog(void);