in utils/ci_iot_thing.py [0:0]
def delete_iot_thing(thing_name, region):
""" Delete IoT thing and all its principals. """
try:
iot_client = boto3.client('iot', region_name=region)
except Exception as e:
print(f"ERROR: Could not make Boto3 client. Credentials likely could not be sourced", file=sys.stderr)
raise
cert_ids = []
# Detach and delete thing's principals.
try:
thing_principals = iot_client.list_thing_principals(thingName=thing_name)
print(f"Detaching and deleting principals: {thing_principals}", file=sys.stderr)
for principal in thing_principals["principals"]:
certificate_id = principal.split("/")[1]
iot_client.detach_thing_principal(thingName=thing_name, principal=principal)
iot_client.update_certificate(certificateId=certificate_id, newStatus='INACTIVE')
cert_ids.append(certificate_id)
except Exception:
print(f"ERROR: Could not detatch principal or set its certificate to INACTIVE for {thing_name}, probably thing does not exist",
file=sys.stderr)
raise
# Wait for thing to be free of principals
ThingDetachedWaiter(iot_client, timeout=10).wait(thing_name)
# Delete all the certificates
for cert in cert_ids:
try:
iot_client.delete_certificate(certificateId=cert, forceDelete=True)
except Exception:
print(f"ERROR: Could not delete certificate for IoT thing {thing_name}.",
file=sys.stderr)
raise
# Delete thing.
try:
iot_client.delete_thing(thingName=thing_name)
except Exception:
raise
print("IoT thing deleted successfully", file=sys.stderr)
return 0