Skip to content

Commit 21ffee7

Browse files
committed
add blocks and docs
1 parent ea2deb8 commit 21ffee7

File tree

7 files changed

+1407
-4
lines changed

7 files changed

+1407
-4
lines changed

action/schema/attribute.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
88
)
99

10-
// TODO:Actions: Add all of the attribute and nested attribute types listed below
11-
//
1210
// Attribute define a value field inside an action type schema. Implementations in this
1311
// package include:
1412
// - BoolAttribute

action/schema/block.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
88
)
99

10-
// TODO:Actions: Add all of the block and nested block types listed below
11-
//
1210
// Block defines a structural field inside an action type schema. Implementations in this
1311
// package include:
1412
// - ListNestedBlock

action/schema/doc.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
// Package schema contains all available schema functionality for actions.
5+
// Action schemas define the structure and value types for configuration data.
6+
// Schemas are implemented via the action.Action type Schema method.
7+
//
8+
// There are three different types of action schemas, which define how a practitioner can trigger an action,
9+
// as well as what effect the action can have on the state.
10+
// - [UnlinkedSchema] actions are actions that cannot cause changes to resource states.
11+
// - [LifecycleSchema] actions are actions that can cause changes to exactly one resource state.
12+
// - [LinkedSchema] actions are actions that can cause changes to one or more resource states.
13+
package schema

