Printing novaclient.v1_1.servers.Server object causes AttributeError

Bug #1280453 reported by Christian Huebner
22
This bug affects 4 people
Affects Status Importance Assigned to Milestone
python-novaclient
Fix Released
Undecided
ZhiQiang Fan

Bug Description

I tried printing a novaclient.v1_1 server object returned by novaclient.v1_1.server.Server.interface_list() with this code:

Note: self.clients[item['tenant'] is an instance of novaclient.v1_1.client

nvlist = self.clients[item['tenant']].servers.list()

for ins in nvlist:
    lst = ins.interface_list()
    print type(lst[0])
    print lst

This results in:
<class 'novaclient.v1_1.servers.Server'>
Traceback (most recent call last):
  File "./collect.py", line 38, in <module>
    points = cl.score()
  File "/home/stack/Cert/mcoe/instances.py", line 38, in score
    print lst[0]
  File "/opt/stack/python-novaclient/novaclient/v1_1/servers.py", line 36, in __repr__
    return "<Server: %s>" % self.name
  File "/opt/stack/python-novaclient/novaclient/base.py", line 458, in __getattr__
    raise AttributeError(k)
AttributeError: name

The issue here is that neither the novaclient.v1_1.servers.Server object nor its base.Resource parent class returned by interface_list has a name field:

[['HUMAN_ID', 'NAME_ATTR', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_add_details', '_info', '_loaded', 'add_fixed_ip', 'add_floating_ip', 'add_security_group', 'backup', 'change_password', 'clear_password', 'confirm_resize', 'create_image', 'delete', 'diagnostics', 'evacuate', 'fixed_ips', 'force_delete', 'get', 'get_console_output', 'get_password', 'get_spice_console', 'get_vnc_console', 'human_id', 'interface_attach', 'interface_detach', 'interface_list', 'is_loaded', 'list_security_group', 'live_migrate', 'lock', 'mac_addr', 'manager', 'migrate', 'net_id', 'networks', 'pause', 'port_id', 'port_state', 'reboot', 'rebuild', 'remove_fixed_ip', 'remove_floating_ip', 'remove_security_group', 'rescue', 'reset_network', 'reset_state', 'resize', 'restore', 'resume', 'revert_resize', 'set_loaded', 'shelve', 'shelve_offload', 'start', 'stop', 'suspend', 'unlock', 'unpause', 'unrescue', 'unshelve', 'update']

Possible remedies:
 - A name field could be added to base.Resource
 - Another way could be used to identify the interface returned, maybe IP or MAC of the interface
 - the __repr__ method could be removed entirely if deemed unnecessary

Revision history for this message
Sahid Orentino (sahid-ferdjaoui) wrote :

This needs more information about the version of the client and server used.

Changed in python-novaclient:
status: New → Incomplete
Revision history for this message
Christian Huebner (ossarchitect) wrote :

This bug was observed in the current version of devstack (Havana)

Nova API is v1.1 as mentioned before. Version of the nova client is 2.15.0.77 as seen here:

$ nova --version
2.15.0.77

Nova version is 2013.2.1 from /opt/stack/nova/setup.cfg

I confirmed the problem is present and reproducible with the versions mentioned above.

The issue is that the __repr__ method in the novaclient.v1_1.servers.Server class is trying to return a class field (self.name) that does not exist.

There are three possible solutions for this issue:

1) Add and populate a self.name field to the novaclient.v1_1.servers.Server class (preferable)

2) Remove the __repr__ method of the class.

This is the actual code from novaclient/v1_1/servers.py:
 32 class Server(base.Resource):
 33 HUMAN_ID = True
 34
 35 def __repr__(self):
 36 return "<Server: %s>" % self.name

The repr method has no task other than to return the name, which does not exist.

3) Modify the __repr__ method to return something else that does exist.

My recommendation is to add and populate a name field.

Revision history for this message
Christian Huebner (ossarchitect) wrote :

if the __repr__ method is removed altogether this is what print lst returns:

[<Server fixed_ips=[{u'subnet_id': u'f1ad93ad-2967-46ba-b403-e8cbbe65f7fa', u'ip_address': u'10.2.0.96'}], mac_addr=fa:16:3e:4c:37:c8, net_id=d7745cf5-63f9-4883-b0ae-983f061e4f23, port_id=f35079da-36d5-4513-8ec1-0298d703f70e, port_state=ACTIVE>]

This would be more much more useful than than getting an attribute error.

it probably would be best to just remove __repr__ altogether, this gives all the information needed on this interface.

Revision history for this message
Kun Huang (academicgareth) wrote :

I got this problem again, but don't know the reason. My environment is built by devstack, which is about current codes

Changed in python-novaclient:
status: Incomplete → Confirmed
Revision history for this message
ZhiQiang Fan (aji-zqfan) wrote :

this is because the interface has no attributes named 'name', when initialize the Server object, it will inject attributes with the dict (here is the fixed_ips dict), since the fixed ips has no name, it hiccups

Changed in python-novaclient:
assignee: nobody → ZhiQiang Fan (aji-zqfan)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to python-novaclient (master)

Fix proposed to branch: master
Review: https://review.openstack.org/82443

Changed in python-novaclient:
status: Confirmed → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to python-novaclient (master)

Reviewed: https://review.openstack.org/82443
Committed: https://git.openstack.org/cgit/openstack/python-novaclient/commit/?id=6fbddcb4b629608a6b5ff556990c13563a36f858
Submitter: Jenkins
Branch: master

commit 6fbddcb4b629608a6b5ff556990c13563a36f858
Author: ZhiQiang Fan <email address hidden>
Date: Mon Mar 24 15:29:01 2014 +0800

    Avoid AttributeError in servers.Server.__repr__

    servers.Server represents various object now, and some of them may
    don't have attribute 'name', for example, the interface_list() result
    object. It will cause AttributeError when we try to format string with
    such object, so I add a check for the 'name' attribute in __repr__
    method, it will use 'unknown-name' instead when 'name' is not found.

    Change-Id: If4757d5d73721774543d58a4cc875710a6013f34
    Closes-Bug: #1280453

Changed in python-novaclient:
status: In Progress → Fix Committed
Michael Still (mikal)
Changed in python-novaclient:
milestone: none → 2.18.0
Michael Still (mikal)
Changed in python-novaclient:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.