Skip to content

Commit 7b533e4

Browse files
authored
Merge pull request #47899 from hashicorp/td-quicksight-update-schema-more
Moves SDKv2 primitive caching to `sdkschema`
2 parents 98f6e28 + 72c9714 commit 7b533e4

49 files changed

Lines changed: 1314 additions & 1181 deletions

Some content is hidden

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

.ci/.golangci3.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ linters:
6262
- retry.RetryContext
6363
- schema.DefaultTimeout
6464
- validation.*
65-
- floatBetweenSchema
66-
- intBetweenSchema
67-
- stringLenBetweenSchema
65+
- sdkschema.FloatBetweenSchema
66+
- sdkschema.IntBetweenSchema
67+
- sdkschema.StringLenBetweenSchema
6868
# Terraform Plugin Framework
6969
- float32validator.*
7070
- float64validator.*

.ci/semgrep/pluginsdk/quicksight/schema.yml

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,43 @@
22
# "SPDX-License-Identifier: MPL-2.0"
33

44
rules:
5-
- id: quicksight-schema-string-len-between-required
5+
- id: schema-string-arn-requried
66
languages: [go]
7-
message: String attributes with length validation should use stringLenBetweenSchema
7+
message: ARN String attributes should use sdkschema.ArnStringSchema
8+
paths:
9+
include:
10+
- "/internal/service/quicksight/schema"
11+
patterns:
12+
- pattern-inside: "Schema: map[string]*schema.Schema{ ... }"
13+
- pattern: |
14+
{
15+
Type: schema.TypeString,
16+
Required: true,
17+
ValidateFunc: verify.ValidARN,
18+
}
19+
fix: sdkschema.ARNStringSchema(sdkschema.AttrRequired)
20+
severity: WARNING
21+
22+
- id: schema-string-arn-optional
23+
languages: [go]
24+
message: ARN String attributes should use sdkschema.ArnStringSchema
25+
paths:
26+
include:
27+
- "/internal/service/quicksight/schema"
28+
patterns:
29+
- pattern-inside: "Schema: map[string]*schema.Schema{ ... }"
30+
- pattern: |
31+
{
32+
Type: schema.TypeString,
33+
Optional: true,
34+
ValidateFunc: verify.ValidARN,
35+
}
36+
fix: sdkschema.ARNStringSchema(sdkschema.AttrOptional)
37+
severity: WARNING
38+
39+
- id: schema-string-len-between-required
40+
languages: [go]
41+
message: String attributes with length validation should use sdkschema.StringLenBetweenSchema
842
paths:
943
include:
1044
- "/internal/service/quicksight/schema"
@@ -16,12 +50,12 @@ rules:
1650
Required: true,
1751
ValidateFunc: validation.StringLenBetween($MIN, $MAX),
1852
}
19-
fix: stringLenBetweenSchema(attrRequired, $MIN, $MAX)
53+
fix: sdkschema.StringLenBetweenSchema(sdkschema.AttrRequired, $MIN, $MAX)
2054
severity: WARNING
2155

22-
- id: quicksight-schema-string-len-between-optional
56+
- id: schema-string-len-between-optional
2357
languages: [go]
24-
message: String attributes with length validation should use stringLenBetweenSchema
58+
message: String attributes with length validation should use sdkschema.StringLenBetweenSchema
2559
paths:
2660
include:
2761
- "/internal/service/quicksight/schema"
@@ -33,12 +67,12 @@ rules:
3367
Optional: true,
3468
ValidateFunc: validation.StringLenBetween($MIN, $MAX),
3569
}
36-
fix: stringLenBetweenSchema(attrOptional, $MIN, $MAX)
70+
fix: sdkschema.StringLenBetweenSchema(sdkschema.AttrOptional, $MIN, $MAX)
3771
severity: WARNING
3872

39-
- id: quicksight-schema-string-len-between-optionalcomputed
73+
- id: schema-string-len-between-optionalcomputed
4074
languages: [go]
41-
message: String attributes with length validation should use stringLenBetweenSchema
75+
message: String attributes with length validation should use sdkschema.StringLenBetweenSchema
4276
paths:
4377
include:
4478
- "/internal/service/quicksight/schema"
@@ -59,12 +93,12 @@ rules:
5993
Optional: true,
6094
ValidateFunc: validation.StringLenBetween($MIN, $MAX),
6195
}
62-
fix: stringLenBetweenSchema(attrOptionalComputed, $MIN, $MAX)
96+
fix: sdkschema.StringLenBetweenSchema(sdkschema.AttrOptionalComputed, $MIN, $MAX)
6397
severity: WARNING
6498

65-
- id: quicksight-schema-string-enum
99+
- id: schema-string-enum
66100
languages: [go]
67-
message: String attributes with enum validation should use stringEnumSchema[<type>](<required>)
101+
message: String attributes with enum validation should use sdkschema.StringEnumSchema[<type>](<required>)
68102
paths:
69103
include:
70104
- "/internal/service/quicksight/schema"
@@ -100,9 +134,9 @@ rules:
100134
# Cannot be auto-fixed
101135
severity: WARNING
102136

