github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/ansSendEvent.go (about)

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/SAP/jenkins-library/pkg/ans"
     6  	"github.com/SAP/jenkins-library/pkg/log"
     7  	"github.com/SAP/jenkins-library/pkg/telemetry"
     8  	"time"
     9  )
    10  
    11  func ansSendEvent(config ansSendEventOptions, telemetryData *telemetry.CustomData) {
    12  	err := runAnsSendEvent(&config, &ans.ANS{})
    13  	if err != nil {
    14  		log.Entry().WithError(err).Fatal("step execution failed")
    15  	}
    16  }
    17  
    18  func runAnsSendEvent(config *ansSendEventOptions, c ans.Client) error {
    19  	ansServiceKey, err := ans.UnmarshallServiceKeyJSON(config.AnsServiceKey)
    20  	if err != nil {
    21  		log.SetErrorCategory(log.ErrorConfiguration)
    22  		return err
    23  	}
    24  	c.SetServiceKey(ansServiceKey)
    25  
    26  	event := ans.Event{
    27  		EventType: config.EventType,
    28  		Severity:  config.Severity,
    29  		Category:  config.Category,
    30  		Subject:   config.Subject,
    31  		Body:      config.Body,
    32  		Priority:  config.Priority,
    33  		Tags:      config.Tags,
    34  		Resource: &ans.Resource{
    35  			ResourceName:     config.ResourceName,
    36  			ResourceType:     config.ResourceType,
    37  			ResourceInstance: config.ResourceInstance,
    38  			Tags:             config.ResourceTags,
    39  		},
    40  	}
    41  
    42  	if GeneralConfig.Verbose {
    43  		eventJson, _ := json.MarshalIndent(event, "", "  ")
    44  		log.Entry().Infof("Event details: %s", eventJson)
    45  	}
    46  
    47  	if err = event.Validate(); err != nil {
    48  		log.SetErrorCategory(log.ErrorConfiguration)
    49  		return err
    50  	}
    51  	// We set the time
    52  	event.EventTimestamp = time.Now().Unix()
    53  	if err = c.Send(event); err != nil {
    54  		log.SetErrorCategory(log.ErrorService)
    55  	}
    56  	return err
    57  }