|
4 | 4 | package schema |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "context" |
7 | 8 | "fmt" |
8 | 9 |
|
9 | 10 | "github.com/hashicorp/go-cty/cty" |
| 11 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5" |
10 | 12 |
|
11 | 13 | "github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugin/convert" |
12 | 15 | ) |
13 | 16 |
|
14 | 17 | // StringKind represents the format a string is in. |
@@ -397,3 +400,37 @@ func (r *Resource) coreIdentitySchema() (*configschema.Block, error) { |
397 | 400 | // to convert our schema |
398 | 401 | return schemaMap(r.Identity.SchemaMap()).CoreConfigSchema(), nil |
399 | 402 | } |
| 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