Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V2V] Set default address for conversion host #18577

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/models/conversion_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ConversionHost < ApplicationRecord
validate :resource_supports_conversion_host

before_validation :name, :default_name, :on => :create
before_validation :address, :default_address, :on => :create

include_concern 'Configurations'

Expand Down Expand Up @@ -53,7 +54,7 @@ def source_transport_method

def ipaddress(family = 'ipv4')
return address if address.present? && IPAddr.new(address).send("#{family}?")
resource.ipaddresses.detect { |ip| IPAddr.new(ip).send("#{family}?") }
resource&.ipaddresses&.detect { |ip| IPAddr.new(ip).send("#{family}?") }
djberg96 marked this conversation as resolved.
Show resolved Hide resolved
end

def run_conversion(conversion_options)
Expand Down Expand Up @@ -229,4 +230,10 @@ def tag_resource_as_disabled
def default_name
self.name ||= resource&.name
end

# Set the default address to the first IP address associated with the resource.
#
def default_address
djberg96 marked this conversation as resolved.
Show resolved Hide resolved
self.address ||= self.ipaddress
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to NAK based on this, we should be able to handle address not being set and this will break if the vm/host ipaddress changes.

Copy link
Contributor Author

@djberg96 djberg96 Mar 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the thing to do then is to not make the ipaddress address field mandatory, though it makes me wonder why we have it at all then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we have which field? :address?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Seems like it should just be a virtual attribute. But, whatever, that's neither here nor there for now I suppose.

end
end
16 changes: 16 additions & 0 deletions spec/models/conversion_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@

context "address validation" do
let(:vm) { FactoryBot.create(:vm_openstack) }
let(:ems) { FactoryBot.create(:ems_redhat, :zone => FactoryBot.create(:zone), :api_version => '4.2.4') }
let(:host) { FactoryBot.create(:host_redhat, :ext_management_system => ems) }

it "is invalid if the address is not a valid IP address" do
allow(vm).to receive(:ipaddresses).and_return(['127.0.0.1'])
Expand All @@ -215,6 +217,20 @@
conversion_host = ConversionHost.new(:name => "test", :resource => vm, :address => "127.0.0.2")
expect(conversion_host.valid?).to be(true)
end

it "defaults to the first associated ip address if no address is explicitly provided for a vm" do
allow(vm).to receive(:ipaddresses).and_return(['127.0.0.1', '127.0.0.2'])
conversion_host = ConversionHost.new(:name => "test", :resource => vm)
expect(conversion_host.valid?).to be(true)
expect(conversion_host.address).to eql(vm.ipaddresses.first)
end

it "defaults to the first associated ip address if no address is explicitly provided for a host" do
allow(vm).to receive(:ipaddresses).and_return(['127.0.0.3', '127.0.0.4'])
conversion_host = ConversionHost.new(:name => "test", :resource => host)
expect(conversion_host.valid?).to be(true)
expect(conversion_host.address).to eql(host.ipaddresses.first)
end
end

context "resource validation" do
Expand Down