From 12bdd122a405cb7624b7f5573bbfc0f2909d0534 Mon Sep 17 00:00:00 2001 From: Mubashirshariq Date: Mon, 7 Oct 2024 14:19:06 +0530 Subject: [PATCH] moved runbook endpoints to runbook.py --- keep-ui/next-env.d.ts | 2 +- keep-ui/utils/hooks/useRunbook.ts | 4 +- keep/api/api.py | 4 ++ keep/api/routes/providers.py | 68 +------------------------- keep/api/routes/runbooks.py | 79 +++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 70 deletions(-) create mode 100644 keep/api/routes/runbooks.py diff --git a/keep-ui/next-env.d.ts b/keep-ui/next-env.d.ts index 725dd6f24..fd36f9494 100644 --- a/keep-ui/next-env.d.ts +++ b/keep-ui/next-env.d.ts @@ -3,4 +3,4 @@ /// // NOTE: This file should not be edited -// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information. +// see https://nextjs.org/docs/basic-features/typescript for more information. diff --git a/keep-ui/utils/hooks/useRunbook.ts b/keep-ui/utils/hooks/useRunbook.ts index 9d4be158c..37e0e1939 100644 --- a/keep-ui/utils/hooks/useRunbook.ts +++ b/keep-ui/utils/hooks/useRunbook.ts @@ -35,7 +35,7 @@ export const useRunBookTriggers = (values: any, refresh: number) => { return setRepoData([]); } const data = await fetcher( - `${baseApiurl}/providers/${provider?.type}/${provider?.id}/repositories`, + `${baseApiurl}/runbooks/${provider?.type}/${provider?.id}/repositories`, session?.accessToken ); setRepoData(data); @@ -69,7 +69,7 @@ export const useRunBookTriggers = (values: any, refresh: number) => { } //TO DO backend runbook records needs to be created. const response = await fetcher( - `${baseApiurl}/providers/${provider?.type}/${ + `${baseApiurl}/runbooks/${provider?.type}/${ provider?.id }/runbook?${params.toString()}`, session?.accessToken diff --git a/keep/api/api.py b/keep/api/api.py index 33e195f2b..4f184ecda 100644 --- a/keep/api/api.py +++ b/keep/api/api.py @@ -54,6 +54,7 @@ topology, whoami, workflows, + runbooks, ) from keep.api.routes.auth import groups as auth_groups from keep.api.routes.auth import permissions, roles, users @@ -243,6 +244,9 @@ def get_app( app.include_router( deduplications.router, prefix="/deduplications", tags=["deduplications"] ) + app.include_router( + runbooks.router, prefix="/runbooks", tags=["runbooks"] + ) # if its single tenant with authentication, add signin endpoint logger.info(f"Starting Keep with authentication type: {AUTH_TYPE}") # If we run Keep with SINGLE_TENANT auth type, we want to add the signin endpoint diff --git a/keep/api/routes/providers.py b/keep/api/routes/providers.py index c3fd52497..0eb6b59d0 100644 --- a/keep/api/routes/providers.py +++ b/keep/api/routes/providers.py @@ -732,71 +732,5 @@ def get_webhook_settings( keep_webhook_api_url_with_auth=keep_webhook_api_url_with_auth, ), webhookMarkdown=webhookMarkdown, - ) - -@router.get("/{provider_type}/{provider_id}/repositories") -def get_repositories( - provider_type: str, - provider_id: str, - authenticated_entity: AuthenticatedEntity = Depends( - IdentityManagerFactory.get_auth_verifier(["read:providers"]) - ), - session: Session = Depends(get_session), -): - tenant_id = authenticated_entity.tenant_id - logger.info("Getting respositories", extra={"provider_type": provider_type, "provider_id": provider_id}) - tenant_id = authenticated_entity.tenant_id - logger.info( - "Getting provider alerts", - extra={ - "tenant_id": tenant_id, - "provider_type": provider_type, - "provider_id": provider_id, - }, - ) - context_manager = ContextManager(tenant_id=tenant_id) - secret_manager = SecretManagerFactory.get_secret_manager(context_manager) - provider_config = secret_manager.read_secret( - f"{tenant_id}_{provider_type}_{provider_id}", is_json=True - ) - provider = ProvidersFactory.get_provider( - context_manager, provider_id, provider_type, provider_config - ) - - return provider.pull_repositories() - - -@router.get("/{provider_type}/{provider_id}/runbook") -def get_repositories( - provider_type: str, - provider_id: str, - authenticated_entity: AuthenticatedEntity = Depends( - IdentityManagerFactory.get_auth_verifier(["read:providers"]) - ), - session: Session = Depends(get_session), - repo: str = Query(None), - branch: str = Query(None), - md_path: str = Query(None), -): - tenant_id = authenticated_entity.tenant_id - logger.info("Getting runbook", extra={"provider_type": provider_type, "provider_id": provider_id}) - tenant_id = authenticated_entity.tenant_id - logger.info( - "Getting provider alerts", - extra={ - "tenant_id": tenant_id, - "provider_type": provider_type, - "provider_id": provider_id, - }, - ) - context_manager = ContextManager(tenant_id=tenant_id) - secret_manager = SecretManagerFactory.get_secret_manager(context_manager) - provider_config = secret_manager.read_secret( - f"{tenant_id}_{provider_type}_{provider_id}", is_json=True - ) - provider = ProvidersFactory.get_provider( - context_manager, provider_id, provider_type, provider_config - ) - - return provider.pull_runbook(repo=repo, branch=branch, md_path=md_path) + ) diff --git a/keep/api/routes/runbooks.py b/keep/api/routes/runbooks.py new file mode 100644 index 000000000..b94ee3054 --- /dev/null +++ b/keep/api/routes/runbooks.py @@ -0,0 +1,79 @@ +import logging +from fastapi import APIRouter, Depends, Query +from sqlmodel import Session +from keep.api.core.db import get_session +from keep.contextmanager.contextmanager import ContextManager +from keep.identitymanager.authenticatedentity import AuthenticatedEntity +from keep.identitymanager.identitymanagerfactory import IdentityManagerFactory +from keep.providers.providers_factory import ProvidersFactory +from keep.secretmanager.secretmanagerfactory import SecretManagerFactory + +logger = logging.getLogger(__name__) + +router = APIRouter() + + +@router.get("/{provider_type}/{provider_id}/repositories") +def get_repositories( + provider_type: str, + provider_id: str, + authenticated_entity: AuthenticatedEntity = Depends( + IdentityManagerFactory.get_auth_verifier(["read:providers"]) + ), + session: Session = Depends(get_session), +): + tenant_id = authenticated_entity.tenant_id + logger.info("Getting respositories", extra={"provider_type": provider_type, "provider_id": provider_id}) + tenant_id = authenticated_entity.tenant_id + logger.info( + "Getting provider alerts", + extra={ + "tenant_id": tenant_id, + "provider_type": provider_type, + "provider_id": provider_id, + }, + ) + context_manager = ContextManager(tenant_id=tenant_id) + secret_manager = SecretManagerFactory.get_secret_manager(context_manager) + provider_config = secret_manager.read_secret( + f"{tenant_id}_{provider_type}_{provider_id}", is_json=True + ) + provider = ProvidersFactory.get_provider( + context_manager, provider_id, provider_type, provider_config + ) + + return provider.pull_repositories() + + +@router.get("/{provider_type}/{provider_id}/runbook") +def get_repositories( + provider_type: str, + provider_id: str, + authenticated_entity: AuthenticatedEntity = Depends( + IdentityManagerFactory.get_auth_verifier(["read:providers"]) + ), + session: Session = Depends(get_session), + repo: str = Query(None), + branch: str = Query(None), + md_path: str = Query(None), +): + tenant_id = authenticated_entity.tenant_id + logger.info("Getting runbook", extra={"provider_type": provider_type, "provider_id": provider_id}) + logger.info( + "Getting provider alerts", + extra={ + "tenant_id": tenant_id, + "provider_type": provider_type, + "provider_id": provider_id, + }, + ) + context_manager = ContextManager(tenant_id=tenant_id) + secret_manager = SecretManagerFactory.get_secret_manager(context_manager) + provider_config = secret_manager.read_secret( + f"{tenant_id}_{provider_type}_{provider_id}", is_json=True + ) + provider = ProvidersFactory.get_provider( + context_manager, provider_id, provider_type, provider_config + ) + + return provider.pull_runbook(repo=repo, branch=branch, md_path=md_path)