Skip to content

Commit cb39796

Browse files
authored
fix(compute): load floating ip to get the id (#1403)
1 parent c67383c commit cb39796

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

plugins/compute/app/controllers/compute/instances_controller.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,20 @@ def attach_floatingip
454454
@floating_ip.fixed_ip_address = params[:floating_ip][:fixed_ip_address]
455455

456456
if @floating_ip.save
457+
# add floating ip to instance to make it visible in the view
458+
# 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\"}
459+
@instance.addresses.each do |network, addresses|
460+
next unless addresses.find do |addr|
461+
addr["OS-EXT-IPS:type"] == "fixed" && addr["addr"] == @floating_ip.fixed_ip_address
462+
end
463+
addresses << {
464+
"version" => 4,
465+
"addr" => @floating_ip.floating_ip_address,
466+
"OS-EXT-IPS:type" => "floating",
467+
"OS-EXT-IPS-MAC:mac_addr" => port.mac_address,
468+
}
469+
end
470+
# byebug
457471
load_security_groups(@instance) if @action_from_show
458472
respond_to do |format|
459473
format.html { redirect_to instances_url }
@@ -483,12 +497,26 @@ def detach_floatingip
483497

484498
if @floating_ip && @floating_ip.detach
485499
@instance = services.compute.find_server(params[:id])
500+
501+
# because of a delay we have to delete the floating ip from instance manually
502+
@instance.addresses.each do |network,addresses|
503+
addresses.delete_if do |addr|
504+
addr["OS-EXT-IPS:type"] == "floating" && addr["addr"] == @floating_ip.floating_ip_address
505+
end
506+
end
507+
486508
load_security_groups(@instance) if @action_from_show
487509
respond_to do |format|
488510
format.html { redirect_to instances_url }
489511
format.js {}
490512
end
491513
else
514+
if @floating_ip.nil?
515+
@floating_ip = services.networking.new_floating_ip
516+
@floating_ip.errors.add(:floating_ip, "Not found.")
517+
end
518+
# create instance to show the form which needs the instance object
519+
@instance = services.compute.find_server(params[:id])
492520
render action: :remove_floatingip
493521
end
494522
end

plugins/compute/app/helpers/compute/instances_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ def instance_ips(instance)
304304
# puts "############ one to many ip-fip relation found - use project_floating_ips ###########"
305305
@project_floating_ips = services.networking.project_floating_ips(@scoped_project_id)
306306
end
307+
# byebug
307308
instance.ip_maps(@project_floating_ips)
308309
end
309310

plugins/compute/app/models/compute/server.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def ip_maps(project_floating_ips = [])
229229
server_floating_ips_and_network = {}
230230
server_fixed_ips = []
231231
server_fixed_ips_and_network = {}
232-
232+
# byebug
233233
# extract the fips and fixed ips for the server
234234
addresses.each do |network_name, ips|
235235
ips.each do |ip|
@@ -262,6 +262,10 @@ def ip_maps(project_floating_ips = [])
262262
# check if there is only one floating IP and one fixed IP
263263
if fips.length == 1 && server_fixed_ips_and_network[network_name].length == 1
264264
# if there is only one floating IP and one fixed IP, we can assume that the floating IP is associated with the fixed IP
265+
266+
# load the floating IP object to access the floating IP ID
267+
floating_ip = @service.service_manager.networking.floating_ips({floating_ip_address: fips.first}).first
268+
# byebug
265269
fip_ip_one_to_one_maps <<
266270
{
267271
"fixed" => {
@@ -271,6 +275,8 @@ def ip_maps(project_floating_ips = [])
271275
"floating" => {
272276
"addr" => fips.first,
273277
"network_name" => network_name,
278+
# add floating IP ID to the map
279+
"id" => floating_ip&.id,
274280
},
275281
}
276282
end

0 commit comments

Comments
 (0)