2013-02-02 19:56:23 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2013-02-02 19:56:23 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "config/typerule.hpp"
|
|
|
|
#include "config/configitem.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/dictionary.hpp"
|
|
|
|
#include "base/array.hpp"
|
2013-02-02 19:56:23 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-05-03 10:48:28 +02:00
|
|
|
TypeRule::TypeRule(TypeSpecifier type, const String& nameType,
|
|
|
|
const String& namePattern, const TypeRuleList::Ptr& subRules,
|
|
|
|
const DebugInfo& debuginfo)
|
|
|
|
: m_Type(type), m_NameType(nameType), m_NamePattern(namePattern), m_SubRules(subRules), m_DebugInfo(debuginfo)
|
2013-02-02 19:56:23 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
TypeRuleList::Ptr TypeRule::GetSubRules(void) const
|
|
|
|
{
|
|
|
|
return m_SubRules;
|
|
|
|
}
|
|
|
|
|
2013-02-05 13:06:42 +01:00
|
|
|
bool TypeRule::MatchName(const String& name) const
|
2013-02-02 19:56:23 +01:00
|
|
|
{
|
2013-02-05 13:06:42 +01:00
|
|
|
return (Utility::Match(m_NamePattern, name));
|
|
|
|
}
|
2013-02-02 19:56:23 +01:00
|
|
|
|
2014-10-28 11:54:56 +01:00
|
|
|
bool TypeRule::MatchValue(const Value& value, String *hint, const TypeRuleUtilities *utils) const
|
2013-02-05 13:06:42 +01:00
|
|
|
{
|
2014-05-09 19:40:56 +02:00
|
|
|
ConfigItem::Ptr item;
|
|
|
|
|
2013-02-02 19:56:23 +01:00
|
|
|
if (value.IsEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
switch (m_Type) {
|
|
|
|
case TypeAny:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case TypeString:
|
|
|
|
/* fall through; any scalar can be converted to a string */
|
|
|
|
case TypeScalar:
|
|
|
|
return value.IsScalar();
|
|
|
|
|
|
|
|
case TypeNumber:
|
|
|
|
try {
|
|
|
|
Convert::ToDouble(value);
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-14 12:17:46 +01:00
|
|
|
|
2013-02-02 19:56:23 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case TypeDictionary:
|
|
|
|
return value.IsObjectType<Dictionary>();
|
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
case TypeArray:
|
|
|
|
return value.IsObjectType<Array>();
|
|
|
|
|
2015-01-27 10:55:14 +01:00
|
|
|
case TypeFunction:
|
|
|
|
return value.IsObjectType<Function>();
|
|
|
|
|
2013-05-03 10:48:28 +02:00
|
|
|
case TypeName:
|
|
|
|
if (!value.IsScalar())
|
|
|
|
return false;
|
|
|
|
|
2014-10-28 11:54:56 +01:00
|
|
|
return utils->ValidateName(m_NameType, value, hint);
|
2014-05-09 19:40:56 +02:00
|
|
|
|
2014-10-28 11:54:56 +01:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-05-03 10:48:28 +02:00
|
|
|
|
2014-10-28 11:54:56 +01:00
|
|
|
bool TypeRuleUtilities::ValidateName(const String& type, const String& name, String *hint) const
|
|
|
|
{
|
2014-11-06 19:35:47 +01:00
|
|
|
if (name.IsEmpty())
|
|
|
|
return true;
|
|
|
|
|
2014-10-28 11:54:56 +01:00
|
|
|
ConfigItem::Ptr item = ConfigItem::GetObject(type, name);
|
2014-05-09 19:40:56 +02:00
|
|
|
|
2014-10-28 11:54:56 +01:00
|
|
|
if (!item) {
|
|
|
|
*hint = "Object '" + name + "' of type '" + type + "' does not exist.";
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-03 10:48:28 +02:00
|
|
|
|
2014-10-28 11:54:56 +01:00
|
|
|
if (item->IsAbstract()) {
|
|
|
|
*hint = "Object '" + name + "' of type '" + type + "' must not be a template.";
|
|
|
|
return false;
|
2013-02-02 19:56:23 +01:00
|
|
|
}
|
2014-10-28 11:54:56 +01:00
|
|
|
|
|
|
|
return true;
|
2013-02-02 19:56:23 +01:00
|
|
|
}
|