103-
- id: quicksight-schema-int-between-required
137+
- id: schema-int-between-required
104138
languages: [go]
105-
message: Int attributes with between validation should use intBetweenSchema
139+
message: Int attributes with between validation should use sdkschema.IntBetweenSchema
106140
paths:
107141
include:
108142
- "/internal/service/quicksight/schema"
@@ -114,12 +148,12 @@ rules:
114148
Required: true,
115149
ValidateFunc: validation.IntBetween($MIN, $MAX),
116150
}
117-
fix: intBetweenSchema(attrRequired, $MIN, $MAX)
151+
fix: sdkschema.IntBetweenSchema(sdkschema.AttrRequired, $MIN, $MAX)
118152
severity: WARNING
119153

120-
- id: quicksight-schema-int-between-optional
154+
- id: schema-int-between-optional
121155
languages: [go]
122-
message: Int attributes with between validation should use intBetweenSchema
156+
message: Int attributes with between validation should use sdkschema.IntBetweenSchema
123157
paths:
124158
include:
125159
- "/internal/service/quicksight/schema"
@@ -131,12 +165,12 @@ rules:
131165
Optional: true,
132166
ValidateFunc: validation.IntBetween($MIN, $MAX),
133167
}
134-
fix: intBetweenSchema(attrOptional, $MIN, $MAX)
168+
fix: sdkschema.IntBetweenSchema(sdkschema.AttrOptional, $MIN, $MAX)
135169
severity: WARNING
136170

137-
- id: quicksight-schema-float-between-required
171+
- id: schema-float-between-required
138172
languages: [go]
139-
message: Float attributes with between validation should use floatBetweenSchema
173+
message: Float attributes with between validation should use sdkschema.FloatBetweenSchema
140174
paths:
141175
include:
142176
- "/internal/service/quicksight/schema"
@@ -148,12 +182,12 @@ rules:
148182
Required: true,
149183
ValidateFunc: validation.FloatBetween($MIN, $MAX),
150184
}
151-
fix: floatBetweenSchema(attrRequired, $MIN, $MAX)
185+
fix: sdkschema.FloatBetweenSchema(sdkschema.AttrRequired, $MIN, $MAX)
152186
severity: WARNING
153187

154-
- id: quicksight-schema-float-between-optional
188+
- id: schema-float-between-optional
155189
languages: [go]
156-
message: Float attributes with between validation should use floatBetweenSchema
190+
message: Float attributes with between validation should use sdkschema.FloatBetweenSchema
157191
paths:
158192
include:
159193
- "/internal/service/quicksight/schema"
@@ -165,12 +199,12 @@ rules:
165199
Optional: true,
166200
ValidateFunc: validation.FloatBetween($MIN, $MAX),
167201
}
168-
fix: floatBetweenSchema(attrOptional, $MIN, $MAX)
202+
fix: sdkschema.FloatBetweenSchema(sdkschema.AttrOptional, $MIN, $MAX)
169203
severity: WARNING
170204

171-
- id: quicksight-schema-string-computed-only
205+
- id: schema-string-computed-only
172206
languages: [go]
173-
message: Computed-Only String attributes should use stringComputedOnly
207+
message: Computed-Only String attributes should use sdkschema.StringComputedOnly
174208
paths:
175209
include:
176210
- "/internal/service/quicksight/schema"
@@ -184,9 +218,9 @@ rules:
184218
fix: stringComputedOnly()
185219
severity: WARNING
186220

187-
- id: quicksight-schema-bool-computed-only
221+
- id: schema-bool-computed-only
188222
languages: [go]
189-
message: Computed-Only Bool attributes should use boolComputedOnly
223+
message: Computed-Only Bool attributes should use sdkschema.BoolComputedOnly
190224
paths:
191225
include:
192226
- "/internal/service/quicksight/schema"
@@ -197,12 +231,12 @@ rules:
197231
Type: schema.TypeBool,
198232
Computed: true,
199233
}
200-
fix: boolComputedOnly()
234+
fix: sdkschema.BoolComputedOnly()
201235
severity: WARNING
202236

203-
- id: quicksight-schema-int-computed-only
237+
- id: schema-int-computed-only
204238
languages: [go]
205-
message: Computed-Only Int attributes should use intComputedOnly
239+
message: Computed-Only Int attributes should use sdkschema.IntComputedOnly
206240
paths:
207241
include:
208242
- "/internal/service/quicksight/schema"
@@ -216,9 +250,9 @@ rules:
216250
fix: intComputedOnly()
217251
severity: WARNING
218252

219-
- id: quicksight-schema-float-computed-only
253+
- id: schema-float-computed-only
220254
languages: [go]
221-
message: Computed-Only Float attributes should use floatComputedOnly
255+
message: Computed-Only Float attributes should use sdkschema.FloatComputedOnly
222256
paths:
223257
include:
224258
- "/internal/service/quicksight/schema"
@@ -229,5 +263,5 @@ rules:
229263
Type: schema.TypeFloat,
230264
Computed: true,
231265
}
232-
fix: floatComputedOnly()
266+
fix: sdkschema.FloatComputedOnly()
233267
severity: WARNING

