/
attribute.go
271 lines (234 loc) · 9.51 KB
/
attribute.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
package tfsdk
import (
"errors"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// Attribute must satify the fwschema.Attribute interface. It must also satisfy
// fwxschema.AttributeWithPlanModifiers and fwxschema.AttributeWithValidators
// interfaces, however we cannot check that here or it would introduce an
// import cycle.
var (
_ fwschema.Attribute = Attribute{}
_ fwschema.NestedAttribute = Attribute{}
)
// Attribute defines the constraints and behaviors of a single value field in a
// schema. Attributes are the fields that show up in Terraform state files and
// can be used in configuration files.
//
// Deprecated: Use datasource/schema.Attribute, provider/schema.Attribute, or
// resource/schema.Attribute instead. This can be switched by using the
// datasource/schema.Schema, provider/schema.Schema, or resource/schema.Schema
// types.
type Attribute struct {
// Type indicates what kind of attribute this is. You'll most likely
// want to use one of the types in the types package.
//
// If Type is set, Attributes cannot be.
Type attr.Type
// Attributes can have their own, nested attributes. This nested map of
// attributes behaves exactly like the map of attributes on the Schema
// type.
//
// If Attributes is set, Type cannot be.
Attributes fwschema.NestedAttributes
// Description is used in various tooling, like the language server, to
// give practitioners more information about what this attribute 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 attribute is, what it's for, and how it should be
// used. It should be formatted using Markdown.
MarkdownDescription string
// Required indicates whether the practitioner must enter a value for
// this attribute or not. Required and Optional cannot both be true,
// and Required and Computed cannot both be true.
Required bool
// Optional indicates whether the practitioner can choose not to enter
// a value for this attribute or not. Optional and Required cannot both
// be true.
//
// When defining an attribute that has Optional set to true,
// and uses PlanModifiers to set a "default value" when none is provided,
// Computed must also be set to true. This is necessary because default
// values are, in effect, set by the provider (i.e. computed).
Optional bool
// Computed indicates whether the provider may return its own value for
// this Attribute or not. Required and Computed cannot both be true. If
// Required and Optional are both false, Computed must be true, and the
// attribute will be considered "read only" for the practitioner, with
// only the provider able to set its value.
//
// When defining an Optional Attribute that has a "default value"
// plan modifier, Computed must also be set to true. Otherwise,
// Terraform will return an error like:
//
// planned value ... for a non-computed attribute
//
Computed bool
// Sensitive indicates whether the value of this attribute should be
// considered sensitive data. Setting it to true will obscure the value
// in CLI output. Sensitive does not impact how values are stored, and
// practitioners are encouraged to store their state as if the entire
// file is sensitive.
Sensitive bool
// DeprecationMessage defines warning diagnostic details to display when
// practitioner configurations use this Attribute. The warning diagnostic
// summary is automatically set to "Attribute Deprecated" along with
// configuration source file and line information.
//
// Set this field to a practitioner actionable message such as:
//
// - "Configure other_attribute instead. This attribute will be removed
// in the next major version of the provider."
// - "Remove this attribute's configuration as it no longer is used and
// the attribute will be removed in the next major version of the
// provider."
//
// In Terraform 1.2.7 and later, this warning diagnostic is displayed any
// time a practitioner attempts to configure a value for this attribute and
// certain scenarios where this attribute is referenced.
//
// In Terraform 1.2.6 and earlier, this warning diagnostic is only
// displayed when the Attribute is Required or Optional, and if the
// practitioner configuration sets the value to a known or unknown value
// (which may eventually be null). It has no effect when the Attribute is
// Computed-only (read-only; not Required or Optional).
//
// Across any Terraform version, there are no warnings raised for
// practitioner configuration values set directly to null, as there is no
// way for the framework to differentiate between an unset and null
// configuration due to how Terraform sends configuration information
// across the protocol.
//
// Additional information about deprecation enhancements for read-only
// attributes can be found in:
//
// - https://github.com/hashicorp/terraform/issues/7569
//
DeprecationMessage string
// Validators define value validation functionality for the attribute. All
// elements of the slice of AttributeValidator are run, regardless of any
// previous error diagnostics.
//
// Many common use case validators can be found in the
// github.com/hashicorp/terraform-plugin-framework-validators Go module.
//
// If the Type field points to a custom type that implements the
// xattr.TypeWithValidate interface, the validators defined in this field
// are run in addition to the validation defined by the type.
Validators []AttributeValidator
// PlanModifiers defines a sequence of modifiers for this attribute at
// plan time. Attribute-level plan modifications occur before any
// resource-level plan modifications.
//
// Any errors will prevent further execution of this sequence
// of modifiers and modifiers associated with any nested Attribute, but
// will not prevent execution of PlanModifiers on any other Attribute or
// Block in the Schema.
//
// Plan modification only applies to resources, not data sources or
// providers. Setting PlanModifiers on a data source or provider attribute
// will have no effect.
//
// When providing PlanModifiers, it's necessary to set Computed to true.
PlanModifiers AttributePlanModifiers
}
// ApplyTerraform5AttributePathStep transparently calls
// ApplyTerraform5AttributePathStep on a.Type or a.Attributes, whichever is
// non-nil. It allows Attributes to be walked using tftypes.Walk and
// tftypes.Transform.
func (a Attribute) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (interface{}, error) {
if a.Type != nil {
return a.Type.ApplyTerraform5AttributePathStep(step)
}
if a.Attributes != nil {
return a.Attributes.ApplyTerraform5AttributePathStep(step)
}
return nil, errors.New("Attribute has no type or nested attributes")
}
// Equal returns true if `a` and `o` should be considered Equal.
func (a Attribute) Equal(o fwschema.Attribute) bool {
other, ok := o.(Attribute)
if !ok {
return false
}
if a.GetNestingMode() != other.GetNestingMode() {
return false
}
if a.GetNestedObject() != nil && !a.GetNestedObject().Equal(other.GetNestedObject()) {
return false
}
if other.GetNestedObject() != nil && !other.GetNestedObject().Equal(a.GetNestedObject()) {
return false
}
return fwschema.AttributesEqual(a, o)
}
// GetDeprecationMessage satisfies the fwschema.Attribute interface.
func (a Attribute) GetDeprecationMessage() string {
return a.DeprecationMessage
}
// GetDescription satisfies the fwschema.Attribute interface.
func (a Attribute) GetDescription() string {
return a.Description
}
// GetMarkdownDescription satisfies the fwschema.Attribute interface.
func (a Attribute) GetMarkdownDescription() string {
return a.MarkdownDescription
}
// GetNestedObject returns a generated NestedAttributeObject if the
// Attribute represents nested attributes, otherwise nil.
func (a Attribute) GetNestedObject() fwschema.NestedAttributeObject {
if a.GetNestingMode() == fwschema.NestingModeUnknown {
return nil
}
return nestedAttributeObject{
Attributes: a.Attributes.GetAttributes(),
}
}
// GetNestingMode returns the Attributes nesting mode, if set. Otherwise,
// returns NestingModeUnset.
func (a Attribute) GetNestingMode() fwschema.NestingMode {
if a.Type != nil {
return fwschema.NestingModeUnknown
}
if a.Attributes != nil {
return a.Attributes.GetNestingMode()
}
return fwschema.NestingModeUnknown
}
// GetPlanModifiers satisfies the fwxschema.AttributeWithPlanModifiers
// interface.
func (a Attribute) GetPlanModifiers() AttributePlanModifiers {
return a.PlanModifiers
}
// GetValidators satisfies the fwxschema.AttributeWithValidators interface.
func (a Attribute) GetValidators() []AttributeValidator {
return a.Validators
}
// GetType satisfies the fwschema.Attribute interface.
func (a Attribute) GetType() attr.Type {
if a.Attributes != nil {
return a.Attributes.Type()
}
return a.Type
}
// IsComputed satisfies the fwschema.Attribute interface.
func (a Attribute) IsComputed() bool {
return a.Computed
}
// IsOptional satisfies the fwschema.Attribute interface.
func (a Attribute) IsOptional() bool {
return a.Optional
}
// IsRequired satisfies the fwschema.Attribute interface.
func (a Attribute) IsRequired() bool {
return a.Required
}
// IsSensitive satisfies the fwschema.Attribute interface.
func (a Attribute) IsSensitive() bool {
return a.Sensitive
}