Skip to content

Commit c23b409

Browse files
authored
Merge pull request #598 from labd/597-support-for-product-level-attributes
feat: added product level fields to product type attributes
2 parents 5279f6e + bc992f0 commit c23b409

File tree

7 files changed

+247
-1
lines changed

7 files changed

+247
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Added
2+
body: Added support for product type attribute level field
3+
time: 2025-07-11T13:35:38.453809966+02:00

Taskfile.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
version: '3'
22

33
tasks:
4+
default:
5+
silent: true
6+
cmd: task --list-all
7+
48
build-local:
59
cmds:
610
- go build -o terraform-provider-{{ .NAME }}_{{ .VERSION }}
@@ -22,6 +26,7 @@ tasks:
2226
format:
2327
cmds:
2428
- go fmt ./...
29+
- terraform fmt -diff -recursive ./examples/
2530

2631
test:
2732
cmds:

commercetools/resource_product_type.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
78
"reflect"
89
"slices"
910
"strings"
@@ -84,6 +85,16 @@ func resourceProductType() *schema.Resource {
8485
Type: schema.TypeString,
8586
Required: true,
8687
},
88+
"level": {
89+
Description: "Specifies whether the Attribute is defined at the Product or Variant level.",
90+
Type: schema.TypeString,
91+
Optional: true,
92+
Default: platform.AttributeLevelEnumVariant,
93+
ValidateFunc: validation.StringInSlice([]string{
94+
string(platform.AttributeLevelEnumProduct),
95+
string(platform.AttributeLevelEnumVariant),
96+
}, false),
97+
},
8798
"label": {
8899
Description: "A human-readable label for the attribute",
89100
Type: TypeLocalizedString,
@@ -274,6 +285,7 @@ func flattenProductTypeAttributes(t *platform.ProductType) ([]map[string]any, er
274285
attrs[i] = map[string]any{
275286
"type": attrType,
276287
"name": attrDef.Name,
288+
"level": attrDef.Level,
277289
"label": attrDef.Label,
278290
"required": attrDef.IsRequired,
279291
"input_hint": attrDef.InputHint,
@@ -533,6 +545,7 @@ func resourceProductTypeAttributeChangeActions(oldValues []any, newValues []any)
533545
Type: newAttr.Type,
534546
Name: newAttr.Name,
535547
Label: newAttr.Label,
548+
Level: ref(newAttr.Level),
536549
IsRequired: newAttr.IsRequired,
537550
AttributeConstraint: &newAttr.AttributeConstraint,
538551
InputTip: newAttr.InputTip,
@@ -604,6 +617,10 @@ func resourceProductTypeAttributeChangeActions(oldValues []any, newValues []any)
604617
})
605618
}
606619

620+
if !reflect.DeepEqual(oldAttr.Level, newAttr.Level) {
621+
return nil, fmt.Errorf("changing the level of an attribute is not supported in commercetools. Remove the attribute and re-add it with the new level")
622+
}
623+
607624
// Specific updates for EnumType, LocalizedEnumType and a Set of these
608625
switch t := newAttr.Type.(type) {
609626

@@ -886,11 +903,17 @@ func expandProductTypeAttributeDefinitionItem(input map[string]any, draft bool)
886903
constraint = platform.AttributeConstraintEnumNone
887904
}
888905

906+
lString, ok := input["level"].(string)
907+
if !ok {
908+
lString = string(platform.AttributeLevelEnumVariant)
909+
}
910+
889911
inputHint := platform.TextInputHint(input["input_hint"].(string))
890912
if draft {
891913
return platform.AttributeDefinitionDraft{
892914
Type: attrType,
893915
Name: input["name"].(string),
916+
Level: ref(platform.AttributeLevelEnum(lString)),
894917
Label: label,
895918
AttributeConstraint: &constraint,
896919
IsRequired: input["required"].(bool),
@@ -902,6 +925,7 @@ func expandProductTypeAttributeDefinitionItem(input map[string]any, draft bool)
902925
return platform.AttributeDefinition{
903926
Type: attrType,
904927
Name: input["name"].(string),
928+
Level: platform.AttributeLevelEnum(lString),
905929
Label: label,
906930
AttributeConstraint: constraint,
907931
IsRequired: input["required"].(bool),

0 commit comments

Comments
 (0)