Skip to content

Commit d6f94a2

Browse files
authored
helper/schema: Export tfprotov5 schema and identity schema for consumption by other Go modules (#1504)
* quick helper * add identity func * add changelogs * copyright * remove weird global modification * sort identity schema
1 parent dae43f4 commit d6f94a2

File tree

5 files changed

+796
-14
lines changed

5 files changed

+796
-14
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: ENHANCEMENTS
2+
body: 'helper/schema: Added new helper methods for converting Resource and Identity schemas to protocol representations.'
3+
time: 2025-08-07T18:09:02.117077-04:00
4+
custom:
5+
Issue: "1504"

helper/schema/core_schema.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
package schema
55

66
import (
7+
"context"
78
"fmt"
89

910
"github.com/hashicorp/go-cty/cty"
11+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
1012

1113
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugin/convert"
1215
)
1316

1417
// StringKind represents the format a string is in.
@@ -397,3 +400,37 @@ func (r *Resource) coreIdentitySchema() (*configschema.Block, error) {
397400
// to convert our schema
398401
return schemaMap(r.Identity.SchemaMap()).CoreConfigSchema(), nil
399402
}
403+
404+
// ProtoSchema will return a function that returns the *tfprotov5.Schema
405+
func (r *Resource) ProtoSchema(ctx context.Context) func() *tfprotov5.Schema {
406+
return func() *tfprotov5.Schema {
407+
return &tfprotov5.Schema{
408+
Version: int64(r.SchemaVersion),
409+
Block: convert.ConfigSchemaToProto(ctx, r.CoreConfigSchema()),
410+
}
411+
}
412+
}
413+
414+
// ProtoIdentitySchema will return a function that returns the *tfprotov5.ResourceIdentitySchema if the resource supports identity,
415+
// otherwise it will return nil.
416+
func (r *Resource) ProtoIdentitySchema(ctx context.Context) func() *tfprotov5.ResourceIdentitySchema {
417+
// Resource doesn't support identity, return nil
418+
if r.Identity == nil {
419+
return nil
420+
}
421+
422+
return func() *tfprotov5.ResourceIdentitySchema {
423+
idschema, err := r.CoreIdentitySchema()
424+
425+
if err != nil {
426+
// This shouldn't be reachable unless there is an implementation error in the provider, which should raise
427+
// a diagnostic prior to reaching this point.
428+
panic(fmt.Sprintf("unexpected error retrieving identity schema: %s", err))
429+
}
430+
431+
return &tfprotov5.ResourceIdentitySchema{
432+
Version: r.Identity.Version,
433+
IdentityAttributes: convert.ConfigIdentitySchemaToProto(ctx, idschema),
434+
}
435+
}
436+
}

0 commit comments

Comments
 (0)