2 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmqctl'))
3 Puppet::Type.type(:rabbitmq_exchange).provide(:rabbitmqadmin, :parent => Puppet::Provider::Rabbitmqctl) do
5 if Puppet::PUPPETVERSION.to_f < 3
6 commands :rabbitmqctl => 'rabbitmqctl'
7 commands :rabbitmqadmin => '/usr/local/bin/rabbitmqadmin'
9 has_command(:rabbitmqctl, 'rabbitmqctl') do
10 environment :HOME => "/tmp"
12 has_command(:rabbitmqadmin, '/usr/local/bin/rabbitmqadmin') do
13 environment :HOME => "/tmp"
16 defaultfor :feature => :posix
22 @should_vhost = resource[:name].split('@')[1]
28 self.run_with_retries {
29 rabbitmqctl('-q', 'list_vhosts')
30 }.split(/\n/).each do |vhost|
36 def self.all_exchanges(vhost)
38 self.run_with_retries {
39 rabbitmqctl('-q', 'list_exchanges', '-p', vhost, 'name', 'type', 'internal', 'durable', 'auto_delete', 'arguments')
40 }.split(/\n/).each do |exchange|
41 exchanges.push(exchange)
48 all_vhosts.each do |vhost|
49 all_exchanges(vhost).each do |line|
50 name, type, internal, durable, auto_delete, arguments = line.split()
52 # if name is empty, it will wrongly get the type's value.
53 # This way type will get the correct value
57 # Convert output of arguments from the rabbitmqctl command to a json string.
59 arguments = arguments.gsub(/^\[(.*)\]$/, "").gsub(/\{("(?:.|\\")*?"),/, '{\1:').gsub(/\},\{/, ",")
69 :internal => internal,
71 :auto_delete => auto_delete,
72 :name => "%s@%s" % [name, vhost],
73 :arguments => JSON.parse(arguments),
75 resources << new(exchange) if exchange[:type]
81 def self.prefetch(resources)
83 resources.keys.each do |name|
84 if provider = packages.find{ |pkg| pkg.name == name }
85 resources[name].provider = provider
91 @property_hash[:ensure] == :present
95 vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
96 name = resource[:name].split('@')[0]
97 arguments = resource[:arguments]
101 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')
102 @property_hash[:ensure] = :present
106 vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
107 name = resource[:name].split('@')[0]
108 rabbitmqadmin('delete', 'exchange', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", "name=#{name}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf')
109 @property_hash[:ensure] = :absent