Skip to content

Commit 4c1bdeb

Browse files
authored
Merge pull request #544 from gasttor/522-proposal-add-active-flag-to-shipping-method
522 proposal add active flag to shipping method
2 parents 6f72b25 + b4d785e commit 4c1bdeb

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
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 the attribute ShippingMethod.active to the resource shipping_method.
3+
time: 2024-12-02T21:43:22.74309+01:00

commercetools/resource_shipping_method.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package commercetools
22

33
import (
44
"context"
5-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
65
"time"
76

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
8+
89
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
"github.com/labd/commercetools-go-sdk/platform"
@@ -49,6 +50,11 @@ func resourceShippingMethod() *schema.Resource {
4950
ValidateDiagFunc: validateLocalizedStringKey,
5051
Optional: true,
5152
},
53+
"active": {
54+
Description: "Activate or deactivate a shippinh method. Default is active.",
55+
Type: schema.TypeBool,
56+
Optional: true,
57+
},
5258
"is_default": {
5359
Description: "One shipping method in a project can be default",
5460
Type: schema.TypeBool,
@@ -96,6 +102,7 @@ func resourceShippingMethodCreate(ctx context.Context, d *schema.ResourceData, m
96102
Description: stringRef(d.Get("description")),
97103
LocalizedDescription: &localizedDescription,
98104
LocalizedName: &localizedName,
105+
Active: boolRef(d.Get("active")),
99106
IsDefault: d.Get("is_default").(bool),
100107
TaxCategory: taxCategory,
101108
Predicate: nilIfEmpty(stringRef(d.Get("predicate"))),
@@ -144,6 +151,7 @@ func resourceShippingMethodRead(ctx context.Context, d *schema.ResourceData, m a
144151
_ = d.Set("description", shippingMethod.Description)
145152
_ = d.Set("localized_description", shippingMethod.LocalizedDescription)
146153
_ = d.Set("localized_name", shippingMethod.LocalizedName)
154+
_ = d.Set("active", shippingMethod.Active)
147155
_ = d.Set("is_default", shippingMethod.IsDefault)
148156
_ = d.Set("tax_category_id", shippingMethod.TaxCategory.ID)
149157
_ = d.Set("predicate", shippingMethod.Predicate)
@@ -206,6 +214,13 @@ func resourceShippingMethodUpdate(ctx context.Context, d *schema.ResourceData, m
206214
&platform.ShippingMethodSetLocalizedNameAction{LocalizedName: &newLocalizedName})
207215
}
208216

217+
if d.HasChange("active") {
218+
newActive := d.Get("active").(bool)
219+
input.Actions = append(
220+
input.Actions,
221+
&platform.ShippingMethodChangeActiveAction{Active: newActive})
222+
}
223+
209224
if d.HasChange("is_default") {
210225
newIsDefault := d.Get("is_default").(bool)
211226
input.Actions = append(

commercetools/resource_shipping_method_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ func TestAccShippingMethod_createAndUpdateWithID(t *testing.T) {
1515
key := "test-sh-method"
1616
description := "test shipping method description"
1717
localizedName := "some localized shipping method test name"
18+
active := true
1819
predicate := "1 = 1"
1920
resourceName := "commercetools_shipping_method.standard"
2021

2122
newName := "new test sh method"
2223
newKey := "new-test-sh-method"
2324
newDescription := "new test shipping method description"
2425
newLocalizedName := "some new localized shipping method test name"
26+
newActive := false
2527
newPredicate := "2 = 2"
2628

2729
resource.Test(t, resource.TestCase{
@@ -30,25 +32,27 @@ func TestAccShippingMethod_createAndUpdateWithID(t *testing.T) {
3032
CheckDestroy: testAccCheckShippingMethodDestroy,
3133
Steps: []resource.TestStep{
3234
{
33-
Config: testAccShippingMethodConfig(name, key, description, description, localizedName, false, true, predicate),
35+
Config: testAccShippingMethodConfig(name, key, description, description, localizedName, active, false, true, predicate),
3436
Check: resource.ComposeTestCheckFunc(
3537
resource.TestCheckResourceAttr(resourceName, "name", name),
3638
resource.TestCheckResourceAttr(resourceName, "key", key),
3739
resource.TestCheckResourceAttr(resourceName, "description", description),
3840
resource.TestCheckResourceAttr(resourceName, "localized_description.en", description),
3941
resource.TestCheckResourceAttr(resourceName, "localized_name.en", localizedName),
42+
resource.TestCheckResourceAttr(resourceName, "active", "true"),
4043
resource.TestCheckResourceAttr(resourceName, "is_default", "false"),
4144
resource.TestCheckResourceAttr(resourceName, "predicate", predicate),
4245
),
4346
},
4447
{
45-
Config: testAccShippingMethodConfig(newName, newKey, newDescription, newDescription, newLocalizedName, true, true, newPredicate),
48+
Config: testAccShippingMethodConfig(newName, newKey, newDescription, newDescription, newLocalizedName, newActive, true, true, newPredicate),
4649
Check: resource.ComposeTestCheckFunc(
4750
resource.TestCheckResourceAttr(resourceName, "name", newName),
4851
resource.TestCheckResourceAttr(resourceName, "key", newKey),
4952
resource.TestCheckResourceAttr(resourceName, "description", newDescription),
5053
resource.TestCheckResourceAttr(resourceName, "localized_description.en", newDescription),
5154
resource.TestCheckResourceAttr(resourceName, "localized_name.en", newLocalizedName),
55+
resource.TestCheckResourceAttr(resourceName, "active", "false"),
5256
resource.TestCheckResourceAttr(resourceName, "is_default", "true"),
5357
resource.TestCheckResourceAttrSet(resourceName, "tax_category_id"),
5458
resource.TestCheckResourceAttr(resourceName, "predicate", newPredicate),
@@ -58,7 +62,7 @@ func TestAccShippingMethod_createAndUpdateWithID(t *testing.T) {
5862
})
5963
}
6064

61-
func testAccShippingMethodConfig(name string, key string, description string, localizedDescription string, localizedName string, isDefault bool, setTaxCategory bool, predicate string) string {
65+
func testAccShippingMethodConfig(name string, key string, description string, localizedDescription string, localizedName string, active bool, isDefault bool, setTaxCategory bool, predicate string) string {
6266
taxCategoryReference := ""
6367
if setTaxCategory {
6468
taxCategoryReference = "tax_category_id = commercetools_tax_category.test.id"
@@ -80,6 +84,7 @@ func testAccShippingMethodConfig(name string, key string, description string, lo
8084
localized_name = {
8185
en = "{{ .localizedName }}"
8286
}
87+
active = "{{ .active }}"
8388
is_default = "{{ .isDefault }}"
8489
predicate = "{{ .predicate }}"
8590
@@ -93,6 +98,7 @@ func testAccShippingMethodConfig(name string, key string, description string, lo
9398
"localizedDescription": localizedDescription,
9499
"localizedName": localizedName,
95100
"isDefault": isDefault,
101+
"active": active,
96102
"predicate": predicate,
97103
"taxCategoryReference": taxCategoryReference,
98104
})

0 commit comments

Comments
 (0)