Skip to content

Commit b66db47

Browse files
authored
Add logging for unsupported types present when generating to/from methods (#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)
1 parent 6bb8db3 commit b66db47

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+817
-152
lines changed

go.mod

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
module github.com/hashicorp/terraform-plugin-codegen-framework
22

3-
go 1.20
3+
go 1.21
44

55
require (
66
github.com/google/go-cmp v0.6.0
77
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.1-0.20231024091233-c659ac8a54fc
8-
github.com/hashicorp/terraform-plugin-framework v1.4.0
9-
github.com/hashicorp/terraform-plugin-go v0.19.0
10-
github.com/mattn/go-colorable v0.1.12
8+
github.com/hashicorp/terraform-plugin-framework v1.4.2
9+
github.com/mattn/go-colorable v0.1.13
1110
github.com/mitchellh/cli v1.1.5
1211
)
1312

1413
require (
1514
github.com/Masterminds/goutils v1.1.1 // indirect
16-
github.com/Masterminds/semver/v3 v3.1.1 // indirect
17-
github.com/Masterminds/sprig/v3 v3.2.1 // indirect
18-
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 // indirect
15+
github.com/Masterminds/semver/v3 v3.2.1 // indirect
16+
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
17+
github.com/armon/go-radix v1.0.0 // indirect
1918
github.com/bgentry/speakeasy v0.1.0 // indirect
20-
github.com/fatih/color v1.13.0 // indirect
21-
github.com/google/uuid v1.1.2 // indirect
22-
github.com/hashicorp/errwrap v1.0.0 // indirect
19+
github.com/fatih/color v1.15.0 // indirect
20+
github.com/google/uuid v1.4.0 // indirect
21+
github.com/hashicorp/errwrap v1.1.0 // indirect
2322
github.com/hashicorp/go-hclog v1.5.0 // indirect
24-
github.com/hashicorp/go-multierror v1.0.0 // indirect
23+
github.com/hashicorp/go-multierror v1.1.1 // indirect
24+
github.com/hashicorp/terraform-plugin-go v0.19.0 // indirect
2525
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
26-
github.com/huandu/xstrings v1.3.2 // indirect
27-
github.com/imdario/mergo v0.3.11 // indirect
28-
github.com/mattn/go-isatty v0.0.14 // indirect
29-
github.com/mitchellh/copystructure v1.0.0 // indirect
26+
github.com/huandu/xstrings v1.4.0 // indirect
27+
github.com/imdario/mergo v0.3.16 // indirect
28+
github.com/mattn/go-isatty v0.0.20 // indirect
29+
github.com/mitchellh/copystructure v1.2.0 // indirect
3030
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
31-
github.com/mitchellh/reflectwalk v1.0.0 // indirect
32-
github.com/posener/complete v1.1.1 // indirect
33-
github.com/shopspring/decimal v1.2.0 // indirect
34-
github.com/spf13/cast v1.3.1 // indirect
35-
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
31+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
32+
github.com/posener/complete v1.2.3 // indirect
33+
github.com/shopspring/decimal v1.3.1 // indirect
34+
github.com/spf13/cast v1.5.1 // indirect
35+
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
3636
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
37-
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
37+
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
3838
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
3939
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
40-
golang.org/x/crypto v0.11.0 // indirect
41-
golang.org/x/sys v0.10.0 // indirect
40+
golang.org/x/crypto v0.14.0 // indirect
41+
golang.org/x/sys v0.13.0 // indirect
4242
)

go.sum

Lines changed: 81 additions & 26 deletions
Large diffs are not rendered by default.

internal/cmd/generate_all.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"context"
88
"flag"
99
"fmt"
10+
"log/slog"
11+
"os"
1012
"strings"
1113

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

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

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

82+
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
83+
Level: slog.LevelWarn,
84+
}))
85+
8086
fs := cmd.Flags()
8187
err := fs.Parse(args)
8288
if err != nil {
83-
cmd.UI.Error(fmt.Sprintf("error parsing command flags: %s", err))
89+
logger.Error("error parsing command flags", "err", err)
8490
return 1
8591
}
8692

