mirror of
https://github.com/microsoft/onefuzz.git
synced 2025-06-19 13:03:44 +00:00
Replace notifications by default (#1084)
This commit is contained in:
@ -7,16 +7,29 @@ import logging
|
||||
|
||||
import azure.functions as func
|
||||
from onefuzztypes.models import Error
|
||||
from onefuzztypes.requests import NotificationCreate, NotificationGet
|
||||
from onefuzztypes.requests import (
|
||||
NotificationCreate,
|
||||
NotificationGet,
|
||||
NotificationSearch,
|
||||
)
|
||||
|
||||
from ..onefuzzlib.endpoint_authorization import call_if_user
|
||||
from ..onefuzzlib.events import get_events
|
||||
from ..onefuzzlib.notifications.main import Notification
|
||||
from ..onefuzzlib.request import not_ok, ok, parse_request
|
||||
from ..onefuzzlib.request import not_ok, ok, parse_request, parse_uri
|
||||
|
||||
|
||||
def get(req: func.HttpRequest) -> func.HttpResponse:
|
||||
entries = Notification.search()
|
||||
logging.info("notification search")
|
||||
request = parse_uri(NotificationSearch, req)
|
||||
if isinstance(request, Error):
|
||||
return not_ok(request, context="notification search")
|
||||
|
||||
if request.container:
|
||||
entries = Notification.search(query={"container": request.container})
|
||||
else:
|
||||
entries = Notification.search()
|
||||
|
||||
return ok(entries)
|
||||
|
||||
|
||||
@ -26,7 +39,11 @@ def post(req: func.HttpRequest) -> func.HttpResponse:
|
||||
if isinstance(request, Error):
|
||||
return not_ok(request, context="notification create")
|
||||
|
||||
entry = Notification.create(container=request.container, config=request.config)
|
||||
entry = Notification.create(
|
||||
container=request.container,
|
||||
config=request.config,
|
||||
replace_existing=request.replace_existing,
|
||||
)
|
||||
if isinstance(entry, Error):
|
||||
return not_ok(entry, context="notification create")
|
||||
|
||||
|
@ -87,7 +87,9 @@ class JobTemplateIndex(BASE_INDEX, ORMMixin):
|
||||
for task_container in request.containers:
|
||||
if task_container.type == notification_config.container_type:
|
||||
notification = Notification.create(
|
||||
task_container.name, notification_config.notification.config
|
||||
task_container.name,
|
||||
notification_config.notification.config,
|
||||
True,
|
||||
)
|
||||
if isinstance(notification, Error):
|
||||
return notification
|
||||
|
@ -57,30 +57,26 @@ class Notification(models.Notification, ORMMixin):
|
||||
notification = notifications[0]
|
||||
return notification
|
||||
|
||||
@classmethod
|
||||
def get_existing(
|
||||
cls, container: Container, config: NotificationTemplate
|
||||
) -> Optional["Notification"]:
|
||||
notifications = Notification.search(query={"container": [container]})
|
||||
for notification in notifications:
|
||||
if notification.config == config:
|
||||
return notification
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def key_fields(cls) -> Tuple[str, str]:
|
||||
return ("notification_id", "container")
|
||||
|
||||
@classmethod
|
||||
def create(
|
||||
cls, container: Container, config: NotificationTemplate
|
||||
cls, container: Container, config: NotificationTemplate, replace_existing: bool
|
||||
) -> Result["Notification"]:
|
||||
if not container_exists(container, StorageType.corpus):
|
||||
return Error(code=ErrorCode.INVALID_REQUEST, errors=["invalid container"])
|
||||
|
||||
existing = cls.get_existing(container, config)
|
||||
if existing is not None:
|
||||
return existing
|
||||
if replace_existing:
|
||||
existing = cls.search(query={"container": [container]})
|
||||
for entry in existing:
|
||||
logging.info(
|
||||
"replacing existing notification: %s",
|
||||
entry.notification_id,
|
||||
container,
|
||||
)
|
||||
entry.delete()
|
||||
|
||||
entry = cls(container=container, config=config)
|
||||
entry.save()
|
||||
|
Reference in New Issue
Block a user