示例#1
0
def get_slr_for_service(service_id=None, slr_id=None, cursor=None):
    """
    Get one slr entry from database by Service ID and Service Link Record ID
    :param account_id:
    :param slr_id:
    :return: dict
    """
    if service_id is None:
        raise AttributeError("Provide service_id as parameter")
    if id is None:
        raise AttributeError("Provide id as parameter")
    if cursor is None:
        # Get DB cursor
        try:
            cursor = get_db_cursor()
        except Exception as exp:
            logger.error('Could not get database cursor: ' + repr(exp))
            raise

    try:
        db_entry_object = ServiceLinkRecord(service_id=service_id,
                                            service_link_record_id=slr_id)
    except Exception as exp:
        error_title = "Failed to create slr object"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("slr object created: " + db_entry_object.log_entry)

    # Get slr from DB
    try:
        cursor = db_entry_object.from_db(cursor=cursor)
    except Exception as exp:
        error_title = "Failed to fetch slr from DB"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.info("slr fetched")
        logger.info("slr fetched from db: " + db_entry_object.log_entry)

    return db_entry_object.to_api_dict, str(db_entry_object.account_id)
示例#2
0
def get_auth_token_data(sink_cr_object=None, endpoint="get_auth_token_data()"):
    if sink_cr_object is None:
        raise AttributeError("Provide sink_cr_object as parameter")

    # Get DB cursor
    try:
        cursor = get_db_cursor()
    except Exception as exp:
        logger.error('Could not get database cursor: ' + repr(exp))
        raise

    # Get Sink's CR from DB
    try:
        logger.info("Get Sink's CR from DB")
        cursor = sink_cr_object.from_db(cursor=cursor)
    except Exception as exp:
        error_title = "Failed to fetch Sink's CR from DB"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("sink_cr_object: " + sink_cr_object.log_entry)

    # Get required id's from Sink's CR
    try:
        logger.info("Get required id's from Sink's CR")
        sink_slr_primary_key = str(sink_cr_object.service_link_records_id)
    except Exception as exp:
        error_title = "Failed to get id's from Sink's CR"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("sink_slr_primary_key: " + sink_slr_primary_key)

    # Init Source's Consent Record Object
    try:
        logger.info("Init Source's Consent Record Object")
        source_cr_entry = ConsentRecord(
            consent_pair_id=sink_cr_object.consent_id,
            accounts_id=sink_cr_object.accounts_id,
            role="Source")
    except Exception as exp:
        error_title = "Failed to create Source's Consent Record object"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("source_cr_entry: " + source_cr_entry.log_entry)

    # Get Source's Consent Record from DB
    try:
        logger.info("Get Source's Consent Record from DB")
        cursor = source_cr_entry.from_db(cursor=cursor)
    except Exception as exp:
        error_title = "Failed to fetch Source's CR from DB"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("source_cr_entry: " + source_cr_entry.log_entry)

    # Init Sink's Service Link Record Object
    try:
        logger.info("Init Sink's Service Link Record Object")
        sink_slr_entry = ServiceLinkRecord(
            id=sink_slr_primary_key,
            service_link_record_id=sink_cr_object.service_link_record_id)
    except Exception as exp:
        error_title = "Failed to create Source's Service Link Record object"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("sink_slr_entry: " + sink_slr_entry.log_entry)

    # Get Source's Consent Record from DB
    try:
        logger.info("Get Source's Consent Record from DB")
        cursor = sink_slr_entry.from_db(cursor=cursor)
    except Exception as exp:
        error_title = "Failed to fetch Sink's SLR from DB"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("sink_slr_entry: " + sink_slr_entry.log_entry)

    return source_cr_entry, sink_slr_entry
