I am creating my very first provider using the plugin framework tutorial for hashicups. This is also my very first attempt at Go, so bear with me
One of the resources has an optional tags
property which is a list of strings, defined in the model as:
Tags types.List `tfsdk:"tags"`
And in the schema as:
"tags": schema.ListAttribute{
Description: "List of tags for this client",
ElementType: types.StringType,
Optional: true,
},
I am having an issue when the resource wants to supply an empty list for the attribute:
resource "my_client" "test" {
name = "Test Client"
tags = []
}
Whenever this is supplied, I get the following error:
my_client.test: Creating...
â•·
│ Error: Provider produced inconsistent result after apply
│
│ When applying changes to my_client.test, provider "provider produced an unexpected new value: .tags: was cty.ListValEmpty(cty.String), but now null.
I am trying to handle this scenario, and having some issues.
This is the current logic on Create
, while evaluating what’s in the plan. I only do something with it when there are elements:
if len(plan.Tags.Elements()) > 0 {
diags = plan.Tags.ElementsAs(ctx, &clientPlan.Tags, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
From what I can tell, when tags
is not supplied (or supplied as tags = null
), the result in the plan has state: ValueStateNull (0) = 0x0
:
While when tags = []
is supplied, the result in the plan has state: ValueStateKnown (2) = 0x0
, despite neither having elements:
I’ve attempted recreating plan.Tags
when there are no elements by updating the logic on Create
:
if len(plan.Tags.Elements()) > 0 {
diags = plan.Tags.ElementsAs(ctx, &clientPlan.Tags, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
} else if !plan.Tags.IsNull() {
// normalize back to a null list in case an empty array was passed
plan.Tags = types.ListNull(types.StringType)
}
I can confirm plan.Tags
has state: ValueStateNull (0) = 0x0
after this change, but then the test fails with:
error: Error running apply: exit status 1
What’s more interesting to me is I am unable to catch this in the debugger. I can step through all the way to the end of Create
, but after it exits that function, the code never reaches Read
, it simply exits with that error (no idea why, I blame my lack of experience with Go).
I appreciate any assistance in getting this sorted out. Thanks!