action/schema/list_nested_block.go

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package schema
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/hashicorp/terraform-plugin-framework/attr"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
12+
"github.com/hashicorp/terraform-plugin-framework/internal/fwtype"
13+
"github.com/hashicorp/terraform-plugin-framework/types"
14+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
15+
"github.com/hashicorp/terraform-plugin-go/tftypes"
16+
)
17+
18+
// Ensure the implementation satisifies the desired interfaces.
19+
var (
20+
_ Block = ListNestedBlock{}
21+
_ fwschema.BlockWithValidateImplementation = ListNestedBlock{}
22+
)
23+
24+
// ListNestedBlock represents a block that is a list of objects where
25+
// the object attributes can be fully defined, including further attributes
26+
// or blocks. When retrieving the value for this block, use types.List
27+
// as the value type unless the CustomType field is set. The NestedObject field
28+
// must be set.
29+
//
30+
// Prefer ListNestedAttribute over ListNestedBlock if the provider is
31+
// using protocol version 6. Nested attributes allow practitioners to configure
32+
// values directly with expressions.
33+
//
34+
// Terraform configurations configure this block repeatedly using curly brace
35+
// syntax without an equals (=) sign or [Dynamic Block Expressions].
36+
//
37+
// # list of blocks with two elements
38+
// example_block {
39+
// nested_attribute = #...
40+
// }
41+
// example_block {
42+
// nested_attribute = #...
43+
// }
44+
//
45+
// Terraform configurations reference this block using expressions that
46+
// accept a list of objects or an element directly via square brace 0-based
47+
// index syntax:
48+
//
49+
// # first known object
50+
// .example_block[0]
51+
// # first known object nested_attribute value
52+
// .example_block[0].nested_attribute
53+
//
54+
// [Dynamic Block Expressions]: https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks
55+
type ListNestedBlock struct {
56+
// NestedObject is the underlying object that contains nested attributes or
57+
// blocks. This field must be set.
58+
//
59+
// Nested attributes that contain a dynamic type (i.e. DynamicAttribute) are not supported.
60+
// If underlying dynamic values are required, replace this block definition with
61+
// a DynamicAttribute.
62+
NestedObject NestedBlockObject
63+
64+
// CustomType enables the use of a custom attribute type in place of the
65+
// default types.ListType of types.ObjectType. When retrieving data, the
66+
// basetypes.ListValuable associated with this custom type must be used in
67+
// place of types.List.
68+
CustomType basetypes.ListTypable
69+
70+
// Description is used in various tooling, like the language server, to
71+
// give practitioners more information about what this attribute is,
72+
// what it's for, and how it should be used. It should be written as
73+
// plain text, with no special formatting.
74+
Description string
75+
76+
// MarkdownDescription is used in various tooling, like the
77+
// documentation generator, to give practitioners more information
78+
// about what this attribute is, what it's for, and how it should be
79+
// used. It should be formatted using Markdown.
80+
MarkdownDescription string
81+
82+
// DeprecationMessage defines warning diagnostic details to display when
83+
// practitioner configurations use this Attribute. The warning diagnostic
84+
// summary is automatically set to "Attribute Deprecated" along with
85+
// configuration source file and line information.
86+
//
87+
// Set this field to a practitioner actionable message such as:
88+
//
89+
// - "Configure other_attribute instead. This attribute will be removed
90+
// in the next major version of the provider."
91+
// - "Remove this attribute's configuration as it no longer is used and
92+
// the attribute will be removed in the next major version of the
93+
// provider."
94+
//
95+
// In Terraform 1.2.7 and later, this warning diagnostic is displayed any
96+
// time a practitioner attempts to configure a value for this attribute and
97+
// certain scenarios where this attribute is referenced.
98+
//
99+
// In Terraform 1.2.6 and earlier, this warning diagnostic is only
100+
// displayed when the Attribute is Required or Optional, and if the
101+
// practitioner configuration sets the value to a known or unknown value
102+
// (which may eventually be null).
103+
//
104+
// Across any Terraform version, there are no warnings raised for
105+
// practitioner configuration values set directly to null, as there is no
106+
// way for the framework to differentiate between an unset and null
107+
// configuration due to how Terraform sends configuration information
108+
// across the protocol.
109+
//
110+
// Additional information about deprecation enhancements for read-only
111+
// attributes can be found in:
112+
//
113+
// - https://github.com/hashicorp/terraform/issues/7569
114+
//
115+
DeprecationMessage string
116+
}
117+
118+
// ApplyTerraform5AttributePathStep returns the NestedObject field value if step
119+
// is ElementKeyInt, otherwise returns an error.
120+
func (b ListNestedBlock) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (interface{}, error) {
121+
_, ok := step.(tftypes.ElementKeyInt)
122+
123+
if !ok {
124+
return nil, fmt.Errorf("cannot apply step %T to ListNestedBlock", step)
125+
}
126+
127+
return b.NestedObject, nil
128+
}
129+
130+
// Equal returns true if the given Block is ListNestedBlock
131+
// and all fields are equal.
132+
func (b ListNestedBlock) Equal(o fwschema.Block) bool {
133+
if _, ok := o.(ListNestedBlock); !ok {
134+
return false
135+
}
136+
137+
return fwschema.BlocksEqual(b, o)
138+
}
139+
140+
// GetDeprecationMessage returns the DeprecationMessage field value.
141+
func (b ListNestedBlock) GetDeprecationMessage() string {
142+
return b.DeprecationMessage
143+
}
144+
145+
// GetDescription returns the Description field value.
146+
func (b ListNestedBlock) GetDescription() string {
147+
return b.Description
148+
}
149+
150+
// GetMarkdownDescription returns the MarkdownDescription field value.
151+
func (b ListNestedBlock) GetMarkdownDescription() string {
152+
return b.MarkdownDescription
153+
}
154+
155+
// GetNestedObject returns the NestedObject field value.
156+
func (b ListNestedBlock) GetNestedObject() fwschema.NestedBlockObject {
157+
return b.NestedObject
158+
}
159+
160+
// GetNestingMode always returns BlockNestingModeList.
161+
func (b ListNestedBlock) GetNestingMode() fwschema.BlockNestingMode {
162+
return fwschema.BlockNestingModeList
163+
}
164+
165+
// Type returns ListType of ObjectType or CustomType.
166+
func (b ListNestedBlock) Type() attr.Type {
167+
if b.CustomType != nil {
168+
return b.CustomType
169+
}
170+
171+
return types.ListType{
172+
ElemType: b.NestedObject.Type(),
173+
}
174+
}
175+
176+
// ValidateImplementation contains logic for validating the
177+
// provider-defined implementation of the block to prevent unexpected
178+
// errors or panics. This logic runs during the GetProviderSchema RPC and
179+
// should never include false positives.
180+
func (b ListNestedBlock) ValidateImplementation(ctx context.Context, req fwschema.ValidateImplementationRequest, resp *fwschema.ValidateImplementationResponse) {
181+
if b.CustomType == nil && fwtype.ContainsCollectionWithDynamic(b.Type()) {
182+
resp.Diagnostics.Append(fwtype.BlockCollectionWithDynamicTypeDiag(req.Path))
183+
}
184+
}

0 commit comments

Comments
 (0)