Skip to content

Commit

Permalink
make 2 requests to retrieve synthetics test (#2068)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Zou <[email protected]>
  • Loading branch information
skarimo and nkzou authored Aug 23, 2023
1 parent 977ae7e commit 956c504
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 59 deletions.
30 changes: 13 additions & 17 deletions datadog/data_source_datadog_synthetics_test_.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package datadog
import (
"context"

"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

Expand Down Expand Up @@ -51,24 +49,22 @@ func dataSourceDatadogSyntheticsTestRead(ctx context.Context, d *schema.Resource
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth

tests, httpresp, err := apiInstances.GetSyntheticsApiV1().ListTests(auth)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpresp, "error getting synthetic tests")
}

urlRegex := regexp.MustCompile(`https:\/\/(.*)\.(datadoghq|ddog-gov)\.(com|eu)\/synthetics\/details\/`)
searchedId := urlRegex.ReplaceAllString(d.Get("test_id").(string), "")

for _, test := range tests.Tests {
if test.GetPublicId() == searchedId {
d.SetId(test.GetPublicId())
d.Set("name", test.GetName())
d.Set("tags", test.GetTags())
d.Set("url", test.GetConfig().Request.GetUrl())

return nil
}
if test, _, err := apiInstances.GetSyntheticsApiV1().GetAPITest(auth, searchedId); err == nil {
d.SetId(test.GetPublicId())
d.Set("name", test.GetName())
d.Set("tags", test.GetTags())
d.Set("url", test.Config.Request.GetUrl())
} else if test, _, err := apiInstances.GetSyntheticsApiV1().GetBrowserTest(auth, searchedId); err == nil {
d.SetId(test.GetPublicId())
d.Set("name", test.GetName())
d.Set("tags", test.GetTags())
d.Set("url", test.Config.Request.GetUrl())
} else {
return diag.Errorf("Couldn't retrieve synthetic test with id %s", searchedId)
}

return diag.Errorf("Couldn't find synthetic test with id %s", searchedId)
return nil
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-08-22T16:44:43.052494-04:00
2023-08-23T13:59:24.944112-04:00
Loading

0 comments on commit 956c504

Please sign in to comment.