aea069886522eb2ee13691b9f0d1ddbb16a1158d
[mirror/dsa-puppet.git] / 3rdparty / modules / rabbitmq / lib / puppet / provider / rabbitmq_exchange / rabbitmqadmin.rb
1 require 'puppet'
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'
7   else
8     has_command(:rabbitmqctl, 'rabbitmqctl') do
9       environment HOME: '/tmp'
10     end
11     has_command(:rabbitmqadmin, '/usr/local/bin/rabbitmqadmin') do
12       environment HOME: '/tmp'
13     end
14   end
15   confine feature: :posix
16
17   def should_vhost
18     if @should_vhost
19       @should_vhost
20     else
21       @should_vhost = resource[:name].split('@')[1]
22     end
23   end
24
25   def self.all_vhosts
26     run_with_retries { rabbitmqctl('-q', 'list_vhosts') }.split(%r{\n})
27   end
28
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')
32     end
33     exchange_list.split(%r{\n}).reject { |exchange| exchange =~ %r{^federation:} }
34   end
35
36   def self.instances
37     resources = []
38     all_vhosts.each do |vhost|
39       all_exchanges(vhost).each do |line|
40         name, type, internal, durable, auto_delete, arguments = line.split
41         if type.nil?
42           # if name is empty, it will wrongly get the type's value.
43           # This way type will get the correct value
44           type = name
45           name = ''
46         end
47         # Convert output of arguments from the rabbitmqctl command to a json string.
48         if !arguments.nil?
49           arguments = arguments.gsub(%r{^\[(.*)\]$}, '').gsub(%r{\{("(?:.|\\")*?"),}, '{\1:').gsub(%r{\},\{}, ',')
50           arguments = '{}' if arguments == ''
51         else
52           arguments = '{}'
53         end
54         exchange = {
55           type: type,
56           ensure: :present,
57           internal: internal,
58           durable: durable,
59           auto_delete: auto_delete,
60           name: format('%s@%s', name, vhost),
61           arguments: JSON.parse(arguments)
62         }
63         resources << new(exchange) if exchange[:type]
64       end
65     end
66     resources
67   end
68
69   def self.prefetch(resources)
70     packages = instances
71     resources.keys.each do |name|
72       if (provider = packages.find { |pkg| pkg.name == name })
73         resources[name].provider = provider
74       end
75     end
76   end
77
78   def exists?
79     @property_hash[:ensure] == :present
80   end
81
82   def create
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]}"
89     cmd << "durable=#{resource[:durable]}"
90     cmd << "auto_delete=#{resource[:auto_delete]}"
91     cmd += ["arguments=#{arguments.to_json}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf']
92     rabbitmqadmin(*cmd)
93     @property_hash[:ensure] = :present
94   end
95
96   def destroy
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
101   end
102 end