Skip to content

Commit

Permalink
Add logging for unsupported types present when generating to/from met…
Browse files Browse the repository at this point in the history
…hods (#85)

* Bumping dependencies (#84)

* Adding logging for unimplemented element type to go type conversion (#84)

* Adding handling for unimplemented error when generating custom value type from functions (#84)

* Return unimplemented error when object attributes contain anything other than primitives (#84)

* Defining type to use with context value (#84)

* Adding To/From methods to nested attributes and blocks (#84)

* Handle To/From unimplemented errors for blocks (#84)

* Return unimplemented error for collections containing collections or objects (#84)

* Return unimplemented error for objects containing collections or objects (#84)
  • Loading branch information
bendbennett authored Nov 7, 2023
1 parent 6bb8db3 commit b66db47
Show file tree
Hide file tree
Showing 46 changed files with 817 additions and 152 deletions.
46 changes: 23 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
module github.com/hashicorp/terraform-plugin-codegen-framework

go 1.20
go 1.21

require (
github.com/google/go-cmp v0.6.0
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.1-0.20231024091233-c659ac8a54fc
github.com/hashicorp/terraform-plugin-framework v1.4.0
github.com/hashicorp/terraform-plugin-go v0.19.0
github.com/mattn/go-colorable v0.1.12
github.com/hashicorp/terraform-plugin-framework v1.4.2
github.com/mattn/go-colorable v0.1.13
github.com/mitchellh/cli v1.1.5
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.1 // indirect
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-multierror v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/terraform-plugin-go v0.19.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/posener/complete v1.1.1 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sys v0.13.0 // indirect
)
107 changes: 81 additions & 26 deletions go.sum

Large diffs are not rendered by default.

24 changes: 16 additions & 8 deletions internal/cmd/generate_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"strings"

"github.com/hashicorp/terraform-plugin-codegen-spec/spec"
Expand Down Expand Up @@ -70,30 +72,34 @@ func (cmd *GenerateAllCommand) Help() string {
return strBuilder.String()
}

func (a *GenerateAllCommand) Synopsis() string {
func (cmd *GenerateAllCommand) Synopsis() string {
return "Generate code for provider, resources, and data sources from an Intermediate Representation (IR) JSON file."
}

func (cmd *GenerateAllCommand) Run(args []string) int {
ctx := context.Background()

logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelWarn,
}))

fs := cmd.Flags()
err := fs.Parse(args)
if err != nil {
cmd.UI.Error(fmt.Sprintf("error parsing command flags: %s", err))
logger.Error("error parsing command flags", "err", err)
return 1
}

err = cmd.runInternal(ctx)
err = cmd.runInternal(ctx, logger)
if err != nil {
cmd.UI.Error(fmt.Sprintf("Error executing command: %s\n", err))
logger.Error("error executing command", "err", err)
return 1
}

return 0
}

func (cmd *GenerateAllCommand) runInternal(ctx context.Context) error {
func (cmd *GenerateAllCommand) runInternal(ctx context.Context, logger *slog.Logger) error {
// read input file
src, err := input.Read(cmd.flagIRInputPath)
if err != nil {
Expand All @@ -112,15 +118,17 @@ func (cmd *GenerateAllCommand) runInternal(ctx context.Context) error {
return fmt.Errorf("error parsing IR JSON: %w", err)
}

err = generateDataSourceCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "DataSource")
err = generateDataSourceCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "DataSource", logger)
if err != nil {
return fmt.Errorf("error generating data source code: %w", err)
}
err = generateResourceCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "Resource")

err = generateResourceCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "Resource", logger)
if err != nil {
return fmt.Errorf("error generating resource code: %w", err)
}
err = generateProviderCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "Provider")

err = generateProviderCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "Provider", logger)
if err != nil {
return fmt.Errorf("error generating provider code: %w", err)
}
Expand Down
19 changes: 14 additions & 5 deletions internal/cmd/generate_data_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"flag"
"fmt"
"log"
"log/slog"
"os"
"strings"

"github.com/hashicorp/terraform-plugin-codegen-spec/spec"
Expand All @@ -16,6 +18,7 @@ import (
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_convert"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/format"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/input"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/output"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/validate"
Expand Down Expand Up @@ -82,14 +85,18 @@ func (a *GenerateDataSourcesCommand) Synopsis() string {
func (cmd *GenerateDataSourcesCommand) Run(args []string) int {
ctx := context.Background()

logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelWarn,
}))

fs := cmd.Flags()
err := fs.Parse(args)
if err != nil {
cmd.UI.Error(fmt.Sprintf("error parsing command flags: %s", err))
return 1
}

err = cmd.runInternal(ctx)
err = cmd.runInternal(ctx, logger)
if err != nil {
cmd.UI.Error(fmt.Sprintf("Error executing command: %s\n", err))
return 1
Expand All @@ -98,7 +105,7 @@ func (cmd *GenerateDataSourcesCommand) Run(args []string) int {
return 0
}

func (cmd *GenerateDataSourcesCommand) runInternal(ctx context.Context) error {
func (cmd *GenerateDataSourcesCommand) runInternal(ctx context.Context, logger *slog.Logger) error {
// read input file
src, err := input.Read(cmd.flagIRInputPath)
if err != nil {
Expand All @@ -117,15 +124,17 @@ func (cmd *GenerateDataSourcesCommand) runInternal(ctx context.Context) error {
return fmt.Errorf("error parsing IR JSON: %w", err)
}

err = generateDataSourceCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "DataSource")
err = generateDataSourceCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "DataSource", logger)
if err != nil {
return fmt.Errorf("error generating data source code: %w", err)
}

return nil
}

func generateDataSourceCode(spec spec.Specification, outputPath, packageName, generatorType string) error {
func generateDataSourceCode(ctx context.Context, spec spec.Specification, outputPath, packageName, generatorType string, logger *slog.Logger) error {
ctxWithPath := logging.SetPathInContext(ctx, "data_source")

// convert IR to framework schema
c := datasource_convert.NewConverter(spec)
s, err := c.ToGeneratorDataSourceSchema()
Expand Down Expand Up @@ -153,7 +162,7 @@ func generateDataSourceCode(spec spec.Specification, outputPath, packageName, ge
}

// generate "expand" and "flatten" code
toFromFunctions, err := g.ToFromFunctions()
toFromFunctions, err := g.ToFromFunctions(ctxWithPath, logger)
if err != nil {
log.Fatal(err)
}
Expand Down
19 changes: 14 additions & 5 deletions internal/cmd/generate_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
"flag"
"fmt"
"log"
"log/slog"
"os"
"strings"

"github.com/hashicorp/terraform-plugin-codegen-spec/spec"
"github.com/mitchellh/cli"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/format"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/input"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/output"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/provider_convert"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
Expand Down Expand Up @@ -82,14 +85,18 @@ func (a *GenerateProviderCommand) Synopsis() string {
func (cmd *GenerateProviderCommand) Run(args []string) int {
ctx := context.Background()

logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelWarn,
}))

fs := cmd.Flags()
err := fs.Parse(args)
if err != nil {
cmd.UI.Error(fmt.Sprintf("error parsing command flags: %s", err))
return 1
}

err = cmd.runInternal(ctx)
err = cmd.runInternal(ctx, logger)
if err != nil {
cmd.UI.Error(fmt.Sprintf("Error executing command: %s\n", err))
return 1
Expand All @@ -98,7 +105,7 @@ func (cmd *GenerateProviderCommand) Run(args []string) int {
return 0
}

func (cmd *GenerateProviderCommand) runInternal(ctx context.Context) error {
func (cmd *GenerateProviderCommand) runInternal(ctx context.Context, logger *slog.Logger) error {
// read input file
src, err := input.Read(cmd.flagIRInputPath)
if err != nil {
Expand All @@ -117,15 +124,17 @@ func (cmd *GenerateProviderCommand) runInternal(ctx context.Context) error {
return fmt.Errorf("error parsing IR JSON: %w", err)
}

err = generateProviderCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "Provider")
err = generateProviderCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "Provider", logger)
if err != nil {
return fmt.Errorf("error generating provider code: %w", err)
}

return nil
}

func generateProviderCode(spec spec.Specification, outputPath, packageName, generatorType string) error {
func generateProviderCode(ctx context.Context, spec spec.Specification, outputPath, packageName, generatorType string, logger *slog.Logger) error {
ctx = logging.SetPathInContext(ctx, "provider")

// convert IR to framework schema
c := provider_convert.NewConverter(spec)
s, err := c.ToGeneratorProviderSchema()
Expand Down Expand Up @@ -153,7 +162,7 @@ func generateProviderCode(spec spec.Specification, outputPath, packageName, gene
}

// generate "expand" and "flatten" code
toFromFunctions, err := g.ToFromFunctions()
toFromFunctions, err := g.ToFromFunctions(ctx, logger)
if err != nil {
log.Fatal(err)
}
Expand Down
19 changes: 14 additions & 5 deletions internal/cmd/generate_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
"flag"
"fmt"
"log"
"log/slog"
"os"
"strings"

"github.com/hashicorp/terraform-plugin-codegen-spec/spec"
"github.com/mitchellh/cli"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/format"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/input"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/output"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/resource_convert"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
Expand Down Expand Up @@ -82,14 +85,18 @@ func (a *GenerateResourcesCommand) Synopsis() string {
func (cmd *GenerateResourcesCommand) Run(args []string) int {
ctx := context.Background()

logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelWarn,
}))

fs := cmd.Flags()
err := fs.Parse(args)
if err != nil {
cmd.UI.Error(fmt.Sprintf("error parsing command flags: %s", err))
return 1
}

err = cmd.runInternal(ctx)
err = cmd.runInternal(ctx, logger)
if err != nil {
cmd.UI.Error(fmt.Sprintf("Error executing command: %s\n", err))
return 1
Expand All @@ -98,7 +105,7 @@ func (cmd *GenerateResourcesCommand) Run(args []string) int {
return 0
}

func (cmd *GenerateResourcesCommand) runInternal(ctx context.Context) error {
func (cmd *GenerateResourcesCommand) runInternal(ctx context.Context, logger *slog.Logger) error {
// read input file
src, err := input.Read(cmd.flagIRInputPath)
if err != nil {
Expand All @@ -117,15 +124,17 @@ func (cmd *GenerateResourcesCommand) runInternal(ctx context.Context) error {
return fmt.Errorf("error parsing IR JSON: %w", err)
}

err = generateResourceCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "Resource")
err = generateResourceCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "Resource", logger)
if err != nil {
return fmt.Errorf("error generating resource code: %w", err)
}

return nil
}

func generateResourceCode(spec spec.Specification, outputPath, packageName, generatorType string) error {
func generateResourceCode(ctx context.Context, spec spec.Specification, outputPath, packageName, generatorType string, logger *slog.Logger) error {
ctx = logging.SetPathInContext(ctx, "resource")

// convert IR to framework schema
c := resource_convert.NewConverter(spec)
s, err := c.ToGeneratorResourceSchema()
Expand Down Expand Up @@ -153,7 +162,7 @@ func generateResourceCode(spec spec.Specification, outputPath, packageName, gene
}

// generate "expand" and "flatten" code
toFromFunctions, err := g.ToFromFunctions()
toFromFunctions, err := g.ToFromFunctions(ctx, logger)
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 6 additions & 1 deletion internal/datasource_generate/list_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ func (g GeneratorListAttribute) ToFromFunctions(name string) ([]byte, error) {

elementTypeType := generatorschema.GetElementType(g.ElementType)
elementTypeValue := generatorschema.GetElementValueType(g.ElementType)
elementFrom := generatorschema.GetElementFromFunc(g.ElementType)

elementFrom, err := generatorschema.GetElementFromFunc(g.ElementType)

if err != nil {
return nil, err
}

toFrom := generatorschema.NewToFromList(name, g.AssociatedExternalType, elementTypeType, elementTypeValue, elementFrom)

Expand Down
Loading

0 comments on commit b66db47

Please sign in to comment.