def sync_thing()

in source/lambda/iot-dr-region-syncer/iot-region-to-region-syncer.py [0:0]


def sync_thing(c_iot_p, c_iot_s, thing):
    global NUM_THINGS_SYNCED, NUM_THINGS_EXIST, NUM_ERRORS
    try:
        logger.info('thing: {}'.format(thing))
        start_time = int(time.time()*1000)
        thing_name = thing['thingName']

        if SYNC_MODE == "smart":
            if thing_exists(c_iot_s, thing_name):
                logger.info('thing_name {} exists already in secondary region {}'.format(thing_name, SECONDARY_REGION))
                NUM_THINGS_EXIST += 1
                return

        thing_type_name = ""
        if 'thingTypeName' in thing:
            thing_type_name = thing['thingTypeName']

        attrs = {}
        if 'attributes' in thing:
            attrs = {'attributes': {}}
            for key in thing['attributes']:
                attrs['attributes'][key] = thing['attributes'][key]

        if 'attributes' in attrs:
            attrs['merge'] = False

        logger.info('thing_name: {} thing_type_name: {} attrs: {}'.format(thing_name, thing_type_name, attrs))

        create_thing_with_cert_and_policy(c_iot_s, c_iot_p, thing_name, thing_type_name, attrs, 2, 1)
        end_time = int(time.time()*1000)
        duration = end_time - start_time
        NUM_THINGS_SYNCED += 1
        logger.info('sync thing: thing_name: {} duration: {}ms'.format(thing_name, duration))
    except Exception as e:
        logger.error('{}'.format(e))
        NUM_ERRORS += 1
        traceback.print_stack()