From 4680ab8c23497d723d61ce53a669628d7e839799 Mon Sep 17 00:00:00 2001 From: Tong Li Date: Thu, 19 Jul 2012 18:13:28 -0400 Subject: [PATCH] Fixes nova-manage fixed list with deleted networks The fix addresses the bug reported in bug 1025827 currently command 'nova-manage fixed list' will return 'Command failed.' message when there is no network defined or all networks have been deleted or even when a network gets deleted, then a new network gets created. In all these cases, the command produces the 'Command failed.' error message which does not really tell what went wrong. This fix will produce 'No fixed IP found' in these conditions. changes made for globalization. changes made for based on the comments for patchset 7. Change-Id: I3c2e9bf3fa3c748e680c1df27d243a648ed47cf5 --- bin/nova-manage | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/bin/nova-manage b/bin/nova-manage index b477ed28..8fef85b6 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -282,24 +282,43 @@ class FixedIpCommands(object): _('IP address'), _('hostname'), _('host')) + + all_networks = {} + try: + # use network_get_all to retrieve all existing networks + # this is to ensure that IPs associated with deleted networks + # will not throw exceptions. + for network in db.network_get_all(context.get_admin_context()): + all_networks[network.id] = network + except exception.NoNetworksFound: + # do not have any networks, so even if there are IPs, these + # IPs should have been deleted ones, so return. + print _('No fixed IP found.') + return + + has_ip = False for fixed_ip in fixed_ips: hostname = None host = None mac_address = None - network = db.network_get(context.get_admin_context(), - fixed_ip['network_id']) - if fixed_ip['instance_id']: - instance = instances_by_id.get(fixed_ip['instance_id']) - if instance: - hostname = instance['hostname'] - host = instance['host'] - else: - print ('WARNING: fixed ip %s allocated to missing' - ' instance' % str(fixed_ip['address'])) - print "%-18s\t%-15s\t%-15s\t%s" % ( - network['cidr'], - fixed_ip['address'], - hostname, host) + network = all_networks.get(fixed_ip['network_id']) + if network: + has_ip = True + if fixed_ip['instance_id']: + instance = instances_by_id.get(fixed_ip['instance_id']) + if instance: + hostname = instance['hostname'] + host = instance['host'] + else: + print _('WARNING: fixed ip %s allocated to missing' + ' instance') % str(fixed_ip['address']) + print "%-18s\t%-15s\t%-15s\t%s" % ( + network['cidr'], + fixed_ip['address'], + hostname, host) + + if not has_ip: + print _('No fixed IP found.') @args('--address', dest="address", metavar='', help='IP address')