From 9affa7753dd29aa58bd86f1a8a50f7021987d6cf Mon Sep 17 00:00:00 2001 From: dreadpirateshawn Date: Fri, 12 Dec 2014 12:46:07 -0800 Subject: [PATCH] Performance: Leverage jinja2's Environment to cache compiled Templates. --- moto/autoscaling/responses.py | 27 +++++---- moto/cloudformation/responses.py | 8 +-- moto/cloudwatch/responses.py | 8 +-- moto/core/responses.py | 38 ++++++++++++- moto/ec2/responses/amis.py | 12 ++-- .../availability_zones_and_regions.py | 6 +- moto/ec2/responses/dhcp_options.py | 9 ++- moto/ec2/responses/elastic_block_store.py | 16 +++--- moto/ec2/responses/elastic_ip_addresses.py | 12 ++-- .../responses/elastic_network_interfaces.py | 12 ++-- moto/ec2/responses/general.py | 4 +- moto/ec2/responses/instances.py | 20 +++---- moto/ec2/responses/internet_gateways.py | 11 ++-- moto/ec2/responses/key_pairs.py | 7 +-- moto/ec2/responses/network_acls.py | 13 ++--- moto/ec2/responses/route_tables.py | 21 ++++--- moto/ec2/responses/security_groups.py | 6 +- moto/ec2/responses/spot_instances.py | 8 +-- moto/ec2/responses/subnets.py | 8 +-- moto/ec2/responses/tags.py | 4 +- .../ec2/responses/virtual_private_gateways.py | 13 ++--- moto/ec2/responses/vpc_peering_connections.py | 12 ++-- moto/ec2/responses/vpcs.py | 8 +-- moto/elb/responses.py | 19 +++---- moto/emr/responses.py | 15 +++-- moto/iam/responses.py | 57 +++++++++---------- moto/s3/responses.py | 42 +++++++------- moto/ses/responses.py | 17 +++--- moto/sqs/responses.py | 23 ++++---- moto/sts/responses.py | 7 +-- 30 files changed, 228 insertions(+), 235 deletions(-) diff --git a/moto/autoscaling/responses.py b/moto/autoscaling/responses.py index 693022d96..4093c6ede 100644 --- a/moto/autoscaling/responses.py +++ b/moto/autoscaling/responses.py @@ -1,5 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template from moto.core.responses import BaseResponse from moto.core.utils import camelcase_to_underscores @@ -52,19 +51,19 @@ class AutoScalingResponse(BaseResponse): associate_public_ip_address=self._get_param("AssociatePublicIpAddress"), block_device_mappings=self._get_list_prefix('BlockDeviceMappings.member') ) - template = Template(CREATE_LAUNCH_CONFIGURATION_TEMPLATE) + template = self.response_template(CREATE_LAUNCH_CONFIGURATION_TEMPLATE) return template.render() def describe_launch_configurations(self): names = self._get_multi_param('LaunchConfigurationNames.member') launch_configurations = self.autoscaling_backend.describe_launch_configurations(names) - template = Template(DESCRIBE_LAUNCH_CONFIGURATIONS_TEMPLATE) + template = self.response_template(DESCRIBE_LAUNCH_CONFIGURATIONS_TEMPLATE) return template.render(launch_configurations=launch_configurations) def delete_launch_configuration(self): launch_configurations_name = self.querystring.get('LaunchConfigurationName')[0] self.autoscaling_backend.delete_launch_configuration(launch_configurations_name) - template = Template(DELETE_LAUNCH_CONFIGURATION_TEMPLATE) + template = self.response_template(DELETE_LAUNCH_CONFIGURATION_TEMPLATE) return template.render() def create_auto_scaling_group(self): @@ -83,13 +82,13 @@ class AutoScalingResponse(BaseResponse): placement_group=self._get_param('PlacementGroup'), termination_policies=self._get_multi_param('TerminationPolicies.member'), ) - template = Template(CREATE_AUTOSCALING_GROUP_TEMPLATE) + template = self.response_template(CREATE_AUTOSCALING_GROUP_TEMPLATE) return template.render() def describe_auto_scaling_groups(self): names = self._get_multi_param("AutoScalingGroupNames.member") groups = self.autoscaling_backend.describe_autoscaling_groups(names) - template = Template(DESCRIBE_AUTOSCALING_GROUPS_TEMPLATE) + template = self.response_template(DESCRIBE_AUTOSCALING_GROUPS_TEMPLATE) return template.render(groups=groups) def update_auto_scaling_group(self): @@ -108,25 +107,25 @@ class AutoScalingResponse(BaseResponse): placement_group=self._get_param('PlacementGroup'), termination_policies=self._get_multi_param('TerminationPolicies.member'), ) - template = Template(UPDATE_AUTOSCALING_GROUP_TEMPLATE) + template = self.response_template(UPDATE_AUTOSCALING_GROUP_TEMPLATE) return template.render() def delete_auto_scaling_group(self): group_name = self._get_param('AutoScalingGroupName') self.autoscaling_backend.delete_autoscaling_group(group_name) - template = Template(DELETE_AUTOSCALING_GROUP_TEMPLATE) + template = self.response_template(DELETE_AUTOSCALING_GROUP_TEMPLATE) return template.render() def set_desired_capacity(self): group_name = self._get_param('AutoScalingGroupName') desired_capacity = self._get_int_param('DesiredCapacity') self.autoscaling_backend.set_desired_capacity(group_name, desired_capacity) - template = Template(SET_DESIRED_CAPACITY_TEMPLATE) + template = self.response_template(SET_DESIRED_CAPACITY_TEMPLATE) return template.render() def describe_auto_scaling_instances(self): instances = self.autoscaling_backend.describe_autoscaling_instances() - template = Template(DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE) + template = self.response_template(DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE) return template.render(instances=instances) def put_scaling_policy(self): @@ -137,24 +136,24 @@ class AutoScalingResponse(BaseResponse): scaling_adjustment=self._get_int_param('ScalingAdjustment'), cooldown=self._get_int_param('Cooldown'), ) - template = Template(CREATE_SCALING_POLICY_TEMPLATE) + template = self.response_template(CREATE_SCALING_POLICY_TEMPLATE) return template.render(policy=policy) def describe_policies(self): policies = self.autoscaling_backend.describe_policies() - template = Template(DESCRIBE_SCALING_POLICIES_TEMPLATE) + template = self.response_template(DESCRIBE_SCALING_POLICIES_TEMPLATE) return template.render(policies=policies) def delete_policy(self): group_name = self._get_param('PolicyName') self.autoscaling_backend.delete_policy(group_name) - template = Template(DELETE_POLICY_TEMPLATE) + template = self.response_template(DELETE_POLICY_TEMPLATE) return template.render() def execute_policy(self): group_name = self._get_param('PolicyName') self.autoscaling_backend.execute_policy(group_name) - template = Template(EXECUTE_POLICY_TEMPLATE) + template = self.response_template(EXECUTE_POLICY_TEMPLATE) return template.render() diff --git a/moto/cloudformation/responses.py b/moto/cloudformation/responses.py index 48e8686de..2a79c06cd 100644 --- a/moto/cloudformation/responses.py +++ b/moto/cloudformation/responses.py @@ -1,8 +1,6 @@ from __future__ import unicode_literals import json -from jinja2 import Template - from moto.core.responses import BaseResponse from .models import cloudformation_backends @@ -39,19 +37,19 @@ class CloudFormationResponse(BaseResponse): stack_name_or_id = self.querystring.get('StackName')[0] stacks = self.cloudformation_backend.describe_stacks(stack_name_or_id) - template = Template(DESCRIBE_STACKS_TEMPLATE) + template = self.response_template(DESCRIBE_STACKS_TEMPLATE) return template.render(stacks=stacks) def describe_stack_resources(self): stack_name = self._get_param('StackName') stack = self.cloudformation_backend.get_stack(stack_name) - template = Template(LIST_STACKS_RESOURCES_RESPONSE) + template = self.response_template(LIST_STACKS_RESOURCES_RESPONSE) return template.render(stack=stack) def list_stacks(self): stacks = self.cloudformation_backend.list_stacks() - template = Template(LIST_STACKS_RESPONSE) + template = self.response_template(LIST_STACKS_RESPONSE) return template.render(stacks=stacks) def get_template(self): diff --git a/moto/cloudwatch/responses.py b/moto/cloudwatch/responses.py index 96f783739..3b2b70d27 100644 --- a/moto/cloudwatch/responses.py +++ b/moto/cloudwatch/responses.py @@ -1,5 +1,3 @@ -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.core.utils import camelcase_to_underscores from .models import cloudwatch_backend @@ -48,18 +46,18 @@ class CloudWatchResponse(BaseResponse): alarm_actions, ok_actions, insufficient_data_actions, unit) - template = Template(PUT_METRIC_ALARM_TEMPLATE) + template = self.response_template(PUT_METRIC_ALARM_TEMPLATE) return template.render(alarm=alarm) def describe_alarms(self): alarms = cloudwatch_backend.get_all_alarms() - template = Template(DESCRIBE_ALARMS_TEMPLATE) + template = self.response_template(DESCRIBE_ALARMS_TEMPLATE) return template.render(alarms=alarms) def delete_alarms(self): alarm_names = self._get_multi_param('AlarmNames.member') cloudwatch_backend.delete_alarms(alarm_names) - template = Template(DELETE_METRIC_ALARMS_TEMPLATE) + template = self.response_template(DELETE_METRIC_ALARMS_TEMPLATE) return template.render() PUT_METRIC_ALARM_TEMPLATE = """ diff --git a/moto/core/responses.py b/moto/core/responses.py index c4b3f38ed..d951d143e 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -3,6 +3,8 @@ import datetime import json import re +from jinja2 import Environment, DictLoader, TemplateNotFound + import six from six.moves.urllib.parse import parse_qs, urlparse @@ -41,7 +43,41 @@ def _decode_dict(d): return decoded -class BaseResponse(object): +class DynamicDictLoader(DictLoader): + """ + Note: There's a bug in jinja2 pre-2.7.3 DictLoader where caching does not work. + Including the fixed (current) method version here to ensure performance benefit + even for those using older jinja versions. + """ + def get_source(self, environment, template): + if template in self.mapping: + source = self.mapping[template] + return source, None, lambda: source == self.mapping.get(template) + raise TemplateNotFound(template) + + def update(self, mapping): + self.mapping.update(mapping) + + def contains(self, template): + return bool(template in self.mapping) + + +class _TemplateEnvironmentMixin(object): + loader = DynamicDictLoader({}) + environment = Environment(loader=loader) + + def contains_template(self, template_id): + return self.loader.contains(template_id) + + def response_template(self, source): + template_id = id(source) + if not self.contains_template(template_id): + self.loader.update({template_id: source}) + self.environment = Environment(loader=self.loader) + return self.environment.get_template(template_id) + + +class BaseResponse(_TemplateEnvironmentMixin): default_region = 'us-east-1' region_regex = r'\.(.+?)\.amazonaws\.com' diff --git a/moto/ec2/responses/amis.py b/moto/ec2/responses/amis.py index 407646145..4af2b9be6 100644 --- a/moto/ec2/responses/amis.py +++ b/moto/ec2/responses/amis.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.ec2.utils import instance_ids_from_querystring, image_ids_from_querystring, filters_from_querystring @@ -15,7 +13,7 @@ class AmisResponse(BaseResponse): instance_ids = instance_ids_from_querystring(self.querystring) instance_id = instance_ids[0] image = self.ec2_backend.create_image(instance_id, name, description) - template = Template(CREATE_IMAGE_RESPONSE) + template = self.response_template(CREATE_IMAGE_RESPONSE) return template.render(image=image) def copy_image(self): @@ -24,26 +22,26 @@ class AmisResponse(BaseResponse): name = self.querystring.get('Name')[0] if self.querystring.get('Name') else None description = self.querystring.get('Description')[0] if self.querystring.get('Description') else None image = self.ec2_backend.copy_image(source_image_id, source_region, name, description) - template = Template(COPY_IMAGE_RESPONSE) + template = self.response_template(COPY_IMAGE_RESPONSE) return template.render(image=image) def deregister_image(self): ami_id = self.querystring.get('ImageId')[0] success = self.ec2_backend.deregister_image(ami_id) - template = Template(DEREGISTER_IMAGE_RESPONSE) + template = self.response_template(DEREGISTER_IMAGE_RESPONSE) return template.render(success=str(success).lower()) def describe_images(self): ami_ids = image_ids_from_querystring(self.querystring) filters = filters_from_querystring(self.querystring) images = self.ec2_backend.describe_images(ami_ids=ami_ids, filters=filters) - template = Template(DESCRIBE_IMAGES_RESPONSE) + template = self.response_template(DESCRIBE_IMAGES_RESPONSE) return template.render(images=images) def describe_image_attribute(self): ami_id = self.querystring.get('ImageId')[0] groups = self.ec2_backend.get_launch_permission_groups(ami_id) - template = Template(DESCRIBE_IMAGE_ATTRIBUTES_RESPONSE) + template = self.response_template(DESCRIBE_IMAGE_ATTRIBUTES_RESPONSE) return template.render(ami_id=ami_id, groups=groups) def modify_image_attribute(self): diff --git a/moto/ec2/responses/availability_zones_and_regions.py b/moto/ec2/responses/availability_zones_and_regions.py index d33a6317c..ee65ce968 100644 --- a/moto/ec2/responses/availability_zones_and_regions.py +++ b/moto/ec2/responses/availability_zones_and_regions.py @@ -1,18 +1,16 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse class AvailabilityZonesAndRegions(BaseResponse): def describe_availability_zones(self): zones = self.ec2_backend.describe_availability_zones() - template = Template(DESCRIBE_ZONES_RESPONSE) + template = self.response_template(DESCRIBE_ZONES_RESPONSE) return template.render(zones=zones) def describe_regions(self): regions = self.ec2_backend.describe_regions() - template = Template(DESCRIBE_REGIONS_RESPONSE) + template = self.response_template(DESCRIBE_REGIONS_RESPONSE) return template.render(regions=regions) DESCRIBE_REGIONS_RESPONSE = """ diff --git a/moto/ec2/responses/dhcp_options.py b/moto/ec2/responses/dhcp_options.py index 3ce558914..a3a454a86 100644 --- a/moto/ec2/responses/dhcp_options.py +++ b/moto/ec2/responses/dhcp_options.py @@ -1,5 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template from moto.core.responses import BaseResponse from moto.ec2.utils import ( dhcp_configuration_from_querystring, @@ -16,7 +15,7 @@ class DHCPOptions(BaseResponse): self.ec2_backend.associate_dhcp_options(dhcp_opt, vpc) - template = Template(ASSOCIATE_DHCP_OPTIONS_RESPONSE) + template = self.response_template(ASSOCIATE_DHCP_OPTIONS_RESPONSE) return template.render() def create_dhcp_options(self): @@ -38,13 +37,13 @@ class DHCPOptions(BaseResponse): netbios_node_type=netbios_node_type ) - template = Template(CREATE_DHCP_OPTIONS_RESPONSE) + template = self.response_template(CREATE_DHCP_OPTIONS_RESPONSE) return template.render(dhcp_options_set=dhcp_options_set) def delete_dhcp_options(self): dhcp_opt_id = self.querystring.get("DhcpOptionsId", [None])[0] delete_status = self.ec2_backend.delete_dhcp_options_set(dhcp_opt_id) - template = Template(DELETE_DHCP_OPTIONS_RESPONSE) + template = self.response_template(DELETE_DHCP_OPTIONS_RESPONSE) return template.render(delete_status=delete_status) def describe_dhcp_options(self): @@ -55,7 +54,7 @@ class DHCPOptions(BaseResponse): dhcp_opt = self.ec2_backend.describe_dhcp_options(dhcp_opt_ids) else: dhcp_opt = self.ec2_backend.describe_dhcp_options() - template = Template(DESCRIBE_DHCP_OPTIONS_RESPONSE) + template = self.response_template(DESCRIBE_DHCP_OPTIONS_RESPONSE) return template.render(dhcp_options=dhcp_opt) diff --git a/moto/ec2/responses/elastic_block_store.py b/moto/ec2/responses/elastic_block_store.py index 3684c042c..ad0857ea1 100644 --- a/moto/ec2/responses/elastic_block_store.py +++ b/moto/ec2/responses/elastic_block_store.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse @@ -11,7 +9,7 @@ class ElasticBlockStore(BaseResponse): device_path = self.querystring.get('Device')[0] attachment = self.ec2_backend.attach_volume(volume_id, instance_id, device_path) - template = Template(ATTACHED_VOLUME_RESPONSE) + template = self.response_template(ATTACHED_VOLUME_RESPONSE) return template.render(attachment=attachment) def copy_snapshot(self): @@ -23,14 +21,14 @@ class ElasticBlockStore(BaseResponse): description = self.querystring.get('Description')[0] volume_id = self.querystring.get('VolumeId')[0] snapshot = self.ec2_backend.create_snapshot(volume_id, description) - template = Template(CREATE_SNAPSHOT_RESPONSE) + template = self.response_template(CREATE_SNAPSHOT_RESPONSE) return template.render(snapshot=snapshot) def create_volume(self): size = self.querystring.get('Size')[0] zone = self.querystring.get('AvailabilityZone')[0] volume = self.ec2_backend.create_volume(size, zone) - template = Template(CREATE_VOLUME_RESPONSE) + template = self.response_template(CREATE_VOLUME_RESPONSE) return template.render(volume=volume) def delete_snapshot(self): @@ -45,12 +43,12 @@ class ElasticBlockStore(BaseResponse): def describe_snapshots(self): snapshots = self.ec2_backend.describe_snapshots() - template = Template(DESCRIBE_SNAPSHOTS_RESPONSE) + template = self.response_template(DESCRIBE_SNAPSHOTS_RESPONSE) return template.render(snapshots=snapshots) def describe_volumes(self): volumes = self.ec2_backend.describe_volumes() - template = Template(DESCRIBE_VOLUMES_RESPONSE) + template = self.response_template(DESCRIBE_VOLUMES_RESPONSE) return template.render(volumes=volumes) def describe_volume_attribute(self): @@ -65,7 +63,7 @@ class ElasticBlockStore(BaseResponse): device_path = self.querystring.get('Device')[0] attachment = self.ec2_backend.detach_volume(volume_id, instance_id, device_path) - template = Template(DETATCH_VOLUME_RESPONSE) + template = self.response_template(DETATCH_VOLUME_RESPONSE) return template.render(attachment=attachment) def enable_volume_io(self): @@ -77,7 +75,7 @@ class ElasticBlockStore(BaseResponse): def describe_snapshot_attribute(self): snapshot_id = self.querystring.get('SnapshotId')[0] groups = self.ec2_backend.get_create_volume_permission_groups(snapshot_id) - template = Template(DESCRIBE_SNAPSHOT_ATTRIBUTES_RESPONSE) + template = self.response_template(DESCRIBE_SNAPSHOT_ATTRIBUTES_RESPONSE) return template.render(snapshot_id=snapshot_id, groups=groups) def modify_snapshot_attribute(self): diff --git a/moto/ec2/responses/elastic_ip_addresses.py b/moto/ec2/responses/elastic_ip_addresses.py index eee46107f..6818b7e9d 100644 --- a/moto/ec2/responses/elastic_ip_addresses.py +++ b/moto/ec2/responses/elastic_ip_addresses.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.ec2.utils import sequence_from_querystring @@ -12,7 +10,7 @@ class ElasticIPAddresses(BaseResponse): else: domain = "standard" address = self.ec2_backend.allocate_address(domain) - template = Template(ALLOCATE_ADDRESS_RESPONSE) + template = self.response_template(ALLOCATE_ADDRESS_RESPONSE) return template.render(address=address) def associate_address(self): @@ -39,11 +37,11 @@ class ElasticIPAddresses(BaseResponse): else: self.ec2_backend.raise_error("MissingParameter", "Invalid request, expect either instance or ENI.") - template = Template(ASSOCIATE_ADDRESS_RESPONSE) + template = self.response_template(ASSOCIATE_ADDRESS_RESPONSE) return template.render(address=eip) def describe_addresses(self): - template = Template(DESCRIBE_ADDRESS_RESPONSE) + template = self.response_template(DESCRIBE_ADDRESS_RESPONSE) if "Filter.1.Name" in self.querystring: raise NotImplementedError("Filtering not supported in describe_address.") @@ -65,7 +63,7 @@ class ElasticIPAddresses(BaseResponse): else: self.ec2_backend.raise_error("MissingParameter", "Invalid request, expect PublicIp/AssociationId parameter.") - return Template(DISASSOCIATE_ADDRESS_RESPONSE).render() + return self.response_template(DISASSOCIATE_ADDRESS_RESPONSE).render() def release_address(self): if "PublicIp" in self.querystring: @@ -75,7 +73,7 @@ class ElasticIPAddresses(BaseResponse): else: self.ec2_backend.raise_error("MissingParameter", "Invalid request, expect PublicIp/AllocationId parameter.") - return Template(RELEASE_ADDRESS_RESPONSE).render() + return self.response_template(RELEASE_ADDRESS_RESPONSE).render() ALLOCATE_ADDRESS_RESPONSE = """ diff --git a/moto/ec2/responses/elastic_network_interfaces.py b/moto/ec2/responses/elastic_network_interfaces.py index e023cc885..a7e270a93 100644 --- a/moto/ec2/responses/elastic_network_interfaces.py +++ b/moto/ec2/responses/elastic_network_interfaces.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.ec2.utils import sequence_from_querystring, filters_from_querystring @@ -12,13 +10,13 @@ class ElasticNetworkInterfaces(BaseResponse): groups = sequence_from_querystring('SecurityGroupId', self.querystring) subnet = self.ec2_backend.get_subnet(subnet_id) eni = self.ec2_backend.create_network_interface(subnet, private_ip_address, groups) - template = Template(CREATE_NETWORK_INTERFACE_RESPONSE) + template = self.response_template(CREATE_NETWORK_INTERFACE_RESPONSE) return template.render(eni=eni) def delete_network_interface(self): eni_id = self.querystring.get('NetworkInterfaceId')[0] self.ec2_backend.delete_network_interface(eni_id) - template = Template(DELETE_NETWORK_INTERFACE_RESPONSE) + template = self.response_template(DELETE_NETWORK_INTERFACE_RESPONSE) return template.render() def describe_network_interface_attribute(self): @@ -28,7 +26,7 @@ class ElasticNetworkInterfaces(BaseResponse): # Partially implemented. Supports only network-interface-id and group-id filters filters = filters_from_querystring(self.querystring) enis = self.ec2_backend.describe_network_interfaces(filters) - template = Template(DESCRIBE_NETWORK_INTERFACES_RESPONSE) + template = self.response_template(DESCRIBE_NETWORK_INTERFACES_RESPONSE) return template.render(enis=enis) def attach_network_interface(self): @@ -36,13 +34,13 @@ class ElasticNetworkInterfaces(BaseResponse): instance_id = self.querystring.get('InstanceId')[0] device_index = self.querystring.get('DeviceIndex')[0] attachment_id = self.ec2_backend.attach_network_interface(eni_id, instance_id, device_index) - template = Template(ATTACH_NETWORK_INTERFACE_RESPONSE) + template = self.response_template(ATTACH_NETWORK_INTERFACE_RESPONSE) return template.render(attachment_id=attachment_id) def detach_network_interface(self): attachment_id = self.querystring.get('AttachmentId')[0] self.ec2_backend.detach_network_interface(attachment_id) - template = Template(DETACH_NETWORK_INTERFACE_RESPONSE) + template = self.response_template(DETACH_NETWORK_INTERFACE_RESPONSE) return template.render() def modify_network_interface_attribute(self): diff --git a/moto/ec2/responses/general.py b/moto/ec2/responses/general.py index 497948a43..b18db7628 100644 --- a/moto/ec2/responses/general.py +++ b/moto/ec2/responses/general.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.ec2.utils import instance_ids_from_querystring @@ -10,7 +8,7 @@ class General(BaseResponse): self.instance_ids = instance_ids_from_querystring(self.querystring) instance_id = self.instance_ids[0] instance = self.ec2_backend.get_instance(instance_id) - template = Template(GET_CONSOLE_OUTPUT_RESULT) + template = self.response_template(GET_CONSOLE_OUTPUT_RESULT) return template.render(instance=instance) diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py index f2fb290d2..7922f9d99 100644 --- a/moto/ec2/responses/instances.py +++ b/moto/ec2/responses/instances.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.core.utils import camelcase_to_underscores from moto.ec2.utils import instance_ids_from_querystring, filters_from_querystring, \ @@ -16,7 +14,7 @@ class InstanceResponse(BaseResponse): else: reservations = self.ec2_backend.all_reservations(make_copy=True, filters=filter_dict) - template = Template(EC2_DESCRIBE_INSTANCES) + template = self.response_template(EC2_DESCRIBE_INSTANCES) return template.render(reservations=reservations) def run_instances(self): @@ -38,31 +36,31 @@ class InstanceResponse(BaseResponse): key_name=key_name, security_group_ids=security_group_ids, nics=nics, private_ip=private_ip, associate_public_ip=associate_public_ip) - template = Template(EC2_RUN_INSTANCES) + template = self.response_template(EC2_RUN_INSTANCES) return template.render(reservation=new_reservation) def terminate_instances(self): instance_ids = instance_ids_from_querystring(self.querystring) instances = self.ec2_backend.terminate_instances(instance_ids) - template = Template(EC2_TERMINATE_INSTANCES) + template = self.response_template(EC2_TERMINATE_INSTANCES) return template.render(instances=instances) def reboot_instances(self): instance_ids = instance_ids_from_querystring(self.querystring) instances = self.ec2_backend.reboot_instances(instance_ids) - template = Template(EC2_REBOOT_INSTANCES) + template = self.response_template(EC2_REBOOT_INSTANCES) return template.render(instances=instances) def stop_instances(self): instance_ids = instance_ids_from_querystring(self.querystring) instances = self.ec2_backend.stop_instances(instance_ids) - template = Template(EC2_STOP_INSTANCES) + template = self.response_template(EC2_STOP_INSTANCES) return template.render(instances=instances) def start_instances(self): instance_ids = instance_ids_from_querystring(self.querystring) instances = self.ec2_backend.start_instances(instance_ids) - template = Template(EC2_START_INSTANCES) + template = self.response_template(EC2_START_INSTANCES) return template.render(instances=instances) def describe_instance_status(self): @@ -77,7 +75,7 @@ class InstanceResponse(BaseResponse): else: instances = self.ec2_backend.all_running_instances() - template = Template(EC2_INSTANCE_STATUS) + template = self.response_template(EC2_INSTANCE_STATUS) return template.render(instances=instances) def describe_instance_attribute(self): @@ -90,9 +88,9 @@ class InstanceResponse(BaseResponse): instance, value = self.ec2_backend.describe_instance_attribute(instance_id, key) if key == "group_set": - template = Template(EC2_DESCRIBE_INSTANCE_GROUPSET_ATTRIBUTE) + template = self.response_template(EC2_DESCRIBE_INSTANCE_GROUPSET_ATTRIBUTE) else: - template = Template(EC2_DESCRIBE_INSTANCE_ATTRIBUTE) + template = self.response_template(EC2_DESCRIBE_INSTANCE_ATTRIBUTE) return template.render(instance=instance, attribute=attribute, value=value) diff --git a/moto/ec2/responses/internet_gateways.py b/moto/ec2/responses/internet_gateways.py index bad658970..50b9bd116 100644 --- a/moto/ec2/responses/internet_gateways.py +++ b/moto/ec2/responses/internet_gateways.py @@ -4,7 +4,6 @@ from moto.ec2.utils import ( sequence_from_querystring, filters_from_querystring, ) -from jinja2 import Template class InternetGateways(BaseResponse): @@ -12,18 +11,18 @@ class InternetGateways(BaseResponse): igw_id = self.querystring.get("InternetGatewayId", [None])[0] vpc_id = self.querystring.get("VpcId", [None])[0] self.ec2_backend.attach_internet_gateway(igw_id, vpc_id) - template = Template(ATTACH_INTERNET_GATEWAY_RESPONSE) + template = self.response_template(ATTACH_INTERNET_GATEWAY_RESPONSE) return template.render() def create_internet_gateway(self): igw = self.ec2_backend.create_internet_gateway() - template = Template(CREATE_INTERNET_GATEWAY_RESPONSE) + template = self.response_template(CREATE_INTERNET_GATEWAY_RESPONSE) return template.render(internet_gateway=igw) def delete_internet_gateway(self): igw_id = self.querystring.get("InternetGatewayId", [None])[0] self.ec2_backend.delete_internet_gateway(igw_id) - template = Template(DELETE_INTERNET_GATEWAY_RESPONSE) + template = self.response_template(DELETE_INTERNET_GATEWAY_RESPONSE) return template.render() def describe_internet_gateways(self): @@ -35,7 +34,7 @@ class InternetGateways(BaseResponse): else: igws = self.ec2_backend.describe_internet_gateways(filters=filter_dict) - template = Template(DESCRIBE_INTERNET_GATEWAYS_RESPONSE) + template = self.response_template(DESCRIBE_INTERNET_GATEWAYS_RESPONSE) return template.render(internet_gateways=igws) def detach_internet_gateway(self): @@ -44,7 +43,7 @@ class InternetGateways(BaseResponse): igw_id = self.querystring.get("InternetGatewayId", [None])[0] vpc_id = self.querystring.get("VpcId", [None])[0] self.ec2_backend.detach_internet_gateway(igw_id, vpc_id) - template = Template(DETACH_INTERNET_GATEWAY_RESPONSE) + template = self.response_template(DETACH_INTERNET_GATEWAY_RESPONSE) return template.render() diff --git a/moto/ec2/responses/key_pairs.py b/moto/ec2/responses/key_pairs.py index 57ea9330b..80c6442f0 100644 --- a/moto/ec2/responses/key_pairs.py +++ b/moto/ec2/responses/key_pairs.py @@ -1,6 +1,5 @@ from __future__ import unicode_literals import six -from jinja2 import Template from moto.core.responses import BaseResponse from moto.ec2.utils import keypair_names_from_querystring, filters_from_querystring @@ -10,13 +9,13 @@ class KeyPairs(BaseResponse): def create_key_pair(self): name = self.querystring.get('KeyName')[0] keypair = self.ec2_backend.create_key_pair(name) - template = Template(CREATE_KEY_PAIR_RESPONSE) + template = self.response_template(CREATE_KEY_PAIR_RESPONSE) return template.render(**keypair) def delete_key_pair(self): name = self.querystring.get('KeyName')[0] success = six.text_type(self.ec2_backend.delete_key_pair(name)).lower() - return Template(DELETE_KEY_PAIR_RESPONSE).render(success=success) + return self.response_template(DELETE_KEY_PAIR_RESPONSE).render(success=success) def describe_key_pairs(self): names = keypair_names_from_querystring(self.querystring) @@ -25,7 +24,7 @@ class KeyPairs(BaseResponse): raise NotImplementedError('Using filters in KeyPairs.describe_key_pairs is not yet implemented') keypairs = self.ec2_backend.describe_key_pairs(names) - template = Template(DESCRIBE_KEY_PAIRS_RESPONSE) + template = self.response_template(DESCRIBE_KEY_PAIRS_RESPONSE) return template.render(keypairs=keypairs) def import_key_pair(self): diff --git a/moto/ec2/responses/network_acls.py b/moto/ec2/responses/network_acls.py index 7f8c5acc5..684bf6821 100644 --- a/moto/ec2/responses/network_acls.py +++ b/moto/ec2/responses/network_acls.py @@ -1,5 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template from moto.core.responses import BaseResponse from moto.ec2.utils import filters_from_querystring, \ network_acl_ids_from_querystring @@ -10,7 +9,7 @@ class NetworkACLs(BaseResponse): def create_network_acl(self): vpc_id = self.querystring.get('VpcId')[0] network_acl = self.ec2_backend.create_network_acl(vpc_id) - template = Template(CREATE_NETWORK_ACL_RESPONSE) + template = self.response_template(CREATE_NETWORK_ACL_RESPONSE) return template.render(network_acl=network_acl) def create_network_acl_entry(self): @@ -30,13 +29,13 @@ class NetworkACLs(BaseResponse): egress, cidr_block, icmp_code, icmp_type, port_range_from, port_range_to) - template = Template(CREATE_NETWORK_ACL_ENTRY_RESPONSE) + template = self.response_template(CREATE_NETWORK_ACL_ENTRY_RESPONSE) return template.render(network_acl_entry=network_acl_entry) def delete_network_acl(self): network_acl_id = self.querystring.get('NetworkAclId')[0] self.ec2_backend.delete_network_acl(network_acl_id) - template = Template(DELETE_NETWORK_ACL_ASSOCIATION) + template = self.response_template(DELETE_NETWORK_ACL_ASSOCIATION) return template.render() def delete_network_acl_entry(self): @@ -47,7 +46,7 @@ class NetworkACLs(BaseResponse): network_acl_ids = network_acl_ids_from_querystring(self.querystring) filters = filters_from_querystring(self.querystring) network_acls = self.ec2_backend.get_all_network_acls(network_acl_ids, filters) - template = Template(DESCRIBE_NETWORK_ACL_RESPONSE) + template = self.response_template(DESCRIBE_NETWORK_ACL_RESPONSE) return template.render(network_acls=network_acls) def replace_network_acl_association(self): @@ -58,7 +57,7 @@ class NetworkACLs(BaseResponse): association_id, network_acl_id ) - template = Template(REPLACE_NETWORK_ACL_ASSOCIATION) + template = self.response_template(REPLACE_NETWORK_ACL_ASSOCIATION) return template.render(association=association) def replace_network_acl_entry(self): @@ -159,4 +158,4 @@ DELETE_NETWORK_ACL_ASSOCIATION = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE true -""" \ No newline at end of file +""" diff --git a/moto/ec2/responses/route_tables.py b/moto/ec2/responses/route_tables.py index f583cbbfb..57b36775c 100644 --- a/moto/ec2/responses/route_tables.py +++ b/moto/ec2/responses/route_tables.py @@ -1,16 +1,15 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.ec2.utils import route_table_ids_from_querystring, filters_from_querystring, optional_from_querystring class RouteTables(BaseResponse): + def associate_route_table(self): route_table_id = self.querystring.get('RouteTableId')[0] subnet_id = self.querystring.get('SubnetId')[0] association_id = self.ec2_backend.associate_route_table(route_table_id, subnet_id) - template = Template(ASSOCIATE_ROUTE_TABLE_RESPONSE) + template = self.response_template(ASSOCIATE_ROUTE_TABLE_RESPONSE) return template.render(association_id=association_id) def create_route(self): @@ -28,39 +27,39 @@ class RouteTables(BaseResponse): interface_id=interface_id, vpc_peering_connection_id=pcx_id) - template = Template(CREATE_ROUTE_RESPONSE) + template = self.response_template(CREATE_ROUTE_RESPONSE) return template.render() def create_route_table(self): vpc_id = self.querystring.get('VpcId')[0] route_table = self.ec2_backend.create_route_table(vpc_id) - template = Template(CREATE_ROUTE_TABLE_RESPONSE) + template = self.response_template(CREATE_ROUTE_TABLE_RESPONSE) return template.render(route_table=route_table) def delete_route(self): route_table_id = self.querystring.get('RouteTableId')[0] destination_cidr_block = self.querystring.get('DestinationCidrBlock')[0] self.ec2_backend.delete_route(route_table_id, destination_cidr_block) - template = Template(DELETE_ROUTE_RESPONSE) + template = self.response_template(DELETE_ROUTE_RESPONSE) return template.render() def delete_route_table(self): route_table_id = self.querystring.get('RouteTableId')[0] self.ec2_backend.delete_route_table(route_table_id) - template = Template(DELETE_ROUTE_TABLE_RESPONSE) + template = self.response_template(DELETE_ROUTE_TABLE_RESPONSE) return template.render() def describe_route_tables(self): route_table_ids = route_table_ids_from_querystring(self.querystring) filters = filters_from_querystring(self.querystring) route_tables = self.ec2_backend.get_all_route_tables(route_table_ids, filters) - template = Template(DESCRIBE_ROUTE_TABLES_RESPONSE) + template = self.response_template(DESCRIBE_ROUTE_TABLES_RESPONSE) return template.render(route_tables=route_tables) def disassociate_route_table(self): association_id = self.querystring.get('AssociationId')[0] self.ec2_backend.disassociate_route_table(association_id) - template = Template(DISASSOCIATE_ROUTE_TABLE_RESPONSE) + template = self.response_template(DISASSOCIATE_ROUTE_TABLE_RESPONSE) return template.render() def replace_route(self): @@ -78,14 +77,14 @@ class RouteTables(BaseResponse): interface_id=interface_id, vpc_peering_connection_id=pcx_id) - template = Template(REPLACE_ROUTE_RESPONSE) + template = self.response_template(REPLACE_ROUTE_RESPONSE) return template.render() def replace_route_table_association(self): route_table_id = self.querystring.get('RouteTableId')[0] association_id = self.querystring.get('AssociationId')[0] new_association_id = self.ec2_backend.replace_route_table_association(association_id, route_table_id) - template = Template(REPLACE_ROUTE_TABLE_ASSOCIATION_RESPONSE) + template = self.response_template(REPLACE_ROUTE_TABLE_ASSOCIATION_RESPONSE) return template.render(association_id=new_association_id) diff --git a/moto/ec2/responses/security_groups.py b/moto/ec2/responses/security_groups.py index 985b51006..9da61d90a 100644 --- a/moto/ec2/responses/security_groups.py +++ b/moto/ec2/responses/security_groups.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.ec2.utils import filters_from_querystring @@ -48,7 +46,7 @@ class SecurityGroups(BaseResponse): description = self.querystring.get('GroupDescription', [None])[0] vpc_id = self.querystring.get("VpcId", [None])[0] group = self.ec2_backend.create_security_group(name, description, vpc_id=vpc_id) - template = Template(CREATE_SECURITY_GROUP_RESPONSE) + template = self.response_template(CREATE_SECURITY_GROUP_RESPONSE) return template.render(group=group) def delete_security_group(self): @@ -75,7 +73,7 @@ class SecurityGroups(BaseResponse): filters=filters ) - template = Template(DESCRIBE_SECURITY_GROUPS_RESPONSE) + template = self.response_template(DESCRIBE_SECURITY_GROUPS_RESPONSE) return template.render(groups=groups) def revoke_security_group_egress(self): diff --git a/moto/ec2/responses/spot_instances.py b/moto/ec2/responses/spot_instances.py index cd5ec0263..30f683e07 100644 --- a/moto/ec2/responses/spot_instances.py +++ b/moto/ec2/responses/spot_instances.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.ec2.utils import filters_from_querystring @@ -17,7 +15,7 @@ class SpotInstances(BaseResponse): def cancel_spot_instance_requests(self): request_ids = self._get_multi_param('SpotInstanceRequestId') requests = self.ec2_backend.cancel_spot_instance_requests(request_ids) - template = Template(CANCEL_SPOT_INSTANCES_TEMPLATE) + template = self.response_template(CANCEL_SPOT_INSTANCES_TEMPLATE) return template.render(requests=requests) def create_spot_datafeed_subscription(self): @@ -32,7 +30,7 @@ class SpotInstances(BaseResponse): def describe_spot_instance_requests(self): filters = filters_from_querystring(self.querystring) requests = self.ec2_backend.describe_spot_instance_requests(filters=filters) - template = Template(DESCRIBE_SPOT_INSTANCES_TEMPLATE) + template = self.response_template(DESCRIBE_SPOT_INSTANCES_TEMPLATE) return template.render(requests=requests) def describe_spot_price_history(self): @@ -77,7 +75,7 @@ class SpotInstances(BaseResponse): subnet_id=subnet_id, ) - template = Template(REQUEST_SPOT_INSTANCES_TEMPLATE) + template = self.response_template(REQUEST_SPOT_INSTANCES_TEMPLATE) return template.render(requests=requests) diff --git a/moto/ec2/responses/subnets.py b/moto/ec2/responses/subnets.py index b7b4af1c4..9f0808648 100644 --- a/moto/ec2/responses/subnets.py +++ b/moto/ec2/responses/subnets.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.ec2.utils import filters_from_querystring @@ -10,19 +8,19 @@ class Subnets(BaseResponse): vpc_id = self.querystring.get('VpcId')[0] cidr_block = self.querystring.get('CidrBlock')[0] subnet = self.ec2_backend.create_subnet(vpc_id, cidr_block) - template = Template(CREATE_SUBNET_RESPONSE) + template = self.response_template(CREATE_SUBNET_RESPONSE) return template.render(subnet=subnet) def delete_subnet(self): subnet_id = self.querystring.get('SubnetId')[0] subnet = self.ec2_backend.delete_subnet(subnet_id) - template = Template(DELETE_SUBNET_RESPONSE) + template = self.response_template(DELETE_SUBNET_RESPONSE) return template.render(subnet=subnet) def describe_subnets(self): filters = filters_from_querystring(self.querystring) subnets = self.ec2_backend.get_all_subnets(filters) - template = Template(DESCRIBE_SUBNETS_RESPONSE) + template = self.response_template(DESCRIBE_SUBNETS_RESPONSE) return template.render(subnets=subnets) diff --git a/moto/ec2/responses/tags.py b/moto/ec2/responses/tags.py index 50403dd7d..a46690bef 100644 --- a/moto/ec2/responses/tags.py +++ b/moto/ec2/responses/tags.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.ec2.models import validate_resource_ids from moto.ec2.utils import sequence_from_querystring, tags_from_query_string, filters_from_querystring @@ -26,7 +24,7 @@ class TagResponse(BaseResponse): def describe_tags(self): filters = filters_from_querystring(querystring_dict=self.querystring) tags = self.ec2_backend.describe_tags(filters=filters) - template = Template(DESCRIBE_RESPONSE) + template = self.response_template(DESCRIBE_RESPONSE) return template.render(tags=tags) diff --git a/moto/ec2/responses/virtual_private_gateways.py b/moto/ec2/responses/virtual_private_gateways.py index 50b5704ea..0e7048202 100644 --- a/moto/ec2/responses/virtual_private_gateways.py +++ b/moto/ec2/responses/virtual_private_gateways.py @@ -1,5 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template from moto.core.responses import BaseResponse from moto.ec2.utils import filters_from_querystring @@ -12,25 +11,25 @@ class VirtualPrivateGateways(BaseResponse): vpn_gateway_id, vpc_id ) - template = Template(ATTACH_VPN_GATEWAY_RESPONSE) + template = self.response_template(ATTACH_VPN_GATEWAY_RESPONSE) return template.render(attachment=attachment) def create_vpn_gateway(self): type = self.querystring.get('Type', None)[0] vpn_gateway = self.ec2_backend.create_vpn_gateway(type) - template = Template(CREATE_VPN_GATEWAY_RESPONSE) + template = self.response_template(CREATE_VPN_GATEWAY_RESPONSE) return template.render(vpn_gateway=vpn_gateway) def delete_vpn_gateway(self): vpn_gateway_id = self.querystring.get('VpnGatewayId')[0] vpn_gateway = self.ec2_backend.delete_vpn_gateway(vpn_gateway_id) - template = Template(DELETE_VPN_GATEWAY_RESPONSE) + template = self.response_template(DELETE_VPN_GATEWAY_RESPONSE) return template.render(vpn_gateway=vpn_gateway) def describe_vpn_gateways(self): filters = filters_from_querystring(self.querystring) vpn_gateways = self.ec2_backend.get_all_vpn_gateways(filters) - template = Template(DESCRIBE_VPN_GATEWAYS_RESPONSE) + template = self.response_template(DESCRIBE_VPN_GATEWAYS_RESPONSE) return template.render(vpn_gateways=vpn_gateways) def detach_vpn_gateway(self): @@ -40,7 +39,7 @@ class VirtualPrivateGateways(BaseResponse): vpn_gateway_id, vpc_id ) - template = Template(DETACH_VPN_GATEWAY_RESPONSE) + template = self.response_template(DETACH_VPN_GATEWAY_RESPONSE) return template.render(attachment=attachment) CREATE_VPN_GATEWAY_RESPONSE = """ @@ -120,4 +119,4 @@ DETACH_VPN_GATEWAY_RESPONSE = """ 7a62c49f-347e-4fc4-9331-6e8eEXAMPLE true -""" \ No newline at end of file +""" diff --git a/moto/ec2/responses/vpc_peering_connections.py b/moto/ec2/responses/vpc_peering_connections.py index 38ebe6bae..660dd7b16 100644 --- a/moto/ec2/responses/vpc_peering_connections.py +++ b/moto/ec2/responses/vpc_peering_connections.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse @@ -9,30 +7,30 @@ class VPCPeeringConnections(BaseResponse): vpc = self.ec2_backend.get_vpc(self.querystring.get('VpcId')[0]) peer_vpc = self.ec2_backend.get_vpc(self.querystring.get('PeerVpcId')[0]) vpc_pcx = self.ec2_backend.create_vpc_peering_connection(vpc, peer_vpc) - template = Template(CREATE_VPC_PEERING_CONNECTION_RESPONSE) + template = self.response_template(CREATE_VPC_PEERING_CONNECTION_RESPONSE) return template.render(vpc_pcx=vpc_pcx) def delete_vpc_peering_connection(self): vpc_pcx_id = self.querystring.get('VpcPeeringConnectionId')[0] vpc_pcx = self.ec2_backend.delete_vpc_peering_connection(vpc_pcx_id) - template = Template(DELETE_VPC_PEERING_CONNECTION_RESPONSE) + template = self.response_template(DELETE_VPC_PEERING_CONNECTION_RESPONSE) return template.render(vpc_pcx=vpc_pcx) def describe_vpc_peering_connections(self): vpc_pcxs = self.ec2_backend.get_all_vpc_peering_connections() - template = Template(DESCRIBE_VPC_PEERING_CONNECTIONS_RESPONSE) + template = self.response_template(DESCRIBE_VPC_PEERING_CONNECTIONS_RESPONSE) return template.render(vpc_pcxs=vpc_pcxs) def accept_vpc_peering_connection(self): vpc_pcx_id = self.querystring.get('VpcPeeringConnectionId')[0] vpc_pcx = self.ec2_backend.accept_vpc_peering_connection(vpc_pcx_id) - template = Template(ACCEPT_VPC_PEERING_CONNECTION_RESPONSE) + template = self.response_template(ACCEPT_VPC_PEERING_CONNECTION_RESPONSE) return template.render(vpc_pcx=vpc_pcx) def reject_vpc_peering_connection(self): vpc_pcx_id = self.querystring.get('VpcPeeringConnectionId')[0] self.ec2_backend.reject_vpc_peering_connection(vpc_pcx_id) - template = Template(REJECT_VPC_PEERING_CONNECTION_RESPONSE) + template = self.response_template(REJECT_VPC_PEERING_CONNECTION_RESPONSE) return template.render() diff --git a/moto/ec2/responses/vpcs.py b/moto/ec2/responses/vpcs.py index 7fb9d4aed..ddc148da3 100644 --- a/moto/ec2/responses/vpcs.py +++ b/moto/ec2/responses/vpcs.py @@ -1,6 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template - from moto.core.responses import BaseResponse from moto.ec2.utils import filters_from_querystring, vpc_ids_from_querystring @@ -9,20 +7,20 @@ class VPCs(BaseResponse): def create_vpc(self): cidr_block = self.querystring.get('CidrBlock')[0] vpc = self.ec2_backend.create_vpc(cidr_block) - template = Template(CREATE_VPC_RESPONSE) + template = self.response_template(CREATE_VPC_RESPONSE) return template.render(vpc=vpc) def delete_vpc(self): vpc_id = self.querystring.get('VpcId')[0] vpc = self.ec2_backend.delete_vpc(vpc_id) - template = Template(DELETE_VPC_RESPONSE) + template = self.response_template(DELETE_VPC_RESPONSE) return template.render(vpc=vpc) def describe_vpcs(self): vpc_ids = vpc_ids_from_querystring(self.querystring) filters = filters_from_querystring(self.querystring) vpcs = self.ec2_backend.get_all_vpcs(vpc_ids=vpc_ids, filters=filters) - template = Template(DESCRIBE_VPCS_RESPONSE) + template = self.response_template(DESCRIBE_VPCS_RESPONSE) return template.render(vpcs=vpcs) diff --git a/moto/elb/responses.py b/moto/elb/responses.py index 8895b75f9..fb114fb22 100644 --- a/moto/elb/responses.py +++ b/moto/elb/responses.py @@ -1,5 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template from moto.core.responses import BaseResponse from .models import elb_backends @@ -35,7 +34,7 @@ class ELBResponse(BaseResponse): zones=availability_zones, ports=ports, ) - template = Template(CREATE_LOAD_BALANCER_TEMPLATE) + template = self.response_template(CREATE_LOAD_BALANCER_TEMPLATE) return template.render() def create_load_balancer_listeners(self): @@ -55,13 +54,13 @@ class ELBResponse(BaseResponse): self.elb_backend.create_load_balancer_listeners(name=load_balancer_name, ports=ports) - template = Template(CREATE_LOAD_BALANCER_LISTENERS_TEMPLATE) + template = self.response_template(CREATE_LOAD_BALANCER_LISTENERS_TEMPLATE) return template.render() def describe_load_balancers(self): names = [value[0] for key, value in self.querystring.items() if "LoadBalancerNames.member" in key] load_balancers = self.elb_backend.describe_load_balancers(names) - template = Template(DESCRIBE_LOAD_BALANCERS_TEMPLATE) + template = self.response_template(DESCRIBE_LOAD_BALANCERS_TEMPLATE) return template.render(load_balancers=load_balancers) def delete_load_balancer_listeners(self): @@ -78,13 +77,13 @@ class ELBResponse(BaseResponse): ports.append(int(port)) self.elb_backend.delete_load_balancer_listeners(load_balancer_name, ports) - template = Template(DELETE_LOAD_BALANCER_LISTENERS) + template = self.response_template(DELETE_LOAD_BALANCER_LISTENERS) return template.render() def delete_load_balancer(self): load_balancer_name = self.querystring.get('LoadBalancerName')[0] self.elb_backend.delete_load_balancer(load_balancer_name) - template = Template(DELETE_LOAD_BALANCER_TEMPLATE) + template = self.response_template(DELETE_LOAD_BALANCER_TEMPLATE) return template.render() def configure_health_check(self): @@ -96,13 +95,13 @@ class ELBResponse(BaseResponse): interval=self.querystring.get('HealthCheck.Interval')[0], target=self.querystring.get('HealthCheck.Target')[0], ) - template = Template(CONFIGURE_HEALTH_CHECK_TEMPLATE) + template = self.response_template(CONFIGURE_HEALTH_CHECK_TEMPLATE) return template.render(check=check) def register_instances_with_load_balancer(self): load_balancer_name = self.querystring.get('LoadBalancerName')[0] instance_ids = [value[0] for key, value in self.querystring.items() if "Instances.member" in key] - template = Template(REGISTER_INSTANCES_TEMPLATE) + template = self.response_template(REGISTER_INSTANCES_TEMPLATE) load_balancer = self.elb_backend.register_instances(load_balancer_name, instance_ids) return template.render(load_balancer=load_balancer) @@ -113,13 +112,13 @@ class ELBResponse(BaseResponse): self.elb_backend.set_load_balancer_listener_sslcertificate(load_balancer_name, lb_port, ssl_certificate_id) - template = Template(SET_LOAD_BALANCER_SSL_CERTIFICATE) + template = self.response_template(SET_LOAD_BALANCER_SSL_CERTIFICATE) return template.render() def deregister_instances_from_load_balancer(self): load_balancer_name = self.querystring.get('LoadBalancerName')[0] instance_ids = [value[0] for key, value in self.querystring.items() if "Instances.member" in key] - template = Template(DEREGISTER_INSTANCES_TEMPLATE) + template = self.response_template(DEREGISTER_INSTANCES_TEMPLATE) load_balancer = self.elb_backend.deregister_instances(load_balancer_name, instance_ids) return template.render(load_balancer=load_balancer) diff --git a/moto/emr/responses.py b/moto/emr/responses.py index c3bcab96b..44f2f0890 100644 --- a/moto/emr/responses.py +++ b/moto/emr/responses.py @@ -1,5 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template from moto.core.responses import BaseResponse from moto.core.utils import camelcase_to_underscores @@ -41,7 +40,7 @@ class ElasticMapReduceResponse(BaseResponse): steps = self._get_list_prefix('Steps.member') job_flow = emr_backend.add_job_flow_steps(job_flow_id, steps) - template = Template(ADD_JOB_FLOW_STEPS_TEMPLATE) + template = self.response_template(ADD_JOB_FLOW_STEPS_TEMPLATE) return template.render(job_flow=job_flow) def run_job_flow(self): @@ -56,38 +55,38 @@ class ElasticMapReduceResponse(BaseResponse): flow_name, log_uri, job_flow_role, visible_to_all_users, steps, instance_attrs ) - template = Template(RUN_JOB_FLOW_TEMPLATE) + template = self.response_template(RUN_JOB_FLOW_TEMPLATE) return template.render(job_flow=job_flow) def describe_job_flows(self): job_flows = emr_backend.describe_job_flows() - template = Template(DESCRIBE_JOB_FLOWS_TEMPLATE) + template = self.response_template(DESCRIBE_JOB_FLOWS_TEMPLATE) return template.render(job_flows=job_flows) def terminate_job_flows(self): job_ids = self._get_multi_param('JobFlowIds.member.') job_flows = emr_backend.terminate_job_flows(job_ids) - template = Template(TERMINATE_JOB_FLOWS_TEMPLATE) + template = self.response_template(TERMINATE_JOB_FLOWS_TEMPLATE) return template.render(job_flows=job_flows) def add_instance_groups(self): jobflow_id = self._get_param('JobFlowId') instance_groups = self._get_list_prefix('InstanceGroups.member') instance_groups = emr_backend.add_instance_groups(jobflow_id, instance_groups) - template = Template(ADD_INSTANCE_GROUPS_TEMPLATE) + template = self.response_template(ADD_INSTANCE_GROUPS_TEMPLATE) return template.render(instance_groups=instance_groups) def modify_instance_groups(self): instance_groups = self._get_list_prefix('InstanceGroups.member') instance_groups = emr_backend.modify_instance_groups(instance_groups) - template = Template(MODIFY_INSTANCE_GROUPS_TEMPLATE) + template = self.response_template(MODIFY_INSTANCE_GROUPS_TEMPLATE) return template.render(instance_groups=instance_groups) def set_visible_to_all_users(self): visible_to_all_users = self._get_param('VisibleToAllUsers') job_ids = self._get_multi_param('JobFlowIds.member') emr_backend.set_visible_to_all_users(job_ids, visible_to_all_users) - template = Template(SET_VISIBLE_TO_ALL_USERS_TEMPLATE) + template = self.response_template(SET_VISIBLE_TO_ALL_USERS_TEMPLATE) return template.render() diff --git a/moto/iam/responses.py b/moto/iam/responses.py index 1f4036ddf..d84780267 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -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') diff --git a/moto/s3/responses.py b/moto/s3/responses.py index 8a0931ec6..3fe30c9d1 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -2,10 +2,10 @@ from __future__ import unicode_literals import re -from jinja2 import Template import six from six.moves.urllib.parse import parse_qs, urlparse +from moto.core.responses import _TemplateEnvironmentMixin from .exceptions import BucketAlreadyExists, MissingBucket from .models import s3_backend @@ -17,7 +17,7 @@ def parse_key_name(pth): return pth.lstrip("/") -class ResponseObject(object): +class ResponseObject(_TemplateEnvironmentMixin): def __init__(self, backend, bucket_name_from_url, parse_key_name): self.backend = backend self.bucket_name_from_url = bucket_name_from_url @@ -26,7 +26,7 @@ class ResponseObject(object): def all_buckets(self): # No bucket specified. Listing all buckets all_buckets = self.backend.get_all_buckets() - template = Template(S3_ALL_BUCKETS) + template = self.response_template(S3_ALL_BUCKETS) return template.render(buckets=all_buckets) def bucket_response(self, request, full_url, headers): @@ -78,13 +78,13 @@ class ResponseObject(object): if unsup in querystring: raise NotImplementedError("Listing multipart uploads with {} has not been implemented yet.".format(unsup)) multiparts = list(self.backend.get_all_multiparts(bucket_name).values()) - template = Template(S3_ALL_MULTIPARTS) + template = self.response_template(S3_ALL_MULTIPARTS) return 200, headers, template.render( bucket_name=bucket_name, uploads=multiparts) elif 'versioning' in querystring: versioning = self.backend.get_bucket_versioning(bucket_name) - template = Template(S3_BUCKET_GET_VERSIONING) + template = self.response_template(S3_BUCKET_GET_VERSIONING) return 200, headers, template.render(status=versioning) elif 'versions' in querystring: delimiter = querystring.get('delimiter', [None])[0] @@ -103,7 +103,7 @@ class ResponseObject(object): max_keys=max_keys, version_id_marker=version_id_marker ) - template = Template(S3_BUCKET_GET_VERSIONS) + template = self.response_template(S3_BUCKET_GET_VERSIONS) return 200, headers, template.render( key_list=versions, bucket=bucket, @@ -121,7 +121,7 @@ class ResponseObject(object): prefix = querystring.get('prefix', [None])[0] delimiter = querystring.get('delimiter', [None])[0] result_keys, result_folders = self.backend.prefix_query(bucket, prefix, delimiter) - template = Template(S3_BUCKET_GET_RESPONSE) + template = self.response_template(S3_BUCKET_GET_RESPONSE) return 200, headers, template.render( bucket=bucket, prefix=prefix, @@ -135,7 +135,7 @@ class ResponseObject(object): ver = re.search('([A-Za-z]+)', request.body.decode('utf-8')) if ver: self.backend.set_bucket_versioning(bucket_name, ver.group(1)) - template = Template(S3_BUCKET_VERSIONING) + template = self.response_template(S3_BUCKET_VERSIONING) return template.render(bucket_versioning_status=ver.group(1)) else: return 404, headers, "" @@ -144,7 +144,7 @@ class ResponseObject(object): new_bucket = self.backend.create_bucket(bucket_name) except BucketAlreadyExists: return 409, headers, "" - template = Template(S3_BUCKET_CREATE_RESPONSE) + template = self.response_template(S3_BUCKET_CREATE_RESPONSE) return 200, headers, template.render(bucket=new_bucket) def _bucket_response_delete(self, bucket_name, headers): @@ -152,16 +152,16 @@ class ResponseObject(object): removed_bucket = self.backend.delete_bucket(bucket_name) except MissingBucket: # Non-existant bucket - template = Template(S3_DELETE_NON_EXISTING_BUCKET) + template = self.response_template(S3_DELETE_NON_EXISTING_BUCKET) return 404, headers, template.render(bucket_name=bucket_name) if removed_bucket: # Bucket exists - template = Template(S3_DELETE_BUCKET_SUCCESS) + template = self.response_template(S3_DELETE_BUCKET_SUCCESS) return 204, headers, template.render(bucket=removed_bucket) else: # Tried to delete a bucket that still has keys - template = Template(S3_DELETE_BUCKET_WITH_ITEMS_ERROR) + template = self.response_template(S3_DELETE_BUCKET_WITH_ITEMS_ERROR) return 409, headers, template.render(bucket=removed_bucket) def _bucket_response_post(self, request, bucket_name, headers): @@ -194,7 +194,7 @@ class ResponseObject(object): return 200, headers, "" def _bucket_response_delete_keys(self, request, bucket_name, headers): - template = Template(S3_DELETE_KEYS_RESPONSE) + template = self.response_template(S3_DELETE_KEYS_RESPONSE) keys = minidom.parseString(request.body.decode('utf-8')).getElementsByTagName('Key') deleted_names = [] @@ -254,7 +254,7 @@ class ResponseObject(object): if 'uploadId' in query: upload_id = query['uploadId'][0] parts = self.backend.list_multipart(bucket_name, upload_id) - template = Template(S3_MULTIPART_LIST_RESPONSE) + template = self.response_template(S3_MULTIPART_LIST_RESPONSE) return 200, headers, template.render( bucket_name=bucket_name, key_name=key_name, @@ -281,7 +281,7 @@ class ResponseObject(object): key = self.backend.copy_part( bucket_name, upload_id, part_number, src_bucket, src_key) - template = Template(S3_MULTIPART_UPLOAD_RESPONSE) + template = self.response_template(S3_MULTIPART_UPLOAD_RESPONSE) response = template.render(part=key) else: key = self.backend.set_part( @@ -306,7 +306,7 @@ class ResponseObject(object): new_key = self.backend.get_key(bucket_name, key_name) metadata = metadata_from_headers(request.headers) new_key.set_metadata(metadata, replace=True) - template = Template(S3_OBJECT_COPY_RESPONSE) + template = self.response_template(S3_OBJECT_COPY_RESPONSE) return template.render(key=src_key) streaming_request = hasattr(request, 'streaming') and request.streaming closing_connection = headers.get('connection') == 'close' @@ -324,7 +324,7 @@ class ResponseObject(object): metadata = metadata_from_headers(request.headers) new_key.set_metadata(metadata) - template = Template(S3_OBJECT_RESPONSE) + template = self.response_template(S3_OBJECT_RESPONSE) headers.update(new_key.response_dict) return 200, headers, template.render(key=new_key) @@ -343,7 +343,7 @@ class ResponseObject(object): self.backend.cancel_multipart(bucket_name, upload_id) return 204, headers, "" removed_key = self.backend.delete_key(bucket_name, key_name) - template = Template(S3_DELETE_OBJECT_SUCCESS) + template = self.response_template(S3_DELETE_OBJECT_SUCCESS) return 204, headers, template.render(bucket=removed_key) def _key_response_post(self, request, body, parsed_url, bucket_name, query, key_name, headers): @@ -351,7 +351,7 @@ class ResponseObject(object): metadata = metadata_from_headers(request.headers) multipart = self.backend.initiate_multipart(bucket_name, key_name, metadata) - template = Template(S3_MULTIPART_INITIATE_RESPONSE) + template = self.response_template(S3_MULTIPART_INITIATE_RESPONSE) response = template.render( bucket_name=bucket_name, key_name=key_name, @@ -364,13 +364,13 @@ class ResponseObject(object): key = self.backend.complete_multipart(bucket_name, upload_id) if key is not None: - template = Template(S3_MULTIPART_COMPLETE_RESPONSE) + template = self.response_template(S3_MULTIPART_COMPLETE_RESPONSE) return template.render( bucket_name=bucket_name, key_name=key.name, etag=key.etag, ) - template = Template(S3_MULTIPART_COMPLETE_TOO_SMALL_ERROR) + template = self.response_template(S3_MULTIPART_COMPLETE_TOO_SMALL_ERROR) return 400, headers, template.render() elif parsed_url.query == 'restore': es = minidom.parseString(body).getElementsByTagName('Days') diff --git a/moto/ses/responses.py b/moto/ses/responses.py index d25a223f4..14b365975 100644 --- a/moto/ses/responses.py +++ b/moto/ses/responses.py @@ -1,5 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template from moto.core.responses import BaseResponse from .models import ses_backend @@ -10,30 +9,30 @@ class EmailResponse(BaseResponse): def verify_email_identity(self): address = self.querystring.get('EmailAddress')[0] ses_backend.verify_email_identity(address) - template = Template(VERIFY_EMAIL_IDENTITY) + template = self.response_template(VERIFY_EMAIL_IDENTITY) return template.render() def list_identities(self): identities = ses_backend.list_identities() - template = Template(LIST_IDENTITIES_RESPONSE) + template = self.response_template(LIST_IDENTITIES_RESPONSE) return template.render(identities=identities) def verify_domain_dkim(self): domain = self.querystring.get('Domain')[0] ses_backend.verify_domain(domain) - template = Template(VERIFY_DOMAIN_DKIM_RESPONSE) + template = self.response_template(VERIFY_DOMAIN_DKIM_RESPONSE) return template.render() def verify_domain_identity(self): domain = self.querystring.get('Domain')[0] ses_backend.verify_domain(domain) - template = Template(VERIFY_DOMAIN_DKIM_RESPONSE) + template = self.response_template(VERIFY_DOMAIN_DKIM_RESPONSE) return template.render() def delete_identity(self): domain = self.querystring.get('Identity')[0] ses_backend.delete_identity(domain) - template = Template(DELETE_IDENTITY_RESPONSE) + template = self.response_template(DELETE_IDENTITY_RESPONSE) return template.render() def send_email(self): @@ -47,7 +46,7 @@ class EmailResponse(BaseResponse): message = ses_backend.send_email(source, subject, body, destination) if not message: return "Did not have authority to send from email {0}".format(source), dict(status=400) - template = Template(SEND_EMAIL_RESPONSE) + template = self.response_template(SEND_EMAIL_RESPONSE) return template.render(message=message) def send_raw_email(self): @@ -58,12 +57,12 @@ class EmailResponse(BaseResponse): message = ses_backend.send_raw_email(source, destination, raw_data) if not message: return "Did not have authority to send from email {0}".format(source), dict(status=400) - template = Template(SEND_RAW_EMAIL_RESPONSE) + template = self.response_template(SEND_RAW_EMAIL_RESPONSE) return template.render(message=message) def get_send_quota(self): quota = ses_backend.get_send_quota() - template = Template(GET_SEND_QUOTA_RESPONSE) + template = self.response_template(GET_SEND_QUOTA_RESPONSE) return template.render(quota=quota) diff --git a/moto/sqs/responses.py b/moto/sqs/responses.py index 3d0cef2ce..3a22f43fb 100644 --- a/moto/sqs/responses.py +++ b/moto/sqs/responses.py @@ -1,5 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template from moto.core.responses import BaseResponse from moto.core.utils import camelcase_to_underscores @@ -30,14 +29,14 @@ class QueuesResponse(BaseResponse): queue_name = self.querystring.get("QueueName")[0] queue = self.sqs_backend.create_queue(queue_name, visibility_timeout=visibility_timeout) - template = Template(CREATE_QUEUE_RESPONSE) + template = self.response_template(CREATE_QUEUE_RESPONSE) return template.render(queue=queue) def get_queue_url(self): queue_name = self.querystring.get("QueueName")[0] queue = self.sqs_backend.get_queue(queue_name) if queue: - template = Template(GET_QUEUE_URL_RESPONSE) + template = self.response_template(GET_QUEUE_URL_RESPONSE) return template.render(queue=queue) else: return "", dict(status=404) @@ -45,7 +44,7 @@ class QueuesResponse(BaseResponse): def list_queues(self): queue_name_prefix = self.querystring.get("QueueNamePrefix", [None])[0] queues = self.sqs_backend.list_queues(queue_name_prefix) - template = Template(LIST_QUEUES_RESPONSE) + template = self.response_template(LIST_QUEUES_RESPONSE) return template.render(queues=queues) @@ -76,13 +75,13 @@ class QueueResponse(BaseResponse): except (ReceiptHandleIsInvalid, MessageNotInflight) as e: return "Invalid request: {0}".format(e.description), dict(status=e.status_code) - template = Template(CHANGE_MESSAGE_VISIBILITY_RESPONSE) + template = self.response_template(CHANGE_MESSAGE_VISIBILITY_RESPONSE) return template.render() def get_queue_attributes(self): queue_name = self.path.split("/")[-1] queue = self.sqs_backend.get_queue(queue_name) - template = Template(GET_QUEUE_ATTRIBUTES_RESPONSE) + template = self.response_template(GET_QUEUE_ATTRIBUTES_RESPONSE) return template.render(queue=queue) def set_queue_attributes(self): @@ -97,7 +96,7 @@ class QueueResponse(BaseResponse): queue = self.sqs_backend.delete_queue(queue_name) if not queue: return "A queue with name {0} does not exist".format(queue_name), dict(status=404) - template = Template(DELETE_QUEUE_RESPONSE) + template = self.response_template(DELETE_QUEUE_RESPONSE) return template.render(queue=queue) def send_message(self): @@ -121,7 +120,7 @@ class QueueResponse(BaseResponse): message_attributes=message_attributes, delay_seconds=delay_seconds ) - template = Template(SEND_MESSAGE_RESPONSE) + template = self.response_template(SEND_MESSAGE_RESPONSE) return template.render(message=message, message_attributes=message_attributes) def send_message_batch(self): @@ -161,14 +160,14 @@ class QueueResponse(BaseResponse): messages.append(message) - template = Template(SEND_MESSAGE_BATCH_RESPONSE) + template = self.response_template(SEND_MESSAGE_BATCH_RESPONSE) return template.render(messages=messages) def delete_message(self): queue_name = self.path.split("/")[-1] receipt_handle = self.querystring.get("ReceiptHandle")[0] self.sqs_backend.delete_message(queue_name, receipt_handle) - template = Template(DELETE_MESSAGE_RESPONSE) + template = self.response_template(DELETE_MESSAGE_RESPONSE) return template.render() def delete_message_batch(self): @@ -198,14 +197,14 @@ class QueueResponse(BaseResponse): message_user_id = self.querystring.get(message_user_id_key)[0] message_ids.append(message_user_id) - template = Template(DELETE_MESSAGE_BATCH_RESPONSE) + template = self.response_template(DELETE_MESSAGE_BATCH_RESPONSE) return template.render(message_ids=message_ids) def receive_message(self): queue_name = self.path.split("/")[-1] message_count = int(self.querystring.get("MaxNumberOfMessages")[0]) messages = self.sqs_backend.receive_messages(queue_name, message_count) - template = Template(RECEIVE_MESSAGE_RESPONSE) + template = self.response_template(RECEIVE_MESSAGE_RESPONSE) output = template.render(messages=messages) return output diff --git a/moto/sts/responses.py b/moto/sts/responses.py index e14305556..193085623 100644 --- a/moto/sts/responses.py +++ b/moto/sts/responses.py @@ -1,5 +1,4 @@ from __future__ import unicode_literals -from jinja2 import Template from moto.core.responses import BaseResponse from .models import sts_backend @@ -10,7 +9,7 @@ class TokenResponse(BaseResponse): def get_session_token(self): duration = int(self.querystring.get('DurationSeconds', [43200])[0]) token = sts_backend.get_session_token(duration=duration) - template = Template(GET_SESSION_TOKEN_RESPONSE) + template = self.response_template(GET_SESSION_TOKEN_RESPONSE) return template.render(token=token) def get_federation_token(self): @@ -19,7 +18,7 @@ class TokenResponse(BaseResponse): name = self.querystring.get('Name')[0] token = sts_backend.get_federation_token( duration=duration, name=name, policy=policy) - template = Template(GET_FEDERATION_TOKEN_RESPONSE) + template = self.response_template(GET_FEDERATION_TOKEN_RESPONSE) return template.render(token=token) def assume_role(self): @@ -37,7 +36,7 @@ class TokenResponse(BaseResponse): duration=duration, external_id=external_id, ) - template = Template(ASSUME_ROLE_RESPONSE) + template = self.response_template(ASSUME_ROLE_RESPONSE) return template.render(role=role)