Skip to content

Commit

Permalink
Add support for multiple bindings with same source/dest/vhost but dif…
Browse files Browse the repository at this point in the history
…ferent routing key (MODULES-3679)

TODO: should improve validation, tests, etc., need to fix resource comparison still
  • Loading branch information
William Yardley committed Aug 15, 2017
1 parent 58cb3a6 commit 1d88136
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
21 changes: 10 additions & 11 deletions lib/puppet/provider/rabbitmq_binding/rabbitmqadmin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def should_vhost
if @should_vhost
@should_vhost
else
@should_vhost = resource[:name].split('@').last
@should_vhost = resource[:vhost]
end
end

Expand Down Expand Up @@ -51,11 +51,14 @@ def self.instances
end
unless(source_name.empty?)
binding = {
:source => source_name,
:dest => destination_name,
:vhost => vhost,
:destination_type => destination_type,
:routing_key => routing_key,
:arguments => JSON.parse(arguments),
:ensure => :present,
:name => "%s@%s@%s" % [source_name, destination_name, vhost],
:name => "%s@%s@%s@%s" % [source_name, destination_name, vhost, routing_key],
}
resources << new(binding) if binding[:name]
end
Expand All @@ -65,9 +68,9 @@ def self.instances
end

def self.prefetch(resources)
packages = instances
bindings = instances
resources.keys.each do |name|
if provider = packages.find{ |pkg| pkg.name == name }
if provider = bindings.find{ |route| route.source == source && route.dest == dest && route.vhost == vhost && route.routing_key == routing_key }
resources[name].provider = provider
end
end
Expand All @@ -79,8 +82,6 @@ def exists?

def create
vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
name = resource[:name].split('@').first
destination = resource[:name].split('@')[1]
arguments = resource[:arguments]
if arguments.nil?
arguments = {}
Expand All @@ -92,8 +93,8 @@ def create
"--password=#{resource[:password]}",
'-c',
'/etc/rabbitmq/rabbitmqadmin.conf',
"source=#{name}",
"destination=#{destination}",
"source=#{resource[:source]}",
"destination=#{resource[:dest]}",
"arguments=#{arguments.to_json}",
"routing_key=#{resource[:routing_key]}",
"destination_type=#{resource[:destination_type]}"
Expand All @@ -103,9 +104,7 @@ def create

def destroy
vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
name = resource[:name].split('@').first
destination = resource[:name].split('@')[1]
rabbitmqadmin('delete', 'binding', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf', "source=#{name}", "destination_type=#{resource[:destination_type]}", "destination=#{destination}", "properties_key=#{resource[:routing_key]}")
rabbitmqadmin('delete', 'binding', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf', "source=#{resource[:source]}", "destination_type=#{resource[:destination_type]}", "destination=#{resource[:dest]}", "properties_key=#{resource[:routing_key]}")
@property_hash[:ensure] = :absent
end

Expand Down
51 changes: 39 additions & 12 deletions lib/puppet/type/rabbitmq_binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,48 @@
end
end

# Match patterns without '@' as arbitrary names; match patterns with
# src@dst@vhost to their named params for backwards compatibility.
def self.title_patterns
[
[ /^(\S+)@(\S+)@(\S+)$/m,
[ [:source], [:dest], [:vhost] ]
],
[ /(^([^@]*)$)/m, [ [:name] ] ]
]
end

newparam(:name, :namevar => true) do
desc 'source and destination of bind'
newvalues(/^\S*@\S+@\S+$/)
end

newparam(:source, :namevar => true) do
desc 'source of binding'
newvalues(/^\S+$/)
end

newparam(:dest, :namevar => true) do
desc 'destination of binding'
newvalues(/^\S+$/)
end

newparam(:vhost, :namevar => true) do
desc 'vhost'
newvalues(/^\S+$/)
defaultto('/')
end

newparam(:routing_key, :namevar => true) do
desc 'binding routing_key'
newvalues(/^\S*$/)
defaultto('#')
end

newparam(:destination_type) do
desc 'binding destination_type'
newvalues(/queue|exchange/)
defaultto('queue')
end

newparam(:routing_key) do
desc 'binding routing_key'
newvalues(/^\S*$/)
end

newparam(:arguments) do
desc 'binding arguments'
Expand All @@ -48,7 +75,7 @@
end

autorequire(:rabbitmq_vhost) do
[self[:name].split('@')[2]]
setup_autorequire('vhost')
end

autorequire(:rabbitmq_exchange) do
Expand All @@ -65,21 +92,21 @@

autorequire(:rabbitmq_user_permissions) do
[
"#{self[:user]}@#{self[:name].split('@')[1]}",
"#{self[:user]}@#{self[:name].split('@')[0]}"
"#{self[:user]}@#{self[:source]}",
"#{self[:user]}@#{self[:dest]}"
]
end

def setup_autorequire(type)
destination_type = value(:destination_type)
if type == 'exchange'
rval = ["#{self[:name].split('@')[0]}@#{self[:name].split('@')[2]}"]
rval = ["#{self[:source]}@#{self[:vhost]}"]
if destination_type == type
rval.push("#{self[:name].split('@')[1]}@#{self[:name].split('@')[2]}")
rval.push("#{self[:dest]}@#{self[:vhost]}")
end
else
if destination_type == type
rval = ["#{self[:name].split('@')[1]}@#{self[:name].split('@')[2]}"]
rval = ["#{self[:dest]}@#{self[:vhost]}"]
else
rval = []
end
Expand Down
17 changes: 0 additions & 17 deletions spec/unit/puppet/type/rabbitmq_binding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@
Puppet::Type.type(:rabbitmq_binding).new({})
}.to raise_error(Puppet::Error, 'Title or name must be provided')
end
it 'should not allow whitespace in the name' do
expect {
@binding[:name] = 'b r'
}.to raise_error(Puppet::Error, /Valid values match/)
end
it 'should not allow names without one @' do
expect {
@binding[:name] = 'b_r'
}.to raise_error(Puppet::Error, /Valid values match/)
end

it 'should not allow names without two @' do
expect {
@binding[:name] = 'b@r'
}.to raise_error(Puppet::Error, /Valid values match/)
end

it 'should accept an binding destination_type' do
@binding[:destination_type] = :exchange
@binding[:destination_type].should == :exchange
Expand Down

0 comments on commit 1d88136

Please sign in to comment.