Skip to content

Commit

Permalink
fix(compute): load floating ip to get the id (#1403)
Browse files Browse the repository at this point in the history
  • Loading branch information
andypf authored Aug 29, 2024
1 parent c67383c commit cb39796
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
28 changes: 28 additions & 0 deletions plugins/compute/app/controllers/compute/instances_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,20 @@ def attach_floatingip
@floating_ip.fixed_ip_address = params[:floating_ip][:fixed_ip_address]

if @floating_ip.save
# add floating ip to instance to make it visible in the view
# example: {\"version\"=>4, \"addr\"=>\"10.237.208.46\", \"OS-EXT-IPS:type\"=>\"floating\", \"OS-EXT-IPS-MAC:mac_addr\"=>\"fa:16:3e:a0:1b:e9\"}
@instance.addresses.each do |network, addresses|
next unless addresses.find do |addr|
addr["OS-EXT-IPS:type"] == "fixed" && addr["addr"] == @floating_ip.fixed_ip_address
end
addresses << {
"version" => 4,
"addr" => @floating_ip.floating_ip_address,
"OS-EXT-IPS:type" => "floating",
"OS-EXT-IPS-MAC:mac_addr" => port.mac_address,
}
end
# byebug
load_security_groups(@instance) if @action_from_show
respond_to do |format|
format.html { redirect_to instances_url }
Expand Down Expand Up @@ -483,12 +497,26 @@ def detach_floatingip

if @floating_ip && @floating_ip.detach
@instance = services.compute.find_server(params[:id])

# because of a delay we have to delete the floating ip from instance manually
@instance.addresses.each do |network,addresses|
addresses.delete_if do |addr|
addr["OS-EXT-IPS:type"] == "floating" && addr["addr"] == @floating_ip.floating_ip_address
end
end

load_security_groups(@instance) if @action_from_show
respond_to do |format|
format.html { redirect_to instances_url }
format.js {}
end
else
if @floating_ip.nil?
@floating_ip = services.networking.new_floating_ip
@floating_ip.errors.add(:floating_ip, "Not found.")
end
# create instance to show the form which needs the instance object
@instance = services.compute.find_server(params[:id])
render action: :remove_floatingip
end
end
Expand Down
1 change: 1 addition & 0 deletions plugins/compute/app/helpers/compute/instances_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def instance_ips(instance)
# puts "############ one to many ip-fip relation found - use project_floating_ips ###########"
@project_floating_ips = services.networking.project_floating_ips(@scoped_project_id)
end
# byebug
instance.ip_maps(@project_floating_ips)
end

Expand Down
8 changes: 7 additions & 1 deletion plugins/compute/app/models/compute/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def ip_maps(project_floating_ips = [])
server_floating_ips_and_network = {}
server_fixed_ips = []
server_fixed_ips_and_network = {}

# byebug
# extract the fips and fixed ips for the server
addresses.each do |network_name, ips|
ips.each do |ip|
Expand Down Expand Up @@ -262,6 +262,10 @@ def ip_maps(project_floating_ips = [])
# check if there is only one floating IP and one fixed IP
if fips.length == 1 && server_fixed_ips_and_network[network_name].length == 1
# if there is only one floating IP and one fixed IP, we can assume that the floating IP is associated with the fixed IP

# load the floating IP object to access the floating IP ID
floating_ip = @service.service_manager.networking.floating_ips({floating_ip_address: fips.first}).first
# byebug
fip_ip_one_to_one_maps <<
{
"fixed" => {
Expand All @@ -271,6 +275,8 @@ def ip_maps(project_floating_ips = [])
"floating" => {
"addr" => fips.first,
"network_name" => network_name,
# add floating IP ID to the map
"id" => floating_ip&.id,
},
}
end
Expand Down

0 comments on commit cb39796

Please sign in to comment.