Skip to content

Commit

Permalink
Merge pull request #2515 from Sytten/fix/delete_iot_principal_thing
Browse files Browse the repository at this point in the history
Detach principal from thing when it is deleted
  • Loading branch information
mikegrima committed Oct 24, 2019
2 parents 3834c74 + 2d5e2e9 commit 6b67002
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions moto/iot/models.py
Expand Up @@ -331,6 +331,12 @@ def delete_thing(self, thing_name, expected_version):

# can raise ResourceNotFoundError
thing = self.describe_thing(thing_name)

# detach all principals
for k in list(self.principal_things.keys()):
if k[1] == thing_name:
del self.principal_things[k]

del self.things[thing.arn]

def delete_thing_type(self, thing_type_name):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_iot/test_iot.py
Expand Up @@ -519,6 +519,25 @@ def test_principal_thing():
res.should.have.key('principals').which.should.have.length_of(0)


@mock_iot
def test_delete_principal_thing():
client = boto3.client('iot', region_name='ap-northeast-1')
thing_name = 'my-thing'
thing = client.create_thing(thingName=thing_name)
cert = client.create_keys_and_certificate(setAsActive=True)
cert_arn = cert['certificateArn']
cert_id = cert['certificateId']

client.attach_thing_principal(thingName=thing_name, principal=cert_arn)

client.delete_thing(thingName=thing_name)
res = client.list_principal_things(principal=cert_arn)
res.should.have.key('things').which.should.have.length_of(0)

client.update_certificate(certificateId=cert_id, newStatus="INACTIVE")
client.delete_certificate(certificateId=cert_id)


@mock_iot
def test_thing_groups():
client = boto3.client('iot', region_name='ap-northeast-1')
Expand Down

0 comments on commit 6b67002

Please sign in to comment.