2 require 'puppet/util/package'
4 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rabbitmqctl'))
5 Puppet::Type.type(:rabbitmq_policy).provide(:rabbitmqctl, :parent => Puppet::Provider::Rabbitmqctl) do
7 defaultfor :feature => :posix
10 def self.policies(name, vhost)
11 @policies = {} unless @policies
12 unless @policies[vhost]
14 self.run_with_retries {
15 rabbitmqctl('list_policies', '-q', '-p', vhost)
16 }.split(/\n/).each do |line|
17 # rabbitmq<3.2 does not support the applyto field
19 # / ha-all all .* {"ha-mode":"all","ha-sync-mode":"automatic"} 0
20 if line =~ /^(\S+)\s+(\S+)\s+(all|exchanges|queues)?\s*(\S+)\s+(\S+)\s+(\d+)$/
22 @policies[vhost][$2] = {
25 :definition => JSON.parse($5),
28 raise Puppet::Error, "cannot parse line from list_policies:#{line}"
32 @policies[vhost][name]
35 def policies(name, vhost)
36 self.class.policies(vhost, name)
40 @should_policy ||= resource[:name].rpartition('@').first
44 @should_vhost ||= resource[:name].rpartition('@').last
52 rabbitmqctl('clear_policy', '-p', should_vhost, should_policy)
56 policies(should_vhost, should_policy)
60 policies(should_vhost, should_policy)[:pattern]
68 policies(should_vhost, should_policy)[:applyto]
76 policies(should_vhost, should_policy)[:definition]
79 def definition=(definition)
84 policies(should_vhost, should_policy)[:priority]
87 def priority=(priority)
94 resource[:applyto] ||= applyto
95 resource[:definition] ||= definition
96 resource[:pattern] ||= pattern
97 resource[:priority] ||= priority
99 if Puppet::Util::Package.versioncmp(self.class.rabbitmq_version, '3.2.0') >= 0
100 rabbitmqctl('set_policy',
102 '--priority', resource[:priority],
103 '--apply-to', resource[:applyto].to_s,
106 resource[:definition].to_json
109 rabbitmqctl('set_policy',
113 resource[:definition].to_json,