X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Frabbitmq%2Flib%2Fpuppet%2Ftype%2Frabbitmq_binding.rb;h=1165fd955c21da08543a7ca106d066cd80dea691;hb=94a8783f522bbf2996cb8a59b977dea583e8b0c7;hp=130948003d4c2bc54d32c47036116553644e7dff;hpb=abf5299108b021bebf2fb9cd7519898e3baf29bd;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/rabbitmq/lib/puppet/type/rabbitmq_binding.rb b/3rdparty/modules/rabbitmq/lib/puppet/type/rabbitmq_binding.rb index 130948003..1165fd955 100644 --- a/3rdparty/modules/rabbitmq/lib/puppet/type/rabbitmq_binding.rb +++ b/3rdparty/modules/rabbitmq/lib/puppet/type/rabbitmq_binding.rb @@ -1,5 +1,42 @@ Puppet::Type.newtype(:rabbitmq_binding) do - desc 'Native type for managing rabbitmq bindings' + desc <<-DESC +Native type for managing rabbitmq bindings + +@example Create a rabbitmq_binding + rabbitmq_binding { 'myexchange@myqueue@myvhost': + user => 'dan', + password => 'bar', + destination_type => 'queue', + routing_key => '#', + arguments => {}, + ensure => present, + } + +@example Create bindings with same source / destination / vhost but different routing key using individual parameters +rabbitmq_binding { 'binding 1': + ensure => present, + source => 'myexchange', + destination => 'myqueue', + vhost => 'myvhost', + user => 'dan', + password => 'bar', + destination_type => 'queue', + routing_key => 'key1', + arguments => {}, +} + +rabbitmq_binding { 'binding 2': + ensure => present, + source => 'myexchange', + destination => 'myqueue', + vhost => 'myvhost', + user => 'dan', + password => 'bar', + destination_type => 'queue', + routing_key => 'key2', + arguments => {}, +} +DESC ensurable do defaultto(:present) @@ -11,23 +48,70 @@ Puppet::Type.newtype(:rabbitmq_binding) do 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 + [ + [ + %r{(^([^@]*)$)}m, + [ + [:name] + ] + ], + [ + %r{^((\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(%r{^\S+$}) + isnamevar + end + + newproperty(:destination) do + desc 'destination of binding' + + newvalues(%r{^\S+$}) + isnamevar + end + + newproperty(:vhost) do + desc 'vhost' + + newvalues(%r{^\S+$}) + defaultto('/') + isnamevar + end + + newproperty(:routing_key) do desc 'binding routing_key' - newvalues(/^\S*$/) + + newvalues(%r{^\S*$}) + isnamevar + end + + newproperty(:destination_type) do + desc 'binding destination_type' + newvalues(%r{queue|exchange}) + defaultto('queue') end - newparam(:arguments) do + newproperty(:arguments) do desc 'binding arguments' defaultto {} validate do |value| @@ -38,19 +122,19 @@ Puppet::Type.newtype(:rabbitmq_binding) do newparam(:user) do desc 'The user to use to connect to rabbitmq' defaultto('guest') - newvalues(/^\S+$/) + newvalues(%r{^\S+$}) end newparam(:password) do desc 'The password to use to connect to rabbitmq' defaultto('guest') - newvalues(/\S+/) + newvalues(%r{\S+}) end autorequire(:rabbitmq_vhost) do - [self[:name].split('@')[2]] + setup_autorequire('vhost') end - + autorequire(:rabbitmq_exchange) do setup_autorequire('exchange') end @@ -65,32 +149,41 @@ Puppet::Type.newtype(:rabbitmq_binding) do 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]}"] - else - rval = [] - end + rval = if destination_type == type + ["#{self[:destination]}@#{self[:vhost]}"] + else + [] + end end rval end def validate_argument(argument) - unless [Hash].include?(argument.class) - raise ArgumentError, "Invalid argument" - end + raise ArgumentError, 'Invalid argument' unless [Hash].include?(argument.class) end + # Validate that we have both source and destination now that these are not + # necessarily only coming from the resource title. + validate do + if !self[:source] && !defined? provider.source + raise ArgumentError, '`source` must be defined' + end + + if !self[:destination] && !defined? provider.destination + raise ArgumentError, '`destination` must be defined' + end + end end