def create_export(ledger_name, start_time, end_time, s3_bucket_name, s3_prefix, encryption_configuration, role_arn): """ Request QLDB to export the contents of the journal for the given time period and S3 configuration. Before calling this function the S3 bucket should be created, see :py:meth:`hash_chain.ledger.export_journal.create_s3_bucket_if_not_exists` :type ledger_name: str :param ledger_name: Name of the ledger. :type start_time: :py:class:`datetime.datetime` :param start_time: Time from when the journal contents should be exported. :type end_time: :py:class:`datetime.datetime` :param end_time: Time until which the journal contents should be exported. :type s3_bucket_name: str :param s3_bucket_name: S3 bucket to write the data to. :type s3_prefix: str :param s3_prefix: S3 prefix to be prefixed to the files written. :type encryption_configuration: dict :param encryption_configuration: Encryption configuration for S3. :type role_arn: str :param role_arn: The IAM role ARN to be used when exporting the journal. :rtype: dict :return: The result of the request. """ logger.info( "Let's create a journal export for ledger with name: {}.".format( ledger_name)) try: result = qldb.client().export_journal_to_s3( Name=ledger_name, InclusiveStartTime=start_time, ExclusiveEndTime=end_time, S3ExportConfiguration={ 'Bucket': s3_bucket_name, 'Prefix': s3_prefix, 'EncryptionConfiguration': encryption_configuration }, RoleArn=role_arn) logger.info("Requested QLDB to export contents of the journal.") return result except qldb.client().exceptions.InvalidParameterException as ipe: logger.error( "The eventually consistent behavior of the IAM service may cause this export to fail its first" " attempts, please retry.") raise ipe
def get_block_with_proof(ledger_name, block_address, digest_tip_address): """ Get the block of a ledger's journal. Also returns a proof of the block for verification. :type ledger_name: str :param ledger_name: Name of the ledger to operate on. :type block_address: dict :param block_address: The location of the block to request. :type digest_tip_address: dict :param digest_tip_address: The location of the digest tip. :rtype: dict :return: The response of the request. """ logger.info( "Let's get the block for block address {}, digest tip address {}, for the ledger named {}." .format(block_address, digest_tip_address, ledger_name)) result = qldb.client().get_block(Name=ledger_name, BlockAddress=block_address, DigestTipAddress=digest_tip_address) logger.info('Success. GetBlock: {}.'.format( block_response_to_string(result))) return result
def ledger_list(): if DdlServices.ledgers: logger.info('Loaded from cash: {}.'.format(DdlServices.ledgers)) return DdlServices.ledgers result = qldb.client().list_ledgers() DdlServices.ledgers = result.get('Ledgers') return DdlServices.ledgers
def wait_for_deleted(ledger_name): logger.info('Waiting for the ledger to be deleted...') while True: try: describe_ledger(ledger_name) logger.info('The ledger is still being deleted. Please wait...') sleep(config.LEDGER_DELETION_POLL_PERIOD_SEC) except qldb.client().exceptions.ResourceNotFoundException: logger.info('Success. The ledger is deleted.') break
def ledger_exist(self): """ Returns information about a ledger, including its state and when it was created. """ try: logger.info("Let's describe ledger...{}".format(self.ledger_name)) ledger = qldb.client().describe_ledger(Name=self.ledger_name) logger.info('Success. describe ledger...{}.'.format(ledger)) return ledger except Exception as e: logger.exception('Unable to list ledgers!') return None
def describe_ledger(ledger_name): """ Returns information about a ledger, including its state and when it was created. """ try: logger.info("Let's describe ledger...{}".format(ledger_name)) ledger = qldb.client().describe_ledger(Name=ledger_name) logger.info('Success. describe ledger...{}.'.format(ledger)) return ledger except Exception as e: logger.exception('Unable to list ledgers!') return fail_response('Unable to list ledgers!. Please try again.', HTTPStatus.UNPROCESSABLE_ENTITY)
def get_digest_result(ledger_name=config.LEDGER_NAME): """ Get the digest of a ledger's journal. :type name: str :param name: Name of the ledger to operate on. :rtype: dict :return: The digest in a 256-bit hash value and a block address. """ logger.info("Let's get the current digest of the ledger named {}".format( ledger_name)) result = qldb.client().get_digest(Name=ledger_name) logger.info('Success. LedgerDigest: {}.'.format( digest_response_to_string(result))) return result
def set_deletion_protection(ledger_name, deletion_protection): """ Update an existing ledger's deletion protection. :type ledger_name: str :param ledger_name: Name of the ledger to update. :type deletion_protection: bool :param deletion_protection: Enable or disable the deletion protection. :rtype: dict :return: Result from the request. """ logger.info( "Let's set deletion protection to {} for the ledger with name {}.". format(deletion_protection, ledger_name)) result = qldb.client().update_ledger( Name=ledger_name, DeletionProtection=deletion_protection) logger.info('Success. Ledger updated: {}'.format(result))
def get_block(ledger_name, block_address): """ Get the block of a ledger's journal. :type ledger_name: str :param ledger_name: Name of the ledger to operate on. :type block_address: dict :param block_address: The location of the block to request. :rtype: dict :return: The response of the request. """ logger.info( "Let's get the block for block address {} of the ledger named {}.". format(block_address, ledger_name)) result = qldb.client().get_block(Name=ledger_name, BlockAddress=block_address) logger.info('Success. GetBlock: {}'.format( block_response_to_string(result))) return result
def describe_journal_export(ledger_name, export_id): """ Describe a journal export. :type ledger_name: str :param ledger_name: The ledger from which the journal is being exported. :type export_id: str :param export_id: The ExportId of the journal. :rtype: dict :return: Result from the request. """ logger.info( "Let's describe a journal export for ledger with name: {}, exportId: {}." .format(ledger_name, export_id)) export_result = qldb.client().describe_journal_s3_export( Name=ledger_name, ExportId=export_id) logger.info('Export described. Result = {}.'.format( export_result['ExportDescription'])) return export_result
def get_revision(ledger_name, document_id, block_address, digest_tip_address): """ Get the revision data object for a specified document ID and block address. Also returns a proof of the specified revision for verification. :type ledger_name: str :param ledger_name: Name of the ledger containing the document to query. :type document_id: str :param document_id: Unique ID for the document to be verified, contained in the committed view of the document. :type block_address: dict :param block_address: The location of the block to request. :type digest_tip_address: dict :param digest_tip_address: The latest block location covered by the digest. :rtype: dict :return: The response of the request. """ result = qldb.client().get_revision(Name=ledger_name, BlockAddress=block_address, DocumentId=document_id, DigestTipAddress=digest_tip_address) return result
def describe_ledger(ledger_name): logger.info('describe ledger with name: {}.'.format(ledger_name)) result = qldb.client().describe_ledger(Name=ledger_name) result.pop('ResponseMetadata') logger.info('Success. Ledger description: {}.'.format(result)) return result
def do_delete_ledger(ledger_name): logger.info('Attempting to delete the ledger with name: {}...'.format( ledger_name)) result = qldb.client().delete_ledger(Name=ledger_name) logger.info('Success.') return result
def do_create_ledger(name): logger.info("Let's create the ledger named: {}...".format(name)) result = qldb.client().create_ledger(Name=name, PermissionsMode='ALLOW_ALL') logger.info('Success. Ledger state: {}.'.format(result.get('State'))) return result