Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions pkg/types/conversion/tfjson/tfjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,31 +147,40 @@ func tfJSONBlockTypeToV2Schema(nb *tfjson.SchemaBlockType) *schemav2.Schema { //
// https://github.com/hashicorp/terraform-plugin-sdk/blob/6461ac6e9044a44157c4e2c8aec0f1ab7efc2055/helper/schema/core_schema.go#L204
v2sch.Computed = false
v2sch.Optional = false
if nb.MinItems > 0 {
v2sch.Required = true
v2sch.Optional = false
}
if nb.MinItems == 0 {
v2sch.Optional = true
}
if nb.MinItems == 0 && nb.MaxItems == 0 {
v2sch.Computed = true
}

switch nb.NestingMode { //nolint:exhaustive
switch nb.NestingMode {
case tfjson.SchemaNestingModeSet:
v2sch.Type = schemav2.TypeSet
case tfjson.SchemaNestingModeList:
v2sch.Type = schemav2.TypeList
case tfjson.SchemaNestingModeMap:
v2sch.Type = schemav2.TypeMap
case tfjson.SchemaNestingModeSingle:
v2sch.Type = schemav2.TypeList
v2sch.MinItems = 0
// TODO(erhan): not sure whether we need this
// the block itself can be optional, even if some child attribute
// or block is required
v2sch.Required = hasRequiredChild(nb)
v2sch.Optional = !v2sch.Required
if v2sch.Required {
v2sch.MinItems = 1
}
case tfjson.SchemaNestingModeSingle, tfjson.SchemaNestingModeGroup:
// This is a Plugin Framework-only nesting mode, and
// FW schemas never get Min/MaxItems specified in their
// core schemas (tfjson schema).
// See https://github.com/hashicorp/terraform-plugin-framework/blob/a0219204842978493e5f7742b0c06d5c39951e73/internal/fwschema/block.go#L24
// Therefore, the heuristics for determining Required/Optional/Computed
// does not make sense and inconclusive. Always make them optional,
// so that they generate a configurable spec field in the CRD.
// This only affects the CRD generation. Runtime schema validations
// are still valid and relevant.
// Provider developers can manually override these in their provider,
// if they want observation-only, or make them Required.
v2sch.Type = SchemaTypeObject

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: SchemaTypeObject is actually a workaround in upjet to represent plugin-fw schemas in sdkv2 schema structs. This actually leads in an invalid sdkv2 schema struct, and calling member functions like CoreConfigSchema() on the resulting TerraformResource causes a panic.

Though for plugin-framework resources, this object is CRD-generation purposes only and never utilized at runtime, and this path is framework-only ( as mentioned in https://github.com/crossplane/upjet/pull/593/changes#r3871736101 ), so I think this is acceptable.

v2sch.Required = false
v2sch.Optional = true
v2sch.Computed = false
v2sch.MaxItems = 1
default:
panic("unhandled nesting mode: " + nb.NestingMode)
Expand Down
Loading
Loading