From 023381479ef781d115dff79b9efb9ed69cfbfbab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergen=20Yal=C3=A7=C4=B1n?= Date: Thu, 24 Feb 2022 17:22:57 +0300 Subject: [PATCH] Support for configuring blocks with optional parameters in attributes-as-blocks mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sergen Yalçın (cherry picked from commit beb36bcf7d98bdf00ea32d943c724d6952bd24f6) --- pkg/types/builder.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/types/builder.go b/pkg/types/builder.go index a65e6098..56509f7b 100644 --- a/pkg/types/builder.go +++ b/pkg/types/builder.go @@ -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, @@ -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, @@ -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 { @@ -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)) }