4
4
"context"
5
5
"fmt"
6
6
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
7
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
7
8
"reflect"
8
9
"slices"
9
10
"strings"
@@ -84,6 +85,16 @@ func resourceProductType() *schema.Resource {
84
85
Type : schema .TypeString ,
85
86
Required : true ,
86
87
},
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
+ },
87
98
"label" : {
88
99
Description : "A human-readable label for the attribute" ,
89
100
Type : TypeLocalizedString ,
@@ -274,6 +285,7 @@ func flattenProductTypeAttributes(t *platform.ProductType) ([]map[string]any, er
274
285
attrs [i ] = map [string ]any {
275
286
"type" : attrType ,
276
287
"name" : attrDef .Name ,
288
+ "level" : attrDef .Level ,
277
289
"label" : attrDef .Label ,
278
290
"required" : attrDef .IsRequired ,
279
291
"input_hint" : attrDef .InputHint ,
@@ -533,6 +545,7 @@ func resourceProductTypeAttributeChangeActions(oldValues []any, newValues []any)
533
545
Type : newAttr .Type ,
534
546
Name : newAttr .Name ,
535
547
Label : newAttr .Label ,
548
+ Level : ref (newAttr .Level ),
536
549
IsRequired : newAttr .IsRequired ,
537
550
AttributeConstraint : & newAttr .AttributeConstraint ,
538
551
InputTip : newAttr .InputTip ,
@@ -604,6 +617,10 @@ func resourceProductTypeAttributeChangeActions(oldValues []any, newValues []any)
604
617
})
605
618
}
606
619
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
+
607
624
// Specific updates for EnumType, LocalizedEnumType and a Set of these
608
625
switch t := newAttr .Type .(type ) {
609
626
@@ -886,11 +903,17 @@ func expandProductTypeAttributeDefinitionItem(input map[string]any, draft bool)
886
903
constraint = platform .AttributeConstraintEnumNone
887
904
}
888
905
906
+ lString , ok := input ["level" ].(string )
907
+ if ! ok {
908
+ lString = string (platform .AttributeLevelEnumVariant )
909
+ }
910
+
889
911
inputHint := platform .TextInputHint (input ["input_hint" ].(string ))
890
912
if draft {
891
913
return platform.AttributeDefinitionDraft {
892
914
Type : attrType ,
893
915
Name : input ["name" ].(string ),
916
+ Level : ref (platform .AttributeLevelEnum (lString )),
894
917
Label : label ,
895
918
AttributeConstraint : & constraint ,
896
919
IsRequired : input ["required" ].(bool ),
@@ -902,6 +925,7 @@ func expandProductTypeAttributeDefinitionItem(input map[string]any, draft bool)
902
925
return platform.AttributeDefinition {
903
926
Type : attrType ,
904
927
Name : input ["name" ].(string ),
928
+ Level : platform .AttributeLevelEnum (lString ),
905
929
Label : label ,
906
930
AttributeConstraint : constraint ,
907
931
IsRequired : input ["required" ].(bool ),
0 commit comments