Skip to content

Commit

Permalink
Consolidate convert and generate packages for datasource, provider, a…
Browse files Browse the repository at this point in the history
…nd resource (#98)

* Replace datasource_convert pkg with constructors

* Renaming datasource_generate pkg to datasource

* Reusing predefined functions

* Switching to using constructor functions for provider generator types

* Moving provider_generate package to provider

* Switching to using constructor functions for resource generator types

* Linting and copyright headers
  • Loading branch information
bendbennett authored Dec 14, 2023
1 parent f4ec669 commit 5e7142e
Show file tree
Hide file tree
Showing 288 changed files with 19,583 additions and 21,468 deletions.
5 changes: 2 additions & 3 deletions internal/cmd/generate_data_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-codegen-spec/spec"
"github.com/mitchellh/cli"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_convert"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource"
"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"
Expand Down Expand Up @@ -136,8 +136,7 @@ func generateDataSourceCode(ctx context.Context, spec spec.Specification, output
ctxWithPath := logging.SetPathInContext(ctx, "data_source")

// convert IR to framework schema
c := datasource_convert.NewConverter(spec)
s, err := c.ToGeneratorDataSourceSchema()
s, err := datasource.NewSchemas(spec)
if err != nil {
return fmt.Errorf("error converting IR to Plugin Framework schema: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/generate_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"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/provider"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/validate"
)
Expand Down Expand Up @@ -136,8 +136,7 @@ func generateProviderCode(ctx context.Context, spec spec.Specification, outputPa
ctx = logging.SetPathInContext(ctx, "provider")

// convert IR to framework schema
c := provider_convert.NewConverter(spec)
s, err := c.ToGeneratorProviderSchema()
s, err := provider.NewSchemas(spec)
if err != nil {
return fmt.Errorf("error converting IR to Plugin Framework schema: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/generate_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"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/resource"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/validate"
)
Expand Down Expand Up @@ -136,8 +136,7 @@ func generateResourceCode(ctx context.Context, spec spec.Specification, outputPa
ctx = logging.SetPathInContext(ctx, "resource")

// convert IR to framework schema
c := resource_convert.NewConverter(spec)
s, err := c.ToGeneratorResourceSchema()
s, err := resource.NewSchemas(spec)
if err != nil {
return fmt.Errorf("error converting IR to Plugin Framework schema: %w", err)
}
Expand Down
108 changes: 108 additions & 0 deletions internal/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package convert

import specschema "github.com/hashicorp/terraform-plugin-codegen-spec/schema"

type DeprecationMessage struct {
deprecationMessage *string
}

func NewDeprecationMessage(d *string) DeprecationMessage {
return DeprecationMessage{
deprecationMessage: d,
}
}

func (s DeprecationMessage) DeprecationMessage() string {
if s.deprecationMessage == nil {
return ""
}

return *s.deprecationMessage
}

type Description struct {
description *string
}

func NewDescription(d *string) Description {
return Description{
description: d,
}
}

func (d Description) Description() string {
if d.description == nil {
return ""
}

return *d.description
}

type ComputedOptionalRequired struct {
computedOptionalRequired specschema.ComputedOptionalRequired
}

func NewComputedOptionalRequired(c specschema.ComputedOptionalRequired) ComputedOptionalRequired {
return ComputedOptionalRequired{
computedOptionalRequired: c,
}
}

func (c ComputedOptionalRequired) IsRequired() bool {
return c.computedOptionalRequired == specschema.Required
}

func (c ComputedOptionalRequired) IsOptional() bool {
if c.computedOptionalRequired == specschema.Optional || c.computedOptionalRequired == specschema.ComputedOptional {
return true
}

return false
}

func (c ComputedOptionalRequired) IsComputed() bool {
if c.computedOptionalRequired == specschema.Computed || c.computedOptionalRequired == specschema.ComputedOptional {
return true
}

return false
}

type OptionalRequired struct {
optionalRequired specschema.OptionalRequired
}

func NewOptionalRequired(c specschema.OptionalRequired) OptionalRequired {
return OptionalRequired{
optionalRequired: c,
}
}

func (c OptionalRequired) IsRequired() bool {
return c.optionalRequired == specschema.Required
}

func (c OptionalRequired) IsOptional() bool {
return c.optionalRequired == specschema.Optional
}

type Sensitive struct {
sensitive *bool
}

func NewSensitive(s *bool) Sensitive {
return Sensitive{
sensitive: s,
}
}

func (s Sensitive) IsSensitive() bool {
if s.sensitive == nil {
return false
}

return *s.sensitive
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package datasource_generate
package datasource

import (
"bytes"
"fmt"
"strings"
"text/template"

"github.com/hashicorp/terraform-plugin-codegen-spec/datasource"
specschema "github.com/hashicorp/terraform-plugin-codegen-spec/schema"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/convert"
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/model"
generatorschema "github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
)
Expand All @@ -26,6 +28,36 @@ type GeneratorBoolAttribute struct {
Validators specschema.BoolValidators
}

func NewGeneratorBoolAttribute(a *datasource.BoolAttribute) (GeneratorBoolAttribute, error) {
if a == nil {
return GeneratorBoolAttribute{}, fmt.Errorf("*datasource.BoolAttribute is nil")
}

c := convert.NewComputedOptionalRequired(a.ComputedOptionalRequired)

s := convert.NewSensitive(a.Sensitive)

d := convert.NewDescription(a.Description)

dm := convert.NewDeprecationMessage(a.DeprecationMessage)

return GeneratorBoolAttribute{
BoolAttribute: schema.BoolAttribute{
Required: c.IsRequired(),
Optional: c.IsOptional(),
Computed: c.IsComputed(),
Sensitive: s.IsSensitive(),
Description: d.Description(),
MarkdownDescription: d.Description(),
DeprecationMessage: dm.DeprecationMessage(),
},

AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
CustomType: a.CustomType,
Validators: a.Validators,
}, nil
}

func (g GeneratorBoolAttribute) GeneratorSchemaType() generatorschema.Type {
return generatorschema.GeneratorBoolAttribute
}
Expand Down
Loading

0 comments on commit 5e7142e

Please sign in to comment.