]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/rabbitmq/lib/puppet/provider/rabbitmq_policy/rabbitmqctl.rb
move to puppetlabs rabbitmq module
[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
7   defaultfor :feature => :posix
8
9   # cache policies
10   def self.policies(name, vhost)
11     @policies = {} unless @policies
12     unless @policies[vhost]
13       @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
18         # 1 2      3?  4  5                                            6
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+)$/
21           applyto = $3 || 'all'
22           @policies[vhost][$2] = {
23             :applyto    => applyto,
24             :pattern    => $4,
25             :definition => JSON.parse($5),
26             :priority   => $6}
27         else
28           raise Puppet::Error, "cannot parse line from list_policies:#{line}"
29         end
30       end
31     end
32     @policies[vhost][name]
33   end
34
35   def policies(name, vhost)
36     self.class.policies(vhost, name)
37   end
38
39   def should_policy
40     @should_policy ||= resource[:name].rpartition('@').first
41   end
42
43   def should_vhost
44     @should_vhost ||= resource[:name].rpartition('@').last
45   end
46
47   def create
48     set_policy
49   end
50
51   def destroy
52     rabbitmqctl('clear_policy', '-p', should_vhost, should_policy)
53   end
54
55   def exists?
56     policies(should_vhost, should_policy)
57   end
58
59   def pattern
60     policies(should_vhost, should_policy)[:pattern]
61   end
62
63   def pattern=(pattern)
64     set_policy
65   end
66
67   def applyto
68     policies(should_vhost, should_policy)[:applyto]
69   end
70
71   def applyto=(applyto)
72     set_policy
73   end
74
75   def definition
76     policies(should_vhost, should_policy)[:definition]
77   end
78
79   def definition=(definition)
80     set_policy
81   end
82
83   def priority
84     policies(should_vhost, should_policy)[:priority]
85   end
86
87   def priority=(priority)
88     set_policy
89   end
90
91   def set_policy
92     unless @set_policy
93       @set_policy = true
94       resource[:applyto]    ||= applyto
95       resource[:definition] ||= definition
96       resource[:pattern]    ||= pattern
97       resource[:priority]   ||= priority
98       # rabbitmq>=3.2.0
99       if Puppet::Util::Package.versioncmp(self.class.rabbitmq_version, '3.2.0') >= 0
100         rabbitmqctl('set_policy',
101           '-p', should_vhost,
102           '--priority', resource[:priority],
103           '--apply-to', resource[:applyto].to_s,
104           should_policy,
105           resource[:pattern],
106           resource[:definition].to_json
107         )
108       else
109         rabbitmqctl('set_policy',
110           '-p', should_vhost,
111           should_policy,
112           resource[:pattern],
113           resource[:definition].to_json,
114           resource[:priority]
115         )
116       end
117     end
118   end
119 end