我不理解 Downtime API 的工作原理,我正在尝试自动化调度停机时间。

huangapple go评论83阅读模式
英文:

I don't understand how the Downtime API works and I'm trying to automate scheduling downtime

问题

文档在涉及DataDog Downtime API时非常不清楚。我根据以下文档编写了示例代码:https://docs.datadoghq.com/api/latest/downtimes/#schedule-a-downtime

我设置了以下环境变量:

    DD_APP_TOKEN: 您的DataDog应用程序令牌。
    DD_API_TOKEN: 您的DataDog API令牌。
    DD_SITE: DD API服务器的地址。通常为“datadoghq.com”。

我的示例代码:

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()		// 当前时间
	var dtime int64 = ctime + (45 * 60)		// 45分钟后

	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, "调用`DowntimesApi.CreateDowntime`时出错:%v\n", err)
		fmt.Fprintf(os.Stderr, "完整的HTTP响应:%v\n", r)
	}
	// `CreateDowntime`的响应:Downtime
	responseContent, _ := json.MarshalIndent(resp, "", "  ")
	fmt.Fprintf(os.Stdout, "DowntimesApi.CreateDowntime的响应:\n%s\n", responseContent)
}

这导致以下错误消息:

/private/var/folders/p0/jbhf5p4n3f3c3rc5rc_rc6400000gn/T/GoLand/___6go_build_ClusterUpgradeNotification
调用`DowntimesApi.CreateDowntime`时出错:400 Bad Request
完整的HTTP响应:&{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}
DowntimesApi.CreateDowntime的响应:
{}

代码部分已翻译完毕,以上是翻译结果。

英文:

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

答案1

得分: 0

datadog.CreateDowntime()中,Scope是一个必需的参数。通过添加body.SetScope([]string{"*"})来解决了我的问题。

英文:

Scope is a required parameter in datadog.CreateDowntime(). Adding body.SetScope([]string{"*"}) resolved my issue.

huangapple
  • 本文由 发表于 2021年11月3日 08:47:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/69818484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定