From a81b5b8716d311b25a46bb6dca777f88e46b2348 Mon Sep 17 00:00:00 2001 From: Mike Dorman Date: Fri, 15 Jan 2016 16:52:12 -0700 Subject: [PATCH] Allow metadata hash for nova_aggregate resources Allow the metadata parameter to the nova_aggregate provider to take a hash of key/value pairs, in addition to a comma- delimited list as a string. Also better handle existing metadata values which contain commas. Change-Id: I148def3be059d87fa9aa8f748cd3a5ec7770473a Closes-bug: 1534853 --- lib/puppet/provider/nova.rb | 23 ++++++++++++++------ lib/puppet/type/nova_aggregate.rb | 6 ++++++ spec/type/nova_aggregate_spec.rb | 36 +++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/lib/puppet/provider/nova.rb b/lib/puppet/provider/nova.rb index 1faa51bb7..c19310512 100644 --- a/lib/puppet/provider/nova.rb +++ b/lib/puppet/provider/nova.rb @@ -121,12 +121,23 @@ class Puppet::Provider::Nova < Puppet::Provider else new = [] end - s.split(",").each do |el| - ret = str2hash(el.strip()) - if s.include? "=" - new.update(ret) - else - new.push(ret) + if s =~ /^'.+'$/ + s.split("', '").each do |el| + ret = str2hash(el.strip()) + if s.include? "=" + new.update(ret) + else + new.push(ret) + end + end + else + s.split(",").each do |el| + ret = str2hash(el.strip()) + if s.include? "=" + new.update(ret) + else + new.push(ret) + end end end return new diff --git a/lib/puppet/type/nova_aggregate.rb b/lib/puppet/type/nova_aggregate.rb index ccc6b9c5e..6620c29a7 100644 --- a/lib/puppet/type/nova_aggregate.rb +++ b/lib/puppet/type/nova_aggregate.rb @@ -80,6 +80,9 @@ Puppet::Type.newtype(:nova_aggregate) do desc 'The metadata of the aggregate' #convert DSL/string form to internal form which is a single hash munge do |value| + if value.is_a?(Hash) + return value + end internal = Hash.new value.split(",").map{|el| el.strip()}.each do |pair| key, value = pair.split("=", 2) @@ -89,6 +92,9 @@ Puppet::Type.newtype(:nova_aggregate) do end validate do |value| + if value.is_a?(Hash) + return true + end value.split(",").each do |kv| raise ArgumentError, "Key/value pairs must be separated by an =" unless value.include?("=") end diff --git a/spec/type/nova_aggregate_spec.rb b/spec/type/nova_aggregate_spec.rb index de300b1e7..7d859a6a1 100644 --- a/spec/type/nova_aggregate_spec.rb +++ b/spec/type/nova_aggregate_spec.rb @@ -33,6 +33,20 @@ describe Puppet::Type.type(:nova_aggregate) do :hosts => "host1, host2")).to_not be_nil end + it "should be able to create an more complex instance with hash for metadata" do + expect(described_class.new(:name => 'agg0', + :availability_zone => 'myzone', + :metadata => { 'a' => 'b', 'c' => 'd' }, + :hosts => "host1, host2")).to_not be_nil + end + + it "should be able to create an more complex instance with hash for metadata and values containing commas" do + expect(described_class.new(:name => 'agg0', + :availability_zone => 'myzone', + :metadata => { 'a' => 'b,e,f,g', 'c' => 'd,h,i,j' }, + :hosts => "host1, host2")).to_not be_nil + end + it "should be able to create a instance and have the default values" do c = described_class.new(:name => 'agg0') expect(c[:name]).to eq("agg0") @@ -52,6 +66,28 @@ describe Puppet::Type.type(:nova_aggregate) do expect(c[:hosts]).to eq(["host1" , "host2"]) end + it "should return the given values with hash for metadata" do + c = described_class.new(:name => 'agg0', + :availability_zone => 'myzone', + :metadata => { 'a' => 'b', 'c' => 'd' },, + :hosts => " host1, host2 ") + expect(c[:name]).to eq("agg0") + expect(c[:availability_zone]).to eq("myzone") + expect(c[:metadata]).to eq({"a" => "b", "c" => "d"}) + expect(c[:hosts]).to eq(["host1" , "host2"]) + end + + it "should return the given values with hash for metadata and values containing commas" do + c = described_class.new(:name => 'agg0', + :availability_zone => 'myzone', + :metadata => { 'a' => 'b,e,f,g', 'c' => 'd,h,i,j' }, + :hosts => " host1, host2 ") + expect(c[:name]).to eq("agg0") + expect(c[:availability_zone]).to eq("myzone") + expect(c[:metadata]).to eq({"a" => "b", "c" => "d"}) + expect(c[:hosts]).to eq(["host1" , "host2"]) + end + it "should return the given values" do c = described_class.new(:name => 'agg0', :availability_zone => "",