-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathdata_source_datadog_synthetics_locations.go
50 lines (40 loc) · 1.38 KB
/
data_source_datadog_synthetics_locations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package datadog
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceDatadogSyntheticsLocations() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to retrieve Datadog's Synthetics Locations (to be used in Synthetics tests).",
ReadContext: dataSourceDatadogSyntheticsLocationsRead,
SchemaFunc: func() map[string]*schema.Schema {
// Locations are a map of IDs to names
return map[string]*schema.Schema{
"locations": {
Description: "A map of available Synthetics location IDs to names for Synthetics tests.",
Type: schema.TypeMap,
Computed: true,
},
}
},
}
}
func dataSourceDatadogSyntheticsLocationsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
syntheticsLocations, _, err := apiInstances.GetSyntheticsApiV1().ListLocations(auth)
if err != nil {
return diag.FromErr(err)
}
locationsMap := make(map[string]string)
for _, location := range syntheticsLocations.GetLocations() {
locationsMap[location.GetId()] = location.GetName()
}
if len(locationsMap) > 0 {
d.SetId("datadog-synthetics-location")
}
d.Set("locations", locationsMap)
return nil
}