Skip to content

Commit 5bb9459

Browse files
skarimotherve
andauthored
Upgrade to SDKv2 (#1082)
* Upgrade to terraform sdk v2 (#972) * initial terraform sdk v2 upgrade * fix provider import * fix context propagation * fix tests * update all resources crud methods to use context aware methods * record cassettes * add 3rd party licenses * lint and update 3rd party licenses * regenerate docs * apply code review suggestions * Update 3rd party licenses * Migrate diag func, cleanups * Static check fixes * Bump SDK version * Add licenses * Minor tests and CI tweaks * Remove silenced docs * fix monitor tests * Don't test 14, try to fix job reporting Co-authored-by: Thomas Hervé <thomas.herve@datadoghq.com>
1 parent 70732d1 commit 5bb9459

500 files changed

Lines changed: 26689 additions & 81115 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.azure-pipelines/all.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ resources:
3131
jobs:
3232
- job: IntegrationTests
3333
pool:
34-
vmImage: "Ubuntu-16.04"
34+
vmImage: "Ubuntu-20.04"
3535
container:
3636
image: golang:latest
3737
services:

.github/workflows/test.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
test:
2727
strategy:
2828
matrix:
29+
terraform_version: ["0.15.4", "0.13.7"]
2930
platform: [ubuntu-latest, macos-latest, windows-latest]
3031
runs-on: ${{ matrix.platform }}
3132
steps:
@@ -45,6 +46,17 @@ jobs:
4546
run: make vet
4647
- name: License Check
4748
run: make license-check
49+
- name: Setup terraform CLI
50+
uses: hashicorp/setup-terraform@v1
51+
with:
52+
terraform_version: ${{ matrix.terraform_version }}
53+
terraform_wrapper: false
54+
- name: Set TF_ACC_TERRAFORM_PATH env var on non-windows platform
55+
if: ${{ matrix.platform != 'windows-latest' }}
56+
run: echo "TF_ACC_TERRAFORM_PATH=$(which terraform)" >> $GITHUB_ENV
57+
- name: Set TF_ACC_TERRAFORM_PATH env var on windows platform
58+
if: ${{ matrix.platform == 'windows-latest' }}
59+
run: echo ("TF_ACC_TERRAFORM_PATH=" + (Get-Command terraform).Path) >> $env:GITHUB_ENV
4860
- name: Test
4961
run: make testall
5062
env:

LICENSE-3rdparty.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,9 @@ go.sum,github.com/Microsoft/go-winio,MIT,2015 Microsoft
192192
go.sum,github.com/konsorten/go-windows-terminal-sequences,MIT,2017 marvin + konsorten GmbH
193193
go.sum,github.com/sirupsen/logrus,MIT,2014 Simon Eskildsen
194194
go.sum,golang.org/x/term,BSD-3-Clause,2009 The Go Authors
195+
go.sum,github.com/hashicorp/go-cty,MIT,Copyright (c) 2017-2018 Martin Atkins
196+
go.sum,github.com/hashicorp/terraform-plugin-go,MPL-2.0,
197+
go.sum,github.com/hashicorp/terraform-plugin-sdk/v2,MPL-2.0,
198+
go.sum,github.com/nsf/jsondiff,MIT,Copyright (C) 2019 nsf <no.smile.face@gmail.com>
199+
go.sum,github.com/apparentlymart/go-textseg/v13,MIT,Copyright (c) 2017-2018 Martin Atkins
200+
go.sum,github.com/klauspost/compress,BSD-3-Clause,Copyright (c) 2019 Klaus Post

datadog/data_source_datadog_dashboard.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package datadog
22

33
import (
4+
"context"
45
"fmt"
56

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

89
datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
9-
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
11-
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1214
)
1315

1416
func dataSourceDatadogDashboard() *schema.Resource {
1517
return &schema.Resource{
1618
Description: "Use this data source to retrieve information about an existing dashboard, for use in other resources. In particular, it can be used in a monitor message to link to a specific dashboard.",
17-
Read: dataSourceDatadogDashboardRead,
19+
ReadContext: dataSourceDatadogDashboardRead,
1820

1921
Schema: map[string]*schema.Schema{
2022
"name": {
@@ -38,13 +40,12 @@ func dataSourceDatadogDashboard() *schema.Resource {
3840
}
3941
}
4042

41-
func dataSourceDatadogDashboardRead(d *schema.ResourceData, meta interface{}) error {
42-
43+
func dataSourceDatadogDashboardRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
4344
providerConf := meta.(*ProviderConfiguration)
4445
datadogClientV1 := providerConf.DatadogClientV1
4546
authV1 := providerConf.AuthV1
4647

47-
return resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
48+
err := resource.RetryContext(ctx, d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
4849
dashResponse, httpresp, err := datadogClientV1.DashboardsApi.ListDashboards(authV1)
4950
if err != nil {
5051
if httpresp != nil && (httpresp.StatusCode == 504 || httpresp.StatusCode == 502) {
@@ -63,7 +64,7 @@ func dataSourceDatadogDashboardRead(d *schema.ResourceData, meta interface{}) er
6364
}
6465

6566
if len(foundDashes) == 0 {
66-
return resource.NonRetryableError(fmt.Errorf("Couldn't find a dashboard named %s", searchedName))
67+
return resource.NonRetryableError(fmt.Errorf("couldn't find a dashboard named %s", searchedName))
6768
} else if len(foundDashes) > 1 {
6869
return resource.NonRetryableError(fmt.Errorf("%s returned more than one dashboard", searchedName))
6970
}
@@ -74,4 +75,9 @@ func dataSourceDatadogDashboardRead(d *schema.ResourceData, meta interface{}) er
7475

7576
return nil
7677
})
78+
if err != nil {
79+
return diag.FromErr(err)
80+
}
81+
82+
return nil
7783
}

datadog/data_source_datadog_dashboard_list.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package datadog
22

33
import (
4-
"fmt"
4+
"context"
55
"strconv"
66

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

99
datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
10-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
11-
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1213
)
1314

1415
func dataSourceDatadogDashboardList() *schema.Resource {
1516
return &schema.Resource{
1617
Description: "Use this data source to retrieve information about an existing dashboard list, for use in other resources. In particular, it can be used in a dashboard to register it in the list.",
17-
Read: dataSourceDatadogDashboardListRead,
18+
ReadContext: dataSourceDatadogDashboardListRead,
1819

1920
Schema: map[string]*schema.Schema{
2021
"name": {
@@ -27,7 +28,7 @@ func dataSourceDatadogDashboardList() *schema.Resource {
2728
}
2829
}
2930

30-
func dataSourceDatadogDashboardListRead(d *schema.ResourceData, meta interface{}) error {
31+
func dataSourceDatadogDashboardListRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
3132

3233
providerConf := meta.(*ProviderConfiguration)
3334
datadogClientV1 := providerConf.DatadogClientV1
@@ -36,7 +37,7 @@ func dataSourceDatadogDashboardListRead(d *schema.ResourceData, meta interface{}
3637
listResponse, _, err := datadogClientV1.DashboardListsApi.ListDashboardLists(authV1)
3738

3839
if err != nil {
39-
return utils.TranslateClientError(err, "error querying dashboard lists")
40+
return utils.TranslateClientErrorDiag(err, "error querying dashboard lists")
4041
}
4142

4243
searchedName := d.Get("name")
@@ -50,7 +51,7 @@ func dataSourceDatadogDashboardListRead(d *schema.ResourceData, meta interface{}
5051
}
5152

5253
if foundList == nil {
53-
return fmt.Errorf("Couldn't find a dashboard list named %s", searchedName)
54+
return diag.Errorf("Couldn't find a dashboard list named %s", searchedName)
5455
}
5556

5657
id := foundList.GetId()

datadog/data_source_datadog_ip_ranges.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package datadog
22

33
import (
4+
"context"
45
"log"
56
"strings"
67

7-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
810
)
911

1012
func dataSourceDatadogIPRanges() *schema.Resource {
1113
return &schema.Resource{
1214
Description: "Use this data source to retrieve information about Datadog's IP addresses.",
13-
Read: dataSourceDatadogIPRangesRead,
15+
ReadContext: dataSourceDatadogIPRangesRead,
1416

1517
// IP ranges are divided between ipv4 and ipv6
1618
Schema: map[string]*schema.Schema{
@@ -114,14 +116,14 @@ func dataSourceDatadogIPRanges() *schema.Resource {
114116
}
115117
}
116118

117-
func dataSourceDatadogIPRangesRead(d *schema.ResourceData, meta interface{}) error {
119+
func dataSourceDatadogIPRangesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
118120
providerConf := meta.(*ProviderConfiguration)
119121
datadogClientV1 := providerConf.DatadogClientV1
120122
authV1 := providerConf.AuthV1
121123

122124
ipAddresses, _, err := datadogClientV1.IPRangesApi.GetIPRanges(authV1)
123125
if err != nil {
124-
return err
126+
return diag.FromErr(err)
125127
}
126128

127129
// v4 and v6
@@ -181,12 +183,12 @@ func dataSourceDatadogIPRangesRead(d *schema.ResourceData, meta interface{}) err
181183
err = d.Set("synthetics_ipv4_by_location", ipv4PrefixesByLocationMap)
182184
if err != nil {
183185
log.Printf("[DEBUG] Error setting IPv4 prefixes by location: %s", err)
184-
return err
186+
return diag.FromErr(err)
185187
}
186188
err = d.Set("synthetics_ipv6_by_location", ipv6PrefixesByLocationMap)
187189
if err != nil {
188190
log.Printf("[DEBUG] Error setting IPv6 prefixes by location: %s", err)
189-
return err
191+
return diag.FromErr(err)
190192
}
191193

192194
return nil

datadog/data_source_datadog_monitor.go

Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package datadog
22

33
import (
4+
"context"
45
"fmt"
56
"sort"
67
"strconv"
@@ -9,13 +10,14 @@ import (
910
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
1011

1112
datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
12-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1315
)
1416

1517
func dataSourceDatadogMonitor() *schema.Resource {
1618
return &schema.Resource{
1719
Description: "Use this data source to retrieve information about an existing monitor for use in other resources.",
18-
Read: dataSourceDatadogMonitorRead,
20+
ReadContext: dataSourceDatadogMonitorRead,
1921
Schema: map[string]*schema.Schema{
2022
"name_filter": {
2123
Description: "A monitor name to limit the search.",
@@ -63,45 +65,10 @@ func dataSourceDatadogMonitor() *schema.Resource {
6365
},
6466

6567
// Options
66-
"thresholds": {
67-
Description: "Alert thresholds of the monitor.",
68-
Deprecated: "Define `monitor_thresholds` list with one element instead.",
69-
Type: schema.TypeMap,
70-
Computed: true,
71-
Elem: &schema.Resource{
72-
Schema: map[string]*schema.Schema{
73-
"ok": {
74-
Type: schema.TypeFloat,
75-
Computed: true,
76-
},
77-
"warning": {
78-
Type: schema.TypeFloat,
79-
Computed: true,
80-
},
81-
"critical": {
82-
Type: schema.TypeFloat,
83-
Computed: true,
84-
},
85-
"unknown": {
86-
Type: schema.TypeFloat,
87-
Computed: true,
88-
},
89-
"warning_recovery": {
90-
Type: schema.TypeFloat,
91-
Computed: true,
92-
},
93-
"critical_recovery": {
94-
Type: schema.TypeFloat,
95-
Computed: true,
96-
},
97-
},
98-
},
99-
},
10068
"monitor_thresholds": {
10169
Description: "Alert thresholds of the monitor.",
10270
Type: schema.TypeList,
10371
Computed: true,
104-
MaxItems: 1,
10572
Elem: &schema.Resource{
10673
Schema: map[string]*schema.Schema{
10774
"ok": {
@@ -131,28 +98,9 @@ func dataSourceDatadogMonitor() *schema.Resource {
13198
},
13299
},
133100
},
134-
"threshold_windows": {
135-
Description: "Mapping containing `recovery_window` and `trigger_window` values, e.g. `last_15m`. This is only used by anomaly monitors.",
136-
Deprecated: "Define `monitor_threshold_windows` list with one element instead.",
137-
Type: schema.TypeMap,
138-
Computed: true,
139-
Elem: &schema.Resource{
140-
Schema: map[string]*schema.Schema{
141-
"recovery_window": {
142-
Type: schema.TypeString,
143-
Computed: true,
144-
},
145-
"trigger_window": {
146-
Type: schema.TypeString,
147-
Computed: true,
148-
},
149-
},
150-
},
151-
},
152101
"monitor_threshold_windows": {
153102
Description: "Mapping containing `recovery_window` and `trigger_window` values, e.g. `last_15m`. This is only used by anomaly monitors.",
154103
Type: schema.TypeList,
155-
MaxItems: 1,
156104
Computed: true,
157105
Elem: &schema.Resource{
158106
Schema: map[string]*schema.Schema{
@@ -240,7 +188,7 @@ func dataSourceDatadogMonitor() *schema.Resource {
240188
}
241189
}
242190

243-
func dataSourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) error {
191+
func dataSourceDatadogMonitorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
244192
providerConf := meta.(*ProviderConfiguration)
245193
datadogClientV1 := providerConf.DatadogClientV1
246194
authV1 := providerConf.AuthV1
@@ -258,13 +206,13 @@ func dataSourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) erro
258206

259207
monitors, _, err := datadogClientV1.MonitorsApi.ListMonitors(authV1, *optionalParams)
260208
if err != nil {
261-
return utils.TranslateClientError(err, "error querying monitors")
209+
return utils.TranslateClientErrorDiag(err, "error querying monitors")
262210
}
263211
if len(monitors) > 1 {
264-
return fmt.Errorf("your query returned more than one result, please try a more specific search criteria")
212+
return diag.Errorf("your query returned more than one result, please try a more specific search criteria")
265213
}
266214
if len(monitors) == 0 {
267-
return fmt.Errorf("your query returned no result, please try a less specific search criteria")
215+
return diag.Errorf("your query returned no result, please try a less specific search criteria")
268216
}
269217

270218
m := monitors[0]
@@ -301,9 +249,7 @@ func dataSourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) erro
301249
}
302250

303251
var tags []string
304-
for _, s := range m.GetTags() {
305-
tags = append(tags, s)
306-
}
252+
tags = append(tags, m.GetTags()...)
307253
sort.Strings(tags)
308254

309255
d.SetId(strconv.FormatInt(m.GetId(), 10))
@@ -312,19 +258,17 @@ func dataSourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) erro
312258
d.Set("query", m.GetQuery())
313259
d.Set("type", m.GetType())
314260

315-
d.Set("thresholds", thresholds)
316261
if err := d.Set("monitor_thresholds", []interface{}{thresholds}); err != nil {
317-
return err
262+
return diag.FromErr(err)
318263
}
319-
d.Set("threshold_windows", thresholdWindows)
320264
if err := d.Set("monitor_threshold_windows", []interface{}{thresholdWindows}); err != nil {
321-
return err
265+
return diag.FromErr(err)
322266
}
323267

324268
d.Set("new_host_delay", m.Options.GetNewHostDelay())
325269
d.Set("evaluation_delay", m.Options.GetEvaluationDelay())
326270
d.Set("notify_no_data", m.Options.GetNotifyNoData())
327-
d.Set("no_data_timeframe", m.Options.NoDataTimeframe)
271+
d.Set("no_data_timeframe", m.Options.GetNoDataTimeframe())
328272
d.Set("renotify_interval", m.Options.GetRenotifyInterval())
329273
d.Set("notify_audit", m.Options.GetNotifyAudit())
330274
d.Set("timeout_h", m.Options.GetTimeoutH())

0 commit comments

Comments
 (0)