Skip to content

Commit

Permalink
Merge pull request #18298 from AlexanderZagaynov/BZ-1658207
Browse files Browse the repository at this point in the history
fix endpoint url uniqueness validation, and disable it for cloud providers
  • Loading branch information
agrare authored Dec 20, 2018
2 parents 73e075e + ab9cc07 commit b8927b0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
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

0 comments on commit b8927b0

Please sign in to comment.