-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Internal] Use Attributes by default for List Objects (#4315)
## Changes Resources implemented on the SDKv2 used blocks to represent two kinds of fields: traditional lists, and nested complex types (which were treated as a list of length at most 1). The Plugin Framework now has support for list attributes, and it is recommended to use this in general, except for places where compatibility with SDKv2 is needed. To achieve this, this change ensures that *StructToSchema methods in the plugin framework generate attributes for lists by default. Then, for resources migrated from SDKv2, the implementer must call `ConfigureForSdkV2Migration()` in the customize callback. This will convert any list attributes into list blocks, preserving compatibility with SDKv2-implemented resources. Additionally, this PR improves the handling of computed, non-optional fields. For lists annotated as `tf:"computed"`, the resulting schema is invalid: it is marked as computed and as required because of the absence of the `"optional"` tag. When a field is labeled as computed, it can be marked as optional but should never be marked as required; that is addressed here as well. Finally, this PR also includes some autogenerated changes modifying Apps to use `types.Object` in place of `types.List` for nested complex types. ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [ ] `make test` run locally - [ ] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [ ] relevant acceptance tests are passing - [ ] using Go SDK
- Loading branch information
Showing
14 changed files
with
227 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 31 additions & 3 deletions
34
internal/providers/pluginfw/tfschema/attribute_converter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,34 @@ | ||
package tfschema | ||
|
||
type BlockToAttributeConverter interface { | ||
// ConvertBlockToAttribute converts a contained block to its corresponding attribute type. | ||
ConvertBlockToAttribute(string) BaseSchemaBuilder | ||
// Blockable is an interface that can be implemented by an AttributeBuilder to convert it to a BlockBuilder. | ||
type Blockable interface { | ||
// ToBlock converts the AttributeBuilder to a BlockBuilder. | ||
ToBlock() BlockBuilder | ||
} | ||
|
||
// convertAttributesToBlocks converts all attributes implementing the Blockable interface to blocks, returning | ||
// a new NestedBlockObject with the converted attributes and the original blocks. | ||
func convertAttributesToBlocks(attributes map[string]AttributeBuilder, blocks map[string]BlockBuilder) NestedBlockObject { | ||
newAttributes := make(map[string]AttributeBuilder) | ||
newBlocks := make(map[string]BlockBuilder) | ||
for name, attr := range attributes { | ||
if lnab, ok := attr.(Blockable); ok { | ||
newBlocks[name] = lnab.ToBlock() | ||
} else { | ||
newAttributes[name] = attr | ||
} | ||
} | ||
for name, block := range blocks { | ||
newBlocks[name] = block | ||
} | ||
if len(newAttributes) == 0 { | ||
newAttributes = nil | ||
} | ||
if len(newBlocks) == 0 { | ||
newBlocks = nil | ||
} | ||
return NestedBlockObject{ | ||
Attributes: newAttributes, | ||
Blocks: newBlocks, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.