Skip to content

Added ability to set the purge behavior for node group rules #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ This parameter is supported for PE >=2017.3.x.

* `purge_behavior`

Defines how purging of classification or data will be handled. By default, or when set to `all`, the node\_group resource will ensure classes and data are matched exactly, and remove any values not described by the resource. When set to `none`, the node\_group resource will ensure data and classes described are present with the prescribed values, but will not remove other classification, or other data, present in the node group. The `data` setting purges only data values, and the `classes` setting purges only classes values.
Defines how purging of classification, data and rules will be handled. By default, or when set to `all`, the node\_group resource will ensure classes, data and rules are matched exactly, and remove any values not described by the resource. When set to `none`, the node\_group resource will ensure data, classes and rules described are present with the prescribed values, but will not remove other classification, or other data, present in the node group. The `data` setting purges only data values, the `classes` setting purges only classes values and the `rule` setting purges only rule values. In case of conflict with the node classifiers `Nodes must match all rules.` and `Nodes may match any rule.` radio buttons. The node classifier takes precedence over defined node_group code.

Default: `all`

Values: `all`, `data`, `classes`, `none`
Values: `all`, `data`, `classes`, `rule`, `none`

## Tasks

Expand Down
76 changes: 75 additions & 1 deletion lib/puppet/type/node_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
end
newparam(:purge_behavior) do
desc 'Whether or not to remove data or class parameters not specified'
newvalues(:none, :data, :classes, :all)
newvalues(:none, :data, :classes, :rule, :all)
defaultto :all
end
newproperty(:id) do
Expand All @@ -35,6 +35,80 @@
end
newproperty(:rule, :array_matching => :all) do
desc 'Match conditions for this group'
def should
case @resource[:purge_behavior]
when :rule, :all
super
else
a = @resource.property(:rule).retrieve || {}
b = shouldorig
aorig = a.map(&:clone)
atmp = a.map(&:clone)
# check if the node classifer has any rules defined before attempting merge.
if a.length >= 2
if a[0] == "or" and a[1][0] == "or" or a[1][0] == "and"
# Merging both rules and pinned nodes
if b[0] == "or" and b[1][0] == "or" or b[1][0] == "and"
# b has rules to merge
rules = (atmp[1] + b[1].drop(1)).uniq
atmp[1] = rules
pinned = (b[2,b.length] + atmp[2,atmp.length]).uniq
merged = (atmp + pinned).uniq
elsif b[0] == "and" or b[0] == "or" and PuppetX::Node_manager::Common.factcheck(b)
# b only has rules to merge
rules = (atmp[1] + b.drop(1)).uniq
atmp[1] = rules
merged = atmp
else
pinned = (b[1,b.length] + atmp[2,atmp.length]).uniq
merged = (atmp + pinned).uniq
end
elsif b[0] == "or" and b[1][0] == "or" or b[1][0] == "and"
# Merging both rules and pinned nodes
rules = b[1] # no rules to merge on a side
pinned = (b[2,b.length] + a[1,a.length]).uniq
merged = (b + pinned).uniq
elsif a[0] == "and" or a[0] == "or" and PuppetX::Node_manager::Common.factcheck(a)
# a only has fact rules
if b[0] == "or" and not PuppetX::Node_manager::Common.factcheck(b)
# b only has pinned nodes
rules = atmp
temp = ['or']
temp[1] = atmp
merged = (temp + b[1,b.length]).uniq
else
# b only has rules
merged = (a + b.drop(1)).uniq
end
elsif a[0] == "or" and a[1][1] == "name"
# a only has pinned nodes
if b[0] == "or" and not PuppetX::Node_manager::Common.factcheck(b)
# b only has pinned nodes
merged = (b + a.drop(1)).uniq
else
# b only has rules
temp = ['or']
temp[1] = b
merged = (temp + atmp[1,atmp.length]).uniq
end
else
# default fall back.
merged = (b + a.drop(1)).uniq
end
if merged == aorig
# values are the same, returning orginal value"
aorig
else
merged
end
else
b
end
end
end
def insync?(is)
is == should
end
end
newproperty(:environment) do
desc 'Environment for this group'
Expand Down
15 changes: 15 additions & 0 deletions lib/puppet_x/node_manager/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,19 @@ def self.sort_hash(data)
end
newhash
end

# check for fact rules in deep array
def self.factcheck(rulecheck)
rulecheck.each_with_index {|x, i|
if x == "fact" and i == 0
return true
end
if x.kind_of?(Array)
if factcheck(x)
return true
end
end
}
false
end
end