opsgenie / opsgenie-go-sdk-v2

Opsgenie GO SDK v2
Apache License 2.0
34 stars 63 forks source link

Cannot define immediate notification in notification rule step #84

Open vthiery opened 3 years ago

vthiery commented 3 years ago

I want to define a notification rule step for a "New Alert" with no delay (so that I immediately receive the notification). The Opsgenie API documentation indicates that the sendAfter field must be specified for New Alert and Assigned Alert notification rules:

Time period (in minute) when notification will be sent after. Valid and Mandatory only for New Alert and Assigned Alert notification rules. sendAfter parameter should be given as an object which has a timeAmount field that takes amount as minutes.

Nevertheless, specifying 0 as TimeAmount won't work because the field would be omitted:

type SendAfter struct {
    TimeAmount uint32 `json:"timeAmount,omitempty"`
    TimeUnit   string `json:"timeUnit,omitempty"`
}

On the other hand, the API is perfectly happy to receive 0.

Would simply removing omitempty be a viable solution?

trabab commented 3 years ago

You can use implementation similar to createRuleStep func to execute the above use case.

func NewNotifiactionClient() (*notification.Client, error) {
    config := client.Config{
        ApiKey:         "******",
        OpsGenieAPIURL: client.ApiUrl("*******"),
    }
    var alertCli, cliErr = notification.NewClient(&config)
    if cliErr != nil {
        message := "Can not create the alert client. " + cliErr.Error()
        return nil, errors.New(message)
    }
    return alertCli, nil
}

func createRuleStep() {
    var cli, _ = NewNotifiactionClient()
    var createRequest = notification.CreateRuleStepRequest{}

    createRequest.UserIdentifier = "123"

    createRequest.RuleId = "123"

    createRequest.Contact.To = "ab@email.com"

    createRequest.Contact.MethodOfContact = og.Email
    b := true
    createRequest.Enabled = &b
    timeAmount := uint32(0)
    sendAfter := og.SendAfter{TimeAmount: &timeAmount, TimeUnit: "minutes"}

    createRequest.SendAfter = &sendAfter
    response, _ := cli.CreateRuleStep(nil, &createRequest)
    fmt.Printf("%+v\n", response)
}
jwebb commented 3 years ago

Any news on this? It's a one-line fix. (trabab's proposed workaround is not valid - cannot possibly compile - since TimeAmount is not a pointer.)