1d112e965ec07f354ba170a3802627897498ec03
[mirror/dsa-puppet.git] / 3rdparty / modules / rabbitmq / lib / puppet / provider / rabbitmq_policy / rabbitmqctl.rb
1 require 'json'
2 require 'puppet/util/package'
3
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
7
8   # cache policies
9   def self.policies(vhost, name)
10     @policies = {} unless @policies
11     unless @policies[vhost]
12       @policies[vhost] = {}
13       policy_list = run_with_retries do
14         rabbitmqctl('list_policies', '-q', '-p', vhost)
15       end
16
17       # rabbitmq<3.2 does not support the applyto field
18       # 1 2      3?  4  5                                            6
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+)$}
23         applyto_index = 4
24         pattern_index = 3
25       else
26         regex = %r{^(\S+)\s+(\S+)\s+(all|exchanges|queues)?\s*(\S+)\s+(\S+)\s+(\d+)$}
27         applyto_index = 3
28         pattern_index = 4
29       end
30
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{\\\\}, '\\')
40
41         @policies[vhost][n] = {
42           applyto: applyto,
43           pattern: pattern,
44           definition: definition,
45           priority: priority
46         }
47       end
48     end
49     @policies[vhost][name]
50   end
51
52   def policies(vhost, name)
53     self.class.policies(vhost, name)
54   end
55
56   def should_policy
57     @should_policy ||= resource[:name].rpartition('@').first
58   end
59
60   def should_vhost
61     @should_vhost ||= resource[:name].rpartition('@').last
62   end
63
64   def create
65     set_policy
66   end
67
68   def destroy
69     rabbitmqctl('clear_policy', '-p', should_vhost, should_policy)
70   end
71
72   def exists?
73     policies(should_vhost, should_policy)
74   end
75
76   def pattern
77     policies(should_vhost, should_policy)[:pattern]
78   end
79
80   def pattern=(_pattern)
81     set_policy
82   end
83
84   def applyto
85     policies(should_vhost, should_policy)[:applyto]
86   end
87
88   def applyto=(_applyto)
89     set_policy
90   end
91
92   def definition
93     policies(should_vhost, should_policy)[:definition]
94   end
95
96   def definition=(_definition)
97     set_policy
98   end
99
100   def priority
101     policies(should_vhost, should_policy)[:priority]
102   end
103
104   def priority=(_priority)
105     set_policy
106   end
107
108   def set_policy
109     return if @set_policy
110     @set_policy = true
111     resource[:applyto]    ||= applyto
112     resource[:definition] ||= definition
113     resource[:pattern]    ||= pattern
114     resource[:priority]   ||= priority
115     # rabbitmq>=3.2.0
116     if Puppet::Util::Package.versioncmp(self.class.rabbitmq_version, '3.2.0') >= 0
117       rabbitmqctl(
118         'set_policy',
119         '-p', should_vhost,
120         '--priority', resource[:priority],
121         '--apply-to', resource[:applyto].to_s,
122         should_policy,
123         resource[:pattern],
124         resource[:definition].to_json
125       )
126     else
127       rabbitmqctl(
128         'set_policy',
129         '-p', should_vhost,
130         should_policy,
131         resource[:pattern],
132         resource[:definition].to_json,
133         resource[:priority]
134       )
135     end
136   end
137 end