awslambda: create_function()
should fail on duplicate (#6626)
This commit is contained in:
parent
824f22a794
commit
b71723b99a
@ -14,6 +14,14 @@ class CrossAccountNotAllowed(LambdaClientError):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class FunctionAlreadyExists(LambdaClientError):
|
||||||
|
code = 409
|
||||||
|
|
||||||
|
def __init__(self, function_name: str) -> None:
|
||||||
|
message = f"Function already exist: {function_name}"
|
||||||
|
super().__init__("ResourceConflictException", message)
|
||||||
|
|
||||||
|
|
||||||
class InvalidParameterValueException(LambdaClientError):
|
class InvalidParameterValueException(LambdaClientError):
|
||||||
def __init__(self, message: str):
|
def __init__(self, message: str):
|
||||||
super().__init__("InvalidParameterValueException", message)
|
super().__init__("InvalidParameterValueException", message)
|
||||||
|
@ -6,6 +6,7 @@ from urllib.parse import unquote
|
|||||||
from moto.core.utils import path_url
|
from moto.core.utils import path_url
|
||||||
from moto.utilities.aws_headers import amz_crc32, amzn_request_id
|
from moto.utilities.aws_headers import amz_crc32, amzn_request_id
|
||||||
from moto.core.responses import BaseResponse, TYPE_RESPONSE
|
from moto.core.responses import BaseResponse, TYPE_RESPONSE
|
||||||
|
from .exceptions import FunctionAlreadyExists, UnknownFunctionException
|
||||||
from .models import lambda_backends, LambdaBackend
|
from .models import lambda_backends, LambdaBackend
|
||||||
|
|
||||||
|
|
||||||
@ -316,9 +317,14 @@ class LambdaResponse(BaseResponse):
|
|||||||
return 200, {}, json.dumps(result)
|
return 200, {}, json.dumps(result)
|
||||||
|
|
||||||
def _create_function(self) -> TYPE_RESPONSE:
|
def _create_function(self) -> TYPE_RESPONSE:
|
||||||
|
function_name = self.json_body["FunctionName"].rsplit(":", 1)[-1]
|
||||||
|
try:
|
||||||
|
self.backend.get_function(function_name, None)
|
||||||
|
except UnknownFunctionException:
|
||||||
fn = self.backend.create_function(self.json_body)
|
fn = self.backend.create_function(self.json_body)
|
||||||
config = fn.get_configuration(on_create=True)
|
config = fn.get_configuration(on_create=True)
|
||||||
return 201, {}, json.dumps(config)
|
return 201, {}, json.dumps(config)
|
||||||
|
raise FunctionAlreadyExists(function_name)
|
||||||
|
|
||||||
def _create_function_url_config(self) -> TYPE_RESPONSE:
|
def _create_function_url_config(self) -> TYPE_RESPONSE:
|
||||||
function_name = unquote(self.path.split("/")[-2])
|
function_name = unquote(self.path.split("/")[-2])
|
||||||
|
@ -1236,7 +1236,8 @@ def test_create_function_with_already_exists():
|
|||||||
Publish=True,
|
Publish=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
response = conn.create_function(
|
with pytest.raises(ClientError) as exc:
|
||||||
|
conn.create_function(
|
||||||
FunctionName=function_name,
|
FunctionName=function_name,
|
||||||
Runtime="python2.7",
|
Runtime="python2.7",
|
||||||
Role=get_role_name(),
|
Role=get_role_name(),
|
||||||
@ -1248,7 +1249,7 @@ def test_create_function_with_already_exists():
|
|||||||
Publish=True,
|
Publish=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response["FunctionName"] == function_name
|
assert exc.value.response["Error"]["Code"] == "ResourceConflictException"
|
||||||
|
|
||||||
|
|
||||||
@mock_lambda
|
@mock_lambda
|
||||||
|
@ -115,6 +115,7 @@ def test_get_lambda_layers():
|
|||||||
assert result["LayerVersions"] == []
|
assert result["LayerVersions"] == []
|
||||||
|
|
||||||
# Test create function with non existant layer version
|
# Test create function with non existant layer version
|
||||||
|
function_name = str(uuid4())[0:6] # Must be different than above
|
||||||
with pytest.raises(ClientError) as exc:
|
with pytest.raises(ClientError) as exc:
|
||||||
conn.create_function(
|
conn.create_function(
|
||||||
FunctionName=function_name,
|
FunctionName=function_name,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user