1165fd955c21da08543a7ca106d066cd80dea691
[mirror/dsa-puppet.git] / 3rdparty / modules / rabbitmq / lib / puppet / type / rabbitmq_binding.rb
1 Puppet::Type.newtype(:rabbitmq_binding) do
2   desc <<-DESC
3 Native type for managing rabbitmq bindings
4
5 @example Create a rabbitmq_binding
6  rabbitmq_binding { 'myexchange@myqueue@myvhost':
7    user             => 'dan',
8    password         => 'bar',
9    destination_type => 'queue',
10    routing_key      => '#',
11    arguments        => {},
12    ensure           => present,
13  }
14
15 @example Create bindings with same source / destination / vhost but different routing key using individual parameters
16 rabbitmq_binding { 'binding 1':
17   ensure           => present,
18   source           => 'myexchange',
19   destination      => 'myqueue',
20   vhost            => 'myvhost',
21   user             => 'dan',
22   password         => 'bar',
23   destination_type => 'queue',
24   routing_key      => 'key1',
25   arguments        => {},
26 }
27
28 rabbitmq_binding { 'binding 2':
29   ensure           => present,
30   source           => 'myexchange',
31   destination      => 'myqueue',
32   vhost            => 'myvhost',
33   user             => 'dan',
34   password         => 'bar',
35   destination_type => 'queue',
36   routing_key      => 'key2',
37   arguments        => {},
38 }
39 DESC
40
41   ensurable do
42     defaultto(:present)
43     newvalue(:present) do
44       provider.create
45     end
46     newvalue(:absent) do
47       provider.destroy
48     end
49   end
50
51   # Match patterns without '@' as arbitrary names; match patterns with
52   # src@destination@vhost to their named params for backwards compatibility.
53   def self.title_patterns
54     [
55       [
56         %r{(^([^@]*)$)}m,
57         [
58           [:name]
59         ]
60       ],
61       [
62         %r{^((\S+)@(\S+)@(\S+))$}m,
63         [
64           [:name],
65           [:source],
66           [:destination],
67           [:vhost]
68         ]
69       ]
70     ]
71   end
72
73   newparam(:name) do
74     desc 'resource name, either source@destination@vhost or arbitrary name with params'
75
76     isnamevar
77   end
78
79   newproperty(:source) do
80     desc 'source of binding'
81
82     newvalues(%r{^\S+$})
83     isnamevar
84   end
85
86   newproperty(:destination) do
87     desc 'destination of binding'
88
89     newvalues(%r{^\S+$})
90     isnamevar
91   end
92
93   newproperty(:vhost) do
94     desc 'vhost'
95
96     newvalues(%r{^\S+$})
97     defaultto('/')
98     isnamevar
99   end
100
101   newproperty(:routing_key) do
102     desc 'binding routing_key'
103
104     newvalues(%r{^\S*$})
105     isnamevar
106   end
107
108   newproperty(:destination_type) do
109     desc 'binding destination_type'
110     newvalues(%r{queue|exchange})
111     defaultto('queue')
112   end
113
114   newproperty(:arguments) do
115     desc 'binding arguments'
116     defaultto {}
117     validate do |value|
118       resource.validate_argument(value)
119     end
120   end
121
122   newparam(:user) do
123     desc 'The user to use to connect to rabbitmq'
124     defaultto('guest')
125     newvalues(%r{^\S+$})
126   end
127
128   newparam(:password) do
129     desc 'The password to use to connect to rabbitmq'
130     defaultto('guest')
131     newvalues(%r{\S+})
132   end
133
134   autorequire(:rabbitmq_vhost) do
135     setup_autorequire('vhost')
136   end
137
138   autorequire(:rabbitmq_exchange) do
139     setup_autorequire('exchange')
140   end
141
142   autorequire(:rabbitmq_queue) do
143     setup_autorequire('queue')
144   end
145
146   autorequire(:rabbitmq_user) do
147     [self[:user]]
148   end
149
150   autorequire(:rabbitmq_user_permissions) do
151     [
152       "#{self[:user]}@#{self[:source]}",
153       "#{self[:user]}@#{self[:destination]}"
154     ]
155   end
156
157   def setup_autorequire(type)
158     destination_type = value(:destination_type)
159     if type == 'exchange'
160       rval = ["#{self[:source]}@#{self[:vhost]}"]
161       if destination_type == type
162         rval.push("#{self[:destination]}@#{self[:vhost]}")
163       end
164     else
165       rval = if destination_type == type
166                ["#{self[:destination]}@#{self[:vhost]}"]
167              else
168                []
169              end
170     end
171     rval
172   end
173
174   def validate_argument(argument)
175     raise ArgumentError, 'Invalid argument' unless [Hash].include?(argument.class)
176   end
177
178   # Validate that we have both source and destination now that these are not
179   # necessarily only coming from the resource title.
180   validate do
181     if !self[:source] && !defined? provider.source
182       raise ArgumentError, '`source` must be defined'
183     end
184
185     if !self[:destination] && !defined? provider.destination
186       raise ArgumentError, '`destination` must be defined'
187     end
188   end
189 end