2 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmqctl'))
3 Puppet::Type.type(:rabbitmq_exchange).provide(:rabbitmqadmin, parent: Puppet::Provider::Rabbitmqctl) do
4 if Puppet::PUPPETVERSION.to_f < 3
5 commands rabbitmqctl: 'rabbitmqctl'
6 commands rabbitmqadmin: '/usr/local/bin/rabbitmqadmin'
8 has_command(:rabbitmqctl, 'rabbitmqctl') do
9 environment HOME: '/tmp'
11 has_command(:rabbitmqadmin, '/usr/local/bin/rabbitmqadmin') do
12 environment HOME: '/tmp'
15 confine feature: :posix
21 @should_vhost = resource[:name].split('@')[1]
26 run_with_retries { rabbitmqctl('-q', 'list_vhosts') }.split(%r{\n})
29 def self.all_exchanges(vhost)
30 exchange_list = run_with_retries do
31 rabbitmqctl('-q', 'list_exchanges', '-p', vhost, 'name', 'type', 'internal', 'durable', 'auto_delete', 'arguments')
33 exchange_list.split(%r{\n}).reject { |exchange| exchange =~ %r{^federation:} }
38 all_vhosts.each do |vhost|
39 all_exchanges(vhost).each do |line|
40 name, type, internal, durable, auto_delete, arguments = line.split
42 # if name is empty, it will wrongly get the type's value.
43 # This way type will get the correct value
47 # Convert output of arguments from the rabbitmqctl command to a json string.
49 arguments = arguments.gsub(%r{^\[(.*)\]$}, '').gsub(%r{\{("(?:.|\\")*?"),}, '{\1:').gsub(%r{\},\{}, ',')
50 arguments = '{}' if arguments == ''
59 auto_delete: auto_delete,
60 name: format('%s@%s', name, vhost),
61 arguments: JSON.parse(arguments)
63 resources << new(exchange) if exchange[:type]
69 def self.prefetch(resources)
71 resources.keys.each do |name|
72 if (provider = packages.find { |pkg| pkg.name == name })
73 resources[name].provider = provider
79 @property_hash[:ensure] == :present
83 vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
84 name = resource[:name].split('@')[0]
85 arguments = resource[:arguments]
86 arguments = {} if arguments.nil?
87 cmd = ['declare', 'exchange', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", "name=#{name}", "type=#{resource[:type]}"]
88 cmd << "internal=#{resource[:internal]}" if resource[:internal]
89 cmd << "durable=#{resource[:durable]}" if resource[:durable]
90 cmd << "auto_delete=#{resource[:auto_delete]}" if resource[:auto_delete]
91 cmd += ["arguments=#{arguments.to_json}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf']
93 @property_hash[:ensure] = :present
97 vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
98 name = resource[:name].split('@')[0]
99 rabbitmqadmin('delete', 'exchange', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", "name=#{name}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf')
100 @property_hash[:ensure] = :absent