python api error when security group is included in server.create()

Bug #1426956 reported by Travis
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-novaclient
Invalid
Undecided
Unassigned

Bug Description

Including a security group in the novaclient server.create() causes this error:

Traceback (most recent call last):
  File "./scripts/vm_boot.py", line 23, in <module>
    ,security_groups=security_group)
  File "/usr/local/lib/python2.7/dist-packages/novaclient/v1_1/servers.py", line 902, in create
    **boot_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/novaclient/v1_1/servers.py", line 554, in _boot
    return_raw=return_raw, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/novaclient/base.py", line 100, in _create
    _resp, body = self.api.client.post(url, body=body)
  File "/usr/local/lib/python2.7/dist-packages/novaclient/client.py", line 490, in post
    return self._cs_request(url, 'POST', **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/novaclient/client.py", line 465, in _cs_request
    resp, body = self._time_request(url, method, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/novaclient/client.py", line 439, in _time_request
    resp, body = self.request(url, method, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/novaclient/client.py", line 394, in request
    kwargs['data'] = json.dumps(kwargs['body'])
  File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <SecurityGroup description=this is a test group, id=3, name=SECURITY_TEST, rules=[{u'from_port': -1, u'group': {}, u'ip_protocol': u'icmp', u'to_port': -1, u'parent_group_id': 3, u'ip_range': {u'cidr': u'0.0.0.0/0'}, u'id': 2}], tenant_id=954ce66bf8dc44f088367e200af0505c> is not JSON serializable

Revision history for this message
melanie witt (melwitt) wrote :

Hi, unfortunately I see the parameter format for security_groups isn't present in the docstring of the public function, but is in the private function as:

:param security_groups: list of security group names

so I think the problem is that you haven't specified the security groups as a list of names.

This is an example:

>>> c.servers.create('hi', '731fcb9a-29ac-4e8d-9147-fa7d2a5c567f', 1, security_groups=['default'])
<Server: hi>
>>> c.servers.list()
[<Server: hi>]

Can you confirm this is the problem? I will get the documentation updated to reflect the parameter description.

Changed in python-novaclient:
status: New → Incomplete
Revision history for this message
Travis (tmill3r) wrote :

Hi,

Thanks for looking in to this.

I did end up passing my security group as a list like so

security_group = [nova.security_groups.find(name="SECURITY_TEST")]

and still got the error. It seemed it has something to do with the json.dumps() on security groups. The only way I could fix it was add str() in line 497 in novaclient/v2/server.py

if security_groups:
            body["server"]["security_groups"] = [{'name': sg}
                                                 for sg in security_groups]

to

if security_groups:
            body["server"]["security_groups"] = [{'name': str(sg)}
                                                 for sg in security_groups]

and then in novaclient/v2/security_groups.py replace

def __str__(self):
        return str(self.id)

with

def __repr__(self):
        return str(self.name)

Revision history for this message
melanie witt (melwitt) wrote :

It appears you are passing a list of security group objects. The parameter has to be a list of security group *names*.

Your call should look something like this based on your example:

client.servers.create('myserver', 'image uuid', 1, security_groups=['SECURITY_TEST'])

Revision history for this message
Travis (tmill3r) wrote :

Aha! You are exactly right. It works perfectly now. Thanks for clearing that up for me.

Revision history for this message
melanie witt (melwitt) wrote :

Thanks for the update, Travis!

I had updated the api docs in this patch: https://review.openstack.org/#/c/163923/

We could use a better error message here though, so I'll look into that.

Changed in python-novaclient:
status: Incomplete → Invalid
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.