88 "io/ioutil"
99 "net/http"
1010 "net/url"
11- "os"
1211 "time"
1312
1413 cloudevents "github.com/cloudevents/sdk-go/v2"
@@ -74,13 +73,14 @@ func (eh ActionTriggeredHandler) HandleEvent() error {
7473 httpMethod = val .(string )
7574 }
7675
77- if flowApiPath == "" || httpMethod == "" || retries == 0 {
78- return fmt .Errorf ("mandatory values missing" )
76+ if flowApiPath == "" || httpMethod == "" {
77+ eh .Logger .Error ("mandatory values missing" )
78+ return err
7979 }
8080
8181 err = triggerFlow (actionTriggeredEvent , flowApiPath , httpMethod , int (retries ))
8282 if err != nil {
83- msg := "Could not trigger " + flowApiPath + err .Error ()
83+ msg := "Could not trigger " + flowApiPath + " " + err .Error ()
8484 eh .Logger .Error (msg )
8585 sendErr := eh .sendEvent (keptnv2 .GetFinishedEventType (keptnv2 .ActionTaskName ),
8686 eh .getActionFinishedEvent (keptnv2 .ResultFailed , keptnv2 .StatusErrored , * actionTriggeredEvent , msg ))
@@ -129,12 +129,10 @@ func (eh ActionTriggeredHandler) getActionStartedEvent(actionTriggeredEvent kept
129129}
130130
131131func (eh ActionTriggeredHandler ) sendEvent (eventType string , data interface {}) error {
132- keptnHandler , err := keptnv2 .NewKeptn (& eh .Event , keptn.KeptnOpts {
133- EventBrokerURL : os .Getenv ("EVENTBROKER" ),
134- })
132+ keptnHandler , err := keptnv2 .NewKeptn (& eh .Event , keptn.KeptnOpts {})
133+
135134 if err != nil {
136- eh .Logger .Error ("Could not initialize Keptn handler: " + err .Error ())
137- return err
135+ return errors .New ("Failed to initialize Keptn handler: " + err .Error ())
138136 }
139137
140138 source , _ := url .Parse ("servicenow-service" )
@@ -148,16 +146,22 @@ func (eh ActionTriggeredHandler) sendEvent(eventType string, data interface{}) e
148146 event .SetData (cloudevents .ApplicationJSON , data )
149147
150148 err = keptnHandler .SendCloudEvent (event )
149+
151150 if err != nil {
152151 eh .Logger .Error ("Could not send " + eventType + " event: " + err .Error ())
153152 return err
154153 }
154+
155155 return nil
156156}
157157
158158// ToggleFeature sets a value for a feature flag
159159func triggerFlow (actionEvent * keptnv2.ActionTriggeredEventData , flowApiPath string , httpMethod string , retries int ) error {
160160
161+ if retries == 0 {
162+ retries = 10
163+ }
164+
161165 creds , err := credentials .GetServicenowCredentials ()
162166 if err != nil {
163167 return fmt .Errorf ("failed to load ServiceNow credentials: %v" , err )
@@ -217,15 +221,17 @@ func waitForFlowExecution(snowInstanceURL string, flowContextExecID string, snow
217221 flowState := value .String ()
218222
219223 if flowState == "WAITING" || flowState == "IN_PROGRESS" || flowState == "QUEUED" || flowState == "CONTINUE_SYNC " {
220- fmt .Printf ("Flow State: %v " + flowState )
224+ fmt .Println ("Flow State: " + flowState )
221225 fmt .Printf ("Flow execution not yet complete sleeping 10 seconds before retry, %v retries left\n " , retries )
222226 time .Sleep (30 * time .Second )
223227 retries -= 1
224228 } else if flowState == "ERROR" || flowState == "CANCELLED" {
225229 return fmt .Errorf ("Flow execution completed with failure, response code %s. State value from API call: %s" , resp .Status , string (body ))
226- } else {
230+ } else if flowState == "COMPLETE" {
227231 fmt .Println ("Flow execution completed, state value from API: " + flowState )
228232 return nil
233+ } else {
234+ return fmt .Errorf ("Flow execution completed with failure" )
229235 }
230236
231237 }
0 commit comments