make duplicate key pair test pass

This commit is contained in:
Konstantinos Koukopoulos 2014-02-24 13:13:23 +02:00
parent 8b7c3a3312
commit 35063ca743
2 changed files with 17 additions and 3 deletions

View File

@ -169,6 +169,8 @@ class KeyPairBackend(object):
super(KeyPairBackend, self).__init__()
def create_key_pair(self, name):
if name in self.keypairs:
raise InvalidIdError(name)
self.keypairs[name] = keypair = random_key_pair()
keypair['name'] = name
return keypair

View File

@ -1,14 +1,21 @@
from jinja2 import Template
from moto.core.responses import BaseResponse
from moto.ec2.models import ec2_backend
from moto.ec2.exceptions import InvalidIdError
class KeyPairs(BaseResponse):
def create_key_pair(self):
name = self.querystring.get('KeyName')[0]
template = Template(CREATE_KEY_PAIR_RESPONSE)
return template.render(**ec2_backend.create_key_pair(name))
try:
name = self.querystring.get('KeyName')[0]
keypair = ec2_backend.create_key_pair(name)
except InvalidIdError as exc:
template = Template(CREATE_KEY_PAIR_INVALID_NAME)
return template.render(keypair_id=exc.id), dict(status=400)
else:
template = Template(CREATE_KEY_PAIR_RESPONSE)
return template.render(**keypair)
def delete_key_pair(self):
raise NotImplementedError('KeyPairs.delete_key_pair is not yet implemented')
@ -36,3 +43,8 @@ CREATE_KEY_PAIR_RESPONSE = """<CreateKeyPairResponse xmlns="http://ec2.amazonaws
<keyMaterial>{{ material }}
</keyMaterial>
</CreateKeyPairResponse>"""
CREATE_KEY_PAIR_INVALID_NAME = """<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidKeyPair.Duplicate</Code><Message>The keypair '{{ keypair_id }}' already exists.</Message></Error></Errors><RequestID>f4f76e81-8ca5-4e61-a6d5-a4a96EXAMPLE</RequestID></Response>
"""