EC2: handle non-existent template name for describe-launch-templates (#5365)

This commit is contained in:
Johannes Loewe 2022-08-10 21:17:30 +02:00 committed by GitHub
parent 1077c458fe
commit b40c9760c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 3 deletions

View File

@ -702,7 +702,7 @@ class OperationNotPermitted4(EC2ClientError):
) )
class InvalidLaunchTemplateNameError(EC2ClientError): class InvalidLaunchTemplateNameAlreadyExistsError(EC2ClientError):
def __init__(self): def __init__(self):
super().__init__( super().__init__(
"InvalidLaunchTemplateName.AlreadyExistsException", "InvalidLaunchTemplateName.AlreadyExistsException",
@ -710,6 +710,14 @@ class InvalidLaunchTemplateNameError(EC2ClientError):
) )
class InvalidLaunchTemplateNameNotFoundError(EC2ClientError):
def __init__(self):
super().__init__(
"InvalidLaunchTemplateName.NotFoundException",
"At least one of the launch templates specified in the request does not exist.",
)
class InvalidParameterDependency(EC2ClientError): class InvalidParameterDependency(EC2ClientError):
def __init__(self, param, param_needed): def __init__(self, param, param_needed):
super().__init__( super().__init__(

View File

@ -1,7 +1,10 @@
from collections import OrderedDict from collections import OrderedDict
from .core import TaggedEC2Resource from .core import TaggedEC2Resource
from ..utils import generic_filter, random_launch_template_id, utc_date_and_time from ..utils import generic_filter, random_launch_template_id, utc_date_and_time
from ..exceptions import InvalidLaunchTemplateNameError from ..exceptions import (
InvalidLaunchTemplateNameAlreadyExistsError,
InvalidLaunchTemplateNameNotFoundError,
)
class LaunchTemplateVersion(object): class LaunchTemplateVersion(object):
@ -81,7 +84,7 @@ class LaunchTemplateBackend:
def create_launch_template(self, name, description, template_data): def create_launch_template(self, name, description, template_data):
if name in self.launch_template_name_to_ids: if name in self.launch_template_name_to_ids:
raise InvalidLaunchTemplateNameError() raise InvalidLaunchTemplateNameAlreadyExistsError()
template = LaunchTemplate(self, name, template_data, description) template = LaunchTemplate(self, name, template_data, description)
self.launch_templates[template.id] = template self.launch_templates[template.id] = template
self.launch_template_name_to_ids[template.name] = template.id self.launch_template_name_to_ids[template.name] = template.id
@ -105,6 +108,8 @@ class LaunchTemplateBackend:
if template_names and not template_ids: if template_names and not template_ids:
template_ids = [] template_ids = []
for name in template_names: for name in template_names:
if name not in self.launch_template_name_to_ids:
raise InvalidLaunchTemplateNameNotFoundError()
template_ids.append(self.launch_template_name_to_ids[name]) template_ids.append(self.launch_template_name_to_ids[name])
if template_ids: if template_ids:

View File

@ -304,6 +304,20 @@ def test_describe_launch_template_versions_with_min_and_max():
) )
@mock_ec2
def test_describe_launch_templates_with_non_existent_name():
cli = boto3.client("ec2", region_name="us-east-1")
template_name = str(uuid4())
with pytest.raises(ClientError) as ex:
cli.describe_launch_templates(LaunchTemplateNames=[template_name])
str(ex.value).should.equal(
"An error occurred (InvalidLaunchTemplateName.NotFoundException) when calling the DescribeLaunchTemplates operation: At least one of the launch templates specified in the request does not exist."
)
@mock_ec2 @mock_ec2
def test_describe_launch_templates(): def test_describe_launch_templates():
cli = boto3.client("ec2", region_name="us-east-1") cli = boto3.client("ec2", region_name="us-east-1")