internal/sdkv2/schema/bool.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright IBM Corp. 2014, 2026
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package schema
5+
6+
import (
7+
"sync"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
var BoolComputedOnly = sync.OnceValue(func() *schema.Schema {
13+
return &schema.Schema{
14+
Type: schema.TypeBool,
15+
Computed: true,
16+
}
17+
})

internal/sdkv2/schema/float.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright IBM Corp. 2014, 2026
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package schema
5+
6+
import (
7+
"sync"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11+
tfsync "github.com/hashicorp/terraform-provider-aws/internal/sync"
12+
)
13+
14+
type floatBetweenIdentity struct {
15+
handling AttrHandling
16+
min, max float64
17+
}
18+
19+
var floatBetweenSchemaCache tfsync.Map[floatBetweenIdentity, *schema.Schema]
20+
21+
func FloatBetweenSchema(handling AttrHandling, min, max float64) *schema.Schema {
22+
id := floatBetweenIdentity{
23+
handling: handling,
24+
min: min,
25+
max: max,
26+
}
27+
28+
s, ok := floatBetweenSchemaCache.Load(id)
29+
if ok {
30+
return s
31+
}
32+
33+
// Use a separate `LoadOrStore` to avoid allocation if item is already in the cache
34+
// Use `LoadOrStore` instead of `Store` in case there is a race
35+
s, _ = floatBetweenSchemaCache.LoadOrStore(
36+
id,
37+
&schema.Schema{
38+
Type: schema.TypeFloat,
39+
Required: handling.IsRequired(),
40+
Optional: handling.IsOptional(),
41+
Computed: handling.IsComputed(),
42+
ValidateFunc: validation.FloatBetween(min, max),
43+
},
44+
)
45+
return s
46+
}
47+
48+
var FloatComputedOnly = sync.OnceValue(func() *schema.Schema {
49+
return &schema.Schema{
50+
Type: schema.TypeFloat,
51+
Computed: true,
52+
}
53+
})

internal/sdkv2/schema/int.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright IBM Corp. 2014, 2026
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package schema
5+
6+
import (
7+
"sync"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11+
tfsync "github.com/hashicorp/terraform-provider-aws/internal/sync"
12+
)
13+
14+
type intAtLeastIdentity struct {
15+
handling AttrHandling
16+
min int
17+
}
18+
19+
var intAtLeastSchemaCache tfsync.Map[intAtLeastIdentity, *schema.Schema]
20+
21+
func IntAtLeastSchema(handling AttrHandling, min int) *schema.Schema {
22+
id := intAtLeastIdentity{
23+
handling: handling,
24+
min: min,
25+
}
26+
27+
s, ok := intAtLeastSchemaCache.Load(id)
28+
if ok {
29+
return s
30+
}
31+
32+
// Use a separate `LoadOrStore` to avoid allocation if item is already in the cache
33+
// Use `LoadOrStore` instead of `Store` in case there is a race
34+
s, _ = intAtLeastSchemaCache.LoadOrStore(
35+
id,
36+
&schema.Schema{
37+
Type: schema.TypeInt,
38+
Required: handling.IsRequired(),
39+
Optional: handling.IsOptional(),
40+
Computed: handling.IsComputed(),
41+
ValidateFunc: validation.IntAtLeast(min),
42+
},
43+
)
44+
return s
45+
}
46+
47+
type intBetweenIdentity struct {
48+
handling AttrHandling
49+
min, max int
50+
}
51+
52+
var intBetweenSchemaCache tfsync.Map[intBetweenIdentity, *schema.Schema]
53+
54+
func IntBetweenSchema(handling AttrHandling, min, max int) *schema.Schema {
55+
id := intBetweenIdentity{
56+
handling: handling,
57+
min: min,
58+
max: max,
59+
}
60+
61+
s, ok := intBetweenSchemaCache.Load(id)
62+
if ok {
63+
return s
64+
}
65+
66+
// Use a separate `LoadOrStore` to avoid allocation if item is already in the cache
67+
// Use `LoadOrStore` instead of `Store` in case there is a race
68+
s, _ = intBetweenSchemaCache.LoadOrStore(
69+
id,
70+
&schema.Schema{
71+
Type: schema.TypeInt,
72+
Required: handling.IsRequired(),
73+
Optional: handling.IsOptional(),
74+
Computed: handling.IsComputed(),
75+
ValidateFunc: validation.IntBetween(min, max),
76+
},
77+
)
78+
return s
79+
}
80+
81+
var IntComputedOnly = sync.OnceValue(func() *schema.Schema {
82+
return &schema.Schema{
83+
Type: schema.TypeInt,
84+
Computed: true,
85+
}
86+
})

0 commit comments

Comments
 (0)