-
Notifications
You must be signed in to change notification settings - Fork 91
/
single_nested_attribute.go
233 lines (198 loc) · 8.31 KB
/
single_nested_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
package schema
import (
"fmt"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure the implementation satisifies the desired interfaces.
var (
_ NestedAttribute = SingleNestedAttribute{}
_ fwxschema.AttributeWithObjectValidators = SingleNestedAttribute{}
)
// SingleNestedAttribute represents an attribute that is a single object where
// the object attributes can be fully defined, including further nested
// attributes. When retrieving the value for this attribute, use types.Object
// as the value type unless the CustomType field is set. The Attributes field
// must be set. Nested attributes are only compatible with protocol version 6.
//
// Use ObjectAttribute if the underlying attributes do not require definition
// beyond type information.
//
// Terraform configurations configure this attribute using expressions that
// return an object or directly via curly brace syntax.
//
// # single object
// example_attribute = {
// nested_attribute = #...
// }
//
// Terraform configurations reference this attribute using expressions that
// accept an object or an attribute name directly via period syntax:
//
// # object nested_attribute value
// .example_attribute.nested_attribute
type SingleNestedAttribute struct {
// Attributes is the mapping of underlying attribute names to attribute
// definitions. This field must be set.
Attributes map[string]Attribute
// CustomType enables the use of a custom attribute type in place of the
// default types.ObjectType. When retrieving data, the types.ObjectValuable
// associated with this custom type must be used in place of types.Object.
CustomType types.ObjectTypable
// 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 to enter a value
// for this attribute or not. Optional and Required cannot both be true.
Optional 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
// 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
// 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 []validator.Object
}
// ApplyTerraform5AttributePathStep returns the Attributes field value if step
// is AttributeName, otherwise returns an error.
func (a SingleNestedAttribute) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (interface{}, error) {
name, ok := step.(tftypes.AttributeName)
if !ok {
return nil, fmt.Errorf("cannot apply step %T to SingleNestedAttribute", step)
}
attribute, ok := a.Attributes[string(name)]
if !ok {
return nil, fmt.Errorf("no attribute %q on SingleNestedAttribute", name)
}
return attribute, nil
}
// Equal returns true if the given Attribute is a SingleNestedAttribute
// and all fields are equal.
func (a SingleNestedAttribute) Equal(o fwschema.Attribute) bool {
if _, ok := o.(SingleNestedAttribute); !ok {
return false
}
return fwschema.AttributesEqual(a, o)
}
// GetAttributes returns the Attributes field value.
func (a SingleNestedAttribute) GetAttributes() fwschema.UnderlyingAttributes {
return schemaAttributes(a.Attributes)
}
// GetDeprecationMessage returns the DeprecationMessage field value.
func (a SingleNestedAttribute) GetDeprecationMessage() string {
return a.DeprecationMessage
}
// GetDescription returns the Description field value.
func (a SingleNestedAttribute) GetDescription() string {
return a.Description
}
// GetMarkdownDescription returns the MarkdownDescription field value.
func (a SingleNestedAttribute) GetMarkdownDescription() string {
return a.MarkdownDescription
}
// GetNestedObject returns a generated NestedAttributeObject from the
// Attributes, CustomType, and Validators field values.
func (a SingleNestedAttribute) GetNestedObject() fwschema.NestedAttributeObject {
return NestedAttributeObject{
Attributes: a.Attributes,
CustomType: a.CustomType,
Validators: a.Validators,
}
}
// GetNestingMode always returns NestingModeList.
func (a SingleNestedAttribute) GetNestingMode() fwschema.NestingMode {
return fwschema.NestingModeSingle
}
// GetType returns ListType of ObjectType or CustomType.
func (a SingleNestedAttribute) GetType() attr.Type {
if a.CustomType != nil {
return a.CustomType
}
attrTypes := make(map[string]attr.Type, len(a.Attributes))
for name, attribute := range a.Attributes {
attrTypes[name] = attribute.GetType()
}
return types.ObjectType{
AttrTypes: attrTypes,
}
}
// IsComputed always returns false as provider schemas cannot be Computed.
func (a SingleNestedAttribute) IsComputed() bool {
return false
}
// IsOptional returns the Optional field value.
func (a SingleNestedAttribute) IsOptional() bool {
return a.Optional
}
// IsRequired returns the Required field value.
func (a SingleNestedAttribute) IsRequired() bool {
return a.Required
}
// IsSensitive returns the Sensitive field value.
func (a SingleNestedAttribute) IsSensitive() bool {
return a.Sensitive
}
// ObjectValidators returns the Validators field value.
func (a SingleNestedAttribute) ObjectValidators() []validator.Object {
return a.Validators
}