Skip to content

Commit

Permalink
Greengrass Implement update_function_definition
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-iwata committed Jun 16, 2022
1 parent 915e1c2 commit 49c2fea
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
10 changes: 10 additions & 0 deletions moto/greengrass/models.py
Expand Up @@ -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
):
Expand Down
11 changes: 11 additions & 0 deletions moto/greengrass/responses.py
Expand Up @@ -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(
Expand All @@ -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)

Expand Down
82 changes: 82 additions & 0 deletions tests/test_greengrass/test_greengrass_functions.py
Expand Up @@ -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():
Expand Down

0 comments on commit 49c2fea

Please sign in to comment.