-
-
Notifications
You must be signed in to change notification settings - Fork 71
Description
🐞 Version information
You can reproduce the issue using either Terraform or OpenTofu
Terraform
- Terraform CLI: v1.11.3 (
darwin_arm64
) - Provider:
registry.terraform.io/labd/commercetools
v1.19.3
OpenTofu
- OpenTofu CLI: v1.9.0 (
darwin_arm64
) - Provider:
registry.opentofu.org/labd/commercetools
v1.19.3
🧩 Describe the Bug
Applying a configuration that provisions multiple commercetools_business_unit_company
resources with optional custom fields results in an error or unexpected behaviour. Specifically, not all custom fields are applied correctly when using conditional logic (? : null
) for optional fields of type Reference
.
🔁 Steps to Reproduce
- Export environment variables
export CTP_CLIENT_ID=...
export CTP_CLIENT_SECRET=...
export CTP_PROJECT_KEY=...
export CTP_SCOPES=...
export CTP_API_URL=https://api.europe-west1.gcp.commercetools.com
export CTP_AUTH_URL=https://auth.europe-west1.gcp.commercetools.com
- Create the following Terraform configuration files
# main.tf
terraform {
required_providers {
commercetools = {
source = "labd/commercetools"
version = "1.19.3"
}
}
}
resource "commercetools_type" "business-unit-custom-fields" {
key = "business-unit-custom-fields"
name = {
en = "Custom Fields"
}
description = {
en = "Business Unit Custom Fields"
}
resource_type_ids = ["business-unit"]
field {
name = "codaAccountNumber"
required = false
label = { en = "My property" }
type = { name = "String" }
}
field {
name = "customerGroup"
required = false
label = { en = "A customer group associated with the business unit" }
type = {
name = "Reference"
reference_type_id = "customer-group"
}
}
}
resource "commercetools_business_unit_company" "business_units" {
for_each = { for index, unit in var.business_units : unit.key => unit }
key = each.key
name = each.value.name
custom {
type_id = commercetools_type.business-unit-custom-fields.id
fields = {
codaAccountNumber = each.value.fields.codaAccountNumber
customerGroup = each.value.fields.customerGroup != null ? jsonencode({
id = each.value.fields.customerGroup
typeId = "customer-group"
}) : null
}
}
}
# variables.tf
variable "business_units" {
type = set(object({
key = string
name = string
fields = optional(object({
codaAccountNumber = optional(string, null)
customerGroup = optional(string)
}), {})
}))
description = "List of business units to create in Commercetools"
}
# variables.tfvars
business_units = [
{
key = "business-a"
name = "business as usual A",
},
{
key = "business-b"
name = "business as usual B",
fields = {
codaAccountNumber = "My value 1"
}
},
{
key = "business-c"
name = "business as usual C",
fields = {
customerGroup = "98d74390-b6ad-4363-83c5-effb6c9e49f7"
}
},
{
key = "business-d"
name = "business as usual D",
fields = {
codaAccountNumber = "My value 2"
customerGroup = "98d74390-b6ad-4363-83c5-effb6c9e49f7"
}
}
]
- Initialize Terraform/OpenTofu
$ terraform init
- Run a plan
$ terraform plan -var-file=variables.tfvars
- Apply the plan:
terraform apply -var-file=variables.tfvars
✅ Expected behaviour
All four business units should be created in Commercetools with the specified key
, name
, and custom fields (if provided). Optional custom fields should be omitted cleanly when null
.
📸 Screenshots



📎 Additional Context
This issue may be related to how the provider handles conditional serialization of Reference types with null values. Please let me know if a workaround exists or if additional logs would help.