87-
err = cmd.runInternal(ctx)
93+
err = cmd.runInternal(ctx, logger)
8894
if err != nil {
89-
cmd.UI.Error(fmt.Sprintf("Error executing command: %s\n", err))
95+
logger.Error("error executing command", "err", err)
9096
return 1
9197
}
9298

9399
return 0
94100
}
95101

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

115-
err = generateDataSourceCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "DataSource")
121+
err = generateDataSourceCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "DataSource", logger)
116122
if err != nil {
117123
return fmt.Errorf("error generating data source code: %w", err)
118124
}
119-
err = generateResourceCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "Resource")
125+
126+
err = generateResourceCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "Resource", logger)
120127
if err != nil {
121128
return fmt.Errorf("error generating resource code: %w", err)
122129
}
123-
err = generateProviderCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "Provider")
130+
131+
err = generateProviderCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "Provider", logger)
124132
if err != nil {
125133
return fmt.Errorf("error generating provider code: %w", err)
126134
}

internal/cmd/generate_data_sources.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"flag"
99
"fmt"
1010
"log"
11+
"log/slog"
12+
"os"
1113
"strings"
1214

1315
"github.com/hashicorp/terraform-plugin-codegen-spec/spec"
@@ -16,6 +18,7 @@ import (
1618
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_convert"
1719
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/format"
1820
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/input"
21+
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/logging"
1922
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/output"
2023
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
2124
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/validate"
@@ -82,14 +85,18 @@ func (a *GenerateDataSourcesCommand) Synopsis() string {
8285
func (cmd *GenerateDataSourcesCommand) Run(args []string) int {
8386
ctx := context.Background()
8487

88+
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
89+
Level: slog.LevelWarn,
90+
}))
91+
8592
fs := cmd.Flags()
8693
err := fs.Parse(args)
8794
if err != nil {
8895
cmd.UI.Error(fmt.Sprintf("error parsing command flags: %s", err))
8996
return 1
9097
}
9198

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

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

120-
err = generateDataSourceCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "DataSource")
127+
err = generateDataSourceCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "DataSource", logger)
121128
if err != nil {
122129
return fmt.Errorf("error generating data source code: %w", err)
123130
}
124131

125132
return nil
126133
}
127134

128-
func generateDataSourceCode(spec spec.Specification, outputPath, packageName, generatorType string) error {
135+
func generateDataSourceCode(ctx context.Context, spec spec.Specification, outputPath, packageName, generatorType string, logger *slog.Logger) error {
136+
ctxWithPath := logging.SetPathInContext(ctx, "data_source")
137+
129138
// convert IR to framework schema
130139
c := datasource_convert.NewConverter(spec)
131140
s, err := c.ToGeneratorDataSourceSchema()
@@ -153,7 +162,7 @@ func generateDataSourceCode(spec spec.Specification, outputPath, packageName, ge
153162
}
154163

155164
// generate "expand" and "flatten" code
156-
toFromFunctions, err := g.ToFromFunctions()
165+
toFromFunctions, err := g.ToFromFunctions(ctxWithPath, logger)
157166
if err != nil {
158167
log.Fatal(err)
159168
}

internal/cmd/generate_provider.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import (
88
"flag"
99
"fmt"
1010
"log"
11+
"log/slog"
12+
"os"
1113
"strings"
1214

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

1618
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/format"
1719
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/input"
20+
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/logging"
1821
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/output"
1922
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/provider_convert"
2023
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
@@ -82,14 +85,18 @@ func (a *GenerateProviderCommand) Synopsis() string {
8285
func (cmd *GenerateProviderCommand) Run(args []string) int {
8386
ctx := context.Background()
8487

88+
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
89+
Level: slog.LevelWarn,
90+
}))
91+
8592
fs := cmd.Flags()
8693
err := fs.Parse(args)
8794
if err != nil {
8895
cmd.UI.Error(fmt.Sprintf("error parsing command flags: %s", err))
8996
return 1
9097
}
9198

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

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

120-
err = generateProviderCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "Provider")
127+
err = generateProviderCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "Provider", logger)
121128
if err != nil {
122129
return fmt.Errorf("error generating provider code: %w", err)
123130
}
124131

