hashicorp / hashicorp/terraform-plugin-framework

Make IdentitySchema Attributes Available as Resource References

Open
#1,244 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
384
Forks
107
Avg merge
3m
Merged PRs (30d)
1

Description

## Feature Request: Make IdentitySchema Attributes Available as Resource References

### Current Behavior

When a resource defines an `id` attribute only in the `IdentitySchema` (via `resource.ResourceWithIdentity` interface), this attribute is not available for reference by other resources in Terraform configurations.

**Example:**

```go
// Resource with IdentitySchema only
func (r *ResourceKubernetes) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, res *resource.IdentitySchemaResponse) {
res.IdentitySchema = identityschema.Schema{
Attributes: map[string]identityschema.Attribute{
"id": identityschema.StringAttribute{RequiredForImport: true},
},
}
}

func (r ResourceR) Schema(_ context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{Required: true},
// Note: no "id" attribute here
},
}
}
```

**Attempted Usage:**

```hcl

resource "clevercloud_s" "nodes" {
kubernetes_id = clevercloud_r.cluster.id # ❌ Error: Unsupported attribute
name = "worker-nodes"
flavor = "XS"
size = 3
}
```

**Error:**
```
Error: Unsupported attribute

on terraform_plugin_test.tf line 20, in resource "clevercloud_s":
20: kubernetes_id = "${clevercloud_r.cluster.id}"

This object does not have an attribute named "id".
```

### Expected Behavior

Attributes defined in `IdentitySchema` should be automatically available as computed attributes in the resource's main schema, allowing them to be referenced by other resources without requiring manual duplication.

### Current Workaround

Currently, providers must manually duplicate the `id` attribute in both schemas:

```go
func (r ResourceR) Schema(_ context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{Computed: true, PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}},
"name": schema.StringAttribute{Required: true},
"kubeconfig": schema.StringAttribute{Computed: true},
},
}
}

func (r *ResourceR) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, res *resource.IdentitySchemaResponse) {
res.IdentitySchema = identityschema.Schema{
Attributes: map[string]identityschema.Attribute{
"id": identityschema.StringAttribute{RequiredForImport: true},
},
}
}
```

This creates unnecessary duplication and potential for inconsistency between the two schemas.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.