Update rabbitmq module
[mirror/dsa-puppet.git] / 3rdparty / modules / rabbitmq / lib / puppet / provider / rabbitmq_queue / rabbitmqadmin.rb
1 require 'json'
2 require 'puppet'
3 Puppet::Type.type(:rabbitmq_queue).provide(:rabbitmqadmin) 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].rpartition('@').last
22     end
23   end
24
25   def self.all_vhosts
26     rabbitmqctl('list_vhosts', '-q').split(%r{\n})
27   end
28
29   def self.all_queues(vhost)
30     rabbitmqctl('list_queues', '-q', '-p', vhost, 'name', 'durable', 'auto_delete', 'arguments').split(%r{\n})
31   end
32
33   def self.instances
34     resources = []
35     all_vhosts.each do |vhost|
36       all_queues(vhost).map do |line|
37         next if line =~ %r{^federation:}
38         name, durable, auto_delete, arguments = line.split("\t")
39         # Convert output of arguments from the rabbitmqctl command to a json string.
40         if !arguments.nil?
41           arguments = arguments.gsub(%r{^\[(.*)\]$}, '').gsub(%r{\{("(?:.|\\")*?"),}, '{\1:').gsub(%r{\},\{}, ',')
42           arguments = '{}' if arguments == ''
43         else
44           arguments = '{}'
45         end
46         queue = {
47           durable: durable,
48           auto_delete: auto_delete,
49           arguments: JSON.parse(arguments),
50           ensure: :present,
51           name: format('%s@%s', name, vhost)
52         }
53         resources << new(queue) if queue[:name]
54       end
55     end
56     resources
57   end
58
59   def self.prefetch(resources)
60     packages = instances
61     resources.keys.each do |name|
62       if (provider = packages.find { |pkg| pkg.name == name })
63         resources[name].provider = provider
64       end
65     end
66   end
67
68   def exists?
69     @property_hash[:ensure] == :present
70   end
71
72   def create
73     vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
74     name = resource[:name].rpartition('@').first
75     arguments = resource[:arguments]
76     arguments = {} if arguments.nil?
77     rabbitmqadmin('declare',
78                   'queue',
79                   vhost_opt,
80                   "--user=#{resource[:user]}",
81                   "--password=#{resource[:password]}",
82                   '-c',
83                   '/etc/rabbitmq/rabbitmqadmin.conf',
84                   "name=#{name}",
85                   "durable=#{resource[:durable]}",
86                   "auto_delete=#{resource[:auto_delete]}",
87                   "arguments=#{arguments.to_json}")
88     @property_hash[:ensure] = :present
89   end
90
91   def destroy
92     vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
93     name = resource[:name].rpartition('@').first
94     rabbitmqadmin('delete', 'queue', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf', "name=#{name}")
95     @property_hash[:ensure] = :absent
96   end
97 end