diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 65008458a..595b96d7e 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -19,6 +19,7 @@ from .utils import ( random_eip_association_id, random_eip_allocation_id, random_ip, + random_key_pair, ) @@ -163,25 +164,26 @@ class InstanceBackend(object): class KeyPairBackend(object): - class KeyPair(object): - pass - def __init__(self): self.keypairs = defaultdict(dict) super(KeyPairBackend, self).__init__() def create_key_pair(self, name): - self.keypairs[name] = self.KeyPair() - return name + self.keypairs[name] = keypair = random_key_pair() + keypair['name'] = name + return keypair def delete_key_pair(self, name): - return self.keypairs.pop(name) + keypair = self.keypairs.pop(name) + keypair['name'] = name + return keypair def describe_key_pairs(self, filter_names=None): results = [] for name, keypair in self.keypairs.iteritems(): if not filter_names or name in filter_names: - results.append({name: keypair}) + keypair['name'] = name + results.append(keypair) return results diff --git a/moto/ec2/responses/key_pairs.py b/moto/ec2/responses/key_pairs.py index 565d0506a..30cd2dd6c 100644 --- a/moto/ec2/responses/key_pairs.py +++ b/moto/ec2/responses/key_pairs.py @@ -6,7 +6,9 @@ from moto.ec2.models import ec2_backend class KeyPairs(BaseResponse): def create_key_pair(self): - raise NotImplementedError('KeyPairs.create_key_pair is not yet implemented') + name = self.querystring.get('KeyName')[0] + template = Template(CREATE_KEY_PAIR_RESPONSE) + return template.render(**ec2_backend.create_key_pair(name)) def delete_key_pair(self): raise NotImplementedError('KeyPairs.delete_key_pair is not yet implemented') @@ -24,3 +26,13 @@ DESCRIBE_KEY_PAIRS_RESPONSE = """ + {{ name }} + + {{ fingerprint }} + + {{ material }} + +""" diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index f138919db..385f4cced 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -144,3 +144,27 @@ def filter_reservations(reservations, filter_dict): reservation.instances = new_instances result.append(reservation) return result + + +# not really random +def random_key_pair(): + return { + 'fingerprint': ('1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:' + '7d:b8:ca:9f:f5:f1:6f'), + 'material': """---- BEGIN RSA PRIVATE KEY ---- +MIICiTCCAfICCQD6m7oRw0uXOjANBgkqhkiG9w0BAQUFADCBiDELMAkGA1UEBhMC +VVMxCzAJBgNVBAgTAldBMRAwDgYDVQQHEwdTZWF0dGxlMQ8wDQYDVQQKEwZBbWF6 +b24xFDASBgNVBAsTC0lBTSBDb25zb2xlMRIwEAYDVQQDEwlUZXN0Q2lsYWMxHzAd +BgkqhkiG9w0BCQEWEG5vb25lQGFtYXpvbi5jb20wHhcNMTEwNDI1MjA0NTIxWhcN +MTIwNDI0MjA0NTIxWjCBiDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAldBMRAwDgYD +VQQHEwdTZWF0dGxlMQ8wDQYDVQQKEwZBbWF6b24xFDASBgNVBAsTC0lBTSBDb25z +b2xlMRIwEAYDVQQDEwlUZXN0Q2lsYWMxHzAdBgkqhkiG9w0BCQEWEG5vb25lQGFt +YXpvbi5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMaK0dn+a4GmWIWJ +21uUSfwfEvySWtC2XADZ4nB+BLYgVIk60CpiwsZ3G93vUEIO3IyNoH/f0wYK8m9T +rDHudUZg3qX4waLG5M43q7Wgc/MbQITxOUSQv7c7ugFFDzQGBzZswY6786m86gpE +Ibb3OhjZnzcvQAaRHhdlQWIMm2nrAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAtCu4 +nUhVVxYUntneD9+h8Mg9q6q+auNKyExzyLwaxlAoo7TJHidbtS4J5iNmZgXL0Fkb +FFBjvSfpJIlJ00zbhNYS5f6GuoEDmFJl0ZxBHjJnyp378OD8uTs7fLvjx79LjSTb +NYiytVbZPQUQ5Yaxu2jXnimvw3rrszlaEXAMPLE +-----END RSA PRIVATE KEY-----""" + } diff --git a/tests/test_ec2/test_key_pairs.py b/tests/test_ec2/test_key_pairs.py index 9b488e3d9..5bb261737 100644 --- a/tests/test_ec2/test_key_pairs.py +++ b/tests/test_ec2/test_key_pairs.py @@ -14,4 +14,4 @@ def test_key_pairs_empty(): def test_key_pairs_create(): conn = boto.connect_ec2('the_key', 'the_secret') kp = conn.create_key_pair('foo') - assert kp.material.startswith('-----BEGIN RSA PRIVATE KEY-----') + assert kp.material.startswith('---- BEGIN RSA PRIVATE KEY ----')