Skip to content

Commit

Permalink
addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalander Ramagiri committed Oct 24, 2023
1 parent 0ebb3ce commit 1f783f6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@
import lombok.Getter;

public abstract class BaseCDEvent {
/**
* This method can be implemented to create various types of CDEvents that can return a specific
* type of CDEvents in a CloudEvent format, more details on CDEvent types can be found in
* Documentation at https://cdevents.dev
*
* @return cdEvent
*/
abstract CloudEvent createCDEvent();

/** Common properties used in most of the CDEvents */
@Getter private String source;

@Getter private String subjectId;
@Getter private String subjectSource;
@Getter private String subjectUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ public CloudEvent createCDEvent(
String configType =
Optional.ofNullable(config)
.map(c -> (String) c.get("type"))
.orElseThrow(FieldNotFoundException::new);
.orElseThrow(() -> new FieldNotFoundException("type"));
String configLink =
Optional.ofNullable(config)
.map(c -> (String) c.get("link"))
.orElseThrow(FieldNotFoundException::new);
.orElseThrow(() -> new FieldNotFoundException("link"));

String executionId =
Optional.ofNullable(event.content)
.map(e -> (Map) e.get("execution"))
.map(e -> (String) e.get("id"))
.orElseThrow(FieldNotFoundException::new);
.orElseThrow(() -> new FieldNotFoundException("execution.id"));

String executionUrl =
String.format(
Expand All @@ -65,14 +65,13 @@ public CloudEvent createCDEvent(
Optional.ofNullable(event.content)
.map(e -> (Map) e.get("execution"))
.map(e -> (String) e.get("name"))
.orElseThrow(FieldNotFoundException::new);
.orElseThrow(() -> new FieldNotFoundException("execution.name"));

String cdEventsType =
Optional.ofNullable(preference)
.map(p -> (String) p.get("cdEventsType"))
.orElseThrow(FieldNotFoundException::new);
.orElseThrow(() -> new FieldNotFoundException("notifications.cdEventsType"));
log.info("Event type {} received to create CDEvent.", cdEventsType);
BaseCDEvent cdEvent = null;
// This map will be updated to add more event types that Spinnaker needs to send
Map<String, BaseCDEvent> cdEventsMap =
Map.of(
Expand All @@ -90,21 +89,21 @@ public CloudEvent createCDEvent(
CDEventTypes.TaskRunFinishedEvent.getEventType(),
new CDEventTaskRunFinished(
executionId, executionUrl, executionName, spinnakerUrl, status));
for (String keyType : cdEventsMap.keySet()) {
if (keyType.contains(cdEventsType)) {
cdEvent = cdEventsMap.get(keyType);
break;
}
}

if (cdEvent == null) {
log.error("No mapping event type found for {}", cdEventsType);
log.error(
"The event type should be an event or substring of an event from the list of event types {}",
cdEventsMap.keySet());
throw new CDEventsException("No mapping eventType found to create CDEvent");
} else {
return cdEvent.createCDEvent();
}
BaseCDEvent cdEvent =
cdEventsMap.keySet().stream()
.filter(keyType -> keyType.contains(cdEventsType))
.map(cdEventsMap::get)
.findFirst()
.orElseThrow(
() -> {
log.error("No mapping event type found for {}", cdEventsType);
log.error(
"The event type should be an event or substring of an event from the list of event types {}",
cdEventsMap.keySet());
return new CDEventsException("No mapping eventType found to create CDEvent");
});

return cdEvent.createCDEvent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import org.springframework.http.MediaType;
import retrofit.converter.ConversionException;
import retrofit.converter.Converter;
import retrofit.mime.TypedByteArray;
Expand All @@ -39,8 +40,6 @@ public class CDEventsHTTPMessageConverter implements Converter {

private final ObjectMapper objectMapper;

private static final String MIME_TYPE = "application/json";

public CDEventsHTTPMessageConverter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
Expand All @@ -65,9 +64,7 @@ public Object fromBody(TypedInput body, Type type) throws ConversionException {
try {
JavaType javaType = objectMapper.getTypeFactory().constructType(type);
return objectMapper.readValue(body.in(), javaType);
} catch (JsonParseException e) {
throw new ConversionException(e);
} catch (JsonMappingException e) {
} catch (JsonParseException | JsonMappingException e) {
throw new ConversionException(e);
} catch (IOException e) {
throw new ConversionException(e);
Expand All @@ -78,7 +75,7 @@ public Object fromBody(TypedInput body, Type type) throws ConversionException {
public TypedOutput toBody(Object object) {
try {
String json = objectMapper.writeValueAsString(object);
return new TypedByteArray(MIME_TYPE, json.getBytes("UTF-8"));
return new TypedByteArray(MediaType.APPLICATION_JSON_VALUE, json.getBytes("UTF-8"));
} catch (JsonProcessingException e) {
throw new AssertionError(e);
} catch (UnsupportedEncodingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.netflix.spinnaker.echo.cdevents;

import retrofit.client.Response;
import retrofit.http.*;
import retrofit.http.Body;
import retrofit.http.POST;
import retrofit.http.Path;

public interface CDEventsSenderClient {
@POST("/{brokerUrl}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.URL;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void intercept(RequestInterceptor.RequestFacade request) {
request.addHeader("Ce-Specversion", cdEvent.getSpecVersion().V1.toString());
request.addHeader("Ce-Source", cdEvent.getSource().toString());
request.addHeader("Ce-Type", cdEvent.getType());
request.addHeader("Content-Type", "application/json");
request.addHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public void sendNotifications(
Optional.ofNullable(event.content)
.map(e -> (Map) e.get("execution"))
.map(e -> (String) e.get("id"))
.orElseThrow(FieldNotFoundException::new);
.orElseThrow(() -> new FieldNotFoundException("execution.id"));
String cdEventsType =
Optional.ofNullable(preference)
.map(p -> (String) p.get("cdEventsType"))
.orElseThrow(FieldNotFoundException::new);
.orElseThrow(() -> new FieldNotFoundException("notifications.cdEventsType"));
String eventsBrokerUrl =
Optional.ofNullable(preference)
.map(p -> (String) p.get("address"))
.orElseThrow(FieldNotFoundException::new);
.orElseThrow(() -> new FieldNotFoundException("notifications.address"));

CloudEvent cdEvent =
cdEventsBuilderService.createCDEvent(
Expand Down

0 comments on commit 1f783f6

Please sign in to comment.