Skip to content

Commit

Permalink
[dashboard datasource] Retry on 504 errors (#975)
Browse files Browse the repository at this point in the history
* [dashboard datasource] Retry on 504 errors
* Use TimeoutRead instead of TimeoutCreate

Signed-off-by: Jared Ledvina <[email protected]>
  • Loading branch information
jaredledvina authored Mar 16, 2021
1 parent 75d9b95 commit 89a723c
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions datadog/data_source_datadog_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"

datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
Expand Down Expand Up @@ -43,30 +44,34 @@ func dataSourceDatadogDashboardRead(d *schema.ResourceData, meta interface{}) er
datadogClientV1 := providerConf.DatadogClientV1
authV1 := providerConf.AuthV1

dashResponse, _, err := datadogClientV1.DashboardsApi.ListDashboards(authV1).Execute()

if err != nil {
return utils.TranslateClientError(err, "error querying dashboard")
}
return resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
dashResponse, httpresp, err := datadogClientV1.DashboardsApi.ListDashboards(authV1).Execute()
if err != nil {
if httpresp != nil && httpresp.StatusCode == 504 {
return resource.RetryableError(utils.TranslateClientError(err, "error querying dashboard, retrying"))
}
return resource.NonRetryableError(utils.TranslateClientError(err, "error querying dashboard"))
}

searchedName := d.Get("name")
var foundDashes []datadogV1.DashboardSummaryDefinition
searchedName := d.Get("name")
var foundDashes []datadogV1.DashboardSummaryDefinition

for _, dash := range dashResponse.GetDashboards() {
if dash.GetTitle() == searchedName {
foundDashes = append(foundDashes, dash)
for _, dash := range dashResponse.GetDashboards() {
if dash.GetTitle() == searchedName {
foundDashes = append(foundDashes, dash)
}
}
}

if len(foundDashes) == 0 {
return fmt.Errorf("Couldn't find a dashboard named %s", searchedName)
} else if len(foundDashes) > 1 {
return fmt.Errorf("%s returned more than one dashboard", searchedName)
}
if len(foundDashes) == 0 {
return resource.NonRetryableError(fmt.Errorf("Couldn't find a dashboard named %s", searchedName))
} else if len(foundDashes) > 1 {
return resource.NonRetryableError(fmt.Errorf("%s returned more than one dashboard", searchedName))
}

d.SetId(foundDashes[0].GetId())
d.Set("url", foundDashes[0].GetUrl())
d.Set("title", foundDashes[0].GetTitle())
d.SetId(foundDashes[0].GetId())
d.Set("url", foundDashes[0].GetUrl())
d.Set("title", foundDashes[0].GetTitle())

return nil
return nil
})
}

0 comments on commit 89a723c

Please sign in to comment.