Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
luisr-escobar committed Aug 10, 2021
1 parent 004dcfc commit f706795
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions pkg/event_handler/action_triggered_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os"
"time"

cloudevents "github.com/cloudevents/sdk-go/v2"
Expand Down Expand Up @@ -74,13 +73,14 @@ func (eh ActionTriggeredHandler) HandleEvent() error {
httpMethod = val.(string)
}

if flowApiPath == "" || httpMethod == "" || retries == 0 {
return fmt.Errorf("mandatory values missing")
if flowApiPath == "" || httpMethod == "" {
eh.Logger.Error("mandatory values missing")
return err
}

err = triggerFlow(actionTriggeredEvent, flowApiPath, httpMethod, int(retries))
if err != nil {
msg := "Could not trigger " + flowApiPath + err.Error()
msg := "Could not trigger " + flowApiPath + " " + err.Error()
eh.Logger.Error(msg)
sendErr := eh.sendEvent(keptnv2.GetFinishedEventType(keptnv2.ActionTaskName),
eh.getActionFinishedEvent(keptnv2.ResultFailed, keptnv2.StatusErrored, *actionTriggeredEvent, msg))
Expand Down Expand Up @@ -129,12 +129,10 @@ func (eh ActionTriggeredHandler) getActionStartedEvent(actionTriggeredEvent kept
}

func (eh ActionTriggeredHandler) sendEvent(eventType string, data interface{}) error {
keptnHandler, err := keptnv2.NewKeptn(&eh.Event, keptn.KeptnOpts{
EventBrokerURL: os.Getenv("EVENTBROKER"),
})
keptnHandler, err := keptnv2.NewKeptn(&eh.Event, keptn.KeptnOpts{})

if err != nil {
eh.Logger.Error("Could not initialize Keptn handler: " + err.Error())
return err
return errors.New("Failed to initialize Keptn handler: " + err.Error())
}

source, _ := url.Parse("servicenow-service")
Expand All @@ -148,16 +146,22 @@ func (eh ActionTriggeredHandler) sendEvent(eventType string, data interface{}) e
event.SetData(cloudevents.ApplicationJSON, data)

err = keptnHandler.SendCloudEvent(event)

if err != nil {
eh.Logger.Error("Could not send " + eventType + " event: " + err.Error())
return err
}

return nil
}

// ToggleFeature sets a value for a feature flag
func triggerFlow(actionEvent *keptnv2.ActionTriggeredEventData, flowApiPath string, httpMethod string, retries int) error {

if retries == 0 {
retries = 10
}

creds, err := credentials.GetServicenowCredentials()
if err != nil {
return fmt.Errorf("failed to load ServiceNow credentials: %v", err)
Expand Down Expand Up @@ -217,15 +221,17 @@ func waitForFlowExecution(snowInstanceURL string, flowContextExecID string, snow
flowState := value.String()

if flowState == "WAITING" || flowState == "IN_PROGRESS" || flowState == "QUEUED" || flowState == "CONTINUE_SYNC " {
fmt.Printf("Flow State: %v" + flowState)
fmt.Println("Flow State: " + flowState)
fmt.Printf("Flow execution not yet complete sleeping 10 seconds before retry, %v retries left\n", retries)
time.Sleep(30 * time.Second)
retries -= 1
} else if flowState == "ERROR" || flowState == "CANCELLED" {
return fmt.Errorf("Flow execution completed with failure, response code %s. State value from API call: %s", resp.Status, string(body))
} else {
} else if flowState == "COMPLETE" {
fmt.Println("Flow execution completed, state value from API: " + flowState)
return nil
} else {
return fmt.Errorf("Flow execution completed with failure")
}

}
Expand Down

0 comments on commit f706795

Please sign in to comment.