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

Reference nested fields in other resources #412

Closed
marcoandredinis opened this issue Jul 13, 2022 · 9 comments
Closed

Reference nested fields in other resources #412

marcoandredinis opened this issue Jul 13, 2022 · 9 comments
Assignees
Labels
bug Something isn't working

Comments

@marcoandredinis
Copy link

marcoandredinis commented Jul 13, 2022

Module version

github.com/hashicorp/terraform-plugin-framework v0.9.0

Relevant provider source code

Slightly modified version of the https://github.com/hashicorp/terraform-provider-scaffolding-framework
This version adds two extra fields:

		Attributes: map[string]tfsdk.Attribute{
			"id": {
				Computed:            true,
				MarkdownDescription: "Example identifier",
				PlanModifiers: tfsdk.AttributePlanModifiers{
					tfsdk.UseStateForUnknown(),
				},
				Type: types.StringType,
			},
			"metadata": {
				MarkdownDescription: "Meta info",
				Optional:            true,
				Attributes: tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
					"name": {
						Description: "Name is an object name",
						Required:    true,
						Type:        types.StringType,
					},
				}),
			},
		},

Terraform Configuration Files

The following fails to run with

resource "scaffolding_example" "test" {
  metadata = {
    name = "abc"
  }
}
resource "scaffolding_example" "test2" {
  metadata = {
    name = scaffolding_example.test.metadata.name
  }
}

Debug Output

Error when running Acceptance Tests

TF_ACC=1 go test ./... -v  -timeout 120m
?       github.com/hashicorp/terraform-provider-scaffolding-framework   [no test files]
=== RUN   TestAccExampleResource
    example_resource_test.go:10: Step 1/1 error: Error running pre-apply refresh: exit status 1
        
        Error: Unsupported attribute
        
          on terraform_plugin_test.tf line 9, in resource "scaffolding_example" "test2":
           9:     name = scaffolding_example.test.metadata.name
        
        This value does not have any attributes.
--- FAIL: TestAccExampleResource (0.08s)

Expected Behavior

It was expected that this code ran without any issues

Actual Behavior

Fails with error above

Steps to Reproduce

Clone https://github.com/hashicorp/terraform-provider-scaffolding-framework
Add the schema above to the example_resource.go
Change example_resource_test.go and testAccExampleResourceConfig with the config above
You must also replace the Steps list with this single step:

Steps: []resource.TestStep{
			// Create and Read testing
			{
				Config: testAccExampleResourceConfig(),
				Check: resource.ComposeAggregateTestCheckFunc(
					resource.TestCheckResourceAttr("scaffolding_example.test", "metadata.name", "abc"),
					resource.TestCheckResourceAttr("scaffolding_example.test2", "metadata.name", "abc"),
				),
			},
		},

Run make acctest

Workaround

If you change name = scaffolding_example.test.metadata.name to name = (scaffolding_example.test).metadata.name everything works as expected

References

@marcoandredinis marcoandredinis added the bug Something isn't working label Jul 13, 2022
@marcoandredinis
Copy link
Author

I created a small reproducible repo here
https://github.com/marcoandredinis/terraform-provider-scaffolding-framework

@marcoandredinis marcoandredinis changed the title Reference nest fields in other resources Reference nested fields in other resources Jul 14, 2022
@bflad
Copy link
Member

bflad commented Jul 26, 2022

Hi @marcoandredinis 👋 Thank you for raising this and sorry you ran into trouble here.

I have two clarification questions:

  • Which version of Terraform CLI is being used?
  • Does this only happen when using the terraform-plugin-sdk based acceptance testing framework or does it also occur in real Terraform configurations outside the testing framework (e.g. a regular terraform apply command)?

Thanks so much.

@bflad bflad self-assigned this Jul 26, 2022
@bflad bflad added the waiting-response Issues or pull requests waiting for an external response label Jul 26, 2022
@bflad
Copy link
Member

bflad commented Jul 26, 2022

By the way, thank you for the easy reproduction. I was able to generate TRACE logging for everything with that repository and running:

