Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Generated client v2 API #477

Merged
merged 11 commits into from
Apr 29, 2020
46 changes: 45 additions & 1 deletion datadog/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/DataDog/datadog-api-client-go/api/v1/datadog"
skarimo marked this conversation as resolved.
Show resolved Hide resolved
datadogV2 "github.com/DataDog/datadog-api-client-go/api/v2/datadog"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform-plugin-sdk/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -77,7 +78,9 @@ func Provider() terraform.ResourceProvider {
type ProviderConfiguration struct {
CommunityClient *datadogCommunity.Client
DatadogClientV1 *datadog.APIClient
DatadogClientV2 *datadogV2.APIClient
Auth context.Context
skarimo marked this conversation as resolved.
Show resolved Hide resolved
AuthV2 context.Context
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
Expand Down Expand Up @@ -105,7 +108,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}
log.Printf("[INFO] Datadog Client successfully validated.")

// Initialize the official Datadog client
// Initialize the official Datadog V1 API client
auth := context.WithValue(
context.Background(),
datadog.ContextAPIKeys,
Expand All @@ -127,10 +130,36 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}
}
datadogClient := datadog.NewAPIClient(config)

// Initialize the official Datadog V2 API client
authV2 := context.WithValue(
context.Background(),
datadogV2.ContextAPIKeys,
map[string]datadogV2.APIKey{
"apiKeyAuth": {
Key: d.Get("api_key").(string),
},
"appKeyAuth": {
Key: d.Get("app_key").(string),
},
},
)
configV2 := datadogV2.NewConfiguration()
if apiURL := d.Get("api_url").(string); apiURL != "" {
if strings.Contains(apiURL, "datadoghq.eu") {
authV2 = context.WithValue(authV2, datadogV2.ContextServerVariables, map[string]string{
"site": "datadoghq.eu",
})
}
}
datadogClientV2 := datadogV2.NewAPIClient(configV2)

return &ProviderConfiguration{
CommunityClient: communityClient,
DatadogClientV1: datadogClient,
DatadogClientV2: datadogClientV2,
Auth: auth,
AuthV2: authV2,
}, nil
}

Expand All @@ -148,3 +177,18 @@ func translateClientError(err error, msg string) error {

return fmt.Errorf(msg+": %s", err.Error())
}

func translateClientErrorV2(err error, msg string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not have everything in one method?

if msg == "" {
msg = "an error occurred"
}

if _, ok := err.(datadogV2.GenericOpenAPIError); ok {
return fmt.Errorf(msg+": %s", err.Error())
}
skarimo marked this conversation as resolved.
Show resolved Hide resolved
if errUrl, ok := err.(*url.Error); ok {
return fmt.Errorf(msg+" (url.Error): %s", errUrl)
}

return fmt.Errorf(msg+": %s", err.Error())
}
31 changes: 30 additions & 1 deletion datadog/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/DataDog/datadog-api-client-go/api/v1/datadog"
datadogV2 "github.com/DataDog/datadog-api-client-go/api/v2/datadog"
"github.com/dnaeon/go-vcr/cassette"
"github.com/dnaeon/go-vcr/recorder"
"github.com/hashicorp/go-cleanhttp"
Expand Down Expand Up @@ -141,7 +142,21 @@ func testProviderConfigure(r *recorder.Recorder) schema.ConfigureFunc {
},
)

//config.HTTPClient
// Initialize the official datadog v2 API client
authV2 := context.WithValue(
context.Background(),
datadogV2.ContextAPIKeys,
map[string]datadogV2.APIKey{
"apiKeyAuth": datadogV2.APIKey{
Key: d.Get("api_key").(string),
},
"appKeyAuth": datadogV2.APIKey{
Key: d.Get("app_key").(string),
},
},
)

//Datadog V1 API config.HTTPClient
config := datadog.NewConfiguration()
config.Debug = true
config.HTTPClient = c
Expand All @@ -153,11 +168,25 @@ func testProviderConfigure(r *recorder.Recorder) schema.ConfigureFunc {
}
}
datadogClient := datadog.NewAPIClient(config)
//Datadog V2 API config.HTTPClient
configV2 := datadogV2.NewConfiguration()
configV2.Debug = true
configV2.HTTPClient = c
if apiURL := d.Get("api_url").(string); apiURL != "" {
if strings.Contains(apiURL, "datadoghq.eu") {
auth = context.WithValue(auth, datadogV2.ContextServerVariables, map[string]string{
"site": "datadoghq.eu",
})
}
}
skarimo marked this conversation as resolved.
Show resolved Hide resolved
datadogClientV2 := datadogV2.NewAPIClient(configV2)

return &ProviderConfiguration{
CommunityClient: communityClient,
DatadogClientV1: datadogClient,
DatadogClientV2: datadogClientV2,
Auth: auth,
AuthV2: authV2,
}, nil
}
}
Expand Down