diff --git a/lib/fog/openstack/models/compute/server.rb b/lib/fog/openstack/models/compute/server.rb index efb6f8b1a..36fb98581 100644 --- a/lib/fog/openstack/models/compute/server.rb +++ b/lib/fog/openstack/models/compute/server.rb @@ -103,6 +103,7 @@ module Fog def all_addresses # currently openstack API does not tell us what is a floating ip vs a fixed ip for the vm listing, # we fall back to get all addresses and filter sadly. + # Only includes manually-assigned addresses, not auto-assigned @all_addresses ||= service.list_all_addresses.body["floating_ips"].select{|data| data['instance_id'] == id} end @@ -118,7 +119,22 @@ module Fog end def floating_ip_addresses - all_addresses.map{|addr| addr["ip"]} + all_floating=addresses.values.flatten.select{ |data| data["OS-EXT-IPS:type"]=="floating" }.map{|addr| addr["addr"] } + + # Return them all, leading with manually assigned addresses + manual = all_addresses.map{|addr| addr["ip"]} + + all_floating.sort{ |a,b| + a_manual = manual.include? a + b_manual = manual.include? b + + if a_manual and !b_manual + -1 + elsif !a_manual and b_manual + 1 + else 0 end + } + end alias_method :public_ip_addresses, :floating_ip_addresses diff --git a/lib/fog/openstack/requests/compute/associate_address.rb b/lib/fog/openstack/requests/compute/associate_address.rb index 362550069..7184a91b3 100644 --- a/lib/fog/openstack/requests/compute/associate_address.rb +++ b/lib/fog/openstack/requests/compute/associate_address.rb @@ -12,6 +12,12 @@ module Fog class Mock def associate_address(server_id, ip_address) + + server = data[:servers][server_id] + server["addresses"]['mocknet'] ||= [] + ip_hash = {"OS-EXT-IPS-MAC:mac_addr"=>"fa:16:3e:85:47:40", "version"=>4, "addr"=>ip_address, "OS-EXT-IPS:type"=>"floating"} + server["addresses"]['mocknet'] << ip_hash + response = Excon::Response.new response.status = 202 response.headers = { diff --git a/tests/openstack/models/compute/server_tests.rb b/tests/openstack/models/compute/server_tests.rb index d33b3e78f..231e61db8 100644 --- a/tests/openstack/models/compute/server_tests.rb +++ b/tests/openstack/models/compute/server_tests.rb @@ -2,6 +2,27 @@ Shindo.tests("Fog::Compute[:openstack] | server", ['openstack']) do tests('success') do + tests('#floating_ips').succeeds do + fog = Fog::Compute[:openstack] + net = Fog::Network[:openstack] + flavor = fog.flavors.first.id + image = fog.images.first.id + + server = fog.servers.new(:name => 'test server', + :flavor_ref => flavor, + :image_ref => image) + server.save + + ip1 = net.floating_ips.create(:floating_network_id => 'f0000000-0000-0000-0000\ +-000000000000', + :fixed_ip_address => '192.168.11.3') + + server.associate_address(ip1.fixed_ip_address) + server.reload + + returns( ["192.168.11.3"] ) { server.floating_ip_addresses } + end + tests('#security_groups').succeeds do fog = Fog::Compute[:openstack]