diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index 884213fc4de..8b792782c6a 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -19,6 +19,8 @@ class ConversionHost < ApplicationRecord validate :resource_supports_conversion_host + before_validation :name, :default_name, :on => :create + include_concern 'Configurations' after_create :tag_resource_as_enabled @@ -227,4 +229,10 @@ def tag_resource_as_disabled resource.tag_remove('v2v_transformation_method/vddk') resource.tag_remove('v2v_transformation_method/ssh') end + + # Set the default name to the name of the associated resource. + # + def default_name + self.name ||= resource&.name + end end diff --git a/spec/models/conversion_host_spec.rb b/spec/models/conversion_host_spec.rb index 56e6654897a..625e4f174be 100644 --- a/spec/models/conversion_host_spec.rb +++ b/spec/models/conversion_host_spec.rb @@ -210,4 +210,15 @@ expect(conversion_host.errors[:resource].first).to eql("can't be blank") end end + + context "name validation" do + let(:ems) { FactoryBot.create(:ems_redhat, :zone => FactoryBot.create(:zone), :api_version => '4.2.4') } + let(:redhat_host) { FactoryBot.create(:host_redhat, :name => 'foo', :ext_management_system => ems) } + + it "defaults to the associated resource name if no name is explicitly provided" do + conversion_host = ConversionHost.new(:resource => redhat_host) + expect(conversion_host.valid?).to be(true) + expect(conversion_host.name).to eql(redhat_host.name) + end + end end