Update rabbitmq module
[mirror/dsa-puppet.git] / 3rdparty / modules / rabbitmq / lib / puppet / provider / rabbitmq_binding / rabbitmqadmin.rb
1 require 'json'
2 require 'puppet'
3 require 'digest'
4
5 Puppet::Type.type(:rabbitmq_binding).provide(:rabbitmqadmin) do
6   has_command(:rabbitmqctl, 'rabbitmqctl') do
7     environment HOME: '/tmp'
8   end
9   has_command(:rabbitmqadmin, '/usr/local/bin/rabbitmqadmin') do
10     environment HOME: '/tmp'
11   end
12
13   confine feature: :posix
14
15   # Without this, the composite namevar stuff doesn't work properly.
16   mk_resource_methods
17
18   def should_vhost
19     if @should_vhost
20       @should_vhost
21     else
22       @should_vhost = resource[:vhost]
23     end
24   end
25
26   def self.all_vhosts
27     vhosts = []
28     rabbitmqctl('list_vhosts', '-q').split(%r{\n}).map do |vhost|
29       vhosts.push(vhost)
30     end
31     vhosts
32   end
33
34   def self.all_bindings(vhost)
35     rabbitmqctl('list_bindings', '-q', '-p', vhost, 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments').split(%r{\n})
36   end
37
38   def self.instances
39     resources = []
40     all_vhosts.each do |vhost|
41       all_bindings(vhost).map do |line|
42         source_name, destination_name, destination_type, routing_key, arguments = line.split(%r{\t})
43         # Convert output of arguments from the rabbitmqctl command to a json string.
44         if !arguments.nil?
45           arguments = arguments.gsub(%r{^\[(.*)\]$}, '').gsub(%r{\{("(?:.|\\")*?"),}, '{\1:').gsub(%r{\},\{}, ',')
46           arguments = '{}' if arguments == ''
47         else
48           arguments = '{}'
49         end
50         hashed_name = Digest::SHA256.hexdigest format('%s@%s@%s@%s', source_name, destination_name, vhost, routing_key)
51         next if source_name.empty?
52         binding = {
53           source: source_name,
54           destination: destination_name,
55           vhost: vhost,
56           destination_type: destination_type,
57           routing_key: routing_key,
58           arguments: JSON.parse(arguments),
59           ensure: :present,
60           name: hashed_name
61         }
62         resources << new(binding) if binding[:name]
63       end
64     end
65     resources
66   end
67
68   # see
69   # https://github.com/puppetlabs/puppetlabs-netapp/blob/d0a655665463c69c932f835ba8756be32417a4e9/lib/puppet/provider/netapp_qtree/sevenmode.rb#L66-L73
70   def self.prefetch(resources)
71     bindings = instances
72     resources.each do |name, res|
73       if (provider = bindings.find { |binding| binding.source == res[:source] && binding.destination == res[:destination] && binding.vhost == res[:vhost] && binding.routing_key == res[:routing_key] })
74         resources[name].provider = provider
75       end
76     end
77   end
78
79   def exists?
80     @property_hash[:ensure] == :present
81   end
82
83   def create
84     vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
85     arguments = resource[:arguments]
86     arguments = {} if arguments.nil?
87     rabbitmqadmin('declare',
88                   'binding',
89                   vhost_opt,
90                   "--user=#{resource[:user]}",
91                   "--password=#{resource[:password]}",
92                   '-c',
93                   '/etc/rabbitmq/rabbitmqadmin.conf',
94                   "source=#{resource[:source]}",
95                   "destination=#{resource[:destination]}",
96                   "arguments=#{arguments.to_json}",
97                   "routing_key=#{resource[:routing_key]}",
98                   "destination_type=#{resource[:destination_type]}")
99     @property_hash[:ensure] = :present
100   end
101
102   def destroy
103     vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
104     rabbitmqadmin('delete', 'binding', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf', "source=#{resource[:source]}", "destination_type=#{resource[:destination_type]}", "destination=#{resource[:destination]}", "properties_key=#{resource[:routing_key]}")
105     @property_hash[:ensure] = :absent
106   end
107 end