From 2f5a702f1fa3e809e6c9ca4f55e764ac42b739a2 Mon Sep 17 00:00:00 2001 From: Ariel Beck <33662254+arielb135@users.noreply.github.com> Date: Tue, 17 Aug 2021 20:05:18 +0300 Subject: [PATCH] added deprecate_thing_type method for IOT (#4184) Co-authored-by: arielb --- moto/iot/models.py | 9 +++++++++ moto/iot/responses.py | 8 ++++++++ tests/test_iot/test_iot.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/moto/iot/models.py b/moto/iot/models.py index c1349224f..706582ac9 100644 --- a/moto/iot/models.py +++ b/moto/iot/models.py @@ -629,6 +629,15 @@ class IoTBackend(BaseBackend): thing_type = self.describe_thing_type(thing_type_name) del self.thing_types[thing_type.arn] + def deprecate_thing_type(self, thing_type_name, undo_deprecate): + thing_types = [ + _ for _ in self.thing_types.values() if _.thing_type_name == thing_type_name + ] + if len(thing_types) == 0: + raise ResourceNotFoundException() + thing_types[0].metadata["deprecated"] = not undo_deprecate + return thing_types[0] + def update_thing( self, thing_name, diff --git a/moto/iot/responses.py b/moto/iot/responses.py index 2d7f636c0..572cfd99d 100644 --- a/moto/iot/responses.py +++ b/moto/iot/responses.py @@ -106,6 +106,14 @@ class IoTResponse(BaseResponse): self.iot_backend.delete_thing_type(thing_type_name=thing_type_name) return json.dumps(dict()) + def deprecate_thing_type(self): + thing_type_name = self._get_param("thingTypeName") + undo_deprecate = self._get_param("undoDeprecate") + thing_type = self.iot_backend.deprecate_thing_type( + thing_type_name=thing_type_name, undo_deprecate=undo_deprecate + ) + return json.dumps(thing_type.to_dict()) + def update_thing(self): thing_name = self._get_param("thingName") thing_type_name = self._get_param("thingTypeName") diff --git a/tests/test_iot/test_iot.py b/tests/test_iot/test_iot.py index 1aa955313..ac32dc3af 100644 --- a/tests/test_iot/test_iot.py +++ b/tests/test_iot/test_iot.py @@ -2210,3 +2210,33 @@ class TestTopicRules: res = client.list_topic_rules() res.should.have.key("rules").which.should.have.length_of(0) + + @mock_iot + def test_deprecate_undeprecate_thing_type(self): + client = boto3.client("iot", region_name="ap-northeast-1") + thing_type_name = "my-type-name" + client.create_thing_type( + thingTypeName=thing_type_name, + thingTypeProperties={"searchableAttributes": ["s1", "s2", "s3"]}, + ) + + res = client.describe_thing_type(thingTypeName=thing_type_name) + res["thingTypeMetadata"]["deprecated"].should.equal(False) + client.deprecate_thing_type(thingTypeName=thing_type_name, undoDeprecate=False) + + res = client.describe_thing_type(thingTypeName=thing_type_name) + res["thingTypeMetadata"]["deprecated"].should.equal(True) + + client.deprecate_thing_type(thingTypeName=thing_type_name, undoDeprecate=True) + + res = client.describe_thing_type(thingTypeName=thing_type_name) + res["thingTypeMetadata"]["deprecated"].should.equal(False) + + @mock_iot + def test_deprecate_thing_type_not_exist(self): + client = boto3.client("iot", region_name="ap-northeast-1") + thing_type_name = "my-type-name" + with pytest.raises(client.exceptions.ResourceNotFoundException): + client.deprecate_thing_type( + thingTypeName=thing_type_name, undoDeprecate=False + )