Skip to content

Commit 6dbe210

Browse files
authored
Merge pull request #536 from Icinga/feature/easier_include_exclude_filtering
Feature: Adds function for easier include/exclude filtering Adds generic function for easier managing include/exclude filtering for plugins and for cleaning arrays with not wanted content.
2 parents 5f8b59e + f908d5b commit 6dbe210

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
2121
* [#40](https://github.com/Icinga/icinga-powershell-framework/issues/40) Adds support to set service recovery for the Icinga Agent and Icinga for Windows service, to restart them in case of a crash or error
2222
* [#525](https://github.com/Icinga/icinga-powershell-framework/pull/525) Adds new developer mode for `icinga` command and improved cache handling, to ensure within `-DeveloperMode` and inside a VS Code environment, the framework cache file is never overwritten, while still all functions are loaded and imported.
2323
* [#531](https://github.com/Icinga/icinga-powershell-framework/pull/531) Adds `Test-IcingaStateFile` and `Repair-IcingaStateFile`, which is integrated into `Test-IcingaAgent`, to ensure the Icinga Agent state file is healthy and not corrupt, causing the Icinga Agent to fail on start
24+
* [#536](https://github.com/Icinga/icinga-powershell-framework/pull/536) Adds new function `Test-IcingaArrayFilter` for easier include and exclude filtering during plugin runtime and to allow filtering of array content for intended values only
2425

2526
## 1.9.2 (2022-06-03)
2627

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<#
2+
.SYNOPSIS
3+
Compares an InputObject of type [array] or [string] to a specified
4+
include and exclude array filter, returning either if the filter applies
5+
as true/false or an object of type [array] for filtered InputObject values
6+
.DESCRIPTION
7+
Compares an InputObject of type [array] or [string] to a specified
8+
include and exclude array filter, returning either if the filter applies
9+
as true/false or an object of type [array] for filtered InputObject values.
10+
11+
The function is designed to work as a general filter approach for check plugins as
12+
example, to ensure filtering certain values is easy. After comparing the include
13+
and exclude filter, the function will return True if the input object can be included
14+
and will return False in case it should not be included.
15+
16+
For [array] objects, the function will return a filtered [array] object on which all
17+
values which should be included and excluded were evaluated and only the remaining ones
18+
are returned.
19+
.PARAMETER InputObject
20+
The object to compare the filter against. This can be of type [array] or [string].
21+
Using an [array] object will return a filtered [array] result, while a [string] value
22+
will return a [bool], if the filter can be applied or not
23+
.PARAMETER Include
24+
An [array] of values to compare the InputObject against and include only certain input
25+
.PARAMETER Exclude
26+
An [array] of values to compare the InputObject against and exclude only certain input
27+
.EXAMPLE
28+
PS> Test-IcingaArrayFilter -InputObject @('icinga2', 'icingapowershell', 'winrm') -Exclude @('icinga2');
29+
30+
icingapowershell
31+
winrm
32+
.EXAMPLE
33+
PS> Test-IcingaArrayFilter -InputObject @('icinga2', 'icingapowershell', 'winrm') -Exclude @('*icinga*');
34+
35+
winrm
36+
.EXAMPLE
37+
PS> Test-IcingaArrayFilter -InputObject 'icinga2' -Include @('*icinga*', 'winrm');
38+
39+
True
40+
.EXAMPLE
41+
PS> Test-IcingaArrayFilter -InputObject 'icinga2' -Include @('*icinga*', 'winrm') -Exclude @('*icinga*');
42+
43+
False
44+
#>
45+
46+
function Test-IcingaArrayFilter()
47+
{
48+
param (
49+
$InputObject = $null,
50+
[array]$Include = @(),
51+
[array]$Exclude = @()
52+
);
53+
54+
[bool]$ReturnArray = $FALSE;
55+
56+
if ($InputObject -Is [array]) {
57+
$ReturnArray = $TRUE;
58+
}
59+
60+
[array]$FilteredArray = @();
61+
62+
if ($null -eq $InputObject) {
63+
if ($ReturnArray) {
64+
return $InputObject;
65+
}
66+
67+
return $FALSE;
68+
}
69+
70+
if ($Include.Count -eq 0 -And $Exclude.Count -eq 0) {
71+
if ($ReturnArray) {
72+
return $InputObject;
73+
}
74+
75+
return $TRUE;
76+
}
77+
78+
[bool]$IncludeFound = $FALSE;
79+
80+
if ($ReturnArray) {
81+
# Handles if our input object is an array
82+
# Will return an array object instead of boolean
83+
foreach ($input in $InputObject) {
84+
if ((Test-IcingaArrayFilter -InputObject $input -Include $Include -Exclude $Exclude)) {
85+
$FilteredArray += $input;
86+
}
87+
}
88+
89+
return $FilteredArray;
90+
} else {
91+
foreach ($entry in $Exclude) {
92+
if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) {
93+
return $FALSE;
94+
}
95+
}
96+
97+
if ($Include.Count -eq 0) {
98+
return $TRUE;
99+
}
100+
101+
foreach ($entry in $Include) {
102+
if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) {
103+
$IncludeFound = $TRUE;
104+
break;
105+
}
106+
}
107+
108+
return $IncludeFound;
109+
}
110+
111+
return $IncludeFound;
112+
}

0 commit comments

Comments
 (0)