Merge lp:~justin-fathomdb/nova/justinsb-api-fix-tolerate-no-ip into lp:~hudson-openstack/nova/trunk

Proposed by justinsb
Status: Superseded
Proposed branch: lp:~justin-fathomdb/nova/justinsb-api-fix-tolerate-no-ip
Merge into: lp:~hudson-openstack/nova/trunk
Diff against target: 36 lines (+13/-11)
1 file modified
nova/api/openstack/servers.py (+13/-11)
To merge this branch: bzr merge lp:~justin-fathomdb/nova/justinsb-api-fix-tolerate-no-ip
Reviewer Review Type Date Requested Status
Jay Pipes (community) Needs Fixing
Dan Prince (community) Approve
Review via email: mp+50858@code.launchpad.net

This proposal supersedes a proposal from 2011-02-19.

This proposal has been superseded by a proposal from 2011-02-23.

Description of the change

The describe servers call was throwing an error due to subscripting on None, if there was no fixed_ip element present

To post a comment you must log in.
Revision history for this message
Jay Pipes (jaypipes) wrote : Posted in a previous version of this proposal

Are there any bug reports to track these kinds of patches?

review: Needs Information
Revision history for this message
justinsb (justin-fathomdb) wrote : Posted in a previous version of this proposal

Filed a bug. However, right now the OpenStack API code is not thoroughly tested, so I don't know that this is a wise requirement unless we want to spend our lives opening and closing trivial bugs.

Revision history for this message
justinsb (justin-fathomdb) wrote : Posted in a previous version of this proposal

Also, Dan Prince pointed out that this bug has already been reported. Linked the original bug report directly to this branch also.

Revision history for this message
Dan Prince (dan-prince) wrote :

I like Justin's solution better. Lets use it.

review: Approve
Revision history for this message
Jay Pipes (jaypipes) wrote :

15 + fixed_ip = inst['fixed_ip']

should be:

fixed_ip = inst.get('fixed_ip')

the former will throw a KeyError if fixed_ip is not a key in the dict. The latter will return None.

Similarly, this:

18 + try:
19 + private_ip = fixed_ip['address']
20 + if private_ip:
21 + inst_dict['addresses']['private'].append(private_ip)
22 + except KeyError:
23 + LOG.debug(_("Failed to read private ip"))

Can be rewritten to:

private_ip = fixed_ip.get('address')
if private_ip:
    inst_dict['addresses']['private'].append(private_ip)
else:
    LOG.debug(_("Failed to read private IP"))

Cheers!
jay

review: Needs Fixing
702. By justinsb

Merged with trunk

Revision history for this message
justinsb (justin-fathomdb) wrote :

Thanks Jay - I don't know what I was thinking (or if I was thinking). But it does show that this code is error prone if "someone" isn't thinking :-)

I revised the code; I'm not the world's biggest fan of this approach though, so I also coded up a helper function that may make this cleaner, but (as you suggested on IRC) I've made it a separate patch:
http://bazaar.launchpad.net/~justin-fathomdb/nova/mini-xpath/revision/702

703. By justinsb

Another case where was using [] when meant .get(). Doh!

Unmerged revisions

703. By justinsb

Another case where was using [] when meant .get(). Doh!

702. By justinsb

Merged with trunk

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'nova/api/openstack/servers.py'
2--- nova/api/openstack/servers.py 2011-02-23 18:40:59 +0000
3+++ nova/api/openstack/servers.py 2011-02-23 20:06:26 +0000
4@@ -62,20 +62,22 @@
5 inst_dict['status'] = power_mapping[inst_dict['status']]
6 inst_dict['addresses'] = dict(public=[], private=[])
7
8- # grab single private fixed ip
9- try:
10- private_ip = inst['fixed_ip']['address']
11+ fixed_ip = inst.get('fixed_ip')
12+ if fixed_ip:
13+ # grab single private fixed ip
14+ private_ip = fixed_ip.get('address')
15 if private_ip:
16 inst_dict['addresses']['private'].append(private_ip)
17- except KeyError:
18- LOG.debug(_("Failed to read private ip"))
19
20- # grab all public floating ips
21- try:
22- for floating in inst['fixed_ip']['floating_ips']:
23- inst_dict['addresses']['public'].append(floating['address'])
24- except KeyError:
25- LOG.debug(_("Failed to read public ip(s)"))
26+ # grab all public floating ips
27+ floating_ips = fixed_ip['floating_ips']
28+ if floating_ips:
29+ for floating_ip in floating_ips:
30+ if floating_ip:
31+ floating_ip_address = floating_ip.get('address')
32+ if floating_ip_address:
33+ inst_dict['addresses']['public'].append(
34+ floating_ip_address)
35
36 inst_dict['hostId'] = ''
37