def create_baseline(system_baseline_in): """ create a baseline """ ensure_rbac_write() account_number = view_helpers.get_account_number(request) if "values" in system_baseline_in and "value" in system_baseline_in: message = "'values' and 'value' cannot both be defined for system baseline" current_app.logger.audit(message, request=request, success=False) raise HTTPError( HTTPStatus.BAD_REQUEST, message=message, ) _check_for_existing_display_name(system_baseline_in["display_name"], account_number) _check_for_whitespace_in_display_name(system_baseline_in["display_name"]) message = "counted baselines" current_app.logger.audit(message, request=request) baseline_facts = [] if "baseline_facts" in system_baseline_in: if "inventory_uuid" in system_baseline_in: message = ( "Both baseline facts and inventory id provided, can clone only one." ) current_app.logger.audit(message, request=request, success=False) raise HTTPError( HTTPStatus.BAD_REQUEST, message=message, ) if "hsp_uuid" in system_baseline_in: message = "Both baseline facts and hsp id provided, can clone only one." current_app.logger.audit(message, request=request, success=False) raise HTTPError( HTTPStatus.BAD_REQUEST, message=message, ) baseline_facts = system_baseline_in["baseline_facts"] elif "hsp_uuid" in system_baseline_in: if "inventory_uuid" in system_baseline_in: message = "Both hsp id and system id provided, can clone only one." current_app.logger.audit(message, request=request, success=False) raise HTTPError( HTTPStatus.BAD_REQUEST, message=message, ) validate_uuids([system_baseline_in["hsp_uuid"]]) auth_key = get_key_from_headers(request.headers) try: hsp = fetch_historical_sys_profiles( [system_baseline_in["hsp_uuid"]], auth_key, current_app.logger, get_event_counters(), )[0] message = "read historical system profiles" current_app.logger.audit(message, request=request) except ItemNotReturned: message = "hsp UUID %s not available" % system_baseline_in[ "hsp_uuid"] current_app.logger.audit(message, request=request, success=False) raise HTTPError( HTTPStatus.NOT_FOUND, message=message, ) except RBACDenied as error: message = error.message current_app.logger.audit(message, request=request, success=False) raise HTTPError(HTTPStatus.FORBIDDEN, message=message) system_name = "clone_from_hsp_unused" baseline_facts = _parse_from_sysprofile(hsp["system_profile"], system_name, current_app.logger) elif "inventory_uuid" in system_baseline_in: validate_uuids([system_baseline_in["inventory_uuid"]]) auth_key = get_key_from_headers(request.headers) try: system_with_profile = fetch_systems_with_profiles( [system_baseline_in["inventory_uuid"]], auth_key, current_app.logger, get_event_counters(), )[0] message = "read system with profiles" current_app.logger.audit(message, request=request) except ItemNotReturned: message = ("inventory UUID %s not available" % system_baseline_in["inventory_uuid"]) current_app.logger.audit(message, request=request, success=False) raise HTTPError( HTTPStatus.NOT_FOUND, message=message, ) except RBACDenied as error: message = error.message current_app.logger.audit(message, request=request, success=False) raise HTTPError(HTTPStatus.FORBIDDEN, message=message) system_name = profile_parser.get_name(system_with_profile) baseline_facts = _parse_from_sysprofile( system_with_profile["system_profile"], system_name, current_app.logger) try: _validate_facts(baseline_facts) except FactValidationError as error: message = error.message current_app.logger.audit(message, request=request, success=False) raise HTTPError(HTTPStatus.BAD_REQUEST, message=message) baseline = SystemBaseline( account=account_number, display_name=system_baseline_in["display_name"], baseline_facts=baseline_facts, ) baseline.baseline_facts = _sort_baseline_facts(baseline.baseline_facts) db.session.add(baseline) db.session.commit( ) # commit now so we get a created/updated time before json conversion message = "creat baselines" current_app.logger.audit(message, request=request) return baseline.to_json()
def create_baseline(system_baseline_in): """ create a baseline """ account_number = view_helpers.get_account_number(request) if "values" in system_baseline_in and "value" in system_baseline_in: raise HTTPError( HTTPStatus.BAD_REQUEST, message= "'values' and 'value' cannot both be defined for system baseline", ) _check_for_existing_display_name(system_baseline_in["display_name"], account_number) baseline_facts = [] if "baseline_facts" in system_baseline_in: baseline_facts = system_baseline_in["baseline_facts"] elif "inventory_uuid" in system_baseline_in: _validate_uuids([system_baseline_in["inventory_uuid"]]) auth_key = get_key_from_headers(request.headers) try: system_with_profile = fetch_systems_with_profiles( [system_baseline_in["inventory_uuid"]], auth_key, current_app.logger, get_event_counters(), )[0] except ItemNotReturned: raise HTTPError( HTTPStatus.BAD_REQUEST, message="inventory UUID %s not available" % system_baseline_in["inventory_uuid"], ) system_name = profile_parser.get_name(system_with_profile) parsed_profile = profile_parser.parse_profile( system_with_profile["system_profile"], system_name, current_app.logger) facts = [] for fact in parsed_profile: if fact not in ["id", "name"] and parsed_profile[fact] not in [ "N/A", "None", None, ]: facts.append({"name": fact, "value": parsed_profile[fact]}) baseline_facts = group_baselines(facts) try: _validate_facts(baseline_facts) except FactValidationError as e: raise HTTPError(HTTPStatus.BAD_REQUEST, message=e.message) baseline = SystemBaseline( account=account_number, display_name=system_baseline_in["display_name"], baseline_facts=baseline_facts, ) baseline.baseline_facts = _sort_baseline_facts(baseline.baseline_facts) db.session.add(baseline) db.session.commit( ) # commit now so we get a created/updated time before json conversion return baseline.to_json()