diff --git a/moto/ec2/exceptions.py b/moto/ec2/exceptions.py index 453f75d1d..b7a49cc57 100644 --- a/moto/ec2/exceptions.py +++ b/moto/ec2/exceptions.py @@ -524,6 +524,7 @@ class OperationNotPermitted3(EC2ClientError): acceptor_region) ) + class InvalidLaunchTemplateNameError(EC2ClientError): def __init__(self): super(InvalidLaunchTemplateNameError, self).__init__( diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 2310585ac..3cb0bad93 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -19,8 +19,6 @@ from boto.ec2.instance import Instance as BotoInstance, Reservation from boto.ec2.blockdevicemapping import BlockDeviceMapping, BlockDeviceType from boto.ec2.spotinstancerequest import SpotInstanceRequest as BotoSpotRequest from boto.ec2.launchspecification import LaunchSpecification -from xml.etree import ElementTree -from xml.dom import minidom from moto.compat import OrderedDict from moto.core import BaseBackend @@ -4115,6 +4113,7 @@ class NatGatewayBackend(object): def delete_nat_gateway(self, nat_gateway_id): return self.nat_gateways.pop(nat_gateway_id) + class LaunchTemplateVersion(object): def __init__(self, template, number, data, description): self.template = template @@ -4123,6 +4122,7 @@ class LaunchTemplateVersion(object): self.description = description self.create_time = utc_date_and_time() + class LaunchTemplate(TaggedEC2Resource): def __init__(self, backend, name, template_data, version_description): self.ec2_backend = backend @@ -4144,10 +4144,10 @@ class LaunchTemplate(TaggedEC2Resource): return self.default_version == version.number def get_version(self, num): - return self.versions[num-1] + return self.versions[num - 1] def default_version(self): - return self.versions[self.default_version_number-1] + return self.versions[self.default_version_number - 1] def latest_version(self): return self.versions[-1] @@ -4163,6 +4163,7 @@ class LaunchTemplate(TaggedEC2Resource): return super(LaunchTemplate, self).get_filter_value( filter_name, "DescribeLaunchTemplates") + class LaunchTemplateBackend(object): def __init__(self): self.launch_templates_by_name = {} diff --git a/moto/ec2/responses/launch_templates.py b/moto/ec2/responses/launch_templates.py index ebce54294..d1bfaa6d4 100644 --- a/moto/ec2/responses/launch_templates.py +++ b/moto/ec2/responses/launch_templates.py @@ -1,4 +1,3 @@ -import json import six import uuid from moto.core.responses import BaseResponse @@ -19,6 +18,7 @@ def xml_root(name): return root + def xml_serialize(tree, key, value): if key: name = key[0].lower() + key[1:] @@ -43,19 +43,21 @@ def xml_serialize(tree, key, value): for item in value: xml_serialize(node, 'item', item) + def pretty_xml(tree): rough = ElementTree.tostring(tree, 'utf-8') parsed = minidom.parseString(rough) return parsed.toprettyxml(indent=' ') + def parse_object(raw_data): out_data = {} for key, value in six.iteritems(raw_data): key_fix_splits = key.split("_") - l = len(key_fix_splits) + key_len = len(key_fix_splits) new_key = "" - for i in range(0, l): + for i in range(0, key_len): new_key += key_fix_splits[i][0].upper() + key_fix_splits[i][1:] data = out_data @@ -70,6 +72,7 @@ def parse_object(raw_data): out_data = parse_lists(out_data) return out_data + def parse_lists(data): for key, value in six.iteritems(data): if isinstance(value, dict): @@ -87,6 +90,7 @@ def parse_lists(data): data[key] = new_value return data + class LaunchTemplates(BaseResponse): def create_launch_template(self): name = self._get_param('LaunchTemplateName') @@ -126,8 +130,6 @@ class LaunchTemplates(BaseResponse): template = self.ec2_backend.get_launch_template_by_id(tmpl_id) version_description = self._get_param('VersionDescription') - tag_spec = self._get_param('TagSpecifications') - # source_version = self._get_int_param('SourceVersion') raw_template_data = self._get_dict_param('LaunchTemplateData.') template_data = parse_object(raw_template_data) @@ -148,7 +150,6 @@ class LaunchTemplates(BaseResponse): }) return pretty_xml(tree) - # def delete_launch_template(self): # pass @@ -191,9 +192,12 @@ class LaunchTemplates(BaseResponse): else: vMax = min_version + max_results - ret_versions = template.versions[min_version-1:vMax-1] + vMin = min_version - 1 + vMax = vMax - 1 + ret_versions = template.versions[vMin:vMax] elif max_version: - ret_versions = template.versions[0:max_version-1] + vMax = max_version - 1 + ret_versions = template.versions[:vMax] else: ret_versions = template.versions