Skip to content

Commit

Permalink
validate resource identity schema
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Feb 12, 2025
1 parent a7441b6 commit 7e6d5d7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
18 changes: 15 additions & 3 deletions internal/plugin/convert/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package convert

import (
"encoding/json"
"fmt"
"reflect"
"sort"

"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/tfdiags"
proto "github.com/hashicorp/terraform/internal/tfplugin5"
)

Expand Down Expand Up @@ -189,7 +191,8 @@ func sortedKeys(m interface{}) []string {
return keys
}

func ProtoToResourceIdentitySchema(s *proto.ResourceIdentitySchema) providers.IdentitySchema {
func ProtoToResourceIdentitySchema(s *proto.ResourceIdentitySchema) (providers.IdentitySchema, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
schema := providers.IdentitySchema{
Version: s.Version,
Attributes: make(configschema.IdentityAttributes),
Expand All @@ -204,12 +207,21 @@ func ProtoToResourceIdentitySchema(s *proto.ResourceIdentitySchema) providers.Id

if a.Type != nil {
if err := json.Unmarshal(a.Type, &attr.Type); err != nil {
panic(err)
diags = diags.Append(fmt.Errorf("Could not unmarshal type for attribute %q: %w", a.Name, err))
}
} else {
diags = diags.Append(fmt.Errorf("Attribute %q is missing a type definition", a.Name))
}

if attr.RequiredForImport && attr.OptionalForImport {
diags = diags.Append(fmt.Errorf("Attribute %q cannot be both required and optional for import", a.Name))
}
if !attr.RequiredForImport && !attr.OptionalForImport {
diags = diags.Append(fmt.Errorf("Attribute %q must be either required or optional for import", a.Name))
}

schema.Attributes[a.Name] = attr
}

return schema
return schema, diags
}
10 changes: 9 additions & 1 deletion internal/plugin/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hashicorp/terraform/internal/logging"
"github.com/hashicorp/terraform/internal/plugin/convert"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/tfdiags"
proto "github.com/hashicorp/terraform/internal/tfplugin5"
)

Expand Down Expand Up @@ -218,7 +219,14 @@ func (p *GRPCProvider) GetResourceIdentitySchemas() providers.GetResourceIdentit
}

for name, res := range protoResp.IdentitySchemas {
resp.IdentityTypes[name] = convert.ProtoToResourceIdentitySchema(res)
var protoDiags tfdiags.Diagnostics
resp.IdentityTypes[name], protoDiags = convert.ProtoToResourceIdentitySchema(res)

var wrappedDiags tfdiags.Diagnostics
for _, diag := range protoDiags {
wrappedDiags = wrappedDiags.Append(fmt.Errorf("error in resource identity schema for resource %q: %s", name, diag))
}
resp.Diagnostics = resp.Diagnostics.Append(wrappedDiags)
}

// set the global cache if we can
Expand Down
18 changes: 15 additions & 3 deletions internal/plugin6/convert/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package convert

import (
"encoding/json"
"fmt"
"reflect"
"sort"

"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/tfdiags"
proto "github.com/hashicorp/terraform/internal/tfplugin6"
"github.com/zclconf/go-cty/cty"
)
Expand Down Expand Up @@ -103,7 +105,8 @@ func ProtoToProviderSchema(s *proto.Schema) providers.Schema {
}
}

func ProtoToResourceIdentitySchema(s *proto.ResourceIdentitySchema) providers.IdentitySchema {
func ProtoToResourceIdentitySchema(s *proto.ResourceIdentitySchema) (providers.IdentitySchema, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
schema := providers.IdentitySchema{
Version: s.Version,
Attributes: make(configschema.IdentityAttributes),
Expand All @@ -118,14 +121,23 @@ func ProtoToResourceIdentitySchema(s *proto.ResourceIdentitySchema) providers.Id

if a.Type != nil {
if err := json.Unmarshal(a.Type, &attr.Type); err != nil {
panic(err)
diags = diags.Append(fmt.Errorf("Could not unmarshal type for attribute %q: %w", a.Name, err))
}
} else {
diags = diags.Append(fmt.Errorf("Attribute %q is missing a type definition", a.Name))
}

if attr.RequiredForImport && attr.OptionalForImport {
diags = diags.Append(fmt.Errorf("Attribute %q cannot be both required and optional for import", a.Name))
}
if !attr.RequiredForImport && !attr.OptionalForImport {
diags = diags.Append(fmt.Errorf("Attribute %q must be either required or optional for import", a.Name))
}

schema.Attributes[a.Name] = attr
}

return schema
return schema, diags
}

// ProtoToConfigSchema takes the GetSchcema_Block from a grpc response and converts it
Expand Down
14 changes: 13 additions & 1 deletion internal/plugin6/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hashicorp/terraform/internal/logging"
"github.com/hashicorp/terraform/internal/plugin6/convert"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/tfdiags"
proto6 "github.com/hashicorp/terraform/internal/tfplugin6"
)

Expand Down Expand Up @@ -216,7 +217,18 @@ func (p *GRPCProvider) GetResourceIdentitySchemas() providers.GetResourceIdentit
}

for name, res := range protoResp.IdentitySchemas {
resp.IdentityTypes[name] = convert.ProtoToResourceIdentitySchema(res)
var protoDiags tfdiags.Diagnostics
resp.IdentityTypes[name], protoDiags = convert.ProtoToResourceIdentitySchema(res)

var wrappedDiags tfdiags.Diagnostics
for _, diag := range protoDiags {
wrappedDiags = wrappedDiags.Append(fmt.Errorf("error in resource identity schema for resource %q: %s", name, diag))
}
resp.Diagnostics = resp.Diagnostics.Append(wrappedDiags)
}

if resp.Diagnostics.HasErrors() {
return resp
}

// set the global cache if we can
Expand Down

0 comments on commit 7e6d5d7

Please sign in to comment.