@@ -2578,27 +2578,6 @@ def get_fixed_ip_by_address(self, context, address):
2578
2578
raise exception .FixedIpAssociatedWithMultipleInstances (
2579
2579
address = address )
2580
2580
2581
- def _setup_net_dict (self , client , network_id ):
2582
- if not network_id :
2583
- return {}
2584
- pool = client .show_network (network_id )['network' ]
2585
- return {pool ['id' ]: pool }
2586
-
2587
- def _setup_port_dict (self , context , client , port_id ):
2588
- if not port_id :
2589
- return {}
2590
- port = self ._show_port (context , port_id , neutron_client = client )
2591
- return {port ['id' ]: port }
2592
-
2593
- def _setup_pools_dict (self , client ):
2594
- pools = self ._get_floating_ip_pools (client )
2595
- return {i ['id' ]: i for i in pools }
2596
-
2597
- def _setup_ports_dict (self , client , project_id = None ):
2598
- search_opts = {'tenant_id' : project_id } if project_id else {}
2599
- ports = client .list_ports (** search_opts )['ports' ]
2600
- return {p ['id' ]: p for p in ports }
2601
-
2602
2581
def get_floating_ip (self , context , id ):
2603
2582
"""Return floating IP object given the floating IP id."""
2604
2583
client = get_client (context )
@@ -2607,72 +2586,64 @@ def get_floating_ip(self, context, id):
2607
2586
except neutron_client_exc .NeutronClientException as e :
2608
2587
if e .status_code == 404 :
2609
2588
raise exception .FloatingIpNotFound (id = id )
2610
- else :
2611
- with excutils .save_and_reraise_exception ():
2612
- LOG .exception ('Unable to access floating IP %s' , id )
2613
- pool_dict = self . _setup_net_dict ( client ,
2614
- fip [ 'floating_network_id' ])
2615
- port_dict = self . _setup_port_dict ( context , client , fip [ 'port_id' ])
2616
- return self . _make_floating_ip_obj ( context , fip , pool_dict , port_dict )
2617
-
2618
- def _get_floating_ip_pools ( self , client , project_id = None ):
2619
- search_opts = { constants . NET_EXTERNAL : True }
2620
- if project_id :
2621
- search_opts . update ({ 'tenant_id' : project_id } )
2622
- data = client . list_networks ( ** search_opts )
2623
- return data [ 'networks' ]
2589
+
2590
+ with excutils .save_and_reraise_exception ():
2591
+ LOG .exception ('Unable to access floating IP %s' , id )
2592
+
2593
+ # retrieve and cache the network details now since many callers need
2594
+ # the network name which isn't present in the response from neutron
2595
+ network_uuid = fip [ 'floating_network_id' ]
2596
+ try :
2597
+ fip [ 'network_details' ] = client . show_network (
2598
+ network_uuid )[ 'network' ]
2599
+ except neutron_client_exc . NetworkNotFoundClient :
2600
+ raise exception . NetworkNotFound ( network_id = network_uuid )
2601
+
2602
+ return fip
2624
2603
2625
2604
def get_floating_ip_pools (self , context ):
2626
- """Return floating IP pool names ."""
2605
+ """Return floating IP pools a.k.a. external networks ."""
2627
2606
client = get_client (context )
2628
- pools = self ._get_floating_ip_pools (client )
2629
- return [n ['name' ] or n ['id' ] for n in pools ]
2630
-
2631
- def _make_floating_ip_obj (self , context , fip , pool_dict , port_dict ):
2632
- pool = pool_dict [fip ['floating_network_id' ]]
2633
- # NOTE(danms): Don't give these objects a context, since they're
2634
- # not lazy-loadable anyway
2635
- floating = objects .floating_ip .NeutronFloatingIP (
2636
- id = fip ['id' ], address = fip ['floating_ip_address' ],
2637
- pool = (pool ['name' ] or pool ['id' ]), project_id = fip ['tenant_id' ],
2638
- fixed_ip_id = fip ['port_id' ])
2639
- # In Neutron v2 API fixed_ip_address and instance uuid
2640
- # (= device_id) are known here, so pass it as a result.
2641
- if fip ['fixed_ip_address' ]:
2642
- floating .fixed_ip = objects .FixedIP (
2643
- address = fip ['fixed_ip_address' ])
2644
- else :
2645
- floating .fixed_ip = None
2646
- if fip ['port_id' ]:
2647
- instance_uuid = port_dict [fip ['port_id' ]]['device_id' ]
2648
- # NOTE(danms): This could be .refresh()d, so give it context
2649
- floating .instance = objects .Instance (context = context ,
2650
- uuid = instance_uuid )
2651
- if floating .fixed_ip :
2652
- floating .fixed_ip .instance_uuid = instance_uuid
2653
- else :
2654
- floating .instance = None
2655
- return floating
2607
+ data = client .list_networks (** {constants .NET_EXTERNAL : True })
2608
+ return data ['networks' ]
2656
2609
2657
2610
def get_floating_ip_by_address (self , context , address ):
2658
2611
"""Return a floating IP given an address."""
2659
2612
client = get_client (context )
2660
2613
fip = self ._get_floating_ip_by_address (client , address )
2661
- pool_dict = self ._setup_net_dict (client ,
2662
- fip ['floating_network_id' ])
2663
- port_dict = self ._setup_port_dict (context , client , fip ['port_id' ])
2664
- return self ._make_floating_ip_obj (context , fip , pool_dict , port_dict )
2614
+
2615
+ # retrieve and cache the network details now since many callers need
2616
+ # the network name which isn't present in the response from neutron
2617
+ network_uuid = fip ['floating_network_id' ]
2618
+ try :
2619
+ fip ['network_details' ] = client .show_network (
2620
+ network_uuid )['network' ]
2621
+ except neutron_client_exc .NetworkNotFoundClient :
2622
+ raise exception .NetworkNotFound (network_id = network_uuid )
2623
+
2624
+ return fip
2665
2625
2666
2626
def get_floating_ips_by_project (self , context ):
2667
2627
client = get_client (context )
2668
2628
project_id = context .project_id
2669
2629
fips = self ._safe_get_floating_ips (client , tenant_id = project_id )
2670
- if not fips :
2671
- return []
2672
- pool_dict = self ._setup_pools_dict (client )
2673
- port_dict = self ._setup_ports_dict (client , project_id )
2674
- return [self ._make_floating_ip_obj (context , fip , pool_dict , port_dict )
2675
- for fip in fips ]
2630
+
2631
+ # retrieve and cache the network details now since many callers need
2632
+ # the network name which isn't present in the response from neutron
2633
+ networks = {}
2634
+ for fip in fips :
2635
+ network_uuid = fip ['floating_network_id' ]
2636
+ if network_uuid not in networks :
2637
+ try :
2638
+ network = client .show_network (network_uuid )['network' ]
2639
+ except neutron_client_exc .NetworkNotFoundClient :
2640
+ raise exception .NetworkNotFound (network_id = network_uuid )
2641
+
2642
+ networks [network ['id' ]] = network
2643
+
2644
+ fip ['network_details' ] = networks [network_uuid ]
2645
+
2646
+ return fips
2676
2647
2677
2648
def get_instance_id_by_floating_address (self , context , address ):
2678
2649
"""Return the instance id a floating IP's fixed IP is allocated to."""
@@ -2787,7 +2758,7 @@ def release_floating_ip(self, context, address,
2787
2758
self ._release_floating_ip (context , address )
2788
2759
2789
2760
def disassociate_and_release_floating_ip (self , context , instance ,
2790
- floating_ip ):
2761
+ floating_ip ):
2791
2762
"""Removes (deallocates) and deletes the floating IP.
2792
2763
2793
2764
This api call was added to allow this to be done in one operation
@@ -2797,14 +2768,17 @@ def disassociate_and_release_floating_ip(self, context, instance,
2797
2768
@refresh_cache
2798
2769
def _release_floating_ip_and_refresh_cache (self , context , instance ,
2799
2770
floating_ip ):
2800
- self ._release_floating_ip (context , floating_ip ['address' ],
2801
- raise_if_associated = False )
2771
+ self ._release_floating_ip (
2772
+ context , floating_ip ['floating_ip_address' ],
2773
+ raise_if_associated = False )
2774
+
2802
2775
if instance :
2803
2776
_release_floating_ip_and_refresh_cache (self , context , instance ,
2804
2777
floating_ip )
2805
2778
else :
2806
- self ._release_floating_ip (context , floating_ip ['address' ],
2807
- raise_if_associated = False )
2779
+ self ._release_floating_ip (
2780
+ context , floating_ip ['floating_ip_address' ],
2781
+ raise_if_associated = False )
2808
2782
2809
2783
def _release_floating_ip (self , context , address ,
2810
2784
raise_if_associated = True ):
0 commit comments