Skip to content

Commit

Permalink
Merge pull request voxpupuli#504 from wyardley/feature-modules-3679-t…
Browse files Browse the repository at this point in the history
…ake2

Multiple routing keys for bindings using namevars
  • Loading branch information
hunner committed Aug 24, 2017
2 parents cfaef82 + 3961da3 commit 4415648
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 69 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,34 @@ rabbitmq_binding { 'myexchange@myqueue@myvhost':
}
```

```puppet
rabbitmq_binding { 'binding 1':
source => 'myexchange',
destination => 'myqueue',
vhost => 'myvhost',
user => 'dan',
password => 'bar',
destination_type => 'queue',
routing_key => 'key1',
arguments => {},
ensure => present,
}
rabbitmq_binding { 'binding 2':
source => 'myexchange',
destination => 'myqueue',
vhost => 'myvhost',
user => 'dan',
password => 'bar',
destination_type => 'queue',
routing_key => 'key2',
arguments => {},
ensure => present,
}
```


### rabbitmq\_user\_permissions

```puppet
Expand Down
31 changes: 19 additions & 12 deletions lib/puppet/provider/rabbitmq_binding/rabbitmqadmin.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'json'
require 'puppet'
require 'digest'

Puppet::Type.type(:rabbitmq_binding).provide(:rabbitmqadmin) do

if Puppet::PUPPETVERSION.to_f < 3
Expand All @@ -15,11 +17,14 @@
end
defaultfor :feature => :posix

# Without this, the composite namevar stuff doesn't work properly.
mk_resource_methods

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 @@ -49,13 +54,17 @@ def self.instances
else
arguments = '{}'
end
hashed_name = Digest::SHA256.hexdigest "%s@%s@%s@%s" % [source_name, destination_name, vhost, routing_key]
unless(source_name.empty?)
binding = {
:source => source_name,
:destination => 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 => hashed_name,
}
resources << new(binding) if binding[:name]
end
Expand All @@ -64,10 +73,12 @@ def self.instances
resources
end

# see
# https://github.com/puppetlabs/puppetlabs-netapp/blob/d0a655665463c69c932f835ba8756be32417a4e9/lib/puppet/provider/netapp_qtree/sevenmode.rb#L66-L73
def self.prefetch(resources)
packages = instances
resources.keys.each do |name|
if provider = packages.find{ |pkg| pkg.name == name }
bindings = instances
resources.each do |name, res|
if provider = bindings.find{ |binding| binding.source == res[:source] && binding.destination == res[:destination] && binding.vhost == res[:vhost] && binding.routing_key == res[:routing_key] }
resources[name].provider = provider
end
end
Expand All @@ -79,8 +90,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 +101,8 @@ def create
"--password=#{resource[:password]}",
'-c',
'/etc/rabbitmq/rabbitmqadmin.conf',
"source=#{name}",
"destination=#{destination}",
"source=#{resource[:source]}",
"destination=#{resource[:destination]}",
"arguments=#{arguments.to_json}",
"routing_key=#{resource[:routing_key]}",
"destination_type=#{resource[:destination_type]}"
Expand All @@ -103,9 +112,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[:destination]}", "properties_key=#{resource[:routing_key]}")
@property_hash[:ensure] = :absent
end

Expand Down
87 changes: 71 additions & 16 deletions lib/puppet/type/rabbitmq_binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,70 @@
end
end

newparam(:name, :namevar => true) do
desc 'source and destination of bind'
newvalues(/^\S*@\S+@\S+$/)
# Match patterns without '@' as arbitrary names; match patterns with
# src@destination@vhost to their named params for backwards compatibility.
def self.title_patterns
[
[
/(^([^@]*)$)/m,
[
[ :name ]
]
],
[
/^((\S+)@(\S+)@(\S+))$/m,
[
[ :name ],
[ :source ],
[ :destination],
[ :vhost ]
]
]
]
end

newparam(:destination_type) do
desc 'binding destination_type'
newvalues(/queue|exchange/)
defaultto('queue')
newparam(:name) do
desc 'resource name, either source@destination@vhost or arbitrary name with params'

isnamevar
end

newparam(:routing_key) do

newproperty(:source) do
desc 'source of binding'

newvalues(/^\S+$/)
isnamevar
end

newproperty(:destination) do
desc 'destination of binding'

newvalues(/^\S+$/)
isnamevar
end

newproperty(:vhost) do
desc 'vhost'

newvalues(/^\S+$/)
defaultto('/')
isnamevar
end

newproperty(:routing_key) do
desc 'binding routing_key'

newvalues(/^\S*$/)
isnamevar
end

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

newparam(:arguments) do
newproperty(:arguments) do
desc 'binding arguments'
defaultto {}
validate do |value|
Expand All @@ -48,7 +95,7 @@
end

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

autorequire(:rabbitmq_exchange) do
Expand All @@ -65,21 +112,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[:destination]}"
]
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[:destination]}@#{self[:vhost]}")
end
else
if destination_type == type
rval = ["#{self[:name].split('@')[1]}@#{self[:name].split('@')[2]}"]
rval = ["#{self[:destination]}@#{self[:vhost]}"]
else
rval = []
end
Expand All @@ -93,4 +140,12 @@ def validate_argument(argument)
end
end

# Validate that we have both source and destination now that these are not
# necessarily only coming from the resource title.
validate do
unless self[:source] and self[:destination]
raise ArgumentError, "Source and destination must both be defined."
end
end

end
Loading

0 comments on commit 4415648

Please sign in to comment.