From 2eecfe0fe6e8e2f11c9f767624270d37dd9d0c16 Mon Sep 17 00:00:00 2001 From: Jalander Ramagiri Date: Wed, 18 Oct 2023 13:19:21 +0100 Subject: [PATCH] Making CDEventCreator as an abstract --- .../echo/cdevents/CDEventCreator.java | 48 ++++++++++++++++- .../cdevents/CDEventPipelineRunFinished.java | 40 ++++++++------ .../cdevents/CDEventPipelineRunQueued.java | 29 +++++----- .../cdevents/CDEventPipelineRunStarted.java | 29 +++++----- .../echo/cdevents/CDEventTaskRunFinished.java | 54 ++++++++++++------- .../echo/cdevents/CDEventTaskRunStarted.java | 39 ++++++++------ .../echo/cdevents/CDEventsBuilderService.java | 1 - 7 files changed, 159 insertions(+), 81 deletions(-) diff --git a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventCreator.java b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventCreator.java index 8eb454b4b..9277fcb9c 100644 --- a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventCreator.java +++ b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventCreator.java @@ -18,6 +18,50 @@ import io.cloudevents.CloudEvent; -public interface CDEventCreator { - CloudEvent createCDEvent(); +public abstract class CDEventCreator { + abstract CloudEvent createCDEvent(); + + private String source; + private String subjectId; + private String subjectSource; + private String subjectUrl; + + public CDEventCreator(String source, String subjectId, String subjectSource, String subjectUrl) { + this.source = source; + this.subjectId = subjectId; + this.subjectSource = subjectSource; + this.subjectUrl = subjectUrl; + } + + public String getSource() { + return source; + } + + public void setSource(String source) { + this.source = source; + } + + public String getSubjectId() { + return subjectId; + } + + public void setSubjectId(String subjectId) { + this.subjectId = subjectId; + } + + public String getSubjectSource() { + return subjectSource; + } + + public void setSubjectSource(String subjectSource) { + this.subjectSource = subjectSource; + } + + public String getSubjectUrl() { + return subjectUrl; + } + + public void setSubjectUrl(String subjectUrl) { + this.subjectUrl = subjectUrl; + } } diff --git a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunFinished.java b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunFinished.java index 7af5483aa..200c18225 100644 --- a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunFinished.java +++ b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunFinished.java @@ -22,14 +22,9 @@ import io.cloudevents.CloudEvent; import java.net.URI; -public class CDEventPipelineRunFinished implements CDEventCreator { +public class CDEventPipelineRunFinished extends CDEventCreator { - private String source; - private String subjectId; - private String subjectSource; private String subjectPipelineName; - private String subjectUrl; - private String subjectError; public CDEventPipelineRunFinished( @@ -38,23 +33,36 @@ public CDEventPipelineRunFinished( String executionName, String spinnakerUrl, String status) { - this.source = spinnakerUrl; - this.subjectId = executionId; - this.subjectSource = spinnakerUrl; + super(spinnakerUrl, executionId, spinnakerUrl, executionUrl); this.subjectPipelineName = executionName; - this.subjectUrl = executionUrl; this.subjectError = status; } + public String getSubjectPipelineName() { + return subjectPipelineName; + } + + public void setSubjectPipelineName(String subjectPipelineName) { + this.subjectPipelineName = subjectPipelineName; + } + + public String getSubjectError() { + return subjectError; + } + + public void setSubjectError(String subjectError) { + this.subjectError = subjectError; + } + @Override public CloudEvent createCDEvent() { PipelineRunFinishedCDEvent cdEvent = new PipelineRunFinishedCDEvent(); - cdEvent.setSource(URI.create(source)); - cdEvent.setSubjectId(subjectId); - cdEvent.setSubjectSource(URI.create(subjectSource)); - cdEvent.setSubjectPipelineName(subjectSource); - cdEvent.setSubjectUrl(URI.create(subjectUrl)); - cdEvent.setSubjectErrors(subjectError); + cdEvent.setSource(URI.create(getSource())); + cdEvent.setSubjectId(getSubjectId()); + cdEvent.setSubjectSource(URI.create(getSubjectSource())); + cdEvent.setSubjectPipelineName(getSubjectPipelineName()); + cdEvent.setSubjectUrl(URI.create(getSubjectUrl())); + cdEvent.setSubjectErrors(getSubjectError()); if (subjectError.equals("complete")) { cdEvent.setSubjectOutcome(CDEventConstants.Outcome.SUCCESS); diff --git a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunQueued.java b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunQueued.java index 8a23781dd..0e95dd817 100644 --- a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunQueued.java +++ b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunQueued.java @@ -20,31 +20,32 @@ import io.cloudevents.CloudEvent; import java.net.URI; -public class CDEventPipelineRunQueued implements CDEventCreator { +public class CDEventPipelineRunQueued extends CDEventCreator { - private String source; - private String subjectId; - private String subjectSource; private String subjectPipelineName; - private String subjectUrl; public CDEventPipelineRunQueued( String executionId, String executionUrl, String executionName, String spinnakerUrl) { - this.source = spinnakerUrl; - this.subjectId = executionId; - this.subjectSource = spinnakerUrl; + super(spinnakerUrl, executionId, spinnakerUrl, executionUrl); this.subjectPipelineName = executionName; - this.subjectUrl = executionUrl; + } + + public String getSubjectPipelineName() { + return subjectPipelineName; + } + + public void setSubjectPipelineName(String subjectPipelineName) { + this.subjectPipelineName = subjectPipelineName; } @Override public CloudEvent createCDEvent() { PipelineRunQueuedCDEvent cdEvent = new PipelineRunQueuedCDEvent(); - cdEvent.setSource(URI.create(source)); - cdEvent.setSubjectId(subjectId); - cdEvent.setSubjectSource(URI.create(subjectSource)); - cdEvent.setSubjectPipelineName(subjectPipelineName); - cdEvent.setSubjectUrl(URI.create(subjectUrl)); + cdEvent.setSource(URI.create(getSource())); + cdEvent.setSubjectId(getSubjectId()); + cdEvent.setSubjectSource(URI.create(getSubjectSource())); + cdEvent.setSubjectPipelineName(getSubjectPipelineName()); + cdEvent.setSubjectUrl(URI.create(getSubjectUrl())); return CDEvents.cdEventAsCloudEvent(cdEvent); } diff --git a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunStarted.java b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunStarted.java index 642e83e18..b08074f35 100644 --- a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunStarted.java +++ b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventPipelineRunStarted.java @@ -21,31 +21,32 @@ import io.cloudevents.CloudEvent; import java.net.URI; -public class CDEventPipelineRunStarted implements CDEventCreator { +public class CDEventPipelineRunStarted extends CDEventCreator { - private String source; - private String subjectId; - private String subjectSource; private String subjectPipelineName; - private String subjectUrl; public CDEventPipelineRunStarted( String executionId, String executionUrl, String executionName, String spinnakerUrl) { - this.source = spinnakerUrl; - this.subjectId = executionId; - this.subjectSource = spinnakerUrl; + super(spinnakerUrl, executionId, spinnakerUrl, executionUrl); this.subjectPipelineName = executionName; - this.subjectUrl = executionUrl; + } + + public String getSubjectPipelineName() { + return subjectPipelineName; + } + + public void setSubjectPipelineName(String subjectPipelineName) { + this.subjectPipelineName = subjectPipelineName; } @Override public CloudEvent createCDEvent() { PipelineRunStartedCDEvent cdEvent = new PipelineRunStartedCDEvent(); - cdEvent.setSource(URI.create(source)); - cdEvent.setSubjectId(subjectId); - cdEvent.setSubjectSource(URI.create(subjectSource)); - cdEvent.setSubjectPipelineName(subjectPipelineName); - cdEvent.setSubjectUrl(URI.create(subjectUrl)); + cdEvent.setSource(URI.create(getSource())); + cdEvent.setSubjectId(getSubjectId()); + cdEvent.setSubjectSource(URI.create(getSubjectSource())); + cdEvent.setSubjectPipelineName(getSubjectPipelineName()); + cdEvent.setSubjectUrl(URI.create(getSubjectUrl())); return CDEvents.cdEventAsCloudEvent(cdEvent); } diff --git a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventTaskRunFinished.java b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventTaskRunFinished.java index 9ad40eb42..48a0f12cc 100644 --- a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventTaskRunFinished.java +++ b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventTaskRunFinished.java @@ -22,13 +22,9 @@ import io.cloudevents.CloudEvent; import java.net.URI; -public class CDEventTaskRunFinished implements CDEventCreator { +public class CDEventTaskRunFinished extends CDEventCreator { - private String source; - private String subjectId; - private String subjectSource; private String subjectTaskName; - private String subjectUrl; private String subjectPipelineRunId; private String subjectError; @@ -38,29 +34,49 @@ public CDEventTaskRunFinished( String executionName, String spinnakerUrl, String status) { - this.source = spinnakerUrl; - this.subjectId = executionId; - this.subjectSource = spinnakerUrl; + super(spinnakerUrl, executionId, spinnakerUrl, executionUrl); this.subjectTaskName = executionName; - this.subjectUrl = executionUrl; this.subjectPipelineRunId = executionId; this.subjectError = status; } + public String getSubjectTaskName() { + return subjectTaskName; + } + + public void setSubjectTaskName(String subjectTaskName) { + this.subjectTaskName = subjectTaskName; + } + + public String getSubjectPipelineRunId() { + return subjectPipelineRunId; + } + + public void setSubjectPipelineRunId(String subjectPipelineRunId) { + this.subjectPipelineRunId = subjectPipelineRunId; + } + + public String getSubjectError() { + return subjectError; + } + + public void setSubjectError(String subjectError) { + this.subjectError = subjectError; + } + @Override public CloudEvent createCDEvent() { TaskRunFinishedCDEvent cdEvent = new TaskRunFinishedCDEvent(); - cdEvent.setSource(URI.create(source)); - - cdEvent.setSubjectId(subjectId); - cdEvent.setSubjectSource(URI.create(source)); - cdEvent.setSubjectTaskName(subjectTaskName); - cdEvent.setSubjectUrl(URI.create(subjectUrl)); - cdEvent.setSubjectErrors(subjectError); - cdEvent.setSubjectPipelineRunId(subjectPipelineRunId); - if (subjectError.equals("complete")) { + cdEvent.setSource(URI.create(getSource())); + cdEvent.setSubjectId(getSubjectId()); + cdEvent.setSubjectSource(URI.create(getSubjectSource())); + cdEvent.setSubjectTaskName(getSubjectTaskName()); + cdEvent.setSubjectUrl(URI.create(getSubjectUrl())); + cdEvent.setSubjectErrors(getSubjectError()); + cdEvent.setSubjectPipelineRunId(getSubjectPipelineRunId()); + if (getSubjectError().equals("complete")) { cdEvent.setSubjectOutcome(CDEventConstants.Outcome.SUCCESS); - } else if (subjectError.equals("failed")) { + } else if (getSubjectError().equals("failed")) { cdEvent.setSubjectOutcome(CDEventConstants.Outcome.FAILURE); } diff --git a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventTaskRunStarted.java b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventTaskRunStarted.java index 2c8624151..a854507fd 100644 --- a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventTaskRunStarted.java +++ b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventTaskRunStarted.java @@ -21,35 +21,44 @@ import io.cloudevents.CloudEvent; import java.net.URI; -public class CDEventTaskRunStarted implements CDEventCreator { +public class CDEventTaskRunStarted extends CDEventCreator { - private String source; - private String subjectId; - private String subjectSource; private String subjectTaskName; - private String subjectUrl; private String subjectPipelineRunId; public CDEventTaskRunStarted( String executionId, String executionUrl, String executionName, String spinnakerUrl) { - this.source = spinnakerUrl; - this.subjectId = executionId; - this.subjectSource = spinnakerUrl; + super(spinnakerUrl, executionId, spinnakerUrl, executionUrl); this.subjectTaskName = executionName; - this.subjectUrl = executionUrl; this.subjectPipelineRunId = executionId; } + public String getSubjectTaskName() { + return subjectTaskName; + } + + public void setSubjectTaskName(String subjectTaskName) { + this.subjectTaskName = subjectTaskName; + } + + public String getSubjectPipelineRunId() { + return subjectPipelineRunId; + } + + public void setSubjectPipelineRunId(String subjectPipelineRunId) { + this.subjectPipelineRunId = subjectPipelineRunId; + } + @Override public CloudEvent createCDEvent() { TaskRunStartedCDEvent cdEvent = new TaskRunStartedCDEvent(); - cdEvent.setSource(URI.create(source)); + cdEvent.setSource(URI.create(getSource())); - cdEvent.setSubjectId(subjectId); - cdEvent.setSubjectSource(URI.create(subjectSource)); - cdEvent.setSubjectTaskName(subjectTaskName); - cdEvent.setSubjectUrl(URI.create(subjectUrl)); - cdEvent.setSubjectPipelineRunId(subjectPipelineRunId); + cdEvent.setSubjectId(getSubjectId()); + cdEvent.setSubjectSource(URI.create(getSubjectSource())); + cdEvent.setSubjectTaskName(getSubjectTaskName()); + cdEvent.setSubjectUrl(URI.create(getSubjectUrl())); + cdEvent.setSubjectPipelineRunId(getSubjectPipelineRunId()); return CDEvents.cdEventAsCloudEvent(cdEvent); } diff --git a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventsBuilderService.java b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventsBuilderService.java index e6940b538..df5e042c8 100644 --- a/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventsBuilderService.java +++ b/echo-notifications/src/main/groovy/com/netflix/spinnaker/echo/cdevents/CDEventsBuilderService.java @@ -71,7 +71,6 @@ public CloudEvent createCDEvent( Optional.ofNullable(preference) .map(p -> (String) p.get("cdEventsType")) .orElseThrow(FieldNotFoundException::new); - log.info("Event type {} received to create CDEvent.", cdEventsType); CDEventCreator cdEventCreator = null; // This map can be added with more event types that Spinnaker needs to send