Skip to content
Open
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
6 changes: 5 additions & 1 deletion app/models/foreman_kubevirt/kubevirt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ def connection_properties_valid?

def networks
client.networkattachmentdefs.all
rescue StandardError => e
rescue Fog::Kubevirt::Errors::ClientError => e
logger.warn("Failed to retrieve network attachments definition from KubeVirt,
make sure KubeVirt has CNI provider and NetworkAttachmentDefinition CRD deployed: #{e.message}")
[]
end

def editable_network_interfaces?
true
end

def find_vm_by_uuid(uuid)
super
rescue Fog::Kubevirt::Errors::ClientError => e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

<div id='networks' class='<%= 'hide' if pod %>'>
<%= select_f f, :network,
compute_resource.networks,
(compute_resource.networks + [OpenStruct.new(name: f.object.network, id: f.object.network)]).compact,
:id,
:name,
{},
:data => { tags: true },
Copy link
Member Author

Choose a reason for hiding this comment

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

This works fine for creation, but when you try to edit (a host or a compute profile, which uses the same form), the value gets reset, as the custom value we injected is not part of the compute_resource.network list above

Copy link
Member Author

Choose a reason for hiding this comment

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

okay, adding it explicitly works, but ewww

Copy link
Member Author

Choose a reason for hiding this comment

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

it probably should be .compact.uniq, but I bet the openstruct thing won't uniq with a real network anyway 😭

Copy link
Member

Choose a reason for hiding this comment

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

You can use the block parameter in uniq to compare different classes of objects: https://apidock.com/ruby/Array/uniq

:disabled => !new_vm || pod,
:class => "col-md-3 kubevirt-network",
:label => _('Network'),
Expand Down
2 changes: 1 addition & 1 deletion test/models/compute_resources/kubevirt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def new_kubevirt_vcr
compute_resource = FactoryBot.build(:compute_resource_kubevirt)
client = stub
compute_resource.stubs(:client).returns(client)
client.stubs(:networkattachmentdefs).raises("exception")
client.stubs(:networkattachmentdefs).raises(Fog::Kubevirt::Errors::ClientError)
res = compute_resource.networks
assert_equal 0, res.count
end
Expand Down