Update rabbitmq module
[mirror/dsa-puppet.git] / 3rdparty / modules / rabbitmq / lib / puppet / provider / rabbitmq_binding / rabbitmqadmin.rb
index e02c466..e7f9345 100644 (file)
@@ -1,73 +1,76 @@
 require 'json'
 require 'puppet'
-Puppet::Type.type(:rabbitmq_binding).provide(:rabbitmqadmin) do
+require 'digest'
 
-  if Puppet::PUPPETVERSION.to_f < 3
-    commands :rabbitmqctl   => 'rabbitmqctl'
-    commands :rabbitmqadmin => '/usr/local/bin/rabbitmqadmin'
-  else
-    has_command(:rabbitmqctl, 'rabbitmqctl') do
-      environment :HOME => "/tmp"
-    end
-    has_command(:rabbitmqadmin, '/usr/local/bin/rabbitmqadmin') do
-      environment :HOME => "/tmp"
-    end
+Puppet::Type.type(:rabbitmq_binding).provide(:rabbitmqadmin) do
+  has_command(:rabbitmqctl, 'rabbitmqctl') do
+    environment HOME: '/tmp'
+  end
+  has_command(:rabbitmqadmin, '/usr/local/bin/rabbitmqadmin') do
+    environment HOME: '/tmp'
   end
-  defaultfor :feature => :posix
+
+  confine 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
 
   def self.all_vhosts
     vhosts = []
-    rabbitmqctl('list_vhosts', '-q').split(/\n/).collect do |vhost|
+    rabbitmqctl('list_vhosts', '-q').split(%r{\n}).map do |vhost|
       vhosts.push(vhost)
     end
     vhosts
   end
 
   def self.all_bindings(vhost)
-    rabbitmqctl('list_bindings', '-q', '-p', vhost, 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments').split(/\n/)
+    rabbitmqctl('list_bindings', '-q', '-p', vhost, 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments').split(%r{\n})
   end
 
   def self.instances
     resources = []
     all_vhosts.each do |vhost|
-      all_bindings(vhost).collect do |line|
-        source_name, destination_name, destination_type, routing_key, arguments = line.split(/\t/)
+      all_bindings(vhost).map do |line|
+        source_name, destination_name, destination_type, routing_key, arguments = line.split(%r{\t})
         # Convert output of arguments from the rabbitmqctl command to a json string.
         if !arguments.nil?
-          arguments = arguments.gsub(/^\[(.*)\]$/, "").gsub(/\{("(?:.|\\")*?"),/, '{\1:').gsub(/\},\{/, ",")
-          if arguments == ""
-            arguments = '{}'
-          end
+          arguments = arguments.gsub(%r{^\[(.*)\]$}, '').gsub(%r{\{("(?:.|\\")*?"),}, '{\1:').gsub(%r{\},\{}, ',')
+          arguments = '{}' if arguments == ''
         else
           arguments = '{}'
         end
-        if (source_name != '')
-          binding = {
-            :destination_type => destination_type,
-            :routing_key      => routing_key,
-            :arguments        => JSON.parse(arguments),
-            :ensure           => :present,
-            :name             => "%s@%s@%s" % [source_name, destination_name, vhost],
-          }
-          resources << new(binding) if binding[:name]
-        end
+        hashed_name = Digest::SHA256.hexdigest format('%s@%s@%s@%s', source_name, destination_name, vhost, routing_key)
+        next if 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: hashed_name
+        }
+        resources << new(binding) if binding[:name]
       end
     end
     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
@@ -79,34 +82,26 @@ Puppet::Type.type(:rabbitmq_binding).provide(:rabbitmqadmin) do
 
   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 = {}
-    end
+    arguments = {} if arguments.nil?
     rabbitmqadmin('declare',
-      'binding',
-      vhost_opt,
-      "--user=#{resource[:user]}",
-      "--password=#{resource[:password]}",
-      '-c',
-      '/etc/rabbitmq/rabbitmqadmin.conf',
-      "source=#{name}",
-      "destination=#{destination}",
-      "arguments=#{arguments.to_json}",
-      "routing_key=#{resource[:routing_key]}",
-      "destination_type=#{resource[:destination_type]}"
-    )
+                  'binding',
+                  vhost_opt,
+                  "--user=#{resource[:user]}",
+                  "--password=#{resource[:password]}",
+                  '-c',
+                  '/etc/rabbitmq/rabbitmqadmin.conf',
+                  "source=#{resource[:source]}",
+                  "destination=#{resource[:destination]}",
+                  "arguments=#{arguments.to_json}",
+                  "routing_key=#{resource[:routing_key]}",
+                  "destination_type=#{resource[:destination_type]}")
     @property_hash[:ensure] = :present
   end
 
   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
-
 end