Plannable import and required field

Hi there,

I try the new 1.50 Plannable import with -generate-config-out .

import {
  to = resource.japan
  id = "11-11-11"
}

resource "resource" "japan" {
}

One of the issue is during the command plan terraform plan -generate-config-out=generated_resources.tf , I get name field is required but this field is imported, the import command work without any issue terraform import resource.japan 11-11-11 and I can see in the state file that name field is populated.

In my provider I use the following schema for name

"name": {
	Description: "name ... .",
	Type:        types.StringType,
	Required:    true,
},

When I change required to computed it works, but now the field is optional when declaring the resource.

How can I make this field required when defining the resource in the tf file and optional/computed when importing with the -generate-config-out config.

Hi @shinji62,

In your question you’ve shown both an import block and a resource block for the same resource. When you write your configuration that way, -generate-config-out will believe that it has no work to do, because the resource block is already present and so does not need to be generated.

Therefore I think the validation error you’re seeing is from Terraform trying to validate that empty resource block, rather than from the import step.

Have you tried removing your empty resource block before you create the plan? I think in that case the import system should notice that there isn’t already a resource block and so generate one for you based on the state import logic in the provider, and so then the validity of the result will depend on whether the provider’s import function returns something valid.