boto/boto — Issue #386: boto.ec2.connection.EC2Connection.get_key_pair() raises EC2ResponseError: 400 Bad Request rather than returning None when keyname is not found
Author: timtebeek
Created: 2011-11-04 16:48:43+00:00
State: open

The documentation for [boto.ec2.connection.EC2Connection.get_key_pair()](https://github.com/boto/boto/blob/master/boto/ec2/connection.py#L1694) reads that it should return None when an unknown keyname is passed: "The KeyPair specified or None if it is not found"

However, a EC2ResponseError: 400 Bad Request is raised and not caught instead:

InvalidKeyPair.NotFoundThe key pair 'initial-testing-key-pair' does not exist...

The get_key_pair method currently read like this, which raises the error in the call to get_all_key_pairs:

try:
return self.get_all_key_pairs(keynames=[keyname])[0]
except IndexError: # None of those key pairs available
return None

Had the code instead read like this, than it would all work:

try:
return self.get_all_key_pairs(filters = {'key-name': keyname})[0]
except IndexError: # None of those key pairs available
return None

The trick is using filters instead of keynames. Could you see about getting this fixed?