Skip to content

Commit

Permalink
Validate password in create rather than in type (voxpupuli#147) - htt…
Browse files Browse the repository at this point in the history
  • Loading branch information
William Yardley committed Sep 12, 2017
1 parent 810ad4c commit 4a2a470
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
4 changes: 4 additions & 0 deletions lib/puppet/provider/rabbitmq_user/rabbitmqctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def exists?
end

def create
# Fail here (rather than a validate block in the type) if password is not
# set, so that "puppet resource" still works.
raise Puppet::Error, "Password is a required parameter for rabbitmq_user (user: #{name})" if @resource[:password].nil?

rabbitmqctl('add_user', @resource[:name], @resource[:password])

tags = @resource[:tags]
Expand Down
6 changes: 0 additions & 6 deletions lib/puppet/type/rabbitmq_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,4 @@ def should_to_s(value)
Array(value)
end
end

validate do
if self[:ensure] == :present && !self[:password]
raise ArgumentError, 'must set password when creating user' unless self[:password]
end
end
end
14 changes: 14 additions & 0 deletions spec/unit/puppet/provider/rabbitmq_user/rabbitmqctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
provider.expects(:rabbitmqctl).with('add_user', 'rmq_x', 'secret')
provider.create
end
context 'no password supplied' do
let(:resource) do
Puppet::Type.type(:rabbitmq_user).new(
ensure: :present,
name: 'rmq_x'
)
end

it 'raises an error' do
expect do
provider.create
end.to raise_error(Puppet::Error, 'Password is a required parameter for rabbitmq_user (user: rmq_x)')
end
end
end

describe '#destroy' do
Expand Down

0 comments on commit 4a2a470

Please sign in to comment.