diff --git a/chef/lib/chef/provider/group.rb b/chef/lib/chef/provider/group.rb index a9cea021070..56b3638f358 100644 --- a/chef/lib/chef/provider/group.rb +++ b/chef/lib/chef/provider/group.rb @@ -52,6 +52,14 @@ def load_current_resource @current_resource end + + def define_resource_requirements + requirements.assert(:modify) do |a| + a.assertion { @group_exists } + a.failure_message(Chef::Exceptions::Group, "Cannot modify #{@new_resource} - group does not exist!") + a.whyrun("Group #{@new_resource} does not exist. Unless it would have been created earlier in this run, this attempt to modify it would fail.") + end + end # Check to see if a group needs any changes # @@ -76,43 +84,45 @@ def compare_group def action_create case @group_exists when false - create_group - Chef::Log.info("#{@new_resource} created") - @new_resource.updated_by_last_action(true) + converge_by("would create #{@new_resource}") do + create_group + Chef::Log.info("#{@new_resource} created") + end else if compare_group - manage_group - Chef::Log.info("#{@new_resource} altered") - @new_resource.updated_by_last_action(true) + converge_by("would alter group #{@new_resource}") do + manage_group + Chef::Log.info("#{@new_resource} altered") + end end end end def action_remove if @group_exists - remove_group - @new_resource.updated_by_last_action(true) - Chef::Log.info("#{@new_resource} removed") + converge_by("would remove group #{@new_resource}") do + remove_group + Chef::Log.info("#{@new_resource} removed") + end + end end def action_manage if @group_exists && compare_group - manage_group - @new_resource.updated_by_last_action(true) - Chef::Log.info("#{@new_resource} managed") + converge_by("would manage group #{@new_resource}") do + manage_group + Chef::Log.info("#{@new_resource} managed") + end end end def action_modify - if @group_exists - if compare_group + if compare_group + converge_by("would modify group #{@new_resource}") do manage_group - @new_resource.updated_by_last_action(true) Chef::Log.info("#{@new_resource} modified") end - else - raise Chef::Exceptions::Group, "Cannot modify #{@new_resource} - group does not exist!" end end diff --git a/chef/spec/unit/provider/group_spec.rb b/chef/spec/unit/provider/group_spec.rb index c7833397d3b..858e6e7aeae 100644 --- a/chef/spec/unit/provider/group_spec.rb +++ b/chef/spec/unit/provider/group_spec.rb @@ -114,20 +114,20 @@ it "should call create_group if the group does not exist" do @provider.group_exists = false @provider.should_receive(:create_group).and_return(true) - @provider.action_create + @provider.run_action(:create) end it "should set the the new_resources updated flag when it creates the group" do @provider.group_exists = false @provider.stub!(:create_group) - @provider.action_create + @provider.run_action(:create) @provider.new_resource.should be_updated end it "should check to see if the group has mismatched attributes if the group exists" do @provider.group_exists = true @provider.stub!(:compare_group).and_return(false) - @provider.action_create + @provider.run_action(:create) @provider.new_resource.should_not be_updated end @@ -135,14 +135,14 @@ @provider.group_exists = true @provider.stub!(:compare_group).and_return(true) @provider.should_receive(:manage_group).and_return(true) - @provider.action_create + @provider.run_action(:create) end it "should set the the new_resources updated flag when it creates the group if we call manage_group" do @provider.group_exists = true @provider.stub!(:compare_group).and_return(true) @provider.stub!(:manage_group).and_return(true) - @provider.action_create + @provider.run_action(:create) @new_resource.should be_updated end end @@ -152,14 +152,14 @@ it "should not call remove_group if the group does not exist" do @provider.group_exists = false @provider.should_not_receive(:remove_group) - @provider.action_remove + @provider.run_action(:remove) @provider.new_resource.should_not be_updated end it "should call remove_group if the group exists" do @provider.group_exists = true @provider.should_receive(:remove_group) - @provider.action_remove + @provider.run_action(:remove) @provider.new_resource.should be_updated end end @@ -173,26 +173,26 @@ it "should run manage_group if the group exists and has mismatched attributes" do @provider.should_receive(:compare_group).and_return(true) @provider.should_receive(:manage_group).and_return(true) - @provider.action_manage + @provider.run_action(:manage) end it "should set the new resources updated flag to true if manage_group is called" do @provider.stub!(:compare_group).and_return(true) @provider.stub!(:manage_group).and_return(true) - @provider.action_manage + @provider.run_action(:manage) @new_resource.should be_updated end it "should not run manage_group if the group does not exist" do @provider.group_exists = false @provider.should_not_receive(:manage_group) - @provider.action_manage + @provider.run_action(:manage) end it "should not run manage_group if the group exists but has no differing attributes" do @provider.should_receive(:compare_group).and_return(false) @provider.should_not_receive(:manage_group) - @provider.action_manage + @provider.run_action(:manage) end end @@ -205,25 +205,25 @@ it "should run manage_group if the group exists and has mismatched attributes" do @provider.should_receive(:compare_group).and_return(true) @provider.should_receive(:manage_group).and_return(true) - @provider.action_modify + @provider.run_action(:modify) end it "should set the new resources updated flag to true if manage_group is called" do @provider.stub!(:compare_group).and_return(true) @provider.stub!(:manage_group).and_return(true) - @provider.action_modify + @provider.run_action(:modify) @new_resource.should be_updated end it "should not run manage_group if the group exists but has no differing attributes" do @provider.should_receive(:compare_group).and_return(false) @provider.should_not_receive(:manage_group) - @provider.action_modify + @provider.run_action(:modify) end it "should raise a Chef::Exceptions::Group if the group doesn't exist" do @provider.group_exists = false - lambda { @provider.action_modify }.should raise_error(Chef::Exceptions::Group) + lambda { @provider.run_action(:modify) }.should raise_error(Chef::Exceptions::Group) end end end