示例#3
0
def get_last_slr_status(account_id=None,
                        slr_id=None,
                        endpoint="get_last_slr_status()"):
    if slr_id is None:
        raise AttributeError("Provide slr_id as parameter")
    if account_id is None:
        raise AttributeError("Provide account_id as parameter")

    # Get DB cursor
    try:
        cursor = get_db_cursor()
    except Exception as exp:
        logger.error('Could not get database cursor: ' + repr(exp))
        raise ApiError(code=500,
                       title="Failed to get database cursor",
                       detail=repr(exp),
                       source=endpoint)

    # Init ServiceLinkRecord Object
    try:
        logger.info("Create ServiceLinkRecord object")
        slr_entry = ServiceLinkRecord(service_link_record_id=slr_id,
                                      account_id=account_id)
        logger.info(slr_entry.log_entry)
    except Exception as exp:
        error_title = "Failed to create Service Link Record object"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    else:
        logger.debug("slr_entry: " + slr_entry.log_entry)

    # Get ServiceLinkRecord from DB
    try:
        cursor = slr_entry.from_db(cursor=cursor)
    except IndexError as exp:
        error_title = "Service Link Status Record not found with provided information."
        error_detail = "Account ID was {} and Service Link Record ID was {}.".format(
            account_id, slr_id)
        logger.error(error_title + " " + error_detail + ": " + repr(exp))
        raise ApiError(code=404,
                       title=error_title,
                       detail=error_detail,
                       source=endpoint)
    except Exception as exp:
        error_title = "Failed to fetch Service Link Record from DB"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=str(exp.message),
                       source=endpoint)
    else:
        logger.debug("slr_entry: " + slr_entry.log_entry)

    # Create ServiceLinkStatusRecord object
    try:
        slsr_entry = ServiceLinkStatusRecord()
    except Exception as exp:
        error_title = "Failed to create ServiceLinkStatusRecord object"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    else:
        logger.debug("slsr_entry: " + slsr_entry.log_entry)

    # Get database table name for ServiceLinkStatusRecord
    try:
        logger.info("Get ServiceLinkStatusRecord table name")
        slsr_table_name = slsr_entry.table_name
    except Exception as exp:
        error_title = "Failed to get ServiceLinkStatusRecord table name"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    else:
        logger.info("Got ServiceLinkStatusRecord table name: " +
                    str(slsr_table_name))

    # Get ServiceLinkStatusRecord ID
    try:
        cursor, slsr_id = get_last_slsr_id(cursor=cursor,
                                           slr_id=slr_id,
                                           table_name=slsr_table_name)
    except IndexError as exp:
        error_title = "ServiceLinkStatusRecord not found from DB with given Consent Record ID"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=404,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    except Exception as exp:
        error_title = "Failed to get last ServiceLinkStatusRecord ID from database"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    else:
        logger.debug("slsr_id: " + str(slsr_id))

    # Append ID to ServiceLinkStatusRecord Object
    try:
        logger.info("Append ID to ServiceLinkStatusRecord object: " +
                    slsr_entry.log_entry)
        slsr_entry.service_link_status_record_id = slsr_id
    except Exception as exp:
        error_title = "Failed to append ID to ServiceLinkStatusRecord object"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    else:
        logger.info("Appended ID to ServiceLinkStatusRecord object: " +
                    slsr_entry.log_entry)

    # Get ServiceLinkStatusRecord from DB
    try:
        cursor = slsr_entry.from_db(cursor=cursor)
    except IndexError as exp:
        error_title = "ServiceLinkStatusRecord not found from DB with given ID"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=404,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    except Exception as exp:
        error_title = "Failed to fetch ServiceLinkStatusRecord from DB"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    else:
        logger.debug("slsr_entry: " + slsr_entry.log_entry)

    return slsr_entry.to_api_dict
