Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give more information on a failed configuration validation. #17247

Merged
merged 1 commit into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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