diff --git a/moto/greengrass/models.py b/moto/greengrass/models.py index 4d64b745817..9041abac1bf 100644 --- a/moto/greengrass/models.py +++ b/moto/greengrass/models.py @@ -487,6 +487,16 @@ def delete_function_definition(self, function_definition_id): del self.function_definitions[function_definition_id] del self.function_definition_versions[function_definition_id] + def update_function_definition(self, function_definition_id, name): + + if name == "": + raise InvalidContainerDefinitionException( + "Input does not contain any attributes to be updated" + ) + if function_definition_id not in self.function_definitions: + raise IdNotFoundException("That lambdas definition does not exist.") + self.function_definitions[function_definition_id].name = name + def create_function_definition_version( self, function_definition_id, functions, default_config ): diff --git a/moto/greengrass/responses.py b/moto/greengrass/responses.py index 554d6e3c5b5..fed4a8b3b14 100644 --- a/moto/greengrass/responses.py +++ b/moto/greengrass/responses.py @@ -298,6 +298,9 @@ def function_definition(self, request, full_url, headers): if self.method == "DELETE": return self.delete_function_definition() + if self.method == "PUT": + return self.update_function_definition() + def get_function_definition(self): function_definition_id = self.path.split("/")[-1] res = self.greengrass_backend.get_function_definition( @@ -312,6 +315,14 @@ def delete_function_definition(self): ) return 200, {"status": 200}, json.dumps({}) + def update_function_definition(self): + function_definition_id = self.path.split("/")[-1] + name = self._get_param("Name") + self.greengrass_backend.update_function_definition( + function_definition_id=function_definition_id, name=name + ) + return 200, {"status": 200}, json.dumps({}) + def function_definition_versions(self, request, full_url, headers): self.setup_class(request, full_url, headers) diff --git a/tests/test_greengrass/test_greengrass_functions.py b/tests/test_greengrass/test_greengrass_functions.py index b2037a938a8..39a614de727 100644 --- a/tests/test_greengrass/test_greengrass_functions.py +++ b/tests/test_greengrass/test_greengrass_functions.py @@ -182,6 +182,88 @@ def test_delete_function_definition_with_invalid_id(): ex.value.response["Error"]["Code"].should.equal("IdNotFoundException") +@mock_greengrass +def test_update_function_definition(): + + client = boto3.client("greengrass", region_name="ap-northeast-1") + init_ver = { + "Functions": [ + { + "FunctionArn": "arn:aws:lambda:ap-northeast-1:123456789012:function:test-func:1", + "Id": "1234567890", + "FunctionConfiguration": { + "MemorySize": 16384, + "EncodingType": "binary", + "Pinned": True, + "Timeout": 3, + }, + } + ] + } + create_res = client.create_function_definition( + InitialVersion=init_ver, Name="TestFunc" + ) + + func_def_id = create_res["Id"] + updated_func_name = "UpdatedFunction" + update_res = client.update_function_definition( + FunctionDefinitionId=func_def_id, Name=updated_func_name + ) + update_res["ResponseMetadata"]["HTTPStatusCode"].should.equal(200) + + get_res = client.get_function_definition(FunctionDefinitionId=func_def_id) + get_res.should.have.key("Name").equals(updated_func_name) + + +@mock_greengrass +def test_update_function_definition_with_empty_name(): + + client = boto3.client("greengrass", region_name="ap-northeast-1") + init_ver = { + "Functions": [ + { + "FunctionArn": "arn:aws:lambda:ap-northeast-1:123456789012:function:test-func:1", + "Id": "1234567890", + "FunctionConfiguration": { + "MemorySize": 16384, + "EncodingType": "binary", + "Pinned": True, + "Timeout": 3, + }, + } + ] + } + create_res = client.create_function_definition( + InitialVersion=init_ver, Name="TestFunc" + ) + + func_def_id = create_res["Id"] + + with pytest.raises(ClientError) as ex: + client.update_function_definition(FunctionDefinitionId=func_def_id, Name="") + ex.value.response["Error"]["Message"].should.equal( + "Input does not contain any attributes to be updated" + ) + ex.value.response["Error"]["Code"].should.equal( + "InvalidContainerDefinitionException" + ) + + +@mock_greengrass +def test_update_function_definition_with_invalid_id(): + + client = boto3.client("greengrass", region_name="ap-northeast-1") + + with pytest.raises(ClientError) as ex: + client.update_function_definition( + FunctionDefinitionId="6fbffc21-989e-4d29-a793-a42f450a78c6", Name="123" + ) + ex.value.response["Error"]["Message"].should.equal( + "That lambdas definition does not exist." + ) + ex.value.response["Error"]["Code"].should.equal("IdNotFoundException") + + @freezegun.freeze_time("2022-06-01 12:00:00") @mock_greengrass def test_create_function_definition_version():