File: rabbitmq_binding.rb

package info (click to toggle)
puppet-module-puppetlabs-rabbitmq 8.5.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,192 kB
  • sloc: ruby: 5,227; sh: 10; makefile: 4
file content (189 lines) | stat: -rw-r--r-- 4,211 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
Puppet::Type.newtype(:rabbitmq_binding) do
  desc <<-DESC
Native type for managing rabbitmq bindings

@example Create a rabbitmq_binding
 rabbitmq_binding { 'myexchange@myqueue@myvhost':
   user             => 'dan',
   password         => 'bar',
   destination_type => 'queue',
   routing_key      => '#',
   arguments        => {},
   ensure           => present,
 }

@example Create bindings with same source / destination / vhost but different routing key using individual parameters
rabbitmq_binding { 'binding 1':
  ensure           => present,
  source           => 'myexchange',
  destination      => 'myqueue',
  vhost            => 'myvhost',
  user             => 'dan',
  password         => 'bar',
  destination_type => 'queue',
  routing_key      => 'key1',
  arguments        => {},
}

rabbitmq_binding { 'binding 2':
  ensure           => present,
  source           => 'myexchange',
  destination      => 'myqueue',
  vhost            => 'myvhost',
  user             => 'dan',
  password         => 'bar',
  destination_type => 'queue',
  routing_key      => 'key2',
  arguments        => {},
}
DESC

  ensurable do
    defaultto(:present)
    newvalue(:present) do
      provider.create
    end
    newvalue(:absent) do
      provider.destroy
    end
  end

  # Match patterns without '@' as arbitrary names; match patterns with
  # src@destination@vhost to their named params for backwards compatibility.
  def self.title_patterns
    [
      [
        %r{(^([^@]*)$)}m,
        [
          [:name]
        ]
      ],
      [
        %r{^((\S+)@(\S+)@(\S+))$}m,
        [
          [:name],
          [:source],
          [:destination],
          [:vhost]
        ]
      ]
    ]
  end

  newparam(:name) do
    desc 'resource name, either source@destination@vhost or arbitrary name with params'

    isnamevar
  end

  newproperty(:source) do
    desc 'source of binding'

    newvalues(%r{^\S+$})
    isnamevar
  end

  newproperty(:destination) do
    desc 'destination of binding'

    newvalues(%r{^\S+$})
    isnamevar
  end

  newproperty(:vhost) do
    desc 'vhost'

    newvalues(%r{^\S+$})
    defaultto('/')
    isnamevar
  end

  newproperty(:routing_key) do
    desc 'binding routing_key'

    newvalues(%r{^\S*$})
    isnamevar
  end

  newproperty(:destination_type) do
    desc 'binding destination_type'
    newvalues(%r{queue|exchange})
    defaultto('queue')
  end

  newproperty(:arguments) do
    desc 'binding arguments'
    defaultto {}
    validate do |value|
      resource.validate_argument(value)
    end
  end

  newparam(:user) do
    desc 'The user to use to connect to rabbitmq'
    defaultto('guest')
    newvalues(%r{^\S+$})
  end

  newparam(:password) do
    desc 'The password to use to connect to rabbitmq'
    defaultto('guest')
    newvalues(%r{\S+})
  end

  autorequire(:rabbitmq_vhost) do
    setup_autorequire('vhost')
  end

  autorequire(:rabbitmq_exchange) do
    setup_autorequire('exchange')
  end

  autorequire(:rabbitmq_queue) do
    setup_autorequire('queue')
  end

  autorequire(:rabbitmq_user) do
    [self[:user]]
  end

  autorequire(:rabbitmq_user_permissions) do
    [
      "#{self[:user]}@#{self[:source]}",
      "#{self[:user]}@#{self[:destination]}"
    ]
  end

  def setup_autorequire(type)
    destination_type = value(:destination_type)
    if type == 'exchange'
      rval = ["#{self[:source]}@#{self[:vhost]}"]
      if destination_type == type
        rval.push("#{self[:destination]}@#{self[:vhost]}")
      end
    else
      rval = if destination_type == type
               ["#{self[:destination]}@#{self[:vhost]}"]
             else
               []
             end
    end
    rval
  end

  def validate_argument(argument)
    raise ArgumentError, 'Invalid argument' unless [Hash].include?(argument.class)
  end

  # Validate that we have both source and destination now that these are not
  # necessarily only coming from the resource title.
  validate do
    if !self[:source] && provider.source == :absent
      raise ArgumentError, '`source` must be defined'
    end

    if !self[:destination] && provider.destination == :absent
      raise ArgumentError, '`destination` must be defined'
    end
  end
end