diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index 319870e3002..ecb6684f7cc 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' # To be eligible, a conversion host must have the following properties @@ -202,4 +204,10 @@ def tag_resource_as_disabled resource.tag_add('v2v_transformation_host/false') resource.tag_remove('v2v_transformation_method/vddk') 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 c87f88b3e5b..fab5359bd10 100644 --- a/spec/models/conversion_host_spec.rb +++ b/spec/models/conversion_host_spec.rb @@ -239,4 +239,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