Skip to content

Commit

Permalink
feat(extension): Add event parser extension for echo-rest (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsie authored Nov 23, 2019
1 parent eaa4907 commit 2533c9c
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 3 deletions.
6 changes: 6 additions & 0 deletions echo-api/echo-api.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies {
api "com.netflix.spinnaker.kork:kork-plugins-api"

compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2019 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.extension.rest;

import com.netflix.spinnaker.kork.annotations.Alpha;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.pf4j.ExtensionPoint;

@Alpha
public interface RestEventParser extends ExtensionPoint {

/**
* Parse an Event prior to POST to the configured URL
*
* @param event {@link Event}
* @return Map, which conforms to the RestEventListener API
*/
Map parse(Event event);

@Data
class Event {
public Metadata details;
public Map<String, Object> content;
public String rawContent;
public Map<String, Object> payload;
public String eventId;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Metadata {
private String source;
private String type;
private String created;
private String organization;
private String project;
private String application;
private String _content_id;
private Map<String, List> requestHeaders;
private Map<String, String> attributes;
}
}
1 change: 1 addition & 0 deletions echo-rest/echo-rest.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
implementation "com.squareup.okhttp:okhttp-urlconnection"
implementation "com.squareup.okhttp:okhttp-apache"
implementation project(':echo-model')
implementation project(':echo-api')

implementation "commons-codec:commons-codec"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ class RestConfig {
}

@Bean
RestUrls restServices(RestProperties restProperties, RestClientFactory clientFactory, LogLevel retrofitLogLevel, RequestInterceptorAttacher requestInterceptorAttacher, HeadersFromFile headersFromFile) {
RestUrls restServices(RestProperties restProperties,
RestClientFactory clientFactory,
LogLevel retrofitLogLevel,
RequestInterceptorAttacher requestInterceptorAttacher,
HeadersFromFile headersFromFile) {

RestUrls restUrls = new RestUrls()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spectator.api.Registry
import com.netflix.spinnaker.echo.config.RestUrls
import com.netflix.spinnaker.echo.model.Event
import com.netflix.spinnaker.echo.extension.rest.RestEventParser
import com.netflix.spinnaker.kork.annotations.Alpha
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
Expand All @@ -43,6 +45,9 @@ class RestEventListener implements EchoEventListener {

Registry registry

@Alpha
Optional<RestEventParser> restEventParser

@Value('${rest.default-event-name:spinnaker_events}')
String eventName

Expand All @@ -52,9 +57,11 @@ class RestEventListener implements EchoEventListener {
@Autowired
RestEventListener(RestUrls restUrls,
RestEventTemplateEngine restEventTemplateEngine,
Optional<RestEventParser> restEventParser,
Registry registry) {
this.restUrls = restUrls
this.restEventTemplateEngine = restEventTemplateEngine
this.restEventParser = restEventParser
this.registry = registry
}

Expand All @@ -68,6 +75,9 @@ class RestEventListener implements EchoEventListener {
if (service.config.flatten) {
eventAsMap.content = mapper.writeValueAsString(eventAsMap.content)
eventAsMap.details = mapper.writeValueAsString(eventAsMap.details)
} else if (restEventParser.isPresent()) {
RestEventParser.Event e = mapper.convertValue(event, RestEventParser.Event)
eventAsMap = restEventParser.get().parse(e)
}

if (service.config.wrap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ package com.netflix.spinnaker.echo.events
import com.netflix.spectator.api.NoopRegistry
import com.netflix.spinnaker.echo.config.RestUrls
import com.netflix.spinnaker.echo.model.Event
import com.netflix.spinnaker.echo.extension.rest.RestEventParser
import com.netflix.spinnaker.echo.rest.RestService
import spock.lang.Specification
import spock.lang.Subject


class RestEventListenerSpec extends Specification {

Optional<RestEventParser> restEventParser = Optional.empty()

@Subject
RestEventListener listener = new RestEventListener(null, null, new NoopRegistry())
RestEventListener listener = new RestEventListener(null, null, restEventParser, new NoopRegistry())
Event event = new Event(content: ['uno': 'dos'])
RestService restService

Expand Down
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

include 'echo-artifacts',
include 'echo-api',
'echo-artifacts',
'echo-bom',
'echo-core',
'echo-model',
Expand Down

0 comments on commit 2533c9c

Please sign in to comment.