$ TF_ACC=1 TF_LOG=TRACE TF_ACC_LOG_PATH=/tmp/test.txt go test -count=1 -run='TestAccExampleResource' -v ./internal/provider
=== RUN   TestAccExampleResource
    example_resource_test.go:10: Step 1/1 error: Error running pre-apply refresh: exit status 1

        Error: Unsupported attribute

          on terraform_plugin_test.tf line 9, in resource "scaffolding_example" "test2":
           9: 	  name = scaffolding_example.test.metadata.name

        This value does not have any attributes.
--- FAIL: TestAccExampleResource (0.11s)

Gist of TRACE logging: https://gist.github.com/bflad/1fa2b2a3fd92eabb6596f6b1dfa853e7

@marcoandredinis
Copy link
Author

Hey @bflad
Thank you for getting back

So, the version is

Terraform v1.2.5
on linux_amd64

This also happens when running terraform apply on a "real" setting.

It's my understanding that you were able to reproduce the issue.
Did you also validate that adding () fixes the problem?
So, changing to

 name = (scaffolding_example.test).metadata.name

@bflad
Copy link
Member

bflad commented Jul 27, 2022

Hi @marcoandredinis 👋 I was able to validate that using the parenthesis in the configuration reference does indeed workaround the error.

From the TRACE logging, it appears this error is coming from Terraform itself while attempting to validate the Terraform configuration. Given that, I have raised hashicorp/terraform#31527 in their issue tracker to follow up further on this. I would personally expect Terraform in this situation to either "just work" or provide a more helpful error message. 👍

I'd like to keep this issue open here in case there happens to be anything we're doing wrong on our side, or in case others may run into this issue until it is hopefully resolved in some form in Terraform itself. Thanks again for your work here!

@bflad
Copy link
Member

bflad commented Jul 27, 2022

There is now a proposed fix upstream: hashicorp/terraform#31532. If you're feeling really adventurous, you can try building Terraform using that PR branch to see how it behaves or we can wait until this gets released into a 1.2.x patch to verify it. 😄

@marcoandredinis
Copy link
Author

It does seem to work as expected 👍

marco@lenix ~/p/terraform-provider-scaffolding-framework (main)> terraform version
Terraform v1.2.5
on linux_amd64

Your version of Terraform is out of date! The latest version
is 1.2.6. You can update by downloading from https://www.terraform.io/downloads.html
marco@lenix ~/p/terraform-provider-scaffolding-framework (main)> make testacc
TF_ACC=1 go test ./... -v  -timeout 120m
?       github.com/hashicorp/terraform-provider-scaffolding-framework   [no test files]
=== RUN   TestAccExampleResource
    example_resource_test.go:10: Step 1/1 error: Error running pre-apply refresh: exit status 1
        
        Error: Unsupported attribute
        
          on terraform_plugin_test.tf line 9, in resource "scaffolding_example" "test2":
           9:     name = scaffolding_example.test.metadata.name
        
        This value does not have any attributes.
--- FAIL: TestAccExampleResource (0.11s)
FAIL
FAIL    github.com/hashicorp/terraform-provider-scaffolding-framework/internal/provider 0.117s
FAIL
make: *** [GNUmakefile:6: testacc] Error 1
marco@lenix ~/p/terraform-provider-scaffolding-framework (main)> terraform version
Terraform v1.3.0-dev
on linux_amd64
marco@lenix ~/p/terraform-provider-scaffolding-framework (main)> make testacc
TF_ACC=1 go test ./... -v  -timeout 120m
?       github.com/hashicorp/terraform-provider-scaffolding-framework   [no test files]
=== RUN   TestAccExampleResource
--- PASS: TestAccExampleResource (0.34s)
PASS
ok      github.com/hashicorp/terraform-provider-scaffolding-framework/internal/provider 0.342s

@bflad bflad removed the waiting-response Issues or pull requests waiting for an external response label Jul 29, 2022
@bflad
Copy link
Member

bflad commented Aug 9, 2022

The fix for this has been merged upstream in Terraform core and will release with version 1.2.7 and the next 1.3.0 pre-release. 👍

@bflad bflad closed this as completed Aug 9, 2022
@github-actions
Copy link

github-actions bot commented Sep 9, 2022

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants