Question

Newbie question. I'm expecting to be able to do something like the following:

import boto
from boto.iam.connection import IAMConnection
cfn = IAMConnection()
cfn.get_all_users()
for user in userList:
    print user.user_id

The connection works fine, but the last line returns the error "'unicode' object has no attribute 'user_id'".

I did a type(userList) and it reported the type as <class 'boto.jsonresponse.Element'>, which doesn't appear (?) to be documented. Using normal JSON parsing doesn't appear to work either.

From another source, it looks as if the intent is the results of an operation like this are supposed to be "pythonized."

Anyway, I'm pretty new to boto, so I assume there's some simple way to do this that I just haven't stumbled across.

Thx!

Was it helpful?

Solution

For some of the older AWS services, boto takes the XML responses from the service and turns them into nice Python objects. For others, it takes the XML response and transliterates it directly into native Python data structures. The boto.iam module is of the latter form. So, the actual data returned by get_all_users() looks like this:

{u'list_users_response':
  {u'response_metadata': {u'request_id': u'8d694cbd-93ec-11e3-8276-993b3edf6dba'},   
   u'list_users_result': {u'users': 
     [{u'path': u'/', u'create_date': u'2014-01-21T17:19:45Z', u'user_id': 
       u'<userid>', u'arn': u'<arn>', u'user_name': u'foo'},
      {...next user...}
     ]
  }
}

So, all of the data you want is there, it's just a bit difficult to find. The boto.jsonresponse.Element object returned does give you a little help. You can actually do something like this:

data = cfn.get_all_users()
for user in data.user:
    print(user['user_name'])

but for the most part you just have to dig around in the data returned to find what you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top