Update 3rdparty rabbitmq module
[mirror/dsa-puppet.git] / 3rdparty / modules / rabbitmq / lib / puppet / type / rabbitmq_binding.rb
index 1309480..1165fd9 100644 (file)
@@ -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