Closed oscarhermoso closed 2 months ago
Hey there @oscarhermoso 👋🏻 , thanks for reporting the issue and sorry you're running into trouble here.
Appreciate the detailed example, this bug is actually in the custom type code (FirewallRulesValue).ToObjectValue
(which I believe from looking at your repo, is coming from our code generator terraform-plugin-codegen-framework
).
When creating a new object, it doesn't check if the nested value is null or unknown before attempting to create a known value, by default it gets zero elements, and mistakenly creates a known empty list, rather than an unknown list.
This object creation happens during plan modification automatically, so it's silently modifying the plan to an empty list here: https://github.com/hashicorp/terraform-plugin-framework/blob/91cc7808c4760d079d46dee1e7e36d00b89d10e0/internal/fwserver/attribute_plan_modification.go#L194
I believe the correct implementation should look something like:
func (v FirewallRulesValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) {
var diags diag.Diagnostics
// I'd expect the generated code to look something like
var destinationAddressesVal basetypes.ListValue
var d diag.Diagnostics
switch {
case v.DestinationAddresses.IsUnknown():
destinationAddressesVal = types.ListUnknown(types.StringType)
case v.DestinationAddresses.IsNull():
destinationAddressesVal = types.ListNull(types.StringType)
default:
destinationAddressesVal, d = types.ListValue(types.StringType, v.DestinationAddresses.Elements())
}
diags.Append(d...)
// .. the rest of the ToObjectValue function ..
This bug will need to be fixed in the code generator itself as well so that all the other nested values receive the same treatment, so I'm going to transfer this issue over to that repository.
Thanks @austinvalle :heart:
Module version
Relevant provider source code
Terraform Configuration Files
Debug Output
https://gist.github.com/oscarhermoso/53849e90d66a97258ca2cb72906fe848
Expected Behavior
Terraform should plan succeed, with
binarylane_server_firewall_rules.example["tf-example-k8s-agent-2"].firewall_rules[0].destination_addresses
and similar planned as unknown valuesi.e.
GIVEN a resource schema with a list of objects attribute AND one of the object properties is a list of strings WHEN the list of strings property is planned with unknown length with unknown values THEN the plan output should also have unknown length with unknown values
Actual Behavior
Steps to Reproduce
cd examples/k3s
terraform init
binarylane_server_firewall_rules.example
:BINARYLANE_API_TOKEN=anything TF_ACC=1 tf apply
Additional Context
Failing in https://github.com/oscarhermoso/terraform-provider-binarylane/blob/main/internal/provider/server_firewall_rules_resource.go
After adding logging in a ModifyPlan method, I can see that the raw Config has type
"destination_addresses":tftypes.List[tftypes.String]<unknown>
, whereas the raw Plan has type"destination_addresses":tftypes.List[tftypes.String]
.Logging
``` │ Warning: req.config │ │ with binarylane_server_firewall_rules.example["tf-example-k8s-agent-1"], │ on main.tf line 131, in resource "binarylane_server_firewall_rules" "example": │ 131: resource "binarylane_server_firewall_rules" "example" { │ │ tftypes.Object["firewall_rules":tftypes.List[tftypes.Object["action":tftypes.String, "description":tftypes.String, │ "destination_addresses":tftypes.List[tftypes.String], "destination_ports":tftypes.List[tftypes.String], │ "protocol":tftypes.String, "source_addresses":tftypes.List[tftypes.String]]], │ "server_id":tftypes.Number]<"firewall_rules":tftypes.List[tftypes.Object["action":tftypes.String, │ "description":tftypes.String, "destination_addresses":tftypes.List[tftypes.String], │ "destination_ports":tftypes.List[tftypes.String], "protocol":tftypes.String, │ "source_addresses":tftypes.List[tftypes.String]]]Considering the
terraform-provider-binarylane
priovider does not contain any code convertreq.Raw.Config
toreq.Raw.Plan
- my understanding is that either this is a bug with Terraform, or I need to add some code in aModifyPlan
method or similar to handle unknown values.Either way, would appreciate any assistance with investigating the issue. Thanks in advance.
References