125132
return nil
126133
}
127134

128-
func generateProviderCode(spec spec.Specification, outputPath, packageName, generatorType string) error {
135+
func generateProviderCode(ctx context.Context, spec spec.Specification, outputPath, packageName, generatorType string, logger *slog.Logger) error {
136+
ctx = logging.SetPathInContext(ctx, "provider")
137+
129138
// convert IR to framework schema
130139
c := provider_convert.NewConverter(spec)
131140
s, err := c.ToGeneratorProviderSchema()
@@ -153,7 +162,7 @@ func generateProviderCode(spec spec.Specification, outputPath, packageName, gene
153162
}
154163

155164
// generate "expand" and "flatten" code
156-
toFromFunctions, err := g.ToFromFunctions()
165+
toFromFunctions, err := g.ToFromFunctions(ctx, logger)
157166
if err != nil {
158167
log.Fatal(err)
159168
}

internal/cmd/generate_resources.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import (
88
"flag"
99
"fmt"
1010
"log"
11+
"log/slog"
12+
"os"
1113
"strings"
1214

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

1618
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/format"
1719
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/input"
20+
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/logging"
1821
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/output"
1922
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/resource_convert"
2023
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
@@ -82,14 +85,18 @@ func (a *GenerateResourcesCommand) Synopsis() string {
8285
func (cmd *GenerateResourcesCommand) Run(args []string) int {
8386
ctx := context.Background()
8487

88+
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
89+
Level: slog.LevelWarn,
90+
}))
91+
8592
fs := cmd.Flags()
8693
err := fs.Parse(args)
8794
if err != nil {
8895
cmd.UI.Error(fmt.Sprintf("error parsing command flags: %s", err))
8996
return 1
9097
}
9198

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

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

120-
err = generateResourceCode(spec, cmd.flagOutputPath, cmd.flagPackageName, "Resource")
127+
err = generateResourceCode(ctx, spec, cmd.flagOutputPath, cmd.flagPackageName, "Resource", logger)
121128
if err != nil {
122129
return fmt.Errorf("error generating resource code: %w", err)
123130
}
124131

125132
return nil
126133
}
127134

128-
func generateResourceCode(spec spec.Specification, outputPath, packageName, generatorType string) error {
135+
func generateResourceCode(ctx context.Context, spec spec.Specification, outputPath, packageName, generatorType string, logger *slog.Logger) error {
136+
ctx = logging.SetPathInContext(ctx, "resource")
137+
129138
// convert IR to framework schema
130139
c := resource_convert.NewConverter(spec)
131140
s, err := c.ToGeneratorResourceSchema()
@@ -153,7 +162,7 @@ func generateResourceCode(spec spec.Specification, outputPath, packageName, gene
153162
}
154163

155164
// generate "expand" and "flatten" code
156-
toFromFunctions, err := g.ToFromFunctions()
165+
toFromFunctions, err := g.ToFromFunctions(ctx, logger)
157166
if err != nil {
158167
log.Fatal(err)
159168
}

internal/datasource_generate/list_attribute.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,12 @@ func (g GeneratorListAttribute) ToFromFunctions(name string) ([]byte, error) {
205205

206206
elementTypeType := generatorschema.GetElementType(g.ElementType)
207207
elementTypeValue := generatorschema.GetElementValueType(g.ElementType)
208-
elementFrom := generatorschema.GetElementFromFunc(g.ElementType)
208+
209+
elementFrom, err := generatorschema.GetElementFromFunc(g.ElementType)
210+
211+
if err != nil {
212+
return nil, err
213+
}
209214

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

0 commit comments

Comments
 (0)