Skip to content

Commit 7cb32fb

Browse files
committed
(PUP-11631) Adds copy_metaparams method
Some types (e.g. resources, user) generate child resources from parent resources and need to copy all metaparameters, except for :alias, to the newly-generated child resource. This commit adds a new method, copy_metaparams, that enables types to do this.
1 parent 95291a9 commit 7cb32fb

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

lib/puppet/type.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ def self.newmetaparam(name, options = {}, &block)
356356
param
357357
end
358358

359+
# Copies all of a resource's metaparameters (except `alias`) to a generated child resource
360+
# @param parameters [Hash] of a resource's parameters
361+
# @return [Void]
362+
def copy_metaparams(parameters)
363+
parameters.each do |name, param|
364+
self[name] = param.value if param.metaparam? && !name == :alias
365+
end
366+
nil
367+
end
368+
359369
# Returns the list of parameters that comprise the composite key / "uniqueness key".
360370
# All parameters that return true from #isnamevar? or is named `:name` are included in the returned result.
361371
# @see uniqueness_key

lib/puppet/type/resources.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,7 @@ def generate
120120
select { |r| r.class.validproperty?(:ensure) }.
121121
select { |r| able_to_ensure_absent?(r) }.
122122
each { |resource|
123-
@parameters.each do |name, param|
124-
resource[name] = param.value if param.metaparam?
125-
end
126-
127-
# Mark that we're purging, so transactions can handle relationships
128-
# correctly
123+
resource.copy_metaparams(@parameters)
129124
resource.purging
130125
}
131126
end

lib/puppet/type/user.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,9 +811,7 @@ def find_unmanaged_keys
811811
flatten.each do |res|
812812
res[:ensure] = :absent
813813
res[:user] = self[:name]
814-
@parameters.each do |name, param|
815-
res[name] = param.value if param.metaparam?
816-
end
814+
res.copy_metaparams(@parameters)
817815
end
818816
end
819817

spec/unit/type/resources_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,20 @@ def self.instances
270270
@catalog = Puppet::Resource::Catalog.new
271271
end
272272

273+
context "when the catalog contains a purging resource with an alias" do
274+
before do
275+
@resource = Puppet::Type.type(:resources).new(:name => "purgeable_test", :purge => true)
276+
@catalog.add_resource @resource
277+
@catalog.alias(@resource, "purgeable_test_alias")
278+
end
279+
280+
it "should not copy the alias metaparameter" do
281+
allow(Puppet::Type.type(:purgeable_test)).to receive(:instances).and_return([@purgee])
282+
generated = @resource.generate.first
283+
expect(generated[:alias]).to be_nil
284+
end
285+
end
286+
273287
context "when dealing with non-purging resources" do
274288
before do
275289
@resources = Puppet::Type.type(:resources).new(:name => 'purgeable_test')

0 commit comments

Comments
 (0)