diff --git a/providers/location.rb b/providers/location.rb index 99fffd4..fcf7e1e 100644 --- a/providers/location.rb +++ b/providers/location.rb @@ -17,44 +17,56 @@ # limitations under the License. # -require ::File.expand_path('../libraries/pacemaker/cib_object', +require ::File.expand_path('../libraries/pacemaker', ::File.dirname(__FILE__)) +require ::File.expand_path('../libraries/chef/mixin/pacemaker', ::File.dirname(__FILE__)) +include Chef::Mixin::Pacemaker::StandardCIBObject + action :create do name = new_resource.name - rsc = new_resource.rsc - priority = new_resource.priority - loc = new_resource.loc - unless resource_exists?(name) - cmd = "crm configure location #{name} #{rsc} #{priority}: #{loc}" - - cmd_ = Mixlib::ShellOut.new(cmd) - cmd_.environment['HOME'] = ENV.fetch('HOME', '/root') - cmd_.run_command - begin - cmd_.error! - if resource_exists?(name) - new_resource.updated_by_last_action(true) - Chef::Log.info "Successfully configured location '#{name}'." - else - Chef::Log.error "Failed to configure location #{name}." - end - rescue - Chef::Log.error "Failed to configure location #{name}." - end + if @current_resource_definition.nil? + create_resource(name) + else + maybe_modify_resource(name) end end action :delete do - name = new_resource.name - cmd = "crm resource stop #{name}; crm configure delete #{name}" - - e = execute "delete location #{name}" do - command cmd - only_if { resource_exists?(name) } - end - - new_resource.updated_by_last_action(e.updated?) - Chef::Log.info "Deleted location '#{name}'." + next unless @current_resource + standard_delete_resource +end + +def cib_object_class + ::Pacemaker::Constraint::Location +end + +def load_current_resource + standard_load_current_resource +end + +def init_current_resource + name = @new_resource.name + @current_resource = Chef::Resource::PacemakerLocation.new(name) + attrs = [:rsc, :score, :node] + @current_cib_object.copy_attrs_to_chef_resource(@current_resource, *attrs) +end + +def create_resource(name) + standard_create_resource +end + +def maybe_modify_resource(name) + Chef::Log.info "Checking existing #{@current_cib_object} for modifications" + + desired_location = cib_object_class.from_chef_resource(new_resource) + if desired_location.definition_string != @current_cib_object.definition_string + Chef::Log.debug "changed from [#{@current_cib_object.definition_string}] to [#{desired_location.definition_string}]" + cmd = desired_location.reconfigure_command + execute cmd do + action :nothing + end.run_action(:run) + new_resource.updated_by_last_action(true) + end end diff --git a/resources/location.rb b/resources/location.rb index e6d46cc..cd635cc 100644 --- a/resources/location.rb +++ b/resources/location.rb @@ -21,7 +21,7 @@ actions :create, :delete default_action :create -attribute :name, :kind_of => String, :name_attribute => true -attribute :rsc, :kind_of => String -attribute :priority, :kind_of => String -attribute :loc, :kind_of => String +attribute :name, :kind_of => String, :name_attribute => true +attribute :rsc, :kind_of => String +attribute :score, :kind_of => String +attribute :node, :kind_of => String diff --git a/spec/providers/location_spec.rb b/spec/providers/location_spec.rb new file mode 100644 index 0000000..9585336 --- /dev/null +++ b/spec/providers/location_spec.rb @@ -0,0 +1,83 @@ +require 'chef/application' +require File.expand_path('../spec_helper', File.dirname(__FILE__)) +require File.expand_path('../helpers/cib_object', File.dirname(__FILE__)) +require File.expand_path('../fixtures/location_constraint', File.dirname(__FILE__)) + +describe "Chef::Provider::PacemakerLocation" do + # for use inside examples: + let(:fixture) { Chef::RSpec::Pacemaker::Config::LOCATION_CONSTRAINT.dup } + # for use outside examples (e.g. when invoking shared_examples) + fixture = Chef::RSpec::Pacemaker::Config::LOCATION_CONSTRAINT.dup + + before(:each) do + runner_opts = { + :step_into => ['pacemaker_location'] + } + @chef_run = ::ChefSpec::Runner.new(runner_opts) + @chef_run.converge "pacemaker::default" + @node = @chef_run.node + @run_context = @chef_run.run_context + + @resource = Chef::Resource::PacemakerLocation.new(fixture.name, @run_context) + @resource.rsc fixture.rsc + @resource.score fixture.score + @resource.node fixture.node.dup + end + + let (:provider) { Chef::Provider::PacemakerLocation.new(@resource, @run_context) } + + def cib_object_class + Pacemaker::Constraint::Location + end + + include Chef::RSpec::Pacemaker::CIBObject + + describe ":create action" do + def test_modify(expected_cmds) + yield + + stub_shellout(fixture.definition_string) + + provider.run_action :create + + expected_cmds.each do |cmd| + expect(@chef_run).to run_execute(cmd) + end + expect(@resource).to be_updated + end + + it "should modify the constraint if it has a different resource" do + new_resource = 'group2' + fixture.rsc = new_resource + expected_configure_cmd_args = [fixture.reconfigure_command] + test_modify(expected_configure_cmd_args) do + @resource.rsc new_resource + end + end + + it "should modify the constraint if it has a different score" do + new_score = '100' + fixture.score = new_score + expected_configure_cmd_args = [fixture.reconfigure_command] + test_modify(expected_configure_cmd_args) do + @resource.score new_score + end + end + + it "should modify the constraint if it has a different node" do + new_node = 'node2' + fixture.node = new_node + expected_configure_cmd_args = [fixture.reconfigure_command] + test_modify(expected_configure_cmd_args) do + @resource.node new_node + end + end + + end + + describe ":delete action" do + it_should_behave_like "action on non-existent resource", \ + :delete, "crm configure delete #{fixture.name}", nil + end + +end