From 61e862171641f1a2501e6f45d26eb51e5e33eddb Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 19 Jun 2017 10:32:51 +0200 Subject: [PATCH] IcingaObjectFilterHelper: external filter helpers --- .../Director/Db/IcingaObjectFilterHelper.php | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 library/Director/Db/IcingaObjectFilterHelper.php diff --git a/library/Director/Db/IcingaObjectFilterHelper.php b/library/Director/Db/IcingaObjectFilterHelper.php new file mode 100644 index 00000000..dc91cf4a --- /dev/null +++ b/library/Director/Db/IcingaObjectFilterHelper.php @@ -0,0 +1,130 @@ +get('id'); + } elseif (is_string($id) && ctype_digit($id)) { + return (int) $id; + } else { + throw new ProgrammingError( + 'Numeric ID or IcingaObject expected, got %s', + // TODO: just type/class info? + var_export($id, 1) + ); + } + } + + /** + * @param ZfSelect $query + * @param IcingaObject|int|string $template + * @param string $tableAlias + * @param string $inheritanceType + * @return ZfSelect + * @throws ProgrammingError + */ + public static function filterByTemplate( + ZfSelect $query, + $template, + $tableAlias = 'o', + $inheritanceType = self::INHERIT_DIRECT + ) { + $i = $tableAlias . 'i'; + $o = $tableAlias; + $type = $template->getShortTableName(); + $db = $template->getDb(); + $id = static::wantId($template); + $sub = $db->select()->from( + array($i => "icinga_${type}_inheritance"), + array('e' => '(1)') + )->where("$i.${type}_id = $o.id"); + + if ($inheritanceType === self::INHERIT_DIRECT) { + $sub->where("$i.parent_${type}_id = ?", $id); + } elseif ($inheritanceType === self::INHERIT_INDIRECT + || $inheritanceType === self::INHERIT_DIRECT_OR_INDIRECT + ) { + $tree = new TemplateTree($type, $template->getConnection()); + $ids = $tree->listDescendantIdsFor($template); + if ($inheritanceType === self::INHERIT_DIRECT_OR_INDIRECT) { + $ids[] = $template->getAutoincId(); + } + + $sub->where("$i.parent_${type}_id IN (?)", $ids); + } else { + throw new ProgrammingError( + 'Unable to understand "%s" inheritance', + $inheritanceType + ); + } + + return $query->where('EXISTS ?', $sub); + } + + public static function filterByHostgroups( + ZfSelect $query, + $type, + $groups, + $tableAlias = 'o' + ) { + if (empty($groups)) { + // Asked for an empty set of groups? Give no result + $query->where('(1 = 0)'); + } else { + $sub = $query->getAdapter()->select()->from( + array('go' => "icinga_${type}group_${type}"), + array('e' => '(1)') + )->join( + array('g' => "icinga_${type}group"), + "go.${type}group_id = g.id" + )->where("go.${type}_id = ${tableAlias}.id") + ->where('g.object_name IN (?)', $groups); + + $query->where('EXISTS ?', $sub); + } + } + + public static function filterByResolvedHostgroups( + ZfSelect $query, + $type, + $groups, + $tableAlias = 'o' + ) { + if (empty($groups)) { + // Asked for an empty set of groups? Give no result + $query->where('(1 = 0)'); + } else { + $sub = $query->getAdapter()->select()->from( + array('go' => "icinga_${type}group_${type}_resolved"), + array('e' => '(1)') + )->join( + array('g' => "icinga_${type}group"), + "go.${type}group_id = g.id", + [] + )->where("go.${type}_id = ${tableAlias}.id") + ->where('g.object_name IN (?)', $groups); + + $query->where('EXISTS ?', $sub); + } + } +}