Skip to content

Commit 8c780f1

Browse files
authored
list: add primitive, list and map types (#1177)
* add primitive types as well as list and map collection types * sort the listresources result to prevent false positives in test * omit mention of Computed in the code comment for the Required attribute * add number type
1 parent 52edf5b commit 8c780f1

20 files changed

+5565
-0
lines changed

internal/proto5server/server_getmetadata_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,10 @@ func TestServerGetMetadata(t *testing.T) {
810810
return got.Functions[i].Name < got.Functions[j].Name
811811
})
812812

813+
sort.Slice(got.ListResources, func(i int, j int) bool {
814+
return got.ListResources[i].TypeName < got.ListResources[j].TypeName
815+
})
816+
813817
sort.Slice(got.Resources, func(i int, j int) bool {
814818
return got.Resources[i].TypeName < got.Resources[j].TypeName
815819
})

internal/proto6server/server_getmetadata_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,10 @@ func TestServerGetMetadata(t *testing.T) {
810810
return got.Functions[i].Name < got.Functions[j].Name
811811
})
812812

813+
sort.Slice(got.ListResources, func(i int, j int) bool {
814+
return got.ListResources[i].TypeName < got.ListResources[j].TypeName
815+
})
816+
813817
sort.Slice(got.Resources, func(i int, j int) bool {
814818
return got.Resources[i].TypeName < got.Resources[j].TypeName
815819
})

