Skip to content

Commit

Permalink
Merge pull request #17247 from Fryguy/change_validator_exception
Browse files Browse the repository at this point in the history
Give more information on a failed configuration validation.
  • Loading branch information
gtanzillo authored Apr 3, 2018
2 parents 3cdf12f + e089d49 commit 9e95f2b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/models/authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.validate_config(config)
if authenticator
authenticator.validate_config
else
[:mode, "authentication type, #{config[:mode].inspect}, invalid. Should be one of: #{valid_modes.join(", ")}"]
[[:mode, "authentication mode, #{config[:mode].inspect}, is invalid. Should be one of: #{valid_modes.join(", ")}"]]
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/authenticator/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.authenticates_for

def self.validate_config(config)
if config[:ldaphost].blank?
[:ldaphost, "ldaphost can't be blank"]
[[:ldaphost, "ldaphost can't be blank"]]
else
[]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/vmdb/config/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def validate
_log.debug(" Invalid: #{errors}")
errors.each do |e|
key, msg = e
@errors[[k, key].join("_")] = msg
@errors[[k, key].join("-")] = msg
end
valid = false
end
Expand Down
21 changes: 17 additions & 4 deletions lib/vmdb/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Vmdb
class Settings
extend Vmdb::SettingsWalker::ClassMethods

class ConfigurationInvalid < StandardError; end

PASSWORD_FIELDS = Vmdb::SettingsWalker::PASSWORD_FIELDS
DUMP_LOG_FILE = Rails.root.join("log/last_settings.txt").freeze

Expand Down Expand Up @@ -37,17 +39,28 @@ def self.activate
VMDB::Config::Activator.new(::Settings).activate
end

def self.validate
VMDB::Config::Validator.new(::Settings).validate
def self.validator(settings = ::Settings)
VMDB::Config::Validator.new(settings)
end
private_class_method :validator

def self.validate(settings = ::Settings)
validator(settings).validate
end

def self.valid?
validate.first
validator.valid?
end

def self.save!(resource, hash)
new_settings = build_without_local(resource).load!.merge!(hash).to_hash
raise "configuration invalid" unless VMDB::Config::Validator.new(new_settings).valid?

valid, errors = validate(new_settings)
unless valid
message = errors.map { |k, v| "#{k}: #{v}" }.join("; ")
raise ConfigurationInvalid, message
end

hash_for_parent = parent_settings_without_local(resource).load!.to_hash
diff = HashDiffer.diff(hash_for_parent, new_settings)
encrypt_passwords!(diff)
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/vmdb/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
it "does not allow invalid configuration values" do
expect do
described_class.save!(miq_server, :authentication => {:mode => "stuff"})
end.to raise_error(RuntimeError, "configuration invalid")
end.to raise_error(described_class::ConfigurationInvalid)
end

it "with a change" do
Expand Down

0 comments on commit 9e95f2b

Please sign in to comment.