diff --git a/.changes/unreleased/Added-20241202-214322.yaml b/.changes/unreleased/Added-20241202-214322.yaml new file mode 100644 index 00000000..c3b538e1 --- /dev/null +++ b/.changes/unreleased/Added-20241202-214322.yaml @@ -0,0 +1,3 @@ +kind: Added +body: Added the attribute ShippingMethod.active to the resource shipping_method. +time: 2024-12-02T21:43:22.74309+01:00 diff --git a/commercetools/resource_shipping_method.go b/commercetools/resource_shipping_method.go index 042e0145..16baff29 100644 --- a/commercetools/resource_shipping_method.go +++ b/commercetools/resource_shipping_method.go @@ -2,9 +2,10 @@ package commercetools import ( "context" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "time" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/labd/commercetools-go-sdk/platform" @@ -49,6 +50,11 @@ func resourceShippingMethod() *schema.Resource { ValidateDiagFunc: validateLocalizedStringKey, Optional: true, }, + "active": { + Description: "Activate or deactivate a shippinh method. Default is active.", + Type: schema.TypeBool, + Optional: true, + }, "is_default": { Description: "One shipping method in a project can be default", Type: schema.TypeBool, @@ -96,6 +102,7 @@ func resourceShippingMethodCreate(ctx context.Context, d *schema.ResourceData, m Description: stringRef(d.Get("description")), LocalizedDescription: &localizedDescription, LocalizedName: &localizedName, + Active: boolRef(d.Get("active")), IsDefault: d.Get("is_default").(bool), TaxCategory: taxCategory, Predicate: nilIfEmpty(stringRef(d.Get("predicate"))), @@ -144,6 +151,7 @@ func resourceShippingMethodRead(ctx context.Context, d *schema.ResourceData, m a _ = d.Set("description", shippingMethod.Description) _ = d.Set("localized_description", shippingMethod.LocalizedDescription) _ = d.Set("localized_name", shippingMethod.LocalizedName) + _ = d.Set("active", shippingMethod.Active) _ = d.Set("is_default", shippingMethod.IsDefault) _ = d.Set("tax_category_id", shippingMethod.TaxCategory.ID) _ = d.Set("predicate", shippingMethod.Predicate) @@ -206,6 +214,13 @@ func resourceShippingMethodUpdate(ctx context.Context, d *schema.ResourceData, m &platform.ShippingMethodSetLocalizedNameAction{LocalizedName: &newLocalizedName}) } + if d.HasChange("active") { + newActive := d.Get("active").(bool) + input.Actions = append( + input.Actions, + &platform.ShippingMethodChangeActiveAction{Active: newActive}) + } + if d.HasChange("is_default") { newIsDefault := d.Get("is_default").(bool) input.Actions = append( diff --git a/commercetools/resource_shipping_method_test.go b/commercetools/resource_shipping_method_test.go index 9fa94dff..db7b2c75 100644 --- a/commercetools/resource_shipping_method_test.go +++ b/commercetools/resource_shipping_method_test.go @@ -15,6 +15,7 @@ func TestAccShippingMethod_createAndUpdateWithID(t *testing.T) { key := "test-sh-method" description := "test shipping method description" localizedName := "some localized shipping method test name" + active := true predicate := "1 = 1" resourceName := "commercetools_shipping_method.standard" @@ -22,6 +23,7 @@ func TestAccShippingMethod_createAndUpdateWithID(t *testing.T) { newKey := "new-test-sh-method" newDescription := "new test shipping method description" newLocalizedName := "some new localized shipping method test name" + newActive := false newPredicate := "2 = 2" resource.Test(t, resource.TestCase{ @@ -30,25 +32,27 @@ func TestAccShippingMethod_createAndUpdateWithID(t *testing.T) { CheckDestroy: testAccCheckShippingMethodDestroy, Steps: []resource.TestStep{ { - Config: testAccShippingMethodConfig(name, key, description, description, localizedName, false, true, predicate), + Config: testAccShippingMethodConfig(name, key, description, description, localizedName, active, false, true, predicate), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "name", name), resource.TestCheckResourceAttr(resourceName, "key", key), resource.TestCheckResourceAttr(resourceName, "description", description), resource.TestCheckResourceAttr(resourceName, "localized_description.en", description), resource.TestCheckResourceAttr(resourceName, "localized_name.en", localizedName), + resource.TestCheckResourceAttr(resourceName, "active", "true"), resource.TestCheckResourceAttr(resourceName, "is_default", "false"), resource.TestCheckResourceAttr(resourceName, "predicate", predicate), ), }, { - Config: testAccShippingMethodConfig(newName, newKey, newDescription, newDescription, newLocalizedName, true, true, newPredicate), + Config: testAccShippingMethodConfig(newName, newKey, newDescription, newDescription, newLocalizedName, newActive, true, true, newPredicate), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "name", newName), resource.TestCheckResourceAttr(resourceName, "key", newKey), resource.TestCheckResourceAttr(resourceName, "description", newDescription), resource.TestCheckResourceAttr(resourceName, "localized_description.en", newDescription), resource.TestCheckResourceAttr(resourceName, "localized_name.en", newLocalizedName), + resource.TestCheckResourceAttr(resourceName, "active", "false"), resource.TestCheckResourceAttr(resourceName, "is_default", "true"), resource.TestCheckResourceAttrSet(resourceName, "tax_category_id"), resource.TestCheckResourceAttr(resourceName, "predicate", newPredicate), @@ -58,7 +62,7 @@ func TestAccShippingMethod_createAndUpdateWithID(t *testing.T) { }) } -func testAccShippingMethodConfig(name string, key string, description string, localizedDescription string, localizedName string, isDefault bool, setTaxCategory bool, predicate string) string { +func testAccShippingMethodConfig(name string, key string, description string, localizedDescription string, localizedName string, active bool, isDefault bool, setTaxCategory bool, predicate string) string { taxCategoryReference := "" if setTaxCategory { taxCategoryReference = "tax_category_id = commercetools_tax_category.test.id" @@ -80,6 +84,7 @@ func testAccShippingMethodConfig(name string, key string, description string, lo localized_name = { en = "{{ .localizedName }}" } + active = "{{ .active }}" is_default = "{{ .isDefault }}" predicate = "{{ .predicate }}" @@ -93,6 +98,7 @@ func testAccShippingMethodConfig(name string, key string, description string, lo "localizedDescription": localizedDescription, "localizedName": localizedName, "isDefault": isDefault, + "active": active, "predicate": predicate, "taxCategoryReference": taxCategoryReference, })