Skip to content

Commit 5857c14

Browse files
authored
Merge pull request #29005 from hashicorp/td-context-everywhere
Converts AWS API calls to and provider CRUD calls to `Context`-aware versions
2 parents a97f654 + f5a7a59 commit 5857c14

File tree

3,447 files changed

+101327
-84564
lines changed

Some content is hidden

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

3,447 files changed

+101327
-84564
lines changed

.ci/.golangci.yml

-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ linters:
99

1010
run:
1111
timeout: 10m
12-
skip-dirs:
13-
- .ci/providerlint/vendor

.ci/.golangci2.yml

-2
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,3 @@ linters-settings:
106106

107107
run:
108108
timeout: 75m
109-
skip-dirs:
110-
- .ci/providerlint/vendor

.ci/.semgrep-caps-aws-ec2.yml

+2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ rules:
1313
metavariable: $NAME
1414
patterns:
1515
- pattern-regex: "(?i)AWS"
16+
- focus-metavariable: $NAME
1617
severity: WARNING
18+
1719
- id: aws-in-const-name
1820
languages:
1921
- go

.ci/.semgrep.yml

+1-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rules:
1010
- metavariable-regex:
1111
metavariable: "$FUNCNAME"
1212
regex: "^TestAcc[^_]+_([a-zA-Z]+[dD]isappears|[^_]+_disappears)$"
13+
- focus-metavariable: $FUNCNAME
1314
severity: WARNING
1415

1516
- id: aws-sdk-go-multiple-service-imports
@@ -869,16 +870,6 @@ rules:
869870
- pattern: "[]string{..., string($X), ...}"
870871
severity: WARNING
871872

872-
- id: avoid-context-TODO-in-tests
873-
languages: [go]
874-
message: Prefer using context.Background() instead of context.TODO()
875-
paths:
876-
include:
877-
- "internal/**/*_test.go"
878-
patterns:
879-
- pattern: context.TODO
880-
severity: ERROR
881-
882873
- id: avoid-context-CRUD-handlers
883874
languages: [go]
884875
message: Prefer using WithoutTimeout CRUD handlers instead of Context variants

.ci/semgrep/acctest/naming/naming.yml

+28
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,31 @@ rules:
1212
regex: "^testAcc((?!Check)[a-zA-Z]+Destroy$|(Check)?Destroy)"
1313
# regex: "^testAccCheckDestroy"
1414
severity: WARNING
15+
16+
- id: destroy-check-with-provider
17+
languages: [go]
18+
message: The check destroy with provider function should match the pattern "testAccCheck<Resource>DestroyWithProvider".
19+
paths:
20+
include:
21+
- "internal/**/*_test.go"
22+
patterns:
23+
- pattern: func $FUNCNAME(...) { ... }
24+
- metavariable-regex:
25+
metavariable: "$FUNCNAME"
26+
regex: '^testAccCheck[a-zA-Z]+Destroy(?!With)Provider'
27+
severity: WARNING
28+
29+
- id: destroy-check-signature
30+
languages: [go]
31+
message: The check destroy function should have the correct signature
32+
paths:
33+
include:
34+
- "internal/**/*_test.go"
35+
patterns:
36+
- pattern: func $FUNCNAME(...) { ... }
37+
- metavariable-regex:
38+
metavariable: "$FUNCNAME"
39+
regex: "^testAccCheck[a-zA-Z]+Destroy(?!WithProvider)"
40+
- pattern-not: func $FUNCNAME(s *terraform.State) error { ... }
41+
- pattern-not: func $FUNCNAME(...) resource.TestCheckFunc { ... }
42+
severity: WARNING

