Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform plan -generate-config-out fails for schemas with nested and computed "id" attributes #35178

Closed
henryrecker-pingidentity opened this issue May 17, 2024 · 1 comment · Fixed by #35220
Assignees
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code plannable-import

Comments

@henryrecker-pingidentity
Copy link

henryrecker-pingidentity commented May 17, 2024

Terraform Version

Terraform v1.8.3
on darwin_amd64

Terraform Configuration Files

terraform {
  required_version = ">=1.1"
  required_providers {
    example = {
      version = "~> 0.0.1"
      source  = "example/example"
    }
  }
}

provider "example" {
}

import {
  to = example_import_error.generated
  id = "idVal"
}

Debug Output

https://gist.github.com/henryrecker-pingidentity/96529224dfd95a19d58bdd1843371048

Expected Behavior

terraform plan -generate-config-out should create the HCL in generated.tf as expected, including all attribute values.

Actual Behavior

An error occurs when using an attribute named "id" that is computed in a listnested attribute. The "id" value is left out of the resulting generated HCL file. Note: A normal terraform import works fine. It's just the terraform plan -generate-config-out that fails.

Error:

example_import_error.generated: Preparing import... [id=idVal]
example_import_error.generated: Refreshing state...

Planning failed. Terraform encountered an error while generating this plan.

╷
│ Warning: Config generation is experimental
│
│ Generating configuration during import is currently experimental, and the generated configuration format may change in future
│ versions.
╵
╷
│ Error: attribute "list_val": incorrect list element type: attribute "id" is required
│
│
╵

Output generated HCL file:

# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.

# __generated__ by Terraform
resource "example_import_error" "generated" {
  list_val = [
    {
    },
  ]
  test_id = "id"
}

Steps to Reproduce

I used a sample provider I built with terraform-plugin-framework. The provider has an example_import_error resource with one basic test_id attribute, and one listnested attribute with an id attribute in the nested object.

The implementation of this resource is in this gist - https://gist.github.com/henryrecker-pingidentity/490a9ee2d0a7702463807686ff276c00
The full provider is located here (on the linked ImportBlockBug branch) - https://github.com/henryrecker-pingidentity/terraform-provider-example/tree/ImportBlockBug

Then with a locally built provider, I ran terraform plan -generate-config-out=generated.tf with the configuration file above.

Additional Context

This does not occur with normal terraform import (terraform import example_import_error.ex test for example) or with normal terraform CRUD operations.

This seems to specifically be related to the attribute name "id". If I change the listnested attribute to use a different string attribute name, the error goes away.

It also seems to be specific to the "id" value being computed. If not computed, the error also goes away.

References

No response

@henryrecker-pingidentity henryrecker-pingidentity added bug new new issue not yet triaged labels May 17, 2024
@liamcervante liamcervante self-assigned this May 21, 2024
@liamcervante liamcervante added confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code and removed new new issue not yet triaged labels May 21, 2024
@liamcervante
Copy link
Member

I think the root cause of this is https://github.com/hashicorp/terraform/blob/main/internal/terraform/node_resource_plan_instance.go#L751-L761.

Essentially, the legacy SDK creates ID attributes for resources that users don't necessarily know about. Our config generation then tries to exclude them. I wonder if we could include a path parameter in the filter logic so that it only excludes the id attribute at the root level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code plannable-import
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants