Performance: Leverage jinja2's Environment to cache compiled Templates.

This commit is contained in:
dreadpirateshawn 2014-12-12 12:46:07 -08:00
commit 9affa7753d
30 changed files with 228 additions and 235 deletions

View file

@ -1,5 +1,4 @@
from __future__ import unicode_literals
from jinja2 import Template
from moto.core.responses import BaseResponse
from .models import iam_backend
@ -16,20 +15,20 @@ class IamResponse(BaseResponse):
assume_role_policy_document = self._get_param('AssumeRolePolicyDocument')
role = iam_backend.create_role(role_name, assume_role_policy_document, path)
template = Template(CREATE_ROLE_TEMPLATE)
template = self.response_template(CREATE_ROLE_TEMPLATE)
return template.render(role=role)
def get_role(self):
role_name = self._get_param('RoleName')
role = iam_backend.get_role(role_name)
template = Template(GET_ROLE_TEMPLATE)
template = self.response_template(GET_ROLE_TEMPLATE)
return template.render(role=role)
def list_role_policies(self):
role_name = self._get_param('RoleName')
role_policies_names = iam_backend.list_role_policies(role_name)
template = Template(LIST_ROLE_POLICIES)
template = self.response_template(LIST_ROLE_POLICIES)
return template.render(role_policies=role_policies_names)
def put_role_policy(self):
@ -37,14 +36,14 @@ class IamResponse(BaseResponse):
policy_name = self._get_param('PolicyName')
policy_document = self._get_param('PolicyDocument')
iam_backend.put_role_policy(role_name, policy_name, policy_document)
template = Template(GENERIC_EMPTY_TEMPLATE)
template = self.response_template(GENERIC_EMPTY_TEMPLATE)
return template.render(name="PutRolePolicyResponse")
def get_role_policy(self):
role_name = self._get_param('RoleName')
policy_name = self._get_param('PolicyName')
policy_name, policy_document = iam_backend.get_role_policy(role_name, policy_name)
template = Template(GET_ROLE_POLICY_TEMPLATE)
template = self.response_template(GET_ROLE_POLICY_TEMPLATE)
return template.render(role_name=role_name,
policy_name=policy_name,
policy_document=policy_document)
@ -53,7 +52,7 @@ class IamResponse(BaseResponse):
role_name = self._get_param('RoleName')
role = iam_backend.get_role(role_name)
role.assume_role_policy_document = self._get_param('PolicyDocument')
template = Template(GENERIC_EMPTY_TEMPLATE)
template = self.response_template(GENERIC_EMPTY_TEMPLATE)
return template.render(name="UpdateAssumeRolePolicyResponse")
def create_instance_profile(self):
@ -61,14 +60,14 @@ class IamResponse(BaseResponse):
path = self._get_param('Path')
profile = iam_backend.create_instance_profile(profile_name, path, role_ids=[])
template = Template(CREATE_INSTANCE_PROFILE_TEMPLATE)
template = self.response_template(CREATE_INSTANCE_PROFILE_TEMPLATE)
return template.render(profile=profile)
def get_instance_profile(self):
profile_name = self._get_param('InstanceProfileName')
profile = iam_backend.get_instance_profile(profile_name)
template = Template(GET_INSTANCE_PROFILE_TEMPLATE)
template = self.response_template(GET_INSTANCE_PROFILE_TEMPLATE)
return template.render(profile=profile)
def add_role_to_instance_profile(self):
@ -76,19 +75,19 @@ class IamResponse(BaseResponse):
role_name = self._get_param('RoleName')
iam_backend.add_role_to_instance_profile(profile_name, role_name)
template = Template(ADD_ROLE_TO_INSTANCE_PROFILE_TEMPLATE)
template = self.response_template(ADD_ROLE_TO_INSTANCE_PROFILE_TEMPLATE)
return template.render()
def list_roles(self):
roles = iam_backend.get_roles()
template = Template(LIST_ROLES_TEMPLATE)
template = self.response_template(LIST_ROLES_TEMPLATE)
return template.render(roles=roles)
def list_instance_profiles(self):
profiles = iam_backend.get_instance_profiles()
template = Template(LIST_INSTANCE_PROFILES_TEMPLATE)
template = self.response_template(LIST_INSTANCE_PROFILES_TEMPLATE)
return template.render(instance_profiles=profiles)
def upload_server_certificate(self):
@ -99,18 +98,18 @@ class IamResponse(BaseResponse):
cert_chain = self._get_param('CertificateName')
cert = iam_backend.upload_server_cert(cert_name, cert_body, private_key, cert_chain=cert_chain, path=path)
template = Template(UPLOAD_CERT_TEMPLATE)
template = self.response_template(UPLOAD_CERT_TEMPLATE)
return template.render(certificate=cert)
def list_server_certificates(self, marker=None):
certs = iam_backend.get_all_server_certs(marker=marker)
template = Template(LIST_SERVER_CERTIFICATES_TEMPLATE)
template = self.response_template(LIST_SERVER_CERTIFICATES_TEMPLATE)
return template.render(server_certificates=certs)
def get_server_certificate(self):
cert_name = self._get_param('ServerCertificateName')
cert = iam_backend.get_server_certificate(cert_name)
template = Template(GET_SERVER_CERTIFICATE_TEMPLATE)
template = self.response_template(GET_SERVER_CERTIFICATE_TEMPLATE)
return template.render(certificate=cert)
def create_group(self):
@ -118,14 +117,14 @@ class IamResponse(BaseResponse):
path = self._get_param('Path')
group = iam_backend.create_group(group_name, path)
template = Template(CREATE_GROUP_TEMPLATE)
template = self.response_template(CREATE_GROUP_TEMPLATE)
return template.render(group=group)
def get_group(self):
group_name = self._get_param('GroupName')
group = iam_backend.get_group(group_name)
template = Template(GET_GROUP_TEMPLATE)
template = self.response_template(GET_GROUP_TEMPLATE)
return template.render(group=group)
def create_user(self):
@ -133,13 +132,13 @@ class IamResponse(BaseResponse):
path = self._get_param('Path')
user = iam_backend.create_user(user_name, path)
template = Template(USER_TEMPLATE)
template = self.response_template(USER_TEMPLATE)
return template.render(action='Create', user=user)
def get_user(self):
user_name = self._get_param('UserName')
user = iam_backend.get_user(user_name)
template = Template(USER_TEMPLATE)
template = self.response_template(USER_TEMPLATE)
return template.render(action='Get', user=user)
def create_login_profile(self):
@ -147,7 +146,7 @@ class IamResponse(BaseResponse):
password = self._get_param('Password')
iam_backend.create_login_profile(user_name, password)
template = Template(CREATE_LOGIN_PROFILE_TEMPLATE)
template = self.response_template(CREATE_LOGIN_PROFILE_TEMPLATE)
return template.render(user_name=user_name)
def add_user_to_group(self):
@ -155,7 +154,7 @@ class IamResponse(BaseResponse):
user_name = self._get_param('UserName')
iam_backend.add_user_to_group(group_name, user_name)
template = Template(GENERIC_EMPTY_TEMPLATE)
template = self.response_template(GENERIC_EMPTY_TEMPLATE)
return template.render(name='AddUserToGroup')
def remove_user_from_group(self):
@ -163,7 +162,7 @@ class IamResponse(BaseResponse):
user_name = self._get_param('UserName')
iam_backend.remove_user_from_group(group_name, user_name)
template = Template(GENERIC_EMPTY_TEMPLATE)
template = self.response_template(GENERIC_EMPTY_TEMPLATE)
return template.render(name='RemoveUserFromGroup')
def get_user_policy(self):
@ -171,7 +170,7 @@ class IamResponse(BaseResponse):
policy_name = self._get_param('PolicyName')
policy_document = iam_backend.get_user_policy(user_name, policy_name)
template = Template(GET_USER_POLICY_TEMPLATE)
template = self.response_template(GET_USER_POLICY_TEMPLATE)
return template.render(
user_name=user_name,
policy_name=policy_name,
@ -184,7 +183,7 @@ class IamResponse(BaseResponse):
policy_document = self._get_param('PolicyDocument')
iam_backend.put_user_policy(user_name, policy_name, policy_document)
template = Template(GENERIC_EMPTY_TEMPLATE)
template = self.response_template(GENERIC_EMPTY_TEMPLATE)
return template.render(name='PutUserPolicy')
def delete_user_policy(self):
@ -192,21 +191,21 @@ class IamResponse(BaseResponse):
policy_name = self._get_param('PolicyName')
iam_backend.delete_user_policy(user_name, policy_name)
template = Template(GENERIC_EMPTY_TEMPLATE)
template = self.response_template(GENERIC_EMPTY_TEMPLATE)
return template.render(name='DeleteUserPolicy')
def create_access_key(self):
user_name = self._get_param('UserName')
key = iam_backend.create_access_key(user_name)
template = Template(CREATE_ACCESS_KEY_TEMPLATE)
template = self.response_template(CREATE_ACCESS_KEY_TEMPLATE)
return template.render(key=key)
def list_access_keys(self):
user_name = self._get_param('UserName')
keys = iam_backend.get_all_access_keys(user_name)
template = Template(LIST_ACCESS_KEYS_TEMPLATE)
template = self.response_template(LIST_ACCESS_KEYS_TEMPLATE)
return template.render(user_name=user_name, keys=keys)
def delete_access_key(self):
@ -214,13 +213,13 @@ class IamResponse(BaseResponse):
access_key_id = self._get_param('AccessKeyId')
iam_backend.delete_access_key(access_key_id, user_name)
template = Template(GENERIC_EMPTY_TEMPLATE)
template = self.response_template(GENERIC_EMPTY_TEMPLATE)
return template.render(name='DeleteAccessKey')
def delete_user(self):
user_name = self._get_param('UserName')
iam_backend.delete_user(user_name)
template = Template(GENERIC_EMPTY_TEMPLATE)
template = self.response_template(GENERIC_EMPTY_TEMPLATE)
return template.render(name='DeleteUser')