.ci/semgrep/migrate/context.yml

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
rules:
2+
- id: aws-api-context
3+
languages: [go]
4+
message: All AWS API calls should use the WithContext version
5+
paths: &paths
6+
include:
7+
- internal/service/*
8+
- internal/acctest/*
9+
exclude:
10+
# `pattern-not` not working
11+
- internal/service/kafkaconnect
12+
# WIP
13+
- internal/acctest/acctest.go
14+
patterns:
15+
- pattern: |
16+
$CONN.$API(...)
17+
- metavariable-regex:
18+
metavariable: $CONN
19+
regex: ^(?!conns)\w*([cC]onn)
20+
- metavariable-regex:
21+
metavariable: $API
22+
# This weird construction is to get around greedy matching
23+
regex: ^(?!.*WithContext).*$
24+
- pattern-not: |
25+
$CONN.$APIV2(ctx, ...)
26+
- pattern-not: connect.$API(...)
27+
- pattern-not: tfconnect.$API(...)
28+
- pattern-not: codestarconnections.$API(...)
29+
- pattern-not: tfcodestarconnections.$API(...)
30+
- pattern-not: tfdirectconnect.$API(...)
31+
- pattern-not: kafkaconnect.$API(...)
32+
- pattern-not: tfkafkaconnect.$API(...)
33+
- pattern-not: conn.Handlers.$X(...)
34+
- pattern-not: conn.Handlers.$X.$Y(...)
35+
severity: ERROR
36+
- id: aws-helpers-context
37+
languages: [go]
38+
message: All AWS helper functions should take a context.Context
39+
paths: *paths
40+
patterns:
41+
- pattern: |
42+
$INNER(conn, ...)
43+
- pattern-not: $V2(conn, ...)
44+
- metavariable-regex:
45+
metavariable: $V2
46+
regex: ^New\w+Paginator$
47+
severity: ERROR
48+
- id: retry-context
49+
languages: [go]
50+
message: Waiter and retry functions should use the Context version
51+
paths: *paths
52+
patterns:
53+
- pattern-either:
54+
- pattern: $X.WaitForState()
55+
- pattern: resource.Retry()
56+
- pattern: tfresource.RetryWhen(...)
57+
- pattern: tfresource.RetryWhenAWSErrCodeEquals(...)
58+
- pattern: tfresource.RetryWhenAWSErrMessageContains(...)
59+
- pattern: tfresource.RetryUntilNotFound(...)
60+
- pattern: tfresource.RetryWhenNotFound(...)
61+
- pattern: tfresource.RetryWhenNewResourceNotFound(...)
62+
- pattern: tfresource.WaitUntil(...)
63+
severity: WARNING
64+
- id: context-todo
65+
languages: [go]
66+
message: Should not use `context.TODO()`
67+
paths: *paths
68+
pattern: context.TODO()
69+
severity: ERROR
70+
- id: schema-noop
71+
languages: [go]
72+
message: Should use `schema.NoopContext` instead of `schema.Noop`
73+
paths: *paths
74+
pattern: schema.Noop
75+
severity: ERROR

GNUmakefile

+10-6
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ gh-workflows-lint:
174174

175175
golangci-lint:
176176
@echo "==> Checking source code with golangci-lint..."
177-
@golangci-lint run --config .ci/.golangci.yml ./$(PKG_NAME)/...
178-
@golangci-lint run --config .ci/.golangci2.yml ./$(PKG_NAME)/...
177+
@golangci-lint run \
178+
--config .ci/.golangci.yml \
179+
--config .ci/.golangci2.yml \
180+
./$(PKG_NAME)/...
179181

180182
providerlint:
181183
@echo "==> Checking source code with providerlint..."
@@ -202,11 +204,11 @@ providerlint:
202204
-XR005=false \
203205
-XS001=false \
204206
-XS002=false \
205-
./$(PKG_NAME)/service/... ./$(PKG_NAME)/provider/...
207+
./internal/service/... ./internal/provider/...
206208

207209
importlint:
208210
@echo "==> Checking source code with importlint..."
209-
@impi --local . --scheme stdThirdPartyLocal ./$(PKG_NAME)/...
211+
@impi --local . --scheme stdThirdPartyLocal ./internal/...
210212

211213
tools:
212214
cd .ci/providerlint && $(GO_VER) install .
@@ -263,6 +265,7 @@ semgrep:
263265
semall:
264266
@echo "==> Running Semgrep checks locally (must have semgrep installed)..."
265267
@semgrep --error --metrics=off \
268+
$(if $(filter-out $(origin PKG), undefined),--include $(PKG_NAME),) \
266269
--config .ci/.semgrep.yml \
267270
--config .ci/.semgrep-caps-aws-ec2.yml \
268271
--config .ci/.semgrep-configs.yml \
@@ -271,11 +274,12 @@ semall:
271274
--config .ci/.semgrep-service-name2.yml \
272275
--config .ci/.semgrep-service-name3.yml \
273276
--config .ci/semgrep/acctest/ \
277+
--config .ci/semgrep/migrate/ \
274278
--config 'r/dgryski.semgrep-go.badnilguard' \
275279
--config 'r/dgryski.semgrep-go.errnilcheck' \
276-
--config 'r/dgryski.semgrep-go.marshaljson' \
280+
--config 'r/dgryski.semgrep-go.marshaljson' \
277281
--config 'r/dgryski.semgrep-go.nilerr' \
278-
--config 'r/dgryski.semgrep-go.oddifsequence' \
282+
--config 'r/dgryski.semgrep-go.oddifsequence' \
279283
--config 'r/dgryski.semgrep-go.oserrors'
280284

281285
skaff:

docs/adding-a-tag-resource.md

+11-8
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ import (
2020
)
2121

2222
func TestAcc{Service}Tag_basic(t *testing.T) {
23+
ctx := acctest.Context(t)
2324
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
2425
resourceName := "aws_{service}_tag.test"
2526

2627
resource.ParallelTest(t, resource.TestCase{
2728
PreCheck: func() { acctest.PreCheck(t) },
2829
ErrorCheck: acctest.ErrorCheck(t, {Service}.EndpointsID),
2930
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
30-
CheckDestroy: testAccCheck{Service}TagDestroy,
31+
CheckDestroy: testAccCheck{Service}TagDestroy(ctx),
3132
Steps: []resource.TestStep{
3233
{
3334
Config: testAcc{Service}TagConfig(rName, "key1", "value1"),
3435
Check: resource.ComposeTestCheckFunc(
35-
testAccCheck{Service}TagExists(resourceName),
36+
testAccCheck{Service}TagExists(ctx, resourceName),
3637
resource.TestCheckResourceAttr(resourceName, "key", "key1"),
3738
resource.TestCheckResourceAttr(resourceName, "value", "value1"),
3839
),
@@ -47,20 +48,21 @@ func TestAcc{Service}Tag_basic(t *testing.T) {
4748
}
4849

4950
func TestAcc{Service}Tag_disappears(t *testing.T) {
51+
ctx := acctest.Context(t)
5052
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
5153
resourceName := "aws_{service}_tag.test"
5254

5355
resource.ParallelTest(t, resource.TestCase{
5456
PreCheck: func() { acctest.PreCheck(t) },
5557
ErrorCheck: acctest.ErrorCheck(t, {Service}.EndpointsID),
5658
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
57-
CheckDestroy: testAccCheck{Service}TagDestroy,
59+
CheckDestroy: testAccCheck{Service}TagDestroy(ctx),
5860
Steps: []resource.TestStep{
5961
{
6062
Config: testAcc{Service}TagConfig(rName, "key1", "value1"),
6163
Check: resource.ComposeTestCheckFunc(
62-
testAccCheck{Service}TagExists(resourceName),
63-
acctest.CheckResourceDisappears(acctest.Provider, resourceAws{Service}Tag(), resourceName),
64+
testAccCheck{Service}TagExists(ctx, resourceName),
65+
acctest.CheckResourceDisappears(ctx, acctest.Provider, resourceAws{Service}Tag(), resourceName),
6466
),
6567
ExpectNonEmptyPlan: true,
6668
},
@@ -69,19 +71,20 @@ func TestAcc{Service}Tag_disappears(t *testing.T) {
6971
}
7072

7173
func TestAcc{Service}Tag_Value(t *testing.T) {
74+
ctx := acctest.Context(t)
7275
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
7376
resourceName := "aws_{service}_tag.test"
7477

7578
resource.ParallelTest(t, resource.TestCase{
7679
PreCheck: func() { acctest.PreCheck(t) },
7780
ErrorCheck: acctest.ErrorCheck(t, {Service}.EndpointsID),
7881
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
79-
CheckDestroy: testAccCheck{Service}TagDestroy,
82+
CheckDestroy: testAccCheck{Service}TagDestroy(ctx),
8083
Steps: []resource.TestStep{
8184
{
8285
Config: testAcc{Service}TagConfig(rName, "key1", "value1"),
8386
Check: resource.ComposeTestCheckFunc(
84-
testAccCheck{Service}TagExists(resourceName),
87+
testAccCheck{Service}TagExists(ctx, resourceName),
8588
resource.TestCheckResourceAttr(resourceName, "key", "key1"),
8689
resource.TestCheckResourceAttr(resourceName, "value", "value1"),
8790
),
@@ -94,7 +97,7 @@ func TestAcc{Service}Tag_Value(t *testing.T) {
9497
{
9598
Config: testAcc{Service}TagConfig(rName, "key1", "value1updated"),
9699
Check: resource.ComposeTestCheckFunc(
97-
testAccCheck{Service}TagExists(resourceName),
100+
testAccCheck{Service}TagExists(ctx, resourceName),
98101
resource.TestCheckResourceAttr(resourceName, "key", "key1"),
99102
resource.TestCheckResourceAttr(resourceName, "value", "value1updated"),
100103
),

0 commit comments

Comments
 (0)