Update rabbitmq module
[mirror/dsa-puppet.git] / 3rdparty / modules / rabbitmq / lib / puppet / provider / rabbitmq_exchange / rabbitmqadmin.rb
index c1cff09..bdff0ed 100644 (file)
@@ -1,19 +1,18 @@
 require 'puppet'
 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmqctl'))
-Puppet::Type.type(:rabbitmq_exchange).provide(:rabbitmqadmin, :parent => Puppet::Provider::Rabbitmqctl) do
-
+Puppet::Type.type(:rabbitmq_exchange).provide(:rabbitmqadmin, parent: Puppet::Provider::Rabbitmqctl) do
   if Puppet::PUPPETVERSION.to_f < 3
-    commands :rabbitmqctl   => 'rabbitmqctl'
-    commands :rabbitmqadmin => '/usr/local/bin/rabbitmqadmin'
+    commands rabbitmqctl: 'rabbitmqctl'
+    commands rabbitmqadmin: '/usr/local/bin/rabbitmqadmin'
   else
     has_command(:rabbitmqctl, 'rabbitmqctl') do
-      environment :HOME => "/tmp"
+      environment HOME: '/tmp'
     end
     has_command(:rabbitmqadmin, '/usr/local/bin/rabbitmqadmin') do
-      environment :HOME => "/tmp"
+      environment HOME: '/tmp'
     end
   end
-  defaultfor :feature => :posix
+  confine feature: :posix
 
   def should_vhost
     if @should_vhost
@@ -24,56 +23,45 @@ Puppet::Type.type(:rabbitmq_exchange).provide(:rabbitmqadmin, :parent => Puppet:
   end
 
   def self.all_vhosts
-    vhosts = []
-    self.run_with_retries {
-      rabbitmqctl('-q', 'list_vhosts')
-    }.split(/\n/).each do |vhost|
-      vhosts.push(vhost)
-    end
-    vhosts
+    run_with_retries { rabbitmqctl('-q', 'list_vhosts') }.split(%r{\n})
   end
 
   def self.all_exchanges(vhost)
-    exchanges = []
-    self.run_with_retries {
+    exchange_list = run_with_retries do
       rabbitmqctl('-q', 'list_exchanges', '-p', vhost, 'name', 'type', 'internal', 'durable', 'auto_delete', 'arguments')
-    }.split(/\n/).each do |exchange|
-      exchanges.push(exchange)
     end
-    exchanges
+    exchange_list.split(%r{\n}).reject { |exchange| exchange =~ %r{^federation:} }
   end
 
   def self.instances
     resources = []
     all_vhosts.each do |vhost|
-        all_exchanges(vhost).each do |line|
-            name, type, internal, durable, auto_delete, arguments = line.split()
-            if type.nil?
-                # if name is empty, it will wrongly get the type's value.
-                # This way type will get the correct value
-                type = name
-                name = ''
-            end
-            # 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
-            else
-              arguments = '{}'
-            end
-            exchange = {
-              :type   => type,
-              :ensure => :present,
-              :internal => internal,
-              :durable => durable,
-              :auto_delete => auto_delete,
-              :name   => "%s@%s" % [name, vhost],
-              :arguments => JSON.parse(arguments),
-            }
-            resources << new(exchange) if exchange[:type]
+      all_exchanges(vhost).each do |line|
+        name, type, internal, durable, auto_delete, arguments = line.split
+        if type.nil?
+          # if name is empty, it will wrongly get the type's value.
+          # This way type will get the correct value
+          type = name
+          name = ''
+        end
+        # Convert output of arguments from the rabbitmqctl command to a json string.
+        if !arguments.nil?
+          arguments = arguments.gsub(%r{^\[(.*)\]$}, '').gsub(%r{\{("(?:.|\\")*?"),}, '{\1:').gsub(%r{\},\{}, ',')
+          arguments = '{}' if arguments == ''
+        else
+          arguments = '{}'
         end
+        exchange = {
+          type: type,
+          ensure: :present,
+          internal: internal,
+          durable: durable,
+          auto_delete: auto_delete,
+          name: format('%s@%s', name, vhost),
+          arguments: JSON.parse(arguments)
+        }
+        resources << new(exchange) if exchange[:type]
+      end
     end
     resources
   end
@@ -81,7 +69,7 @@ Puppet::Type.type(:rabbitmq_exchange).provide(:rabbitmqadmin, :parent => Puppet:
   def self.prefetch(resources)
     packages = instances
     resources.keys.each do |name|
-      if provider = packages.find{ |pkg| pkg.name == name }
+      if (provider = packages.find { |pkg| pkg.name == name })
         resources[name].provider = provider
       end
     end
@@ -95,10 +83,13 @@ Puppet::Type.type(:rabbitmq_exchange).provide(:rabbitmqadmin, :parent => Puppet:
     vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
     name = resource[:name].split('@')[0]
     arguments = resource[:arguments]
-    if arguments.nil?
-      arguments = {}
-    end
-    rabbitmqadmin('declare', 'exchange', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", "name=#{name}", "type=#{resource[:type]}", "internal=#{resource[:internal]}", "durable=#{resource[:durable]}", "auto_delete=#{resource[:auto_delete]}", "arguments=#{arguments.to_json}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf')
+    arguments = {} if arguments.nil?
+    cmd = ['declare', 'exchange', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", "name=#{name}", "type=#{resource[:type]}"]
+    cmd << "internal=#{resource[:internal]}" if resource[:internal]
+    cmd << "durable=#{resource[:durable]}" if resource[:durable]
+    cmd << "auto_delete=#{resource[:auto_delete]}" if resource[:auto_delete]
+    cmd += ["arguments=#{arguments.to_json}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf']
+    rabbitmqadmin(*cmd)
     @property_hash[:ensure] = :present
   end
 
@@ -108,5 +99,4 @@ Puppet::Type.type(:rabbitmq_exchange).provide(:rabbitmqadmin, :parent => Puppet:
     rabbitmqadmin('delete', 'exchange', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", "name=#{name}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf')
     @property_hash[:ensure] = :absent
   end
-
 end