Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

[Backport release-0.4] Support for configuring blocks with optional parameters in attributes-as-blocks mode #254

Merged
merged 1 commit into from
Mar 7, 2022
Merged
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
13 changes: 10 additions & 3 deletions pkg/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Builder struct {

// Build returns parameters and observation types built out of Terraform schema.
func (g *Builder) Build(cfg *config.Resource) (Generated, error) {
fp, ap, err := g.buildResource(cfg.TerraformResource, cfg, nil, nil, cfg.Kind)
fp, ap, err := g.buildResource(cfg.TerraformResource, cfg, nil, nil, false, cfg.Kind)
return Generated{
Types: g.genTypes,
Comments: g.comments,
Expand All @@ -73,7 +73,7 @@ func (g *Builder) Build(cfg *config.Resource) (Generated, error) {
}, errors.Wrapf(err, "cannot build the Types")
}

func (g *Builder) buildResource(res *schema.Resource, cfg *config.Resource, tfPath []string, xpPath []string, names ...string) (*types.Named, *types.Named, error) { //nolint:gocyclo
func (g *Builder) buildResource(res *schema.Resource, cfg *config.Resource, tfPath []string, xpPath []string, asBlocksMode bool, names ...string) (*types.Named, *types.Named, error) { //nolint:gocyclo
// NOTE(muvaf): There can be fields in the same CRD with same name but in
// different types. Since we generate the type using the field name, there
// can be collisions. In order to be able to generate unique names consistently,
Expand Down Expand Up @@ -197,6 +197,9 @@ func (g *Builder) buildResource(res *schema.Resource, cfg *config.Resource, tfPa
obsFields = append(obsFields, field)
obsTags = append(obsTags, fmt.Sprintf(`json:"%s" tf:"%s"`, jsonTag, tfTag))
default:
if asBlocksMode {
tfTag = strings.TrimSuffix(tfTag, ",omitempty")
}
if sch.Optional {
paramTags = append(paramTags, fmt.Sprintf(`json:"%s" tf:"%s"`, jsonTag, tfTag))
} else {
Expand Down Expand Up @@ -269,9 +272,13 @@ func (g *Builder) buildSchema(sch *schema.Schema, cfg *config.Resource, tfPath [
return nil, errors.Wrapf(err, "cannot infer type from schema of element type of %s", fieldPath(names))
}
case *schema.Resource:
var asBlocksMode bool
// TODO(muvaf): We skip the other type once we choose one of param
// or obs types. This might cause some fields to be completely omitted.
paramType, obsType, err := g.buildResource(et, cfg, tfPath, xpPath, names...)
if sch.ConfigMode == schema.SchemaConfigModeAttr {
asBlocksMode = true
}
paramType, obsType, err := g.buildResource(et, cfg, tfPath, xpPath, asBlocksMode, names...)
if err != nil {
return nil, errors.Wrapf(err, "cannot infer type from resource schema of element type of %s", fieldPath(names))
}
Expand Down