/
schema.go
272 lines (214 loc) · 8.4 KB
/
schema.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package schema
import (
"context"
"fmt"
"regexp"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/path"
)
// Schema must satify the fwschema.Schema interface.
var _ fwschema.Schema = Schema{}
// Schema defines the structure and value types of data source data. This type
// is used as the datasource.SchemaResponse type Schema field, which is
// implemented by the datasource.DataSource type Schema method.
type Schema struct {
// Attributes is the mapping of underlying attribute names to attribute
// definitions.
//
// Names must only contain lowercase letters, numbers, and underscores.
// Names must not collide with any Blocks names.
Attributes map[string]Attribute
// Blocks is the mapping of underlying block names to block definitions.
//
// Names must only contain lowercase letters, numbers, and underscores.
// Names must not collide with any Attributes names.
Blocks map[string]Block
// Description is used in various tooling, like the language server, to
// give practitioners more information about what this data source is,
// what it's for, and how it should be used. It should be written as
// plain text, with no special formatting.
Description string
// MarkdownDescription is used in various tooling, like the
// documentation generator, to give practitioners more information
// about what this data source is, what it's for, and how it should be
// used. It should be formatted using Markdown.
MarkdownDescription string
// DeprecationMessage defines warning diagnostic details to display when
// practitioner configurations use this data source. The warning diagnostic
// summary is automatically set to "Data Source Deprecated" along with
// configuration source file and line information.
//
// Set this field to a practitioner actionable message such as:
//
// - "Use examplecloud_other data source instead. This data source
// will be removed in the next major version of the provider."
// - "Remove this data source as it no longer is valid and
// will be removed in the next major version of the provider."
//
DeprecationMessage string
}
// ApplyTerraform5AttributePathStep applies the given AttributePathStep to the
// schema.
func (s Schema) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (any, error) {
return fwschema.SchemaApplyTerraform5AttributePathStep(s, step)
}
// AttributeAtPath returns the Attribute at the passed path. If the path points
// to an element or attribute of a complex type, rather than to an Attribute,
// it will return an ErrPathInsideAtomicAttribute error.
func (s Schema) AttributeAtPath(ctx context.Context, p path.Path) (fwschema.Attribute, diag.Diagnostics) {
return fwschema.SchemaAttributeAtPath(ctx, s, p)
}
// AttributeAtPath returns the Attribute at the passed path. If the path points
// to an element or attribute of a complex type, rather than to an Attribute,
// it will return an ErrPathInsideAtomicAttribute error.
func (s Schema) AttributeAtTerraformPath(ctx context.Context, p *tftypes.AttributePath) (fwschema.Attribute, error) {
return fwschema.SchemaAttributeAtTerraformPath(ctx, s, p)
}
// GetAttributes returns the Attributes field value.
func (s Schema) GetAttributes() map[string]fwschema.Attribute {
return schemaAttributes(s.Attributes)
}
// GetBlocks returns the Blocks field value.
func (s Schema) GetBlocks() map[string]fwschema.Block {
return schemaBlocks(s.Blocks)
}
// GetDeprecationMessage returns the DeprecationMessage field value.
func (s Schema) GetDeprecationMessage() string {
return s.DeprecationMessage
}
// GetDescription returns the Description field value.
func (s Schema) GetDescription() string {
return s.Description
}
// GetMarkdownDescription returns the MarkdownDescription field value.
func (s Schema) GetMarkdownDescription() string {
return s.MarkdownDescription
}
// GetVersion always returns 0 as data source schemas cannot be versioned.
func (s Schema) GetVersion() int64 {
return 0
}
// Type returns the framework type of the schema.
func (s Schema) Type() attr.Type {
return fwschema.SchemaType(s)
}
// TypeAtPath returns the framework type at the given schema path.
func (s Schema) TypeAtPath(ctx context.Context, p path.Path) (attr.Type, diag.Diagnostics) {
return fwschema.SchemaTypeAtPath(ctx, s, p)
}
// TypeAtTerraformPath returns the framework type at the given tftypes path.
func (s Schema) TypeAtTerraformPath(ctx context.Context, p *tftypes.AttributePath) (attr.Type, error) {
return fwschema.SchemaTypeAtTerraformPath(ctx, s, p)
}
// Validate verifies that the schema is not using a reserved field name for a top-level attribute.
func (s Schema) Validate() diag.Diagnostics {
var diags diag.Diagnostics
// Raise error diagnostics when data source configuration uses reserved
// field names for root-level attributes.
reservedFieldNames := map[string]struct{}{
"connection": {},
"count": {},
"depends_on": {},
"lifecycle": {},
"provider": {},
"provisioner": {},
}
attributes := s.GetAttributes()
for k, v := range attributes {
if _, ok := reservedFieldNames[k]; ok {
diags.AddAttributeError(
path.Root(k),
"Schema Using Reserved Field Name",
fmt.Sprintf("%q is a reserved field name", k),
)
}
d := validateAttributeFieldName(path.Root(k), k, v)
diags.Append(d...)
}
blocks := s.GetBlocks()
for k, v := range blocks {
if _, ok := reservedFieldNames[k]; ok {
diags.AddAttributeError(
path.Root(k),
"Schema Using Reserved Field Name",
fmt.Sprintf("%q is a reserved field name", k),
)
}
d := validateBlockFieldName(path.Root(k), k, v)
diags.Append(d...)
}
return diags
}
// validFieldNameRegex is used to verify that name used for attributes and blocks
// comply with the defined regular expression.
var validFieldNameRegex = regexp.MustCompile("^[a-z0-9_]+$")
// validateAttributeFieldName verifies that the name used for an attribute complies with the regular
// expression defined in validFieldNameRegex.
func validateAttributeFieldName(path path.Path, name string, attr fwschema.Attribute) diag.Diagnostics {
var diags diag.Diagnostics
if !validFieldNameRegex.MatchString(name) {
diags.AddAttributeError(
path,
"Invalid Schema Field Name",
fmt.Sprintf("Field name %q is invalid, the only allowed characters are a-z, 0-9 and _. This is always a problem with the provider and should be reported to the provider developer.", name),
)
}
if na, ok := attr.(fwschema.NestedAttribute); ok {
nestedObject := na.GetNestedObject()
if nestedObject == nil {
return diags
}
attributes := nestedObject.GetAttributes()
for k, v := range attributes {
d := validateAttributeFieldName(path.AtName(k), k, v)
diags.Append(d...)
}
}
return diags
}
// validateBlockFieldName verifies that the name used for a block complies with the regular
// expression defined in validFieldNameRegex.
func validateBlockFieldName(path path.Path, name string, b fwschema.Block) diag.Diagnostics {
var diags diag.Diagnostics
if !validFieldNameRegex.MatchString(name) {
diags.AddAttributeError(
path,
"Invalid Schema Field Name",
fmt.Sprintf("Field name %q is invalid, the only allowed characters are a-z, 0-9 and _. This is always a problem with the provider and should be reported to the provider developer.", name),
)
}
nestedObject := b.GetNestedObject()
if nestedObject == nil {
return diags
}
blocks := nestedObject.GetBlocks()
for k, v := range blocks {
d := validateBlockFieldName(path.AtName(k), k, v)
diags.Append(d...)
}
attributes := nestedObject.GetAttributes()
for k, v := range attributes {
d := validateAttributeFieldName(path.AtName(k), k, v)
diags.Append(d...)
}
return diags
}
// schemaAttributes is a datasource to fwschema type conversion function.
func schemaAttributes(attributes map[string]Attribute) map[string]fwschema.Attribute {
result := make(map[string]fwschema.Attribute, len(attributes))
for name, attribute := range attributes {
result[name] = attribute
}
return result
}
// schemaBlocks is a datasource to fwschema type conversion function.
func schemaBlocks(blocks map[string]Block) map[string]fwschema.Block {
result := make(map[string]fwschema.Block, len(blocks))
for name, block := range blocks {
result[name] = block
}
return result
}