in source/lambda/iot-dr-layer/device_replication.py [0:0]
def delete_thing(c_iot, thing_name, iot_data_endpoint):
logger.info('delete_thing: thing_name: {} iot_data_endpoint: {}'.format(
thing_name, iot_data_endpoint
)
)
try:
if not thing_exists(c_iot, thing_name):
logger.warning('delete_thing: thing does not exist: {}'.format(thing_name))
return
r_principals = c_iot.list_thing_principals(thingName=thing_name)
logger.info('thing_name: {} principals: {}'.format(thing_name, r_principals['principals']))
for arn in r_principals['principals']:
cert_id = arn.split('/')[-1]
logger.info(
'detach_thing_principal: thing_name: {} principal arn: {} cert_id: {}'.format(
thing_name, arn, cert_id
)
)
r_detach_thing = c_iot.detach_thing_principal(thingName=thing_name, principal=arn)
detach_thing_principal_status_code = \
r_detach_thing['ResponseMetadata']['HTTPStatusCode']
logger.info(
'thing_name: {} arn: {} detach_thing_principal_status_code: {} \
response detach_thing_principal: {}'.format(
thing_name, arn, detach_thing_principal_status_code, r_detach_thing
)
)
if detach_thing_principal_status_code != 200:
error_message = 'thing_name: {} arn: {} \
detach_thing_principal_status_code not equal 200: {} '.format(
thing_name, arn, detach_thing_principal_status_code
)
logger.error(error_message)
raise Exception(error_message)
# still things attached to the principal?
# If yes, don't deactivate cert or detach policies
things = get_principal_things(c_iot, arn)
if things:
logger.info(
'still things {} attached to principal {} - \
certificate will not be inactvated, policies will not be removed'.format(
things, arn
)
)
else:
logger.info('inactivate cert: thing_name: {} cert_id: {}'.format(
thing_name, cert_id))
r_upd_cert = c_iot.update_certificate(certificateId=cert_id,newStatus='INACTIVE')
logger.info('update_certificate: cert_id: {} response: {}'.format(
cert_id, r_upd_cert))
r_policies = c_iot.list_principal_policies(principal=arn)
logger.info('cert arn: {} policies: {}'.format(arn, r_policies['policies']))
for policy in r_policies['policies']:
policy_name = policy['policyName']
logger.info('detaching policy policy_name: {}'.format(policy_name))
r_detach_pol = c_iot.detach_policy(policyName=policy_name,target=arn)
logger.info(
'detach_policy: policy_name: {} response: {}'.format(
policy_name, r_detach_pol
)
)
delete_policy(c_iot, policy_name)
r_del_cert = c_iot.delete_certificate(certificateId=cert_id,forceDelete=True)
logger.info('delete_certificate: cert_id: {} response: {}'.format(
cert_id, r_del_cert))
r_del_thing = c_iot.delete_thing(thingName=thing_name)
logger.info('delete_thing: thing_name: {} response: {}'.format(thing_name, r_del_thing))
delete_shadow(thing_name, iot_data_endpoint)
except Exception as e:
logger.error('delete_thing: thing_name: {}: {}'.format(thing_name, e))
raise DeviceReplicationDeleteThingException(e)