def create_schedule( self, db_session: Session, project: str, name: str, kind: schemas.ScheduleKinds, scheduled_object: Union[Dict, Callable], cron_trigger: Union[str, schemas.ScheduleCronTrigger], labels: Dict = None, ): if isinstance(cron_trigger, str): cron_trigger = schemas.ScheduleCronTrigger.from_crontab( cron_trigger) self._validate_cron_trigger(cron_trigger) logger.debug( "Creating schedule", project=project, name=name, kind=kind, scheduled_object=scheduled_object, cron_trigger=cron_trigger, labels=labels, ) get_project_member().ensure_project(db_session, project) get_db().create_schedule(db_session, project, name, kind, scheduled_object, cron_trigger, labels) self._create_schedule_in_scheduler(project, name, kind, scheduled_object, cron_trigger)
def delete_project( name: str, deletion_strategy: schemas.DeletionStrategy = Header( schemas.DeletionStrategy.default(), alias=schemas.HeaderNames.deletion_strategy), db_session: Session = Depends(deps.get_db_session), ): get_project_member().delete_project(db_session, name, deletion_strategy) return Response(status_code=HTTPStatus.NO_CONTENT.value)
def create_schedule( self, db_session: Session, auth_info: mlrun.api.schemas.AuthInfo, project: str, name: str, kind: schemas.ScheduleKinds, scheduled_object: Union[Dict, Callable], cron_trigger: Union[str, schemas.ScheduleCronTrigger], labels: Dict = None, concurrency_limit: int = config.httpdb.scheduling. default_concurrency_limit, ): if isinstance(cron_trigger, str): cron_trigger = schemas.ScheduleCronTrigger.from_crontab( cron_trigger) self._validate_cron_trigger(cron_trigger) logger.debug( "Creating schedule", project=project, name=name, kind=kind, scheduled_object=scheduled_object, cron_trigger=cron_trigger, labels=labels, concurrency_limit=concurrency_limit, ) get_project_member().ensure_project(db_session, project, leader_session=auth_info.session) get_db().create_schedule( db_session, project, name, kind, scheduled_object, cron_trigger, concurrency_limit, labels, ) self._create_schedule_in_scheduler( project, name, kind, scheduled_object, cron_trigger, concurrency_limit, auth_info, )
def patch_project( project: dict, name: str, patch_mode: mlrun.api.schemas.PatchMode = fastapi.Header( mlrun.api.schemas.PatchMode.replace, alias=mlrun.api.schemas.HeaderNames.patch_mode, ), # TODO: we're in a http request context here, therefore it doesn't make sense that by default it will hold the # request until the process will be completed - after UI supports waiting - change default to False wait_for_completion: bool = fastapi.Query(True, alias="wait-for-completion"), auth_verifier: mlrun.api.api.deps.AuthVerifierDep = fastapi.Depends( mlrun.api.api.deps.AuthVerifierDep), db_session: sqlalchemy.orm.Session = fastapi.Depends( mlrun.api.api.deps.get_db_session), ): project, is_running_in_background = get_project_member().patch_project( db_session, name, project, patch_mode, auth_verifier.auth_info.projects_role, auth_verifier.auth_info.session, wait_for_completion=wait_for_completion, ) if is_running_in_background: return fastapi.Response(status_code=http.HTTPStatus.ACCEPTED.value) return project
def grafana_list_projects(db_session: Session, auth_info: mlrun.api.schemas.AuthInfo) -> List[str]: projects_output = get_project_member().list_projects( db_session, format_=ProjectsFormat.name_only, leader_session=auth_info.session) return projects_output.projects
def get_project( name: str, db_session: Session = fastapi.Depends(deps.get_db_session), auth_verifier: deps.AuthVerifier = fastapi.Depends(deps.AuthVerifier), ): return get_project_member().get_project(db_session, name, auth_verifier.auth_info.session)
def patch_project( project: dict, name: str, patch_mode: schemas.PatchMode = fastapi.Header( schemas.PatchMode.replace, alias=schemas.HeaderNames.patch_mode), projects_role: typing.Optional[schemas.ProjectsRole] = fastapi.Header( None, alias=schemas.HeaderNames.projects_role), # TODO: we're in a http request context here, therefore it doesn't make sense that by default it will hold the # request until the process will be completed - after UI supports waiting - change default to False wait_for_completion: bool = fastapi.Query(True, alias="wait-for-completion"), iguazio_session: typing.Optional[str] = fastapi.Cookie( None, alias="session"), db_session: Session = fastapi.Depends(deps.get_db_session), ): project, is_running_in_background = get_project_member().patch_project( db_session, name, project, patch_mode, projects_role, iguazio_session, wait_for_completion=wait_for_completion, ) if is_running_in_background: return fastapi.Response(status_code=HTTPStatus.ACCEPTED.value) return project
def delete_project( name: str, deletion_strategy: schemas.DeletionStrategy = fastapi.Header( schemas.DeletionStrategy.default(), alias=schemas.HeaderNames.deletion_strategy), projects_role: typing.Optional[schemas.ProjectsRole] = fastapi.Header( None, alias=schemas.HeaderNames.projects_role), # TODO: we're in a http request context here, therefore it doesn't make sense that by default it will hold the # request until the process will be completed - after UI supports waiting - change default to False wait_for_completion: bool = fastapi.Query(True, alias="wait-for-completion"), auth_verifier: deps.AuthVerifier = fastapi.Depends(deps.AuthVerifier), db_session: Session = fastapi.Depends(deps.get_db_session), ): is_running_in_background = get_project_member().delete_project( db_session, name, deletion_strategy, projects_role, auth_verifier.auth_info.session, wait_for_completion=wait_for_completion, ) if is_running_in_background: return fastapi.Response(status_code=HTTPStatus.ACCEPTED.value) return fastapi.Response(status_code=HTTPStatus.NO_CONTENT.value)
def delete_project( name: str, deletion_strategy: mlrun.api.schemas.DeletionStrategy = fastapi.Header( mlrun.api.schemas.DeletionStrategy.default(), alias=mlrun.api.schemas.HeaderNames.deletion_strategy, ), # TODO: we're in a http request context here, therefore it doesn't make sense that by default it will hold the # request until the process will be completed - after UI supports waiting - change default to False wait_for_completion: bool = fastapi.Query(True, alias="wait-for-completion"), auth_info: mlrun.api.schemas.AuthInfo = fastapi.Depends( mlrun.api.api.deps.authenticate_request), db_session: sqlalchemy.orm.Session = fastapi.Depends( mlrun.api.api.deps.get_db_session), ): is_running_in_background = get_project_member().delete_project( db_session, name, deletion_strategy, auth_info.projects_role, auth_info, wait_for_completion=wait_for_completion, ) if is_running_in_background: return fastapi.Response(status_code=http.HTTPStatus.ACCEPTED.value) return fastapi.Response(status_code=http.HTTPStatus.NO_CONTENT.value)
def list_projects( format_: schemas.Format = Query(schemas.Format.full, alias="format"), owner: str = None, labels: typing.List[str] = Query(None, alias="label"), state: schemas.ProjectState = None, db_session: Session = Depends(deps.get_db_session), ): return get_project_member().list_projects(db_session, owner, format_, labels, state)
def patch_project( project: schemas.ProjectPatch, name: str, patch_mode: schemas.PatchMode = Header( schemas.PatchMode.replace, alias=schemas.HeaderNames.patch_mode), db_session: Session = Depends(deps.get_db_session), ): return get_project_member().patch_project(db_session, name, project, patch_mode)
def store_project( project: schemas.Project, name: str, projects_role: typing.Optional[schemas.ProjectsRole] = Header( None, alias=schemas.HeaderNames.projects_role), db_session: Session = Depends(deps.get_db_session), ): project, is_running_in_background = get_project_member().store_project( db_session, name, project, projects_role, wait_for_completion=False) if is_running_in_background: return Response(status_code=HTTPStatus.ACCEPTED.value) return project
def list_projects( format_: mlrun.api.schemas.ProjectsFormat = fastapi.Query( mlrun.api.schemas.ProjectsFormat.full, alias="format"), owner: str = None, labels: typing.List[str] = fastapi.Query(None, alias="label"), state: mlrun.api.schemas.ProjectState = None, auth_verifier: mlrun.api.api.deps.AuthVerifierDep = fastapi.Depends( mlrun.api.api.deps.AuthVerifierDep), db_session: sqlalchemy.orm.Session = fastapi.Depends( mlrun.api.api.deps.get_db_session), ): projects_output = get_project_member().list_projects( db_session, owner, mlrun.api.schemas.ProjectsFormat.name_only, labels, state, auth_verifier.auth_info.projects_role, auth_verifier.auth_info.session, ) allowed_project_names = projects_output.projects # skip permission check if it's the leader if not _is_request_from_leader(auth_verifier.auth_info.projects_role): allowed_project_names = mlrun.api.utils.clients.opa.Client( ).filter_projects_by_permissions( projects_output.projects, auth_verifier.auth_info, ) return get_project_member().list_projects( db_session, owner, format_, labels, state, auth_verifier.auth_info.projects_role, auth_verifier.auth_info.session, allowed_project_names, )
def get_project( name: str, db_session: sqlalchemy.orm.Session = fastapi.Depends( mlrun.api.api.deps.get_db_session), auth_verifier: mlrun.api.api.deps.AuthVerifierDep = fastapi.Depends( mlrun.api.api.deps.AuthVerifierDep), ): project = get_project_member().get_project(db_session, name, auth_verifier.auth_info.session) # skip permission check if it's the leader if not _is_request_from_leader(auth_verifier.auth_info.projects_role): mlrun.api.utils.clients.opa.Client().query_project_permissions( name, mlrun.api.schemas.AuthorizationAction.read, auth_verifier.auth_info, ) return project
def list_projects( format_: schemas.Format = fastapi.Query(schemas.Format.full, alias="format"), owner: str = None, labels: typing.List[str] = fastapi.Query(None, alias="label"), state: schemas.ProjectState = None, auth_verifier: deps.AuthVerifier = fastapi.Depends(deps.AuthVerifier), db_session: Session = fastapi.Depends(deps.get_db_session), ): return get_project_member().list_projects( db_session, owner, format_, labels, state, auth_verifier.auth_info.projects_role, auth_verifier.auth_info.session, )
def delete_project( name: str, deletion_strategy: schemas.DeletionStrategy = Header( schemas.DeletionStrategy.default(), alias=schemas.HeaderNames.deletion_strategy), projects_role: typing.Optional[schemas.ProjectsRole] = Header( None, alias=schemas.HeaderNames.projects_role), db_session: Session = Depends(deps.get_db_session), ): is_running_in_background = get_project_member().delete_project( db_session, name, deletion_strategy, projects_role, wait_for_completion=False) if is_running_in_background: return Response(status_code=HTTPStatus.ACCEPTED.value) return Response(status_code=HTTPStatus.NO_CONTENT.value)
def store_project( project: schemas.Project, name: str, # TODO: we're in a http request context here, therefore it doesn't make sense that by default it will hold the # request until the process will be completed - after UI supports waiting - change default to False wait_for_completion: bool = fastapi.Query(True, alias="wait-for-completion"), auth_verifier: deps.AuthVerifier = fastapi.Depends(deps.AuthVerifier), db_session: Session = fastapi.Depends(deps.get_db_session), ): project, is_running_in_background = get_project_member().store_project( db_session, name, project, auth_verifier.auth_info.projects_role, auth_verifier.auth_info.session, wait_for_completion=wait_for_completion, ) if is_running_in_background: return fastapi.Response(status_code=HTTPStatus.ACCEPTED.value) return project
def create_project( project: mlrun.api.schemas.Project, response: fastapi.Response, # TODO: we're in a http request context here, therefore it doesn't make sense that by default it will hold the # request until the process will be completed - after UI supports waiting - change default to False wait_for_completion: bool = fastapi.Query(True, alias="wait-for-completion"), auth_info: mlrun.api.schemas.AuthInfo = fastapi.Depends( mlrun.api.api.deps.authenticate_request), db_session: sqlalchemy.orm.Session = fastapi.Depends( mlrun.api.api.deps.get_db_session), ): project, is_running_in_background = get_project_member().create_project( db_session, project, auth_info.projects_role, auth_info.session, wait_for_completion=wait_for_completion, ) if is_running_in_background: return fastapi.Response(status_code=http.HTTPStatus.ACCEPTED.value) response.status_code = http.HTTPStatus.CREATED.value return project
async def list_project_summaries( owner: str = None, labels: typing.List[str] = fastapi.Query(None, alias="label"), state: mlrun.api.schemas.ProjectState = None, auth_info: mlrun.api.schemas.AuthInfo = fastapi.Depends( mlrun.api.api.deps.authenticate_request), db_session: sqlalchemy.orm.Session = fastapi.Depends( mlrun.api.api.deps.get_db_session), ): projects_output = await fastapi.concurrency.run_in_threadpool( get_project_member().list_projects, db_session, owner, mlrun.api.schemas.ProjectsFormat.name_only, labels, state, auth_info.projects_role, auth_info.session, ) allowed_project_names = projects_output.projects # skip permission check if it's the leader if not _is_request_from_leader(auth_info.projects_role): allowed_project_names = await fastapi.concurrency.run_in_threadpool( mlrun.api.utils.auth.verifier.AuthVerifier(). filter_projects_by_permissions, projects_output.projects, auth_info, ) return await get_project_member().list_project_summaries( db_session, owner, labels, state, auth_info.projects_role, auth_info.session, allowed_project_names, )
def list_projects( format_: schemas.Format = Query(schemas.Format.full, alias="format"), owner: str = None, db_session: Session = Depends(deps.get_db_session), ): return get_project_member().list_projects(db_session, owner, format_)
def delete_project( name: str, db_session: Session = Depends(deps.get_db_session), ): get_project_member().delete_project(db_session, name) return Response(status_code=HTTPStatus.NO_CONTENT.value)
def get_project(name: str, db_session: Session = Depends(deps.get_db_session)): return get_project_member().get_project(db_session, name)
def store_project( project: schemas.Project, name: str, db_session: Session = Depends(deps.get_db_session), ): return get_project_member().store_project(db_session, name, project)
def create_project(project: schemas.Project, db_session: Session = Depends(deps.get_db_session)): return get_project_member().create_project(db_session, project)
async def shutdown_event(): if get_project_member(): get_project_member().shutdown() cancel_all_periodic_functions() if get_scheduler(): await get_scheduler().stop()