Skip to content

Commit 8aaaece

Browse files
author
Seth Falcon
committed
Move update logic from nodes controller to node model object
1 parent 9f15f3d commit 8aaaece

3 files changed

Lines changed: 49 additions & 7 deletions

File tree

chef-server-api/app/controllers/nodes.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,7 @@ def update
6262
raise NotFound, "Cannot load node #{params[:id]}"
6363
end
6464

65-
updated = params['inflated_object']
66-
@node.run_list.reset!(updated.run_list)
67-
@node.automatic_attrs = updated.automatic_attrs
68-
@node.normal_attrs = updated.normal_attrs
69-
@node.override_attrs = updated.override_attrs
70-
@node.default_attrs = updated.default_attrs
71-
@node.chef_environment(updated.chef_environment)
65+
@node.update_from!(params['inflated_object'])
7266
@node.cdb_save
7367
@node.couchdb_rev = nil
7468
display(@node)

chef/lib/chef/node.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,16 @@ def to_json(*a)
471471
result.to_json(*a)
472472
end
473473

474+
def update_from!(o)
475+
run_list.reset!(o.run_list)
476+
@automatic_attrs = o.automatic_attrs
477+
@normal_attrs = o.normal_attrs
478+
@override_attrs = o.override_attrs
479+
@default_attrs = o.default_attrs
480+
chef_environment(o.chef_environment)
481+
self
482+
end
483+
474484
# Create a Chef::Node from JSON
475485
def self.json_create(o)
476486
node = new

chef/spec/unit/node_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,44 @@
491491
end
492492
end
493493

494+
describe "update_from!" do
495+
before(:each) do
496+
@node.name("orig")
497+
@node.chef_environment("dev")
498+
@node.default_attrs = { "one" => { "two" => "three", "four" => "five", "eight" => "nine" } }
499+
@node.override_attrs = { "one" => { "two" => "three", "four" => "six" } }
500+
@node.normal_attrs = { "one" => { "two" => "seven" } }
501+
@node.run_list << "role[marxist]"
502+
@node.run_list << "role[leninist]"
503+
@node.run_list << "recipe[stalinist]"
504+
505+
@example = Chef::Node.new()
506+
@example.name("newname")
507+
@example.chef_environment("prod")
508+
@example.default_attrs = { "alpha" => { "bravo" => "charlie", "delta" => "echo" } }
509+
@example.override_attrs = { "alpha" => { "bravo" => "foxtrot", "delta" => "golf" } }
510+
@example.normal_attrs = { "alpha" => { "bravo" => "hotel" } }
511+
@example.run_list << "role[comedy]"
512+
@example.run_list << "role[drama]"
513+
@example.run_list << "recipe[mystery]"
514+
end
515+
516+
it "allows update of everything except name" do
517+
@node.update_from!(@example)
518+
@node.name.should == "orig"
519+
@node.chef_environment.should == @example.chef_environment
520+
@node.default_attrs.should == @example.default_attrs
521+
@node.override_attrs.should == @example.override_attrs
522+
@node.normal_attrs.should == @example.normal_attrs
523+
@node.run_list.should == @example.run_list
524+
end
525+
526+
it "should not update the name of the node" do
527+
@node.should_not_receive(:name).with(@example.name)
528+
@node.update_from!(@example)
529+
end
530+
end
531+
494532
describe "to_hash" do
495533
it "should serialize itself as a hash" do
496534
@node.chef_environment("dev")

0 commit comments

Comments
 (0)