From ed82264806b0d47146853a683b0ae5a879ef35a0 Mon Sep 17 00:00:00 2001 From: Don Kuntz Date: Wed, 14 Aug 2019 17:31:57 -0500 Subject: [PATCH] Rework LaunchTemplateBackend to be keep only one copy of a template, and be ordered The original LaunchTemplateBackend kept two copies of a template, one for referencing it by name and one for referencing it by id. This change switches to using one copy, by id, and adding a lookup dict for mapping names to ids. Additionally, to fix the python2 test ordering issues, the launch template dict was changed to an OrderedDict. --- moto/ec2/models.py | 33 +++++++++++++++----------- moto/ec2/responses/launch_templates.py | 4 ++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 3cb0bad93..10d6f2b28 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -4166,31 +4166,36 @@ class LaunchTemplate(TaggedEC2Resource): class LaunchTemplateBackend(object): def __init__(self): - self.launch_templates_by_name = {} - self.launch_templates_by_id = {} + self.launch_template_name_to_ids = {} + self.launch_templates = OrderedDict() + self.launch_template_insert_order = [] super(LaunchTemplateBackend, self).__init__() def create_launch_template(self, name, description, template_data): - if name in self.launch_templates_by_name: + if name in self.launch_template_name_to_ids: raise InvalidLaunchTemplateNameError() template = LaunchTemplate(self, name, template_data, description) - self.launch_templates_by_id[template.id] = template - self.launch_templates_by_name[template.name] = template + self.launch_templates[template.id] = template + self.launch_template_name_to_ids[template.name] = template.id + self.launch_template_insert_order.append(template.id) return template - def get_launch_template_by_name(self, name): - return self.launch_templates_by_name[name] + def get_launch_template(self, template_id): + return self.launch_templates[template_id] - def get_launch_template_by_id(self, templ_id): - return self.launch_templates_by_id[templ_id] + def get_launch_template_by_name(self, name): + return self.get_launch_template(self.launch_template_name_to_ids[name]) def get_launch_templates(self, template_names=None, template_ids=None, filters=None): + if template_names and not template_ids: + template_ids = [] + for name in template_names: + template_ids.append(self.launch_template_name_to_ids[name]) + if template_ids: - templates = [self.launch_templates_by_id[tid] for tid in template_ids] - elif template_names: - templates = [self.launch_templates_by_name[name] for name in template_names] + templates = [self.launch_templates[tid] for tid in template_ids] else: - templates = list(self.launch_templates_by_name.values()) + templates = list(self.launch_templates.values()) return generic_filter(filters, templates) @@ -4260,7 +4265,7 @@ class EC2Backend(BaseBackend, InstanceBackend, TagBackend, EBSBackend, self.describe_internet_gateways( internet_gateway_ids=[resource_id]) elif resource_prefix == EC2_RESOURCE_TO_PREFIX['launch-template']: - self.get_launch_template_by_id(resource_id) + self.get_launch_template(resource_id) elif resource_prefix == EC2_RESOURCE_TO_PREFIX['network-acl']: self.get_all_network_acls() elif resource_prefix == EC2_RESOURCE_TO_PREFIX['network-interface']: diff --git a/moto/ec2/responses/launch_templates.py b/moto/ec2/responses/launch_templates.py index 14337d17f..ab6f54be1 100644 --- a/moto/ec2/responses/launch_templates.py +++ b/moto/ec2/responses/launch_templates.py @@ -127,7 +127,7 @@ class LaunchTemplates(BaseResponse): if name: template = self.ec2_backend.get_launch_template_by_name(name) if tmpl_id: - template = self.ec2_backend.get_launch_template_by_id(tmpl_id) + template = self.ec2_backend.get_launch_template(tmpl_id) version_description = self._get_param('VersionDescription') @@ -162,7 +162,7 @@ class LaunchTemplates(BaseResponse): if name: template = self.ec2_backend.get_launch_template_by_name(name) if template_id: - template = self.ec2_backend.get_launch_template_by_id(template_id) + template = self.ec2_backend.get_launch_template(template_id) max_results = self._get_int_param("MaxResults", 15) versions = self._get_multi_param("Versions")