Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Replace notifications by default #1084

Merged
merged 6 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/api-service/__app__/notifications/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,29 @@

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)


Expand All @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ def execute(cls, request: JobTemplateRequest, user_info: UserInfo) -> Result[Job
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
Expand Down
24 changes: 10 additions & 14 deletions src/api-service/__app__/onefuzzlib/notifications/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,26 @@ def get_by_id(cls, notification_id: UUID) -> Result["Notification"]:
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()
Expand Down
20 changes: 16 additions & 4 deletions src/cli/onefuzz/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,17 @@ class Notifications(Endpoint):
endpoint = "notifications"

def create(
self, container: primitives.Container, config: models.NotificationConfig
self,
container: primitives.Container,
config: models.NotificationConfig,
*,
replace_existing: bool = False,
) -> models.Notification:
"""Create a notification based on a config file"""

config = requests.NotificationCreate(container=container, config=config.config)
config = requests.NotificationCreate(
container=container, config=config.config, replace_existing=replace_existing
)
return self._req_model("POST", models.Notification, data=config)

def create_teams(
Expand Down Expand Up @@ -766,11 +772,17 @@ def delete(self, notification_id: UUID_EXPANSION) -> models.Notification:
data=requests.NotificationGet(notification_id=notification_id_expanded),
)

def list(self) -> List[models.Notification]:
def list(
self, *, container: Optional[List[primitives.Container]] = None
) -> List[models.Notification]:
"""List notification integrations"""

self.logger.debug("listing notification integrations")
return self._req_model_list("GET", models.Notification)
return self._req_model_list(
"GET",
models.Notification,
data=requests.NotificationSearch(container=container),
)


class Tasks(Endpoint):
Expand Down
21 changes: 10 additions & 11 deletions src/cli/onefuzz/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,16 @@ def stop(
self.onefuzz.tasks.delete(task.task_id)

if stop_notifications:
notifications = self.onefuzz.notifications.list()
for container in task.config.containers:
for notification in notifications:
if container.name == notification.container:
self.logger.info(
"removing notification: %s",
notification.notification_id,
)
self.onefuzz.notifications.delete(
notification.notification_id
)
container_names = [x.name for x in task.config.containers]
notifications = self.onefuzz.notifications.list(
container=container_names
)
for notification in notifications:
self.logger.info(
"removing notification: %s",
notification.notification_id,
)
self.onefuzz.notifications.delete(notification.notification_id)


Template.stop.__doc__ = "stop a template job"
14 changes: 1 addition & 13 deletions src/cli/onefuzz/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

import json
import os
import tempfile
import zipfile
Expand Down Expand Up @@ -119,19 +118,8 @@ def setup_notifications(self, config: Optional[NotificationConfig]) -> None:
else:
container = self.containers[ContainerType.reports]

if not container:
return

config_dict = json.loads(config.json())
for entry in self.onefuzz.notifications.list():
if entry.container == container and entry.config == config_dict:
self.logger.debug(
"notification already exists: %s", entry.notification_id
)
return

self.logger.info("creating notification config for %s", container)
self.onefuzz.notifications.create(container, config)
self.onefuzz.notifications.create(container, config, replace_existing=True)

def upload_setup(
self,
Expand Down
5 changes: 5 additions & 0 deletions src/pytypes/onefuzztypes/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class JobSearch(BaseRequest):

class NotificationCreate(BaseRequest, NotificationConfig):
container: Container
replace_existing: bool = Field(default=False)


class NotificationSearch(BaseRequest):
container: Optional[List[Container]]


class NotificationGet(BaseRequest):
Expand Down