The documentation is very unclear when it comes to the DataDog Downtime API. I've based my example off of the following documentation: https://docs.datadoghq.com/api/latest/downtimes/#schedule-a-downtime
I have the following environment variables set:
DD_APP_TOKEN: Your datadog application token.
DD_API_TOKEN: Your datadog API token.
DD_SITE: The address of the DD API server. Usually "datadoghq.com"
My example code:
import (
"context"
"encoding/json"
"fmt"
"github.com/DataDog/datadog-api-client-go/api/v1/datadog"
"github.com/slack-go/slack"
"os"
"time"
)
func main() {
ctx := datadog.NewDefaultContext(context.Background())
var DataDogFiltersByTag = []string{
"printing",
"OTIS",
"env:production",
}
Message := "Downtime Test"
var ctime int64 = time.Now().Unix() // Current Time
var dtime int64 = ctime (45 * 60) // 45 minutes
body := *datadog.NewDowntime() // Downtime | Schedule a downtime request body.
body.Message = &Message
body.Start = &ctime
end := datadog.NullableInt64{}
end.Set(&dtime)
body.End = end
body.MonitorTags = &DataDogFiltersByTag
configuration := datadog.NewConfiguration()
apiClient := datadog.NewAPIClient(configuration)
resp, r, err := apiClient.DowntimesApi.CreateDowntime(ctx, body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `DowntimesApi.CreateDowntime`: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
// response from `CreateDowntime`: Downtime
responseContent, _ := json.MarshalIndent(resp, "", " ")
fmt.Fprintf(os.Stdout, "Response from DowntimesApi.CreateDowntime:\n%s\n", responseContent)
}
This results in the following error message:
/private/var/folders/p0/jbhf5p4n3f3c3rc5rc_rc6400000gn/T/GoLand/___6go_build_ClusterUpgradeNotification
Error when calling `DowntimesApi.CreateDowntime`: 400 Bad Request
Full HTTP response: &{400 Bad Request 400 HTTP/1.1 1 1 map[Cache-Control:[no-cache] Connection:[keep-alive] Content-Length:[39] Content-Security-Policy:[frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report] Content-Type:[application/json] Date:[Wed, 03 Nov 2021 00:33:09 GMT] Pragma:[no-cache] Strict-Transport-Security:[max-age=15724800;] X-Content-Type-Options:[nosniff] X-Frame-Options:[SAMEORIGIN]] {{"errors": ["Invalid scope parameter"]}} 39 [] false false map[] 0xc00019a800 0xc0003342c0}
Response from DowntimesApi.CreateDowntime:
{}
Process finished with the exit code 1
CodePudding user response:
Scope is a required parameter in datadog.CreateDowntime(). Adding body.SetScope([]string{"*"}) resolved my issue.