Skip to content

Commit

Permalink
Merge pull request #12912 from moolitayer/remove_constraint
Browse files Browse the repository at this point in the history
Force unique endpoint hostname only for same type
  • Loading branch information
blomquisg authored Jan 4, 2017
2 parents 59f6825 + 0941c98 commit 7bfb5f3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions app/models/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ def self.supported_types_and_descriptions_hash
def hostname_uniqueness_valid?
return unless hostname_required?
return unless hostname.present? # Presence is checked elsewhere
# check uniqueness per provider type

existing_hostnames = Endpoint.where.not(:resource_id => id).pluck(:hostname).compact.map(&:downcase)
existing_hostnames = (self.class.all - [self]).map(&:hostname).compact.map(&:downcase)

errors.add(:hostname, "has already been taken") if existing_hostnames.include?(hostname.downcase)
errors.add(:hostname, N_("has to be unique per provider type")) if existing_hostnames.include?(hostname.downcase)
end

include NewWithTypeStiMixin
Expand Down
7 changes: 6 additions & 1 deletion spec/models/ext_management_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,16 @@
end.to_not raise_error
end

it "not allowing duplicate hostname" do
it "not allowing duplicate hostname for same type provider" do
expect do
FactoryGirl.create(:ems_vmware, :hostname => @ems.hostname, :tenant => @tenant2)
end.to raise_error(ActiveRecord::RecordInvalid)
end

it "allowing duplicate hostname for different type providers" do
FactoryGirl.create(:ems_microsoft, :hostname => @ems.hostname, :tenant => @tenant2)
expect(ExtManagementSystem.count).to eq(2)
end
end
end

Expand Down

0 comments on commit 7bfb5f3

Please sign in to comment.