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
6 confine feature: :posix
9 def self.policies(vhost, name)
10 @policies = {} unless @policies
11 unless @policies[vhost]
13 policy_list = run_with_retries do
14 rabbitmqctl('list_policies', '-q', '-p', vhost)
17 # rabbitmq<3.2 does not support the applyto field
19 # / ha-all all .* {"ha-mode":"all","ha-sync-mode":"automatic"} 0 << This is for RabbitMQ v < 3.7.0
20 # / ha-all .* all {"ha-mode":"all","ha-sync-mode":"automatic"} 0 << This is for RabbitMQ v >= 3.7.0
21 if Puppet::Util::Package.versioncmp(rabbitmq_version, '3.7') >= 0
22 regex = %r{^(\S+)\s+(\S+)\s+(\S+)\s+(all|exchanges|queues)?\s+(\S+)\s+(\d+)$}
26 regex = %r{^(\S+)\s+(\S+)\s+(all|exchanges|queues)?\s*(\S+)\s+(\S+)\s+(\d+)$}
31 policy_list.split(%r{\n}).each do |line|
32 raise Puppet::Error, "cannot parse line from list_policies:#{line}" unless line =~ regex
33 n = Regexp.last_match(2)
34 applyto = Regexp.last_match(applyto_index) || 'all'
35 priority = Regexp.last_match(6)
36 definition = JSON.parse(Regexp.last_match(5))
37 # be aware that the gsub will reset the captures
38 # from the regexp above
39 pattern = Regexp.last_match(pattern_index).to_s.gsub(%r{\\\\}, '\\')
41 @policies[vhost][n] = {
44 definition: definition,
49 @policies[vhost][name]
52 def policies(vhost, name)
53 self.class.policies(vhost, name)
57 @should_policy ||= resource[:name].rpartition('@').first
61 @should_vhost ||= resource[:name].rpartition('@').last
69 rabbitmqctl('clear_policy', '-p', should_vhost, should_policy)
73 policies(should_vhost, should_policy)
77 policies(should_vhost, should_policy)[:pattern]
80 def pattern=(_pattern)
85 policies(should_vhost, should_policy)[:applyto]
88 def applyto=(_applyto)
93 policies(should_vhost, should_policy)[:definition]
96 def definition=(_definition)
101 policies(should_vhost, should_policy)[:priority]
104 def priority=(_priority)
109 return if @set_policy
111 resource[:applyto] ||= applyto
112 resource[:definition] ||= definition
113 resource[:pattern] ||= pattern
114 resource[:priority] ||= priority
116 if Puppet::Util::Package.versioncmp(self.class.rabbitmq_version, '3.2.0') >= 0
120 '--priority', resource[:priority],
121 '--apply-to', resource[:applyto].to_s,
124 resource[:definition].to_json
132 resource[:definition].to_json,