From 829221d50b5edd6c18cfb961e4e2ddcdfee9811c Mon Sep 17 00:00:00 2001 From: Connor Ameres Date: Wed, 8 May 2024 12:48:26 -0400 Subject: [PATCH 1/4] Add Support For Datadog Powerpacks --- go.mod | 7 ++- go.sum | 13 ++-- providers/datadog/datadog_provider.go | 1 + providers/datadog/powerpack.go | 91 +++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 providers/datadog/powerpack.go diff --git a/go.mod b/go.mod index ac705d93b9..786244db74 100644 --- a/go.mod +++ b/go.mod @@ -257,7 +257,7 @@ require ( github.com/google/go-querystring v1.1.0 // indirect github.com/google/gofuzz v1.1.0 // indirect github.com/google/jsonapi v1.0.0 // indirect - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.5.0 // indirect github.com/googleapis/gax-go/v2 v2.12.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect @@ -323,7 +323,7 @@ require ( go.opencensus.io v0.24.0 // indirect golang.org/x/crypto v0.17.0 // indirect golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect - golang.org/x/net v0.15.0 // indirect + golang.org/x/net v0.17.0 // indirect golang.org/x/sync v0.3.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/term v0.15.0 // indirect @@ -355,7 +355,7 @@ require ( cloud.google.com/go/cloudtasks v1.10.0 cloud.google.com/go/iam v0.13.0 cloud.google.com/go/monitoring v1.13.0 - github.com/DataDog/datadog-api-client-go/v2 v2.11.0 + github.com/DataDog/datadog-api-client-go/v2 v2.25.0 github.com/Myra-Security-GmbH/myrasec-go/v2 v2.28.0 github.com/manicminer/hamilton v0.44.0 github.com/opalsecurity/opal-go v1.0.19 @@ -376,6 +376,7 @@ require ( github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/jsonreference v0.19.5 // indirect github.com/go-openapi/swag v0.19.14 // indirect + github.com/goccy/go-json v0.10.2 // indirect github.com/golang-jwt/jwt/v4 v4.4.3 // indirect github.com/google/gnostic v0.5.7-v3refs // indirect github.com/google/s2a-go v0.1.4 // indirect diff --git a/go.sum b/go.sum index 31eccc180f..8b016428b8 100644 --- a/go.sum +++ b/go.sum @@ -253,8 +253,8 @@ github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/ChrisTrenkamp/goxpath v0.0.0-20170922090931-c385f95c6022/go.mod h1:nuWgzSkT5PnyOd+272uUmV0dnAnAn42Mk7PiQC5VzN4= -github.com/DataDog/datadog-api-client-go/v2 v2.11.0 h1:7KCEQ3S90PIH1GcqFHcnwDpNfZbqa2BsiF8OYmLb4Jk= -github.com/DataDog/datadog-api-client-go/v2 v2.11.0/go.mod h1:kntOqXEh1SmjwSDzW/eJkr9kS7EqttvEkelglWtJRbg= +github.com/DataDog/datadog-api-client-go/v2 v2.25.0 h1:9Zq42D6M3U///VDxjx2SS1g+EW55WhZYZFHtzM+cO4k= +github.com/DataDog/datadog-api-client-go/v2 v2.25.0/go.mod h1:QKOu6vscsh87fMY1lHfLEmNSunyXImj8BUaUWJXOehc= github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8= github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= github.com/IBM-Cloud/bluemix-go v0.0.0-20220624043500-d538cb4fd9be h1:PTW3J9z39tJYnmtdAxi8WvTWQXfkOflTJw6noyjnno4= @@ -770,6 +770,8 @@ github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWe github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ= github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0= github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gocql/gocql v0.0.0-20210707082121-9a3953d1826d h1:k544nNVphXK4Yt0FTduvOvCfJabEY/DMkdNw0zpCwBE= github.com/gocql/gocql v0.0.0-20210707082121-9a3953d1826d/go.mod h1:3gM2c4D3AnkISwBxGnMMsS8Oy4y2lhbPRsH4xnJrHG8= github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= @@ -893,8 +895,9 @@ github.com/google/s2a-go v0.1.4/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkj github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8= github.com/googleapis/enterprise-certificate-proxy v0.1.0/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8= github.com/googleapis/enterprise-certificate-proxy v0.2.0/go.mod h1:8C0jb7/mgJe/9KK8Lm7X9ctZC2t60YyIpYEI16jx0Qg= @@ -1864,8 +1867,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= -golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= -golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= diff --git a/providers/datadog/datadog_provider.go b/providers/datadog/datadog_provider.go index 1449a99ff7..d24311479d 100644 --- a/providers/datadog/datadog_provider.go +++ b/providers/datadog/datadog_provider.go @@ -171,6 +171,7 @@ func (p *DatadogProvider) GetSupportedService() map[string]terraformutils.Servic "logs_integration_pipeline": &LogsIntegrationPipelineGenerator{}, "logs_metric": &LogsMetricGenerator{}, "logs_pipeline_order": &LogsPipelineOrderGenerator{}, + "powerpack": &PowerpackGenerator{}, "integration_aws": &IntegrationAWSGenerator{}, "integration_aws_lambda_arn": &IntegrationAWSLambdaARNGenerator{}, "integration_aws_log_collection": &IntegrationAWSLogCollectionGenerator{}, diff --git a/providers/datadog/powerpack.go b/providers/datadog/powerpack.go new file mode 100644 index 0000000000..64304ae88a --- /dev/null +++ b/providers/datadog/powerpack.go @@ -0,0 +1,91 @@ +// Copyright 2018 The Terraformer Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package datadog + +import ( + "context" + "fmt" + + "github.com/DataDog/datadog-api-client-go/v2/api/datadog" + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +var ( + // PowerpackAllowEmptyValues ... + PowerpackAllowEmptyValues = []string{} +) + +// PowerpackGenerator ... +type PowerpackGenerator struct { + DatadogService +} + +func (g *PowerpackGenerator) createResources(powerpacks []datadogV2.PowerpackData) []terraformutils.Resource { + resources := []terraformutils.Resource{} + for _, powerpack := range powerpacks { + resourceName := powerpack.GetId() + resources = append(resources, g.createResource(resourceName)) + } + + return resources +} + +func (g *PowerpackGenerator) createResource(powerpackName string) terraformutils.Resource { + return terraformutils.NewSimpleResource( + powerpackName, + fmt.Sprintf("powerpack_%s", powerpackName), + "datadog_powerpack", + "datadog", + PowerpackAllowEmptyValues, + ) +} + +// InitResources Generate TerraformResources from Datadog API, +// from each powerpack create 1 TerraformResource. +// Need Powerpack Name as ID for terraform resource +func (g *PowerpackGenerator) InitResources() error { + datadogClient := g.Args["datadogClient"].(*datadog.APIClient) + auth := g.Args["auth"].(context.Context) + api := datadogV2.NewPowerpackApi(datadogClient) + + resources := []terraformutils.Resource{} + for _, filter := range g.Filter { + if filter.FieldPath == "id" && filter.IsApplicable("powerpack") { + for _, value := range filter.AcceptableValues { + // fixme: how to get a powerpack id? dashboard json? + powerpack, _, err := api.GetPowerpack(auth, value) + if err != nil { + return err + } + + resources = append(resources, g.createResource(powerpack.Data.GetId())) + } + } + } + + if len(resources) > 0 { + g.Resources = resources + return nil + } + + powerpacks, _, err := api.ListPowerpacks(auth) + if err != nil { + return err + } + g.Resources = g.createResources(powerpacks.GetData()) + return nil +} From b1c272bec41cfc4c334f7c3df631c1bef59b42a0 Mon Sep 17 00:00:00 2001 From: Connor Ameres Date: Wed, 8 May 2024 13:00:59 -0400 Subject: [PATCH 2/4] Add powerpack to datadog.md --- docs/datadog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/datadog.md b/docs/datadog.md index 9a07a0f521..dd6bd64034 100644 --- a/docs/datadog.md +++ b/docs/datadog.md @@ -105,6 +105,8 @@ Tag filters are order specific. For example, if your monitor has tags (in the or * `datadog_logs_index` * `logs_index_order` * `datadog_logs_index_order` +* `powerpack` + * `datadog_powerpack` * `integration_aws` * `datadog_integration_aws` * `integration_aws_lambda_arn` From 52c5daea2ea0f543e97c87629eea2968261bf9ea Mon Sep 17 00:00:00 2001 From: Connor Ameres Date: Wed, 8 May 2024 13:01:53 -0400 Subject: [PATCH 3/4] remove fixme comment for getting the powerpack id --- providers/datadog/powerpack.go | 1 - 1 file changed, 1 deletion(-) diff --git a/providers/datadog/powerpack.go b/providers/datadog/powerpack.go index 64304ae88a..61fabee7bb 100644 --- a/providers/datadog/powerpack.go +++ b/providers/datadog/powerpack.go @@ -66,7 +66,6 @@ func (g *PowerpackGenerator) InitResources() error { for _, filter := range g.Filter { if filter.FieldPath == "id" && filter.IsApplicable("powerpack") { for _, value := range filter.AcceptableValues { - // fixme: how to get a powerpack id? dashboard json? powerpack, _, err := api.GetPowerpack(auth, value) if err != nil { return err From beef04dd5ccb397b62043a57f03812749af31fdd Mon Sep 17 00:00:00 2001 From: Connor Ameres Date: Wed, 8 May 2024 16:04:56 -0400 Subject: [PATCH 4/4] Use ListPowerpacksWithPagination rather than ListPowerpacks --- providers/datadog/powerpack.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/providers/datadog/powerpack.go b/providers/datadog/powerpack.go index 61fabee7bb..1cee5402b0 100644 --- a/providers/datadog/powerpack.go +++ b/providers/datadog/powerpack.go @@ -81,10 +81,21 @@ func (g *PowerpackGenerator) InitResources() error { return nil } - powerpacks, _, err := api.ListPowerpacks(auth) - if err != nil { - return err + var powerpacks []datadogV2.PowerpackData + optionalParameters := &datadogV2.ListPowerpacksOptionalParameters{} + paginationChan, _ := api.ListPowerpacksWithPagination(auth, + *optionalParameters.WithPageLimit(1000)) + for { + pageResult, more := <-paginationChan + if !more { + break + } + if pageResult.Error != nil { + return pageResult.Error + } + powerpacks = append(powerpacks, pageResult.Item) } - g.Resources = g.createResources(powerpacks.GetData()) + + g.Resources = g.createResources(powerpacks) return nil }