Skip to content

Commit ef2b889

Browse files
author
Seth Falcon
committed
Add node.chef_environment attr to node
chef_environment must be a non-blank string or nil. To follow the getter/setter pattern that facilitates setting values in the DSL without '=', we introduce a NIL_ARGUMENT constant to use instead of nil for the getter case.
1 parent 3d04067 commit ef2b889

5 files changed

Lines changed: 62 additions & 6 deletions

File tree

chef/lib/chef.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#
1818

1919
require 'chef/version'
20-
20+
require 'chef/nil_argument'
2121
require 'extlib'
2222
require 'chef/exceptions'
2323
require 'chef/log'

chef/lib/chef/nil_argument.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Chef
2+
NIL_ARGUMENT = Object.new
3+
end

chef/lib/chef/node.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
require 'chef/config'
2323
require 'chef/cookbook/cookbook_collection'
24+
require 'chef/nil_argument'
2425
require 'chef/mixin/check_helper'
2526
require 'chef/mixin/params_validate'
2627
require 'chef/mixin/from_file'
@@ -78,7 +79,7 @@ class Node
7879
"map" => <<-EOJS
7980
function(doc) {
8081
if (doc.chef_type == "node") {
81-
var to_emit = { "name": doc.name };
82+
var to_emit = { "name": doc.name, "chef_environment": doc.chef_environment };
8283
if (doc["attributes"]["fqdn"]) {
8384
to_emit["fqdn"] = doc["attributes"]["fqdn"];
8485
} else {
@@ -138,7 +139,8 @@ class Node
138139
# Create a new Chef::Node object.
139140
def initialize(couchdb=nil)
140141
@name = nil
141-
142+
143+
@chef_environment = nil
142144
@normal_attrs = Mash.new
143145
@override_attrs = Mash.new
144146
@default_attrs = Mash.new
@@ -206,6 +208,23 @@ def name(arg=nil)
206208
end
207209
end
208210

211+
def chef_environment(arg=Chef::NIL_ARGUMENT)
212+
if arg != Chef::NIL_ARGUMENT
213+
if arg.nil?
214+
@chef_environment = nil
215+
else
216+
validate(
217+
{:name => arg },
218+
{:name => { :kind_of => String,
219+
:cannot_be => :blank}
220+
})
221+
@chef_environment = arg
222+
end
223+
else
224+
@chef_environment
225+
end
226+
end
227+
209228
# Used by the DSL
210229
def attribute
211230
construct_attributes
@@ -425,6 +444,7 @@ def to_hash
425444
index_hash = Hash.new
426445
index_hash["chef_type"] = "node"
427446
index_hash["name"] = name
447+
index_hash["chef_environment"] = chef_environment
428448
attribute.each do |key, value|
429449
index_hash[key] = value
430450
end
@@ -438,6 +458,7 @@ def to_hash
438458
def to_json(*a)
439459
result = {
440460
"name" => name,
461+
"chef_environment" => chef_environment,
441462
'json_class' => self.class.name,
442463
"automatic" => automatic_attrs,
443464
"normal" => normal_attrs,
@@ -454,7 +475,7 @@ def to_json(*a)
454475
def self.json_create(o)
455476
node = new
456477
node.name(o["name"])
457-
478+
node.chef_environment(o["chef_environment"])
458479
if o.has_key?("attributes")
459480
node.normal_attrs = o["attributes"]
460481
end

chef/spec/data/nodes/test.example.com.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
# Nodes should have recipes
1414
##
1515
recipes "operations-master", "operations-monitoring"
16+
17+
chef_environment "dev"

chef/spec/unit/node_spec.rb

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,33 @@
9696

9797
end
9898

99-
describe "when modifying Node attributes" do
100-
it "loads attributes from cookbooks" do
99+
describe "chef_environment" do
100+
it "should set an environment with chef_environment(something)" do
101+
lambda { @node.chef_environment("latte") }.should_not raise_error
102+
end
103+
104+
it "should return the chef_environment with chef_environment()" do
105+
@node.chef_environment("latte")
106+
@node.chef_environment.should == "latte"
107+
end
108+
109+
it "should disallow non-strings (except for nil)" do
110+
lambda { @node.chef_environment(Hash.new) }.should raise_error(ArgumentError)
111+
end
112+
113+
it "can be nil" do
114+
@node.chef_environment("foo")
115+
@node.chef_environment(nil)
116+
@node.chef_environment.should == nil
117+
end
118+
119+
it "cannot be blank" do
120+
lambda { @node.chef_environment("")}.should raise_error(Chef::Exceptions::ValidationFailed)
121+
end
122+
end
123+
124+
describe "attributes" do
125+
it "should be loaded from the node's cookbooks" do
101126
Chef::Config.cookbook_path = File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "cookbooks"))
102127
@node.cookbook_collection = Chef::CookbookCollection.new(Chef::CookbookLoader.new)
103128
@node.load_attributes
@@ -439,6 +464,7 @@
439464
it "should load a node from a file by fqdn" do
440465
@node.find_file("test.example.com")
441466
@node.name.should == "test.example.com"
467+
@node.chef_environment.should == "dev"
442468
end
443469

444470
it "should load a node from a file by hostname" do
@@ -467,6 +493,7 @@
467493

468494
describe "to_hash" do
469495
it "should serialize itself as a hash" do
496+
@node.chef_environment("dev")
470497
@node.default_attrs = { "one" => { "two" => "three", "four" => "five", "eight" => "nine" } }
471498
@node.override_attrs = { "one" => { "two" => "three", "four" => "six" } }
472499
@node.normal_attrs = { "one" => { "two" => "seven" } }
@@ -482,6 +509,7 @@
482509
h["run_list"].should be_include("role[marxist]")
483510
h["run_list"].should be_include("role[leninist]")
484511
h["run_list"].should be_include("recipe[stalinist]")
512+
h["chef_environment"].should == "dev"
485513
end
486514
end
487515

@@ -491,6 +519,7 @@
491519
json = @node.to_json()
492520
json.should =~ /json_class/
493521
json.should =~ /name/
522+
json.should =~ /chef_environment/
494523
json.should =~ /normal/
495524
json.should =~ /default/
496525
json.should =~ /override/
@@ -503,6 +532,7 @@
503532
serialized_node = JSON.parse(json)
504533
serialized_node.should be_a_kind_of(Chef::Node)
505534
serialized_node.name.should eql(@node.name)
535+
serialized_node.chef_environment.should eql(@node.chef_environment)
506536
@node.each_attribute do |k,v|
507537
serialized_node[k].should eql(v)
508538
end

0 commit comments

Comments
 (0)