diff --git a/lib/puppet/provider/swift_ring_builder.rb b/lib/puppet/provider/swift_ring_builder.rb index 7a1bf45b..2b30cc8b 100644 --- a/lib/puppet/provider/swift_ring_builder.rb +++ b/lib/puppet/provider/swift_ring_builder.rb @@ -8,6 +8,14 @@ class Puppet::Provider::SwiftRingBuilder < Puppet::Provider end end + def self.address_string(address) + ip = IPAddr.new(address) + if ip.ipv6? + '[' + ip.to_s + ']' + else + ip.to_s + end + end def self.lookup_ring object_hash = {} @@ -30,7 +38,8 @@ class Puppet::Provider::SwiftRingBuilder < Puppet::Provider # Swift 1.8+ output example: if row =~ /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\d+)\s+\S+\s+\d+\s+(\S+)\s+(\d+\.\d+)\s+(\d+)\s*((-|\s-?)?\d+\.\d+)\s*(\S*)/ - object_hash["#{$4}:#{$5}/#{$6}"] = { + address = address_string("#{$4}") + object_hash["#{address}:#{$5}/#{$6}"] = { :id => $1, :region => $2, :zone => $3, @@ -43,7 +52,8 @@ class Puppet::Provider::SwiftRingBuilder < Puppet::Provider # Swift 1.8.0 output example: elsif row =~ /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\d+)\s+(\S+)\s+(\d+\.\d+)\s+(\d+)\s*((-|\s-?)?\d+\.\d+)\s*(\S*)/ - object_hash["#{$4}:#{$5}/#{$6}"] = { + address = address_string("#{$4}") + object_hash["#{address}:#{$5}/#{$6}"] = { :id => $1, :region => $2, :zone => $3, @@ -55,7 +65,8 @@ class Puppet::Provider::SwiftRingBuilder < Puppet::Provider # This regex is for older swift versions elsif row =~ /^\s+(\d+)\s+(\d+)\s+(\S+)\s+(\d+)\s+(\S+)\s+(\d+\.\d+)\s+(\d+)\s+(-?\d+\.\d+)\s+(\S*)$/ - object_hash["#{$3}:#{$4}/#{$5}"] = { + address = address_string("#{$3}") + object_hash["#{address}:#{$4}/#{$5}"] = { :id => $1, :region => 'none', :zone => $2, diff --git a/lib/puppet/type/ring_account_device.rb b/lib/puppet/type/ring_account_device.rb index 5174f23d..6882193a 100644 --- a/lib/puppet/type/ring_account_device.rb +++ b/lib/puppet/type/ring_account_device.rb @@ -1,15 +1,19 @@ Puppet::Type.newtype(:ring_account_device) do require 'ipaddr' + require 'uri' ensurable newparam(:name, :namevar => true) do validate do |value| - address = value.split(':') - raise(Puppet::Error, "invalid name #{value}, should contain address:port/device") unless address.size == 2 - port_device = address[1].split('/') - raise(Puppet::Error, "namevar should contain a device") unless port_device.size == 2 - IPAddr.new(address[0]) + # we have to have URI Scheme so we just add http:// and ignore it later + uri = URI('http://' + value) + address = uri.host + port_device = uri.port + if ['','/'].include?(uri.path) + raise(Puppet::Error, "namevar should contain a device") + end + IPAddr.new(address) end end diff --git a/lib/puppet/type/ring_container_device.rb b/lib/puppet/type/ring_container_device.rb index 60144b06..2aec5edc 100644 --- a/lib/puppet/type/ring_container_device.rb +++ b/lib/puppet/type/ring_container_device.rb @@ -1,15 +1,19 @@ Puppet::Type.newtype(:ring_container_device) do require 'ipaddr' + require 'uri' ensurable newparam(:name, :namevar => true) do validate do |value| - address = value.split(':') - raise(Puppet::Error, "invalid name #{value}, should contain address:port/device") unless address.size == 2 - port_device = address[1].split('/') - raise(Puppet::Error, "namevar should contain a device") unless port_device.size == 2 - IPAddr.new(address[0]) + # we have to have URI Scheme so we just add http:// and ignore it later + uri = URI('http://' + value) + address = uri.host + port_device = uri.port + if ['','/'].include?(uri.path) + raise(Puppet::Error, "namevar should contain a device") + end + IPAddr.new(address) end end diff --git a/lib/puppet/type/ring_object_device.rb b/lib/puppet/type/ring_object_device.rb index 2de2d806..a977ce41 100644 --- a/lib/puppet/type/ring_object_device.rb +++ b/lib/puppet/type/ring_object_device.rb @@ -1,15 +1,19 @@ Puppet::Type.newtype(:ring_object_device) do require 'ipaddr' + require 'uri' ensurable newparam(:name, :namevar => true) do validate do |value| - address = value.split(':') - raise(Puppet::Error, "invalid name #{value}, should contain address:port/device") unless address.size == 2 - port_device = address[1].split('/') - raise(Puppet::Error, "namevar should contain a device") unless port_device.size == 2 - IPAddr.new(address[0]) + # we have to have URI Scheme so we just add http:// and ignore it later + uri = URI('http://' + value) + address = uri.host + port_device = uri.port + if ['','/'].include?(uri.path) + raise(Puppet::Error, "namevar should contain a device") + end + IPAddr.new(address) end end diff --git a/spec/unit/puppet/type/ring_account_device_spec.rb b/spec/unit/puppet/type/ring_account_device_spec.rb index 98b3f0a4..11d5923e 100644 --- a/spec/unit/puppet/type/ring_account_device_spec.rb +++ b/spec/unit/puppet/type/ring_account_device_spec.rb @@ -1,10 +1,9 @@ require 'puppet' describe Puppet::Type.type(:ring_account_device) do - - it 'should fail if the name has no ":"' do + it 'should fail if the name does not contain valid ipaddress' do expect { - Puppet::Type.type(:ring_account_device).new(:name => 'foo/bar') - }.to raise_error(Puppet::Error, /should contain address:port\/device/) + Puppet::Type.type(:ring_account_device).new(:name => '192.168.1.256:80/a') + }.to raise_error(Puppet::ResourceError, /invalid address/) end it 'should fail if the name does not contain a "/"' do diff --git a/spec/unit/puppet/type/ring_container_device_spec.rb b/spec/unit/puppet/type/ring_container_device_spec.rb index 80b436ab..1b2cea8d 100644 --- a/spec/unit/puppet/type/ring_container_device_spec.rb +++ b/spec/unit/puppet/type/ring_container_device_spec.rb @@ -1,16 +1,15 @@ require 'puppet' describe Puppet::Type.type(:ring_container_device) do - - it 'should fail if the name has no ":"' do + it 'should fail if the name does not contain valid ipaddress' do expect { - Puppet::Type.type(:ring_account_device).new(:name => 'foo/bar') - }.to raise_error(Puppet::Error, /should contain address:port\/device/) + Puppet::Type.type(:ring_container_device).new(:name => '192.168.1.256:80/a') + }.to raise_error(Puppet::ResourceError, /invalid address/) end it 'should fail if the name does not contain a "/"' do expect { - Puppet::Type.type(:ring_account_device).new(:name => 'foo:80') + Puppet::Type.type(:ring_container_device).new(:name => 'foo:80') }.to raise_error(Puppet::Error, /should contain a device/) end end diff --git a/spec/unit/puppet/type/ring_object_device_spec.rb b/spec/unit/puppet/type/ring_object_device_spec.rb index 05861bb1..4897f78c 100644 --- a/spec/unit/puppet/type/ring_object_device_spec.rb +++ b/spec/unit/puppet/type/ring_object_device_spec.rb @@ -1,15 +1,14 @@ require 'puppet' describe Puppet::Type.type(:ring_object_device) do - - it 'should fail if the name has no ":"' do + it 'should fail if the name does not contain valid ipaddress' do expect { - Puppet::Type.type(:ring_account_device).new(:name => 'foo/bar') - }.to raise_error(Puppet::Error, /should contain address:port\/device/) + Puppet::Type.type(:ring_object_device).new(:name => '192.168.1.256:80/a') + }.to raise_error(Puppet::ResourceError, /invalid address/) end it 'should fail if the name does not contain a "/"' do expect { - Puppet::Type.type(:ring_account_device).new(:name => 'foo:80') + Puppet::Type.type(:ring_object_device).new(:name => 'foo:80') }.to raise_error(Puppet::Error, /should contain a device/) end end