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
This commit is contained in:
Mike Dorman
2016-01-15 16:52:12 -07:00
parent 32fb6134c0
commit a81b5b8716
3 changed files with 59 additions and 6 deletions

View File

@@ -121,12 +121,23 @@ class Puppet::Provider::Nova < Puppet::Provider
else else
new = [] new = []
end end
s.split(",").each do |el| if s =~ /^'.+'$/
ret = str2hash(el.strip()) s.split("', '").each do |el|
if s.include? "=" ret = str2hash(el.strip())
new.update(ret) if s.include? "="
else new.update(ret)
new.push(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
end end
return new return new

View File

@@ -80,6 +80,9 @@ Puppet::Type.newtype(:nova_aggregate) do
desc 'The metadata of the aggregate' desc 'The metadata of the aggregate'
#convert DSL/string form to internal form which is a single hash #convert DSL/string form to internal form which is a single hash
munge do |value| munge do |value|
if value.is_a?(Hash)
return value
end
internal = Hash.new internal = Hash.new
value.split(",").map{|el| el.strip()}.each do |pair| value.split(",").map{|el| el.strip()}.each do |pair|
key, value = pair.split("=", 2) key, value = pair.split("=", 2)
@@ -89,6 +92,9 @@ Puppet::Type.newtype(:nova_aggregate) do
end end
validate do |value| validate do |value|
if value.is_a?(Hash)
return true
end
value.split(",").each do |kv| value.split(",").each do |kv|
raise ArgumentError, "Key/value pairs must be separated by an =" unless value.include?("=") raise ArgumentError, "Key/value pairs must be separated by an =" unless value.include?("=")
end end

View File

@@ -33,6 +33,20 @@ describe Puppet::Type.type(:nova_aggregate) do
:hosts => "host1, host2")).to_not be_nil :hosts => "host1, host2")).to_not be_nil
end 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 it "should be able to create a instance and have the default values" do
c = described_class.new(:name => 'agg0') c = described_class.new(:name => 'agg0')
expect(c[:name]).to eq("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"]) expect(c[:hosts]).to eq(["host1" , "host2"])
end 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 it "should return the given values" do
c = described_class.new(:name => 'agg0', c = described_class.new(:name => 'agg0',
:availability_zone => "", :availability_zone => "",