Skip to content

Commit 0003f14

Browse files
committed
The node object should be available from within the definitions paramater block - fixes CHEF-94
1 parent 8adcd47 commit 0003f14

7 files changed

Lines changed: 93 additions & 2 deletions

File tree

chef/lib/chef/recipe.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def method_missing(method_symbol, *args, &block)
161161
# This dupes the high level object, but we still need to dup the params
162162
new_def = @definitions[method_symbol].dup
163163
new_def.params = new_def.params.dup
164+
new_def.node = @node
164165
# This sets up the parameter overrides
165166
new_def.instance_eval(&block) if block
166167
new_recipe = Chef::Recipe.new(@cookbook_name, @recipe_name, @node, @collection, @definitions, @cookbook_loader)

chef/lib/chef/resource_definition.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ class ResourceDefinition
2525
include Chef::Mixin::FromFile
2626
include Chef::Mixin::ParamsValidate
2727

28-
attr_accessor :name, :params, :recipe
28+
attr_accessor :name, :params, :recipe, :node
2929

30-
def initialize
30+
def initialize(node=nil)
3131
@name = nil
3232
@params = Hash.new
3333
@recipe = nil
34+
@node = node
3435
end
3536

3637
def define(resource_name, prototype_params=nil, &block)

chef/spec/unit/recipe_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@
108108
@recipe.resources(:zen_master => "lao tzu").name.should eql("lao tzu")
109109
@recipe.resources(:zen_master => "lao tzu").something.should eql(true)
110110
end
111+
112+
it "should set the node on defined resources" do
113+
crow_define = Chef::ResourceDefinition.new
114+
crow_define.define :crow, :peace => false, :something => true do
115+
zen_master "lao tzu" do
116+
peace params[:peace]
117+
something params[:something]
118+
end
119+
end
120+
@recipe.definitions[:crow] = crow_define
121+
@recipe.node[:foo] = false
122+
@recipe.crow "mine" do
123+
something node[:foo]
124+
end
125+
@recipe.resources(:zen_master => "lao tzu").something.should eql(false)
126+
end
111127

112128
it "should load a resource from a ruby file" do
113129
@recipe.from_file(File.join(File.dirname(__FILE__), "..", "data", "recipes", "test.rb"))

chef/spec/unit/resource_definition_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,37 @@
2323
@def = Chef::ResourceDefinition.new()
2424
end
2525

26+
describe "initialize" do
27+
it "should be a Chef::ResourceDefinition" do
28+
@def.should be_a_kind_of(Chef::ResourceDefinition)
29+
end
30+
31+
it "should not initialize a new node if one is not provided" do
32+
@def.node.should eql(nil)
33+
end
34+
35+
it "should accept a node as an argument" do
36+
node = Chef::Node.new
37+
node.name("bobo")
38+
@def = Chef::ResourceDefinition.new(node)
39+
@def.node.name.should == "bobo"
40+
end
41+
end
42+
43+
describe "node" do
44+
it "should set the node with node=" do
45+
node = Chef::Node.new
46+
node.name("bobo")
47+
@def.node = node
48+
@def.node.name.should == "bobo"
49+
end
50+
51+
it "should return the node" do
52+
@def.node = Chef::Node.new
53+
@def.node.should be_a_kind_of(Chef::Node)
54+
end
55+
end
56+
2657
it "should accept a new definition with a symbol for a name" do
2758
lambda {
2859
@def.define :smoke do
@@ -84,4 +115,5 @@
84115
@def.name = :woot
85116
@def.to_s.should eql("woot")
86117
end
118+
87119
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gnome Mash.new
2+
gnome[:underpants] = [ :one, :two, :three, :four, :five ]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
define :gnome do
2+
params[:underpants].each do |undies|
3+
execute "echo gnome #{undies}" do
4+
command "echo 'gnome has underpants #{undies}'"
5+
end
6+
end
7+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#
2+
# Cookbook Name:: CHEF-94
3+
# Recipe:: default
4+
#
5+
# Copyright 2009, Opscode
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+
gnome "mygnome" do
21+
underpants :one, :two, :three, :four
22+
end
23+
24+
gnome "mygnome" do
25+
underpants [ :one, :two ]
26+
end
27+
28+
puts node[:gnome][:underpants].inspect
29+
30+
gnome "mygnome" do
31+
underpants node[:gnome][:underpants]
32+
end

0 commit comments

Comments
 (0)