moto/moto/core/responses.py

101 lines
3.2 KiB
Python
Raw Normal View History

import datetime
import json
from urlparse import parse_qs, urlparse
2013-02-25 23:48:17 -05:00
2013-05-03 20:14:33 -04:00
from moto.core.utils import camelcase_to_underscores, method_names_from_class
2013-02-25 23:48:17 -05:00
class BaseResponse(object):
def dispatch(self, request, full_url, headers):
querystring = {}
if hasattr(request, 'body'):
# Boto
self.body = request.body
2013-02-25 23:48:17 -05:00
else:
# Flask server
# FIXME: At least in Flask==0.10.1, request.data is an empty string
# and the information we want is in request.form. Keeping self.body
# definition for back-compatibility
self.body = request.data
querystring = {}
for key, value in request.form.iteritems():
2013-12-28 20:15:37 -05:00
querystring[key] = [value, ]
2013-12-28 20:15:37 -05:00
if not querystring:
querystring.update(parse_qs(urlparse(full_url).query))
2013-12-28 20:15:37 -05:00
if not querystring:
querystring.update(parse_qs(self.body))
2013-12-28 20:15:37 -05:00
if not querystring:
querystring.update(headers)
2013-02-25 23:48:17 -05:00
self.uri = full_url
self.path = urlparse(full_url).path
2013-02-25 23:48:17 -05:00
self.querystring = querystring
self.method = request.method
2013-02-25 23:48:17 -05:00
self.headers = dict(request.headers)
self.response_headers = headers
return self.call_action()
2013-02-25 23:48:17 -05:00
def call_action(self):
headers = self.response_headers
action = self.querystring.get('Action', [""])[0]
action = camelcase_to_underscores(action)
2013-02-25 23:48:17 -05:00
method_names = method_names_from_class(self.__class__)
if action in method_names:
method = getattr(self, action)
response = method()
if isinstance(response, basestring):
return 200, headers, response
else:
body, new_headers = response
status = new_headers.pop('status', 200)
headers.update(new_headers)
return status, headers, body
2013-10-03 20:34:13 -04:00
raise NotImplementedError("The {0} action has not been implemented".format(action))
def _get_param(self, param_name):
return self.querystring.get(param_name, [None])[0]
def metadata_response(request, full_url, headers):
"""
Mock response for localhost metadata
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html
"""
parsed_url = urlparse(full_url)
tomorrow = datetime.datetime.now() + datetime.timedelta(days=1)
credentials = dict(
AccessKeyId="test-key",
SecretAccessKey="test-secret-key",
Token="test-session-token",
Expiration=tomorrow.strftime("%Y-%m-%dT%H:%M:%SZ")
)
2013-12-29 08:59:07 -05:00
path = parsed_url.path
meta_data_prefix = "/latest/meta-data/"
# Strip prefix if it is there
if path.startswith(meta_data_prefix):
path = path[len(meta_data_prefix):]
if path == '':
result = 'iam'
elif path == 'iam':
result = json.dumps({
'security-credentials': {
'default-role': credentials
}
})
elif path == 'iam/security-credentials/':
result = 'default-role'
elif path == 'iam/security-credentials/default-role':
result = json.dumps(credentials)
return 200, headers, result