From ac1f93068182bbd4ed938fe97451f779b0ff4f7d Mon Sep 17 00:00:00 2001 From: Adam Spiers Date: Thu, 6 Feb 2014 18:22:28 +0000 Subject: [PATCH] move extract_hash to Pacemaker::Resource It is needed by the imminent Pacemaker::Resource::Group, not just by Pacemaker::Resource::Primitive. --- libraries/pacemaker/resource.rb | 26 +++++++++++++++++++ libraries/pacemaker/resource/primitive.rb | 26 ------------------- .../pacemaker/resource/primitive_spec.rb | 17 ------------ spec/libraries/pacemaker/resource_spec.rb | 21 +++++++++++++++ 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/libraries/pacemaker/resource.rb b/libraries/pacemaker/resource.rb index b8d9ce1..10763b8 100644 --- a/libraries/pacemaker/resource.rb +++ b/libraries/pacemaker/resource.rb @@ -24,5 +24,31 @@ module Pacemaker "crm resource stop '#{name}'" end + # CIB object definitions look something like: + # + # primitive keystone ocf:openstack:keystone \ + # params os_username="crowbar" os_password="crowbar" os_tenant_name="openstack" \ + # meta target-role="Started" is-managed="true" \ + # op monitor interval="10" timeout=30s \ + # op start interval="10s" timeout="240" \ + # + # This method extracts a Hash from one of the params / meta / op lines. + def self.extract_hash(obj_definition, data_type) + unless obj_definition =~ /\s+#{data_type} (.+?)\s*\\?$/ + return {} + end + + h = {} + Shellwords.split($1).each do |kvpair| + break if kvpair == 'op' + unless kvpair =~ /^(.+?)=(.+)$/ + raise "Couldn't understand '#{kvpair}' for '#{data_type}' section "\ + "of #{name} primitive (definition was [#{obj_definition}])" + end + h[$1] = $2.sub(/^"(.*)"$/, "\1") + end + h + end + end end diff --git a/libraries/pacemaker/resource/primitive.rb b/libraries/pacemaker/resource/primitive.rb index 2da7c23..56d710d 100644 --- a/libraries/pacemaker/resource/primitive.rb +++ b/libraries/pacemaker/resource/primitive.rb @@ -96,30 +96,4 @@ class Pacemaker::Resource::Primitive < Pacemaker::Resource end.compact.join(' ') end - # CIB object definitions look something like: - # - # primitive keystone ocf:openstack:keystone \ - # params os_username="crowbar" os_password="crowbar" os_tenant_name="openstack" \ - # meta target-role="Started" is-managed="true" \ - # op monitor interval="10" timeout=30s \ - # op start interval="10s" timeout="240" \ - # - # This method extracts a Hash from one of the params / meta / op lines. - def self.extract_hash(obj_definition, data_type) - unless obj_definition =~ /\s+#{data_type} (.+?)\s*\\?$/ - return {} - end - - h = {} - Shellwords.split($1).each do |kvpair| - break if kvpair == 'op' - unless kvpair =~ /^(.+?)=(.+)$/ - raise "Couldn't understand '#{kvpair}' for '#{data_type}' section "\ - "of #{name} primitive (definition was [#{obj_definition}])" - end - h[$1] = $2.sub(/^"(.*)"$/, "\1") - end - h - end - end diff --git a/spec/libraries/pacemaker/resource/primitive_spec.rb b/spec/libraries/pacemaker/resource/primitive_spec.rb index 358dcd6..6293155 100644 --- a/spec/libraries/pacemaker/resource/primitive_spec.rb +++ b/spec/libraries/pacemaker/resource/primitive_spec.rb @@ -89,23 +89,6 @@ describe Pacemaker::Resource::Primitive do end end - describe "::extract_hash" do - it "should extract a params hash from config" do - expect(fixture.class.extract_hash(fixture.definition_string, "params")).to \ - eq(Hash[fixture.params]) - end - - it "should extract an op start hash from config" do - expect(fixture.class.extract_hash(fixture.definition_string, 'op start')).to \ - eq(Hash[fixture.op]['start']) - end - - it "should extract an op monitor hash from config" do - expect(fixture.class.extract_hash(fixture.definition_string, 'op monitor')).to \ - eq(Hash[fixture.op]['monitor']) - end - end - describe "#definition_string" do it "should return the definition string" do expect(fixture.definition_string).to eq(fixture_definition) diff --git a/spec/libraries/pacemaker/resource_spec.rb b/spec/libraries/pacemaker/resource_spec.rb index e196912..a49aa5f 100644 --- a/spec/libraries/pacemaker/resource_spec.rb +++ b/spec/libraries/pacemaker/resource_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' require File.expand_path('../../../libraries/pacemaker/resource', File.dirname(__FILE__)) +require File.expand_path('../../fixtures/keystone_primitive', + File.dirname(__FILE__)) describe Pacemaker::Resource do describe "#running?" do @@ -25,4 +27,23 @@ describe Pacemaker::Resource do expect(rsc.running?).to be(false) end end + + describe "::extract_hash" do + let(:fixture) { Chef::RSpec::Pacemaker::Config::KEYSTONE_PRIMITIVE.dup } + + it "should extract a params hash from config" do + expect(fixture.class.extract_hash(fixture.definition_string, "params")).to \ + eq(Hash[fixture.params]) + end + + it "should extract an op start hash from config" do + expect(fixture.class.extract_hash(fixture.definition_string, 'op start')).to \ + eq(Hash[fixture.op]['start']) + end + + it "should extract an op monitor hash from config" do + expect(fixture.class.extract_hash(fixture.definition_string, 'op monitor')).to \ + eq(Hash[fixture.op]['monitor']) + end + end end