Skip to content

Commit f81d8ff

Browse files
committed
Resources of the same name now inherit the attributes of the prior resource - see CHEF-26
1 parent e0b005e commit f81d8ff

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

chef/lib/chef/recipe.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ def method_missing(method_symbol, *args, &block)
180180
args << @collection
181181
args << @node
182182
resource = eval(rname).new(*args)
183+
# If we have a resource like this one, we want to steal it's state
184+
resource.load_prior_resource
183185
resource.cookbook_name = @cookbook_name
184186
resource.recipe_name = @recipe_name
185187
resource.params = @params

chef/lib/chef/resource.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def initialize(name, collection=nil, node=nil)
3838
@collection = collection
3939
else
4040
@collection = Chef::ResourceCollection.new()
41-
end
41+
end
4242
@node = node ? node : Chef::Node.new
4343
@noop = nil
4444
@before = nil
@@ -59,6 +59,21 @@ def initialize(name, collection=nil, node=nil)
5959
end
6060
end
6161

62+
def load_prior_resource
63+
begin
64+
prior_resource = @collection.lookup(self.to_s)
65+
Chef::Log.debug("Setting #{self.to_s} to the state of the prior #{self.to_s}")
66+
prior_resource.instance_variables.each do |iv|
67+
unless iv == "@source_line"
68+
self.instance_variable_set(iv, prior_resource.instance_variable_get(iv))
69+
end
70+
end
71+
true
72+
rescue ArgumentError => e
73+
true
74+
end
75+
end
76+
6277
def supports(args={})
6378
if args.any?
6479
@supports = args
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Cookbook Name:: prior_resource_test
3+
# Recipe:: default
4+
#
5+
# Copyright 2009, Example Com
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
file "/tmp/prior_resource_test" do
21+
backup 16
22+
action :create
23+
end
24+
25+
file "/tmp/prior_resource_test" do
26+
action :delete
27+
end

0 commit comments

Comments
 (0)