list/schema/attribute.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import (
99

1010
// Attribute define a value field inside the Schema. Implementations in this
1111
// package include:
12+
// - BoolAttribute
13+
// - Float32Attribute
14+
// - Float64Attribute
15+
// - Int32Attribute
16+
// - Int64Attribute
17+
// - ListAttribute
18+
// - MapAttribute
19+
// - NumberAttribute
1220
// - StringAttribute
1321
//
1422
// In practitioner configurations, an equals sign (=) is required to set

list/schema/bool_attribute.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package schema
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tftypes"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/attr"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
12+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
13+
"github.com/hashicorp/terraform-plugin-framework/types"
14+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
15+
)
16+
17+
// Ensure the implementation satisifies the desired interfaces.
18+
var (
19+
_ Attribute = BoolAttribute{}
20+
_ fwxschema.AttributeWithBoolValidators = BoolAttribute{}
21+
)
22+
23+
// BoolAttribute represents a schema attribute that is a boolean. When
24+
// retrieving the value for this attribute, use types.Bool as the value type
25+
// unless the CustomType field is set.
26+
//
27+
// Terraform configurations configure this attribute using expressions that
28+
// return a boolean or directly via the true/false keywords.
29+
//
30+
// example_attribute = true
31+
//
32+
// Terraform configurations reference this attribute using the attribute name.
33+
//
34+
// .example_attribute
35+
type BoolAttribute struct {
36+
// CustomType enables the use of a custom attribute type in place of the
37+
// default basetypes.BoolType. When retrieving data, the basetypes.BoolValuable
38+
// associated with this custom type must be used in place of types.Bool.
39+
CustomType basetypes.BoolTypable
40+
41+
// Required indicates whether the practitioner must enter a value for
42+
// this attribute or not. Required and Optional cannot both be true.
43+
Required bool
44+
45+
// Optional indicates whether the practitioner can choose to enter a value
46+
// for this attribute or not. Optional and Required cannot both be true.
47+
Optional bool
48+
49+
// Description is used in various tooling, like the language server, to
50+
// give practitioners more information about what this attribute is,
51+
// what it's for, and how it should be used. It should be written as
52+
// plain text, with no special formatting.
53+
Description string
54+
55+
// MarkdownDescription is used in various tooling, like the
56+
// documentation generator, to give practitioners more information
57+
// about what this attribute is, what it's for, and how it should be
58+
// used. It should be formatted using Markdown.
59+
MarkdownDescription string
60+
61+
// DeprecationMessage defines warning diagnostic details to display when
62+
// practitioner configurations use this Attribute. The warning diagnostic
63+
// summary is automatically set to "Attribute Deprecated" along with
64+
// configuration source file and line information.
65+
//
66+
// Set this field to a practitioner actionable message such as:
67+
//
68+
// - "Configure other_attribute instead. This attribute will be removed
69+
// in the next major version of the provider."
70+
// - "Remove this attribute's configuration as it no longer is used and
71+
// the attribute will be removed in the next major version of the
72+
// provider."
73+
//
74+
// In Terraform 1.2.7 and later, this warning diagnostic is displayed any
75+
// time a practitioner attempts to configure a value for this attribute and
76+
// certain scenarios where this attribute is referenced.
77+
//
78+
// In Terraform 1.2.6 and earlier, this warning diagnostic is only
79+
// displayed when the Attribute is Required or Optional, and if the
80+
// practitioner configuration sets the value to a known or unknown value
81+
// (which may eventually be null). It has no effect when the Attribute is
82+
// Computed-only (read-only; not Required or Optional).
83+
//
84+
// Across any Terraform version, there are no warnings raised for
85+
// practitioner configuration values set directly to null, as there is no
86+
// way for the framework to differentiate between an unset and null
87+
// configuration due to how Terraform sends configuration information
88+
// across the protocol.
89+
//
90+
// Additional information about deprecation enhancements for read-only
91+
// attributes can be found in:
92+
//
93+
// - https://github.com/hashicorp/terraform/issues/7569
94+
//
95+
DeprecationMessage string
96+
97+
// Validators define value validation functionality for the attribute. All
98+
// elements of the slice of AttributeValidator are run, regardless of any
99+
// previous error diagnostics.
100+
//
101+
// Many common use case validators can be found in the
102+
// github.com/hashicorp/terraform-plugin-framework-validators Go module.
103+
//
104+
// If the Type field points to a custom type that implements the
105+
// xattr.TypeWithValidate interface, the validators defined in this field
106+
// are run in addition to the validation defined by the type.
107+
Validators []validator.Bool
108+
}
109+
110+
// ApplyTerraform5AttributePathStep always returns an error as it is not
111+
// possible to step further into a BoolAttribute.
112+
func (a BoolAttribute) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (interface{}, error) {
113+
return a.GetType().ApplyTerraform5AttributePathStep(step)
114+
}
115+
116+
// BoolValidators returns the Validators field value.
117+
func (a BoolAttribute) BoolValidators() []validator.Bool {
118+
return a.Validators
119+
}
120+
121+
// Equal returns true if the given Attribute is a BoolAttribute
122+
// and all fields are equal.
123+
func (a BoolAttribute) Equal(o fwschema.Attribute) bool {
124+
if _, ok := o.(BoolAttribute); !ok {
125+
return false
126+
}
127+
128+
return fwschema.AttributesEqual(a, o)
129+
}
130+
131+
// GetDeprecationMessage returns the DeprecationMessage field value.
132+
func (a BoolAttribute) GetDeprecationMessage() string {
133+
return a.DeprecationMessage
134+
}
135+
136+
// GetDescription returns the Description field value.
137+
func (a BoolAttribute) GetDescription() string {
138+
return a.Description
139+
}
140+
141+
// GetMarkdownDescription returns the MarkdownDescription field value.
142+
func (a BoolAttribute) GetMarkdownDescription() string {
143+
return a.MarkdownDescription
144+
}
145+
146+
// GetType returns types.StringType or the CustomType field value if defined.
147+
func (a BoolAttribute) GetType() attr.Type {
148+
if a.CustomType != nil {
149+
return a.CustomType
150+
}
151+
152+
return types.BoolType
153+
}
154+
155+
// IsComputed returns false because it does not apply to ListResource schemas.
156+
func (a BoolAttribute) IsComputed() bool {
157+
return false
158+
}
159+
160+
// IsOptional returns the Optional field value.
161+
func (a BoolAttribute) IsOptional() bool {
162+
return a.Optional
163+
}
164+
165+
// IsRequired returns the Required field value.
166+
func (a BoolAttribute) IsRequired() bool {
167+
return a.Required
168+
}
169+
170+
// IsSensitive returns false because it does not apply to ListResource schemas.
171+
func (a BoolAttribute) IsSensitive() bool {
172+
return false
173+
}
174+
175+
// IsWriteOnly returns false because it does not apply to ListResource schemas.
176+
func (a BoolAttribute) IsWriteOnly() bool {
177+
return false
178+
}
179+
180+
// IsRequiredForImport returns false as this behavior is only relevant
181+
// for managed resource identity schema attributes.
182+
func (a BoolAttribute) IsRequiredForImport() bool {
183+
return false
184+
}
185+
186+
// IsOptionalForImport returns false as this behavior is only relevant
187+
// for managed resource identity schema attributes.
188+
func (a BoolAttribute) IsOptionalForImport() bool {
189+
return false
190+
}

0 commit comments

Comments
 (0)