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