3 Puppet::Type.type(:rabbitmq_binding).provide(:rabbitmqadmin) 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('@').last
28 rabbitmqctl('list_vhosts', '-q').split(/\n/).collect do |vhost|
34 def self.all_bindings(vhost)
35 rabbitmqctl('list_bindings', '-q', '-p', vhost, 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments').split(/\n/)
40 all_vhosts.each do |vhost|
41 all_bindings(vhost).collect do |line|
42 source_name, destination_name, destination_type, routing_key, arguments = line.split(/\t/)
43 # Convert output of arguments from the rabbitmqctl command to a json string.
45 arguments = arguments.gsub(/^\[(.*)\]$/, "").gsub(/\{("(?:.|\\")*?"),/, '{\1:').gsub(/\},\{/, ",")
52 if (source_name != '')
54 :destination_type => destination_type,
55 :routing_key => routing_key,
56 :arguments => JSON.parse(arguments),
58 :name => "%s@%s@%s" % [source_name, destination_name, vhost],
60 resources << new(binding) if binding[:name]
67 def self.prefetch(resources)
69 resources.keys.each do |name|
70 if provider = packages.find{ |pkg| pkg.name == name }
71 resources[name].provider = provider
77 @property_hash[:ensure] == :present
81 vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
82 name = resource[:name].split('@').first
83 destination = resource[:name].split('@')[1]
84 arguments = resource[:arguments]
88 rabbitmqadmin('declare',
91 "--user=#{resource[:user]}",
92 "--password=#{resource[:password]}",
94 '/etc/rabbitmq/rabbitmqadmin.conf',
96 "destination=#{destination}",
97 "arguments=#{arguments.to_json}",
98 "routing_key=#{resource[:routing_key]}",
99 "destination_type=#{resource[:destination_type]}"
101 @property_hash[:ensure] = :present
105 vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
106 name = resource[:name].split('@').first
107 destination = resource[:name].split('@')[1]
108 rabbitmqadmin('delete', 'binding', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf', "source=#{name}", "destination_type=#{resource[:destination_type]}", "destination=#{destination}", "properties_key=#{resource[:routing_key]}")
109 @property_hash[:ensure] = :absent