示例#4
0
def get_slr_record(account_id=None, slr_id=None, endpoint="get_slr_record()"):

    logger.info("get_slr_record()")

    if account_id is None:
        raise AttributeError("Provide account_id as parameter")
    if slr_id is None:
        raise AttributeError("Provide slr_id as parameter")

    if not isinstance(account_id, str):
        try:
            account_id = str(account_id)
        except Exception:
            raise TypeError("account_id MUST be str, not " +
                            str(type(account_id)))
    if not isinstance(slr_id, str):
        try:
            slr_id = str(slr_id)
        except Exception:
            raise TypeError("slr_id MUST be str, not " + str(type(slr_id)))
    if not isinstance(endpoint, str):
        try:
            endpoint = str(endpoint)
        except Exception:
            raise TypeError("endpoint MUST be str, not " + str(type(endpoint)))

    logger.info("Creating ServiceLinkRecord object")
    try:
        slr_entry = ServiceLinkRecord(service_link_record_id=slr_id,
                                      account_id=account_id)
    except Exception as exp:
        logger.error('Could not create Service Link Record object: ' +
                     repr(exp))
        raise ApiError(code=500,
                       title="Failed to create Service Link Record object",
                       detail=repr(exp),
                       source=endpoint)
    else:
        logger.info("Service Link Record entry created")
        logger.debug(slr_entry.log_entry)

    # Get DB cursor
    try:
        cursor = get_db_cursor()
    except Exception as exp:
        logger.error('Could not get database cursor: ' + repr(exp))
        raise ApiError(code=500,
                       title="Failed to get database cursor",
                       detail=repr(exp),
                       source=endpoint)

    logger.info("Get ServiceLinkRecord from database")
    try:
        cursor = slr_entry.from_db(cursor=cursor)
    except Exception as exp:
        error_title = "Could not get ServiceLinkRecord from database"
        error_detail = str(exp.message)
        logger.error(error_title + " - " + error_detail)
        raise
    else:
        logger.info('Got ServiceLinkRecord from database')
        logger.debug("slr_entry: " + slr_entry.log_entry)
        return slr_entry
示例#5
0
def get_auth_token_data(sink_cr_object=None, endpoint="get_auth_token_data()"):
    if sink_cr_object is None:
        raise AttributeError("Provide sink_cr_object as parameter")

    # Get DB cursor
    try:
        cursor = get_db_cursor()
    except Exception as exp:
        logger.error('Could not get database cursor: ' + repr(exp))
        raise ApiError(code=500,
                       title="Failed to get database cursor",
                       detail=repr(exp),
                       source=endpoint)

    # Get Sink's CR from DB
    try:
        cursor = sink_cr_object.from_db(cursor=cursor)
    except Exception as exp:
        error_title = "Failed to fetch Sink's CR from DB"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=404,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    finally:
        logger.debug("sink_cr_object: " + sink_cr_object.log_entry)

    # Get required id's from Sink's CR
    try:
        sink_rs_id = str(sink_cr_object.resource_set_id)
        sink_slr_primary_key = str(sink_cr_object.service_link_records_id)
    except Exception as exp:
        error_title = "Failed to get id's from Sink's CR"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    finally:
        logger.debug("sink_rs_id: " + sink_rs_id)

    # Init Source's Consent Record Object
    try:
        source_cr_entry = ConsentRecord(resource_set_id=sink_rs_id,
                                        role="Source")
    except Exception as exp:
        error_title = "Failed to create Source's Consent Record object"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    finally:
        logger.debug("source_cr_entry: " + source_cr_entry.log_entry)

    # Get Source's Consent Record from DB
    source_cr = {}
    try:
        cursor = source_cr_entry.from_db(cursor=cursor)
        source_cr = source_cr_entry.consent_record
    except Exception as exp:
        error_title = "Failed to fetch Source's CR from DB"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    finally:
        logger.debug("source_cr_entry: " + source_cr_entry.log_entry)
        logger.debug("source_cr: " + json.dumps(source_cr))

    # Init Sink's Service Link Record Object
    try:
        sink_slr_entry = ServiceLinkRecord(id=sink_slr_primary_key)
    except Exception as exp:
        error_title = "Failed to create Source's Service Link Record object"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    finally:
        logger.debug("source_cr_entry: " + source_cr_entry.log_entry)

    # Get Source's Consent Record from DB
    sink_slr = {}
    try:
        cursor = sink_slr_entry.from_db(cursor=cursor)
        sink_slr = sink_slr_entry.service_link_record
    except Exception as exp:
        error_title = "Failed to fetch Sink's SLR from DB"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    finally:
        logger.debug("sink_slr_entry: " + sink_slr_entry.log_entry)
        logger.debug("sink_slr: " + json.dumps(sink_slr))

    return source_cr, json.loads(sink_slr)