From cb3979692290cae90ef68c52fbddb677986ace66 Mon Sep 17 00:00:00 2001 From: Andreas Pfau Date: Thu, 29 Aug 2024 15:33:38 +0200 Subject: [PATCH] fix(compute): load floating ip to get the id (#1403) --- .../compute/instances_controller.rb | 28 +++++++++++++++++++ .../app/helpers/compute/instances_helper.rb | 1 + plugins/compute/app/models/compute/server.rb | 8 +++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/plugins/compute/app/controllers/compute/instances_controller.rb b/plugins/compute/app/controllers/compute/instances_controller.rb index 34d08311a9..6cb5d0dcb1 100644 --- a/plugins/compute/app/controllers/compute/instances_controller.rb +++ b/plugins/compute/app/controllers/compute/instances_controller.rb @@ -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 } @@ -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 diff --git a/plugins/compute/app/helpers/compute/instances_helper.rb b/plugins/compute/app/helpers/compute/instances_helper.rb index ce77c6b480..e64da30d67 100644 --- a/plugins/compute/app/helpers/compute/instances_helper.rb +++ b/plugins/compute/app/helpers/compute/instances_helper.rb @@ -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 diff --git a/plugins/compute/app/models/compute/server.rb b/plugins/compute/app/models/compute/server.rb index ea461f2b4e..f7fd01f268 100644 --- a/plugins/compute/app/models/compute/server.rb +++ b/plugins/compute/app/models/compute/server.rb @@ -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| @@ -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" => { @@ -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