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

fix endpoint url uniqueness validation, and disable it for cloud providers #18298

Merged
merged 3 commits into from
Dec 20, 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
6 changes: 5 additions & 1 deletion app/models/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Endpoint < ApplicationRecord
default_value_for :verify_ssl, OpenSSL::SSL::VERIFY_PEER
validates :verify_ssl, :inclusion => {:in => [OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER]}
validates :port, :numericality => {:only_integer => true, :allow_nil => true, :greater_than => 0}
validates :url, :uniqueness => true, :if => :url
validates :url, :uniqueness => true, :allow_blank => true, :unless => :allow_duplicate_url?
validate :validate_certificate_authority

after_create :endpoint_created
Expand Down Expand Up @@ -77,4 +77,8 @@ def validate_certificate_authority
rescue OpenSSL::OpenSSLError => err
errors.add(:certificate_authority, err.to_s)
end

def allow_duplicate_url?
resource.try(:allow_duplicate_endpoint_url?)
end
end
2 changes: 2 additions & 0 deletions app/models/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -844,4 +844,6 @@ def clear_association_cache
@storages = nil
super
end

define_method(:allow_duplicate_endpoint_url?) { false }
end
2 changes: 2 additions & 0 deletions app/models/manageiq/providers/cloud_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,7 @@ def destroy_mapped_tenants
source_tenant.destroy
end
end

define_method(:allow_duplicate_endpoint_url?) { true }
end
end
17 changes: 15 additions & 2 deletions spec/models/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,27 @@

context "Uniqueness validation on :url" do
it "is not required" do
expect(Endpoint.create!).to be_truthy
expect(Endpoint.create!).to be_truthy
expect(Endpoint.create!(:url => nil)).to be_truthy
expect(Endpoint.create!(:url => nil)).to be_truthy
expect(Endpoint.create!(:url => '')).to be_truthy
expect(Endpoint.create!(:url => '')).to be_truthy
end

it "raises when provided and already exists" do
Endpoint.create!(:url => "abc")
expect { Endpoint.create!(:url => "abc") }.to raise_error("Validation failed: Endpoint: Url has already been taken")
end

it 'disabled for cloud providers' do
expect(Endpoint.create!(:url => 'defined', :resource => FactoryBot.create(:ems_cloud))).to be_truthy
expect(Endpoint.create!(:url => 'defined', :resource => FactoryBot.create(:ems_cloud))).to be_truthy
end

it 'enabled for other emses' do
expect(Endpoint.create!(:url => 'defined', :resource => FactoryBot.create(:ext_management_system))).to be_truthy
expect { Endpoint.create!(:url => 'defined', :resource => FactoryBot.create(:ext_management_system)) }
.to raise_error("Validation failed: Endpoint: Url has already been taken")
end
end

context "certificate_authority" do
Expand Down