Required attribute is unknown for ValidateConfig?

I’m using the terraform plugin framework.

I have this (simplified) resource schema:

type Thing struct{}

func (t *Thing) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
    resp.Schema = schema.Schema{
        Attributes: map[string]schema.Attribute{
            "organization_id": schema.StringAttribute{
                Required: true,
                PlanModifiers: []planmodifier.String{
                    stringplanmodifier.RequiresReplace(),
                },
            },
            "cluster_id": schema.StringAttribute{
                Required: true,
                PlanModifiers: []planmodifier.String{
                    stringplanmodifier.RequiresReplace(),
                },
            },
        },
    }

I’ve implemented resource.ResourceWithValidateConfig. Here is ValidateConfig() . Please note I’m just showing the debug code. I really do need to validate multiple attributes on the resource.

func (t *Thing) ValidateConfig(
    ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse,
) {
    fmt.Println("###DEBUG### ValidateConfig")
    var config ThingModel
    resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
    if resp.Diagnostics.HasError() {
        return
    }

    if config.OrganizationId.IsUnknown() {
        fmt.Println("###DEBUG### config.OrganizationId is unknown")
    }

When I run terraform plan I see this output:

TF_LOG=WARN terraform plan
╷
│ Warning: Provider development overrides are in effect
│
│ The following provider development overrides are set in the CLI configuration:
│  - repo/repo in ~/bin
│
│ The behavior may therefore not match any released version of the provider and applying changes may cause the state to become incompatible with published releases.
╵
2024-09-25T11:36:49.401-0700 [WARN]  unexpected data: registry.terraform.io/blah/provider:stdout="###DEBUG### ValidateConfig"
2024-09-25T11:36:49.402-0700 [WARN]  unexpected data: registry.terraform.io/blah/provider:stdout="###DEBUG### config.OrganizationId is unknown"

In my terraform script I do have organization_id configured (otherwise I would get Missing required argument error).

Why is config.OrganizationId unknown in ValidateConfig ?

Hi @picavoh.mobijaf

The value would be unknown, because that is what Terraform is sending to the provider. Can you explain the problem you are trying to solve?

Consider this configuration:

resource "thing_a" "x" {
  attr_1 = "foo"
}

resource "thing_b" "x" {
  attr_1 = "bar"
  atto_2 = thing_a.x.some_computed_attribute
}

Imagine that the ThingB.ValidateConfig() method wants to inspect its attr_2 attribute.

If we run terraform validate before ever running terraform apply, the value for ThingB.Attr2 is going to be unknown.

This should not be an error. If it was, we’d never be able to get these resources deployed.

Rest assured that the ValidateConfig() method will be called again when all of the Required values are known. It is safe to return without error when a to-be-validated attribute isn’t available because ValidateConfig() will be called again after the missing data is available.