Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20250807-180902.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: ENHANCEMENTS
body: 'helper/schema: Added new helper methods for converting Resource and Identity schemas to protocol representations.'
time: 2025-08-07T18:09:02.117077-04:00
custom:
Issue: "1504"
37 changes: 37 additions & 0 deletions helper/schema/core_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
package schema

import (
"context"
"fmt"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"

"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugin/convert"
)

// StringKind represents the format a string is in.
Expand Down Expand Up @@ -397,3 +400,37 @@ func (r *Resource) coreIdentitySchema() (*configschema.Block, error) {
// to convert our schema
return schemaMap(r.Identity.SchemaMap()).CoreConfigSchema(), nil
}

// ProtoSchema will return a function that returns the *tfprotov5.Schema
func (r *Resource) ProtoSchema(ctx context.Context) func() *tfprotov5.Schema {
return func() *tfprotov5.Schema {
return &tfprotov5.Schema{
Version: int64(r.SchemaVersion),
Block: convert.ConfigSchemaToProto(ctx, r.CoreConfigSchema()),
}
}
}

// ProtoIdentitySchema will return a function that returns the *tfprotov5.ResourceIdentitySchema if the resource supports identity,
// otherwise it will return nil.
func (r *Resource) ProtoIdentitySchema(ctx context.Context) func() *tfprotov5.ResourceIdentitySchema {
// Resource doesn't support identity, return nil
if r.Identity == nil {
return nil
}

return func() *tfprotov5.ResourceIdentitySchema {
idschema, err := r.CoreIdentitySchema()

if err != nil {
// This shouldn't be reachable unless there is an implementation error in the provider, which should raise
// a diagnostic prior to reaching this point.
panic(fmt.Sprintf("unexpected error retrieving identity schema: %s", err))
}

return &tfprotov5.ResourceIdentitySchema{
Version: r.Identity.Version,
IdentityAttributes: convert.ConfigIdentitySchemaToProto(ctx, idschema),
}
}
}
Loading