Hi,
I am currently stuck with the usage of pre-apply plan checks with terraform-plugin-testing and was wondering if anybody could point me in a correct direction.
In our resource we are adding a resource ModifyPlan() to populate the id already during plan phase when a resource needs to be created. The reason for this is because the id refers to a specific naming of the object to be managed which is useful to know for users during plan for other external validation use-cases.
When executing the plan, it correctly displays my desired output in cli and json plan output.
See example (generalised the example) of how the output would look with the constructed id from the required attribute input.
Terraform will perform the following actions:
# resource_name.test_2 will be created
+ resource "resource_name" "test_2" {
+ id = "some-value-[test_key]"
+ key = "test_key"
}
All my current tests are still functioning as expected and the next thing I wanted to add is test validations for the id being set during plan. From reading the documentation my expectation was to use the ConfigPlanChecks - PreApply in my tests. See example below:
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectKnownValue(
"resource_name.test_2",
tfjsonpath.New("id"),
knownvalue.NotNull(),
),
},
},
But this results in the following error:
Step 1/1 error: Pre-apply plan check(s) failed:
path not found: specified key id not found in map at id
To me this seems that tfjsonpath cannot find the “id” attribute and is throwing an error. If I set tfjsonpath.New("test_key")
it seems to be working as expected (assuming my understanding of the usage is correct).
Is there a way to test that the “id” attribute is being populated in plan?