def force_delete_ledger(ledger_name):
    try:
        set_deletion_protection(ledger_name, False)
        delete_ledger(ledger_name)
        wait_for_deleted(ledger_name)
    except Exception:
        pass
예제 #2
0
def main(ledger_name=LEDGER_NAME):
    """
    Demonstrate the protection of QLDB ledgers against deletion.
    """
    try:
        create_with_deletion_protection(ledger_name)
        wait_for_active(ledger_name)
        try:
            delete_ledger(ledger_name)
        except qldb_client.exceptions.ResourcePreconditionNotMetException:
            logger.info(
                'Ledger protected against deletions! Turning off deletion protection now.'
            )
        set_deletion_protection(ledger_name, False)
        delete_ledger(ledger_name)
    except Exception as e:
        logger.exception('Error while updating or deleting the ledger!')
        raise e

def create_with_deletion_protection(ledger_name):
    """
    Create a new ledger with the specified name and with deletion protection enabled.
    :type ledger_name: str
    :param ledger_name: Name for the ledger to be created.
    :rtype: dict
    :return: Result from the request.
    """
    logger.info("Let's create the ledger with name: {}...".format(ledger_name))
    result = qldb_client.create_ledger(Name=ledger_name, PermissionsMode='ALLOW_ALL')
    logger.info('Success. Ledger state: {}'.format(result.get('State')))
    return result


if __name__ == '__main__':
    """
    Demonstrate the protection of QLDB ledgers against deletion.
    """
    try:
        create_with_deletion_protection(LEDGER_NAME)
        wait_for_active(LEDGER_NAME)
        try:
            delete_ledger(LEDGER_NAME)
        except qldb_client.exceptions.ResourcePreconditionNotMetException:
            logger.info('Ledger protected against deletions! Turning off deletion protection now.')
        set_deletion_protection(LEDGER_NAME, False)
        delete_ledger(LEDGER_NAME)
    except Exception:
        logger.exception('Error while updating or deleting the ledger!')