Skip to content

Commit

Permalink
feat(notifications): adds dynamic extension notification parameters (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeach committed Jul 20, 2020
1 parent d27193f commit b951dbf
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 88 deletions.
Expand Up @@ -18,6 +18,7 @@

import com.netflix.spinnaker.kork.annotations.Alpha;
import com.netflix.spinnaker.kork.plugins.api.internal.SpinnakerExtensionPoint;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;

Expand All @@ -40,4 +41,11 @@ void sendNotifications(
@Nonnull String application,
@Nonnull Event event,
@Nonnull String status);

/**
* Notification parameter definitions. Users set these values via Spinnaker's UI; the parameters
* will be passed through {@link NotificationAgent#sendNotifications} as key-value pairs.
*/
@Nonnull
List<NotificationParameter> getParameters();
}
@@ -0,0 +1,46 @@
/*
* Copyright 2020 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.echo.api.events;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

/** Definition of a notification parameter that a user can configure. */
@Data
@NoArgsConstructor
public class NotificationParameter {

/** Name of the parameter */
@NonNull private String name;

/** Label to use in Spinnaker's UI. */
@NonNull private String label;

/** Default value if not specified by the user. */
private String defaultValue;

/** Description to show in Spinnaker's UI. */
private String description;

/** Data Type of the parameter. */
private ParameterType type = ParameterType.string;

public enum ParameterType {
string
}
}

This file was deleted.

@@ -0,0 +1,111 @@
/*
* Copyright 2015 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.echo.controller;

import com.netflix.spinnaker.echo.api.Notification;
import com.netflix.spinnaker.echo.api.events.NotificationAgent;
import com.netflix.spinnaker.echo.notification.InteractiveNotificationCallbackHandler;
import com.netflix.spinnaker.echo.notification.InteractiveNotificationService;
import com.netflix.spinnaker.echo.notification.NotificationService;
import groovy.util.logging.Slf4j;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/notifications")
@RestController
@Slf4j
@AllArgsConstructor
public class NotificationController {
@Autowired(required = false)
Collection<NotificationService> notificationServices;

@Autowired private InteractiveNotificationCallbackHandler interactiveNotificationCallbackHandler;

@Autowired Optional<List<NotificationAgent>> notificationAgents;

/**
* Provides an endpoint for other Spinnaker services to send out notifications to users.
* Processing of the request is delegated to an implementation of {@link NotificationService}
* appropriate for the {@link Notification#notificationType}.
*
* @param notification The notification to be sent, in an echo-generic format.
* @return
*/
@RequestMapping(method = RequestMethod.POST)
public EchoResponse create(@RequestBody Notification notification) {
val notificationService =
notificationServices.stream()
.filter(
it ->
it.supportsType(notification.getNotificationType())
&&
// Only delegate interactive notifications to interactive notification
// services, and vice-versa.
// This allows us to support two implementations in parallel for the same
// notification type.
(it instanceof InteractiveNotificationService
? notification.isInteractive()
: !notification.isInteractive()))
.findFirst();
return notificationService.map(ns -> ns.handle(notification)).orElse(null);
}

/**
* Provides a generic callback API for notification services to call, primarily in response to
* interactive user action (e.g. clicking a button in a message). This method makes as few
* assumptions as possible about the request, delegating the raw request headers, parameters and
* body to the corresponding [InteractiveNotificationService] to process, and similarly allows the
* notification service to return an arbitrary response body to the caller.
*
* <p>Note that this method must be exposed externally through gate so that external services
* (e.g. Slack) may call back to echo.
*
* @param source The unique ID of the calling notification service (e.g. "slack")
* @param headers The request headers
* @param rawBody The raw body of the request
* @param parameters The request parameters, parsed as a Map
* @return
*/
@RequestMapping(
value = "/callbacks/{source}",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> processCallback(
@PathVariable String source, RequestEntity<String> request) {
return interactiveNotificationCallbackHandler.processCallback(source, request);
}

@GetMapping("/metadata")
public List<NotificationAgent> getNotificationTypeMetadata() {
return notificationAgents.orElseGet(ArrayList::new);
}
}
Expand Up @@ -17,6 +17,9 @@
package com.netflix.spinnaker.echo.notification

import com.netflix.spinnaker.echo.api.Notification
import com.netflix.spinnaker.echo.api.events.Event
import com.netflix.spinnaker.echo.api.events.NotificationAgent
import com.netflix.spinnaker.echo.api.events.NotificationParameter
import com.netflix.spinnaker.echo.controller.NotificationController
import com.netflix.spinnaker.echo.notification.InteractiveNotificationCallbackHandler.SpinnakerService
import com.netflix.spinnaker.kork.web.exceptions.InvalidRequestException
Expand Down Expand Up @@ -53,8 +56,9 @@ class NotificationControllerSpec extends Specification {
Mock(Environment)
)
notificationController = new NotificationController(
notificationServices: [ interactiveNotificationService, notificationService ],
interactiveNotificationCallbackHandler: interactiveNotificationCallbackHandler
[interactiveNotificationService, notificationService],
interactiveNotificationCallbackHandler,
Optional.of([new MyNotificationAgent()]),
)
}

Expand Down Expand Up @@ -176,7 +180,40 @@ class NotificationControllerSpec extends Specification {
response == expectedResponse
}

void 'serves notification agent metadata'() {
when:
def response = notificationController.getNotificationTypeMetadata()

then:
response.size() == 1
response[0] instanceof MyNotificationAgent
}

static Response mockResponse() {
new Response("url", 200, "nothing", emptyList(), new TypedByteArray("application/json", "response".bytes))
}

static class MyNotificationAgent implements NotificationAgent {
@Override
List<NotificationParameter> getParameters() {
return [[
type : "string",
name : "my-parameter",
description : "This is an extension notification parameter",
label : "My Parameter",
defaultValue: "wow!"
] as NotificationParameter]
}

@Override
String getNotificationType() {
return "my-notification-agent"
}

@Override
void sendNotifications(Map<String, Object> notificationConfig,
String application,
Event event,
String status) {}
}
}
Expand Up @@ -18,10 +18,12 @@ package com.netflix.spinnaker.echo.plugins

import com.netflix.spinnaker.echo.api.events.Event
import com.netflix.spinnaker.echo.api.events.NotificationAgent
import com.netflix.spinnaker.echo.api.events.NotificationParameter
import org.pf4j.Extension

@Extension
class NotificationAgentExtension : NotificationAgent {
override fun getNotificationType() = "extension_notification"
override fun sendNotifications(notification: MutableMap<String, Any>, application: String, event: Event, status: String) {}
override fun getParameters() = emptyList<NotificationParameter>()
}

0 comments on commit b951dbf

Please sign in to comment.