Skip to content

Commit

Permalink
Add support for vhost metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmybigcommerce committed Dec 6, 2023
1 parent bda02fc commit 9632d42
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
8 changes: 2 additions & 6 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,7 @@ $ puppet resource rabbitmq_vhost`
rabbitmq_vhost { 'myvhost':
ensure => present,
description => 'myvhost description',
tags => ['myvhost tags'],
tags => ['tag1', 'tag2'],
default_queue_type => 'quorum',
}
```
Expand All @@ -2093,18 +2093,14 @@ The following properties are available in the `rabbitmq_vhost` type.

##### `default_queue_type`

Valid values: `%r{undefined|classic|quorum|stream}`
Valid values: `classic`, `quorum`, `stream`

The default queue type for queues in this vhost

Default value: `undefined`

##### `description`

A description of the vhost

Default value: `''`

##### `ensure`

Valid values: `present`, `absent`
Expand Down
19 changes: 8 additions & 11 deletions lib/puppet/provider/rabbitmq_vhost/rabbitmqctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
) do
confine feature: :posix

def initialize(value = {})
super(value)
@property_flush = {}
end

def self.prefetch(resources)
instances.each do |prov|
if (resource = resources[prov.name])
Expand Down Expand Up @@ -47,11 +42,13 @@ def self.instances
(matches = line.match(%r{^(\S+)\t+(.*?)\t+(undefined|quorum|classic|stream)?\t+\[(.*?)\]$}i))

name, description, default_queue_type, tags = matches.captures
new(name: name, description: description, default_queue_type: default_queue_type, tags: tags.split(%r{,\s*}))
# RMQ returns 'undefined' as default_queue_type if it has never been set
default_queue_type = nil if default_queue_type == 'undefined'
new(ensure: :present, name: name, description: description, default_queue_type: default_queue_type, tags: tags.split(%r{,\s*}))
else
raise Puppet::Error, "Cannot parse invalid vhost line: #{line}" unless line =~ %r{^(\S+)$}

new(name: Regexp.last_match(1))
new(ensure: :present, name: Regexp.last_match(1))
end
end
end
Expand All @@ -63,7 +60,7 @@ def create
def params
params = [resource[:name]]
if supports_metadata?
params << ['--description', resource[:description] || '""'] if resource[:description]
params << ['--description', resource[:description]] if resource[:description]
params << ['--default-queue-type', resource[:default_queue_type]] if resource[:default_queue_type] && resource[:default_queue_type] != 'undefined'
params << ['--tags', resource[:tags].join(',')] if resource[:tags]
end
Expand Down Expand Up @@ -95,11 +92,11 @@ def default_queue_type=(value)
end

def flush
return if @property_hash.empty? || !supports_metadata?
return if @property_hash.empty? || !supports_metadata? || !exists?

params = [resource[:name]]
params << ['--description', @property_hash[:description]] if @property_hash[:description] && @property_hash[:description] != ''
params << ['--default-queue-type', @property_hash[:default_queue_type]] if @property_hash[:default_queue_type] && @property_hash[:default_queue_type] != 'undefined'
params << ['--description', @property_hash[:description]] if @property_hash[:description]
params << ['--default-queue-type', @property_hash[:default_queue_type]] if @property_hash[:default_queue_type]
params << ['--tags', @property_hash[:tags].join(',')] if @property_hash[:tags]
rabbitmqctl('update_vhost_metadata', *params)
end
Expand Down
8 changes: 3 additions & 5 deletions lib/puppet/type/rabbitmq_vhost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
rabbitmq_vhost { 'myvhost':
ensure => present,
description => 'myvhost description',
tags => ['myvhost tags'],
tags => ['tag1', 'tag2'],
default_queue_type => 'quorum',
}
DESC
Expand All @@ -35,14 +35,12 @@

newproperty(:description) do
desc 'A description of the vhost'
defaultto('')
# newvalues(%r{^\S+$})
end

newproperty(:default_queue_type) do
desc 'The default queue type for queues in this vhost'
newvalues(%r{undefined|classic|quorum|stream})
defaultto('undefined')
newvalues(:classic, :quorum, :stream)
munge(&:to_s)
end

newproperty(:tags, array_matching: :all) do
Expand Down
10 changes: 6 additions & 4 deletions spec/unit/puppet/provider/rabbitmq_vhost/rabbitmqctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
it 'parses vhost list with valid metadata' do
provider.class.expects(:supports_metadata?).at_least_once.returns true
provider.class.expects(:vhost_list).returns <<~EOT
inventory undefined []
inventory classic []
/ Default virtual host undefined []
search quorum []
testing My cool vhost undefined [tag1, tag2]
Expand All @@ -69,13 +69,13 @@
{
name: 'inventory',
description: '',
default_queue_type: 'undefined',
default_queue_type: 'classic',
tags: []
},
{
name: '/',
description: 'Default virtual host',
default_queue_type: 'undefined',
default_queue_type: :absent,
tags: []
},
{
Expand All @@ -87,7 +87,7 @@
{
name: 'testing',
description: 'My cool vhost',
default_queue_type: 'undefined',
default_queue_type: :absent,
tags: %w[tag1 tag2]
}
]
Expand All @@ -112,13 +112,15 @@

it 'updates tags' do
provider.set(tags: %w[tag1 tag2])
provider.expects(:exists?).at_least_once.returns true
provider.expects(:supports_metadata?).at_least_once.returns true
provider.expects(:rabbitmqctl).with('update_vhost_metadata', 'foo', ['--tags', 'tag1,tag2'])
provider.flush
end

it 'updates description' do
provider.set(description: 'this is the new description')
provider.expects(:exists?).at_least_once.returns true
provider.expects(:supports_metadata?).at_least_once.returns true
provider.expects(:rabbitmqctl).with('update_vhost_metadata', 'foo', ['--description', 'this is the new description'])
provider.flush
Expand Down

0 comments on commit 9632d42

Please sign in to comment.