Skip to content

Commit 89a723c

Browse files
authored
[dashboard datasource] Retry on 504 errors (#975)
* [dashboard datasource] Retry on 504 errors * Use TimeoutRead instead of TimeoutCreate Signed-off-by: Jared Ledvina <[email protected]>
1 parent 75d9b95 commit 89a723c

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

datadog/data_source_datadog_dashboard.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
77

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

46-
dashResponse, _, err := datadogClientV1.DashboardsApi.ListDashboards(authV1).Execute()
47-
48-
if err != nil {
49-
return utils.TranslateClientError(err, "error querying dashboard")
50-
}
47+
return resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
48+
dashResponse, httpresp, err := datadogClientV1.DashboardsApi.ListDashboards(authV1).Execute()
49+
if err != nil {
50+
if httpresp != nil && httpresp.StatusCode == 504 {
51+
return resource.RetryableError(utils.TranslateClientError(err, "error querying dashboard, retrying"))
52+
}
53+
return resource.NonRetryableError(utils.TranslateClientError(err, "error querying dashboard"))
54+
}
5155

52-
searchedName := d.Get("name")
53-
var foundDashes []datadogV1.DashboardSummaryDefinition
56+
searchedName := d.Get("name")
57+
var foundDashes []datadogV1.DashboardSummaryDefinition
5458

55-
for _, dash := range dashResponse.GetDashboards() {
56-
if dash.GetTitle() == searchedName {
57-
foundDashes = append(foundDashes, dash)
59+
for _, dash := range dashResponse.GetDashboards() {
60+
if dash.GetTitle() == searchedName {
61+
foundDashes = append(foundDashes, dash)
62+
}
5863
}
59-
}
6064

61-
if len(foundDashes) == 0 {
62-
return fmt.Errorf("Couldn't find a dashboard named %s", searchedName)
63-
} else if len(foundDashes) > 1 {
64-
return fmt.Errorf("%s returned more than one dashboard", searchedName)
65-
}
65+
if len(foundDashes) == 0 {
66+
return resource.NonRetryableError(fmt.Errorf("Couldn't find a dashboard named %s", searchedName))
67+
} else if len(foundDashes) > 1 {
68+
return resource.NonRetryableError(fmt.Errorf("%s returned more than one dashboard", searchedName))
69+
}
6670

67-
d.SetId(foundDashes[0].GetId())
68-
d.Set("url", foundDashes[0].GetUrl())
69-
d.Set("title", foundDashes[0].GetTitle())
71+
d.SetId(foundDashes[0].GetId())
72+
d.Set("url", foundDashes[0].GetUrl())
73+
d.Set("title", foundDashes[0].GetTitle())
7074

71-
return nil
75+
return nil
76+
})
7277
}

0 commit comments

Comments
 (0)