From ce4bd5d64c2caf734069339928e5d6c075e5b90c Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Tue, 22 Oct 2019 09:31:37 +0100 Subject: [PATCH] AWS Lambda - Allow function deletions by ARN --- moto/awslambda/models.py | 6 ++++-- moto/awslambda/responses.py | 2 +- moto/awslambda/urls.py | 2 +- tests/test_awslambda/test_lambda.py | 31 ++++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/moto/awslambda/models.py b/moto/awslambda/models.py index f8c1d2e9b..265472067 100644 --- a/moto/awslambda/models.py +++ b/moto/awslambda/models.py @@ -645,8 +645,10 @@ class LambdaStorage(object): self._arns[fn.function_arn] = fn return fn - def del_function(self, name, qualifier=None): - if name in self._functions: + def del_function(self, name_or_arn, qualifier=None): + function = self.get_function_by_name_or_arn(name_or_arn) + if function: + name = function.function_name if not qualifier: # Something is still reffing this so delete all arns latest = self._functions[name]['latest'].function_arn diff --git a/moto/awslambda/responses.py b/moto/awslambda/responses.py index 83a1cefca..9ef0fb483 100644 --- a/moto/awslambda/responses.py +++ b/moto/awslambda/responses.py @@ -266,7 +266,7 @@ class LambdaResponse(BaseResponse): return 404, {}, "{}" def _delete_function(self, request, full_url, headers): - function_name = self.path.rsplit('/', 1)[-1] + function_name = unquote(self.path.rsplit('/', 1)[-1]) qualifier = self._get_param('Qualifier', None) if self.lambda_backend.delete_function(function_name, qualifier): diff --git a/moto/awslambda/urls.py b/moto/awslambda/urls.py index a306feff5..0ee8797d3 100644 --- a/moto/awslambda/urls.py +++ b/moto/awslambda/urls.py @@ -9,7 +9,7 @@ response = LambdaResponse() url_paths = { '{0}/(?P[^/]+)/functions/?$': response.root, - r'{0}/(?P[^/]+)/functions/(?P[\w_-]+)/?$': response.function, + r'{0}/(?P[^/]+)/functions/(?P[\w_:%-]+)/?$': response.function, r'{0}/(?P[^/]+)/functions/(?P[\w_-]+)/versions/?$': response.versions, r'{0}/(?P[^/]+)/event-source-mappings/?$': response.event_source_mappings, r'{0}/(?P[^/]+)/event-source-mappings/(?P[\w_-]+)/?$': response.event_source_mapping, diff --git a/tests/test_awslambda/test_lambda.py b/tests/test_awslambda/test_lambda.py index d57722074..bc880f7f7 100644 --- a/tests/test_awslambda/test_lambda.py +++ b/tests/test_awslambda/test_lambda.py @@ -415,7 +415,6 @@ def test_get_function(): conn.get_function(FunctionName='junk', Qualifier='$LATEST') - @mock_lambda @mock_s3 def test_delete_function(): @@ -449,6 +448,36 @@ def test_delete_function(): success_result.should.equal({'ResponseMetadata': {'HTTPStatusCode': 204}}) + function_list = conn.list_functions() + function_list['Functions'].should.have.length_of(0) + + +@mock_lambda +@mock_s3 +def test_delete_function_by_arn(): + bucket_name = 'test-bucket' + s3_conn = boto3.client('s3', 'us-east-1') + s3_conn.create_bucket(Bucket=bucket_name) + + zip_content = get_test_zip_file2() + s3_conn.put_object(Bucket=bucket_name, Key='test.zip', Body=zip_content) + conn = boto3.client('lambda', 'us-east-1') + + fnc = conn.create_function(FunctionName='testFunction', + Runtime='python2.7', Role='test-iam-role', + Handler='lambda_function.lambda_handler', + Code={'S3Bucket': bucket_name, 'S3Key': 'test.zip'}, + Description='test lambda function', + Timeout=3, MemorySize=128, Publish=True) + + conn.delete_function(FunctionName=fnc['FunctionArn']) + function_list = conn.list_functions() + function_list['Functions'].should.have.length_of(0) + + +@mock_lambda +def test_delete_unknown_function(): + conn = boto3.client('lambda', 'us-west-2') conn.delete_function.when.called_with( FunctionName='testFunctionThatDoesntExist').should.throw(botocore.client.ClientError)