Skip to content

Commit

Permalink
Issue GoogleCloudPlatform#911. Add a --validate arg for the datadog p…
Browse files Browse the repository at this point in the history
…rovider to comply with the corresponding schema arg. this is passed as a string and parsed to a bool. it complies with the existing convention of giving priority to the arg before the env var DATADOG_VALIDATE. this defaults to true to comply with the provider schema behavior (GoogleCloudPlatform#914)
  • Loading branch information
ethpran authored May 6, 2021
1 parent ddf071d commit e00592e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cmd/provider_cmd_datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
)

func newCmdDatadogImporter(options ImportOptions) *cobra.Command {
var apiKey, appKey, apiURL string
var apiKey, appKey, apiURL, validate string
cmd := &cobra.Command{
Use: "datadog",
Short: "Import current state to Terraform configuration from Datadog",
Long: "Import current state to Terraform configuration from Datadog",
RunE: func(cmd *cobra.Command, args []string) error {
provider := newDataDogProvider()
err := Import(provider, options, []string{apiKey, appKey, apiURL})
err := Import(provider, options, []string{apiKey, appKey, apiURL, validate})
if err != nil {
return err
}
Expand All @@ -39,6 +39,7 @@ func newCmdDatadogImporter(options ImportOptions) *cobra.Command {
cmd.PersistentFlags().StringVarP(&apiKey, "api-key", "", "", "YOUR_DATADOG_API_KEY or env param DATADOG_API_KEY")
cmd.PersistentFlags().StringVarP(&appKey, "app-key", "", "", "YOUR_DATADOG_APP_KEY or env param DATADOG_APP_KEY")
cmd.PersistentFlags().StringVarP(&apiURL, "api-url", "", "", "YOUR_DATADOG_API_URL or env param DATADOG_HOST")
cmd.PersistentFlags().StringVar(&validate, "validate", "", "bool-parsable values only or env param DATADOG_VALIDATE. Enables validation of the provided API and APP keys during provider initialization. Default is true. When false, api_key and app_key won't be checked")
return cmd
}

Expand Down
4 changes: 2 additions & 2 deletions docs/datadog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Example:

```
./terraformer import datadog --resources=monitor --api-key=YOUR_DATADOG_API_KEY // or DATADOG_API_KEY in env --app-key=YOUR_DATADOG_APP_KEY // or DATADOG_APP_KEY in env --api-url=DATADOG_API_URL // or DATADOG_HOST in env
./terraformer import datadog --resources=monitor --filter=monitor=id1:id2:id4 --api-key=YOUR_DATADOG_API_KEY // or DATADOG_API_KEY in env --app-key=YOUR_DATADOG_APP_KEY // or DATADOG_APP_KEY in env
./terraformer import datadog --resources=monitor --api-key=YOUR_DATADOG_API_KEY // or DATADOG_API_KEY in env --app-key=YOUR_DATADOG_APP_KEY // or DATADOG_APP_KEY in env --api-url=DATADOG_API_URL // or DATADOG_HOST in env --validate=VALIDATE_BOOL // or DATADOG_VALIDATE in env
./terraformer import datadog --resources=monitor --filter=monitor=id1:id2:id4 --api-key=YOUR_DATADOG_API_KEY // or DATADOG_API_KEY in env --app-key=YOUR_DATADOG_APP_KEY // or DATADOG_APP_KEY in env --validate=VALIDATE_BOOL // or DATADOG_VALIDATE in env
```

List of supported Datadog services:
Expand Down
20 changes: 20 additions & 0 deletions providers/datadog/datadog_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net/url"
"os"
"strconv"

datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
datadogV2 "github.com/DataDog/datadog-api-client-go/api/v2/datadog"
Expand All @@ -32,6 +33,7 @@ type DatadogProvider struct { //nolint
apiKey string
appKey string
apiURL string
validate bool
authV1 context.Context
authV2 context.Context
datadogClientV1 *datadogV1.APIClient
Expand Down Expand Up @@ -66,6 +68,22 @@ func (p *DatadogProvider) Init(args []string) error {
p.apiURL = v
}

if args[3] != "" {
validate, validateErr := strconv.ParseBool(args[3])
if validateErr != nil {
return fmt.Errorf(`invalid validate arg : %v`, validateErr)
}
p.validate = validate
} else if os.Getenv("DATADOG_VALIDATE") != "" {
validate, validateErr := strconv.ParseBool(os.Getenv("DATADOG_VALIDATE"))
if validateErr != nil {
return fmt.Errorf(`invalid DATADOG_VALIDATE env var : %v`, validateErr)
}
p.validate = validate
} else {
p.validate = true
}

// Initialize the Datadog V1 API client
authV1 := context.WithValue(
context.Background(),
Expand Down Expand Up @@ -147,6 +165,7 @@ func (p *DatadogProvider) GetConfig() cty.Value {
"api_key": cty.StringVal(p.apiKey),
"app_key": cty.StringVal(p.appKey),
"api_url": cty.StringVal(p.apiURL),
"validate": cty.BoolVal(p.validate),
})
}

Expand All @@ -164,6 +183,7 @@ func (p *DatadogProvider) InitService(serviceName string, verbose bool) error {
"api-key": p.apiKey,
"app-key": p.appKey,
"api-url": p.apiURL,
"validate": p.validate,
"authV1": p.authV1,
"authV2": p.authV2,
"datadogClientV1": p.datadogClientV1,
Expand Down

0 comments on commit e00592e

Please sign in to comment.