I am currently writing a custom provider using the latest Terraform-Plugin-Framework
I am experiencing an issue where setting a top level nested attribute as optional is ignoring all computed fields that are nested.
Example:
"relationships": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"organization": schema.ObjectAttribute{
Computed: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
},
AttributeTypes: map[string]attr.Type{
"data": types.ObjectType{
AttrTypes: map[string]attr.Type{
"id": types.StringType,
"type": types.StringType,
},
},
"links": types.ObjectType{
AttrTypes: map[string]attr.Type{
"self": types.StringType,
},
},
},
},
"workflowrule": schema.SingleNestedAttribute{
Optional: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.RequiresReplace(),
},
Attributes: map[string]schema.Attribute{
"data": schema.ObjectAttribute{
Optional: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.RequiresReplace(),
},
AttributeTypes: map[string]attr.Type{
"id": types.StringType,
"type": types.StringType,
},
},
"links": schema.ObjectAttribute{
Computed: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
},
AttributeTypes: map[string]attr.Type{
"self": types.StringType,
},
},
},
},
},
},
Failing Terraform:
resource "help_me" "help" {
data = {
attributes = {
address = "test"
name = "test"
type = "test"
}
}
}
Working Terraform:
resource "help_me2" "help2" {
data = {
attributes = {
address = "test"
name = "test"
type = "test"
}
relationships = {
workflowrule = {
data = {
id = "422"
type = "workflowRules"
}
}
}
}
}
Error I get when running that first Terraform:
Error: Provider produced inconsistent result after apply
│
│ When applying changes to help_me.help, provider
│ "provider[\"super/secret"]" produced an unexpected new
│ value: .data.relationships: was null, but now
│ cty.ObjectVal(map[string]cty.Value{"organization":cty.ObjectVal(map[string]cty.Value{"data":cty.ObjectVal(map[string]cty.Value{"id":cty.StringVal("1"),
│ "type":cty.StringVal("organizations")}),
│ "links":cty.ObjectVal(map[string]cty.Value{"self":cty.StringVal("alsosupersecret")})}),
│ "workflowrule":cty.ObjectVal(map[string]cty.Value{"data":cty.ObjectVal(map[string]cty.Value{"id":cty.StringVal(""),
│ "type":cty.StringVal("")}),
│ "links":cty.ObjectVal(map[string]cty.Value{"self":cty.StringVal("")})})}).
│
│ This is a bug in the provider, which should be reported in the provider's
│ own issue tracker.
If I set Computed to true at the Relationships level both of these code blocks work. HOWEVER, I am in a situation where if someone changed Workflowrule on the provider side I need to be able to track that drift. So if workflowrule changes on the provider side and I run “terraform plan” or “terraform apply” it will show “no changes needed” for the first terraform block due to Computed not checking for changing values at run time.
Let me know if I’m approaching this wrong or what I could do to make this work.