diff --git a/app/models/ext_management_system.rb b/app/models/ext_management_system.rb index a113b4637b6..5e834b3e761 100644 --- a/app/models/ext_management_system.rb +++ b/app/models/ext_management_system.rb @@ -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 diff --git a/spec/models/ext_management_system_spec.rb b/spec/models/ext_management_system_spec.rb index b442efa7cd5..5e4038900e6 100644 --- a/spec/models/ext_management_system_spec.rb +++ b/spec/models/ext_management_system_spec.rb @@ -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