Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

522 proposal add active flag to shipping method #544

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changes/unreleased/Added-20241202-214322.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Added
gasttor marked this conversation as resolved.
Show resolved Hide resolved
body: Added the attribute ShippingMethod.active to the resource shipping_method.
time: 2024-12-02T21:43:22.74309+01:00
17 changes: 16 additions & 1 deletion commercetools/resource_shipping_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"))),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 9 additions & 3 deletions commercetools/resource_shipping_method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ 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"

newName := "new test sh method"
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{
Expand All @@ -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),
Expand All @@ -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"
Expand All @@ -80,6 +84,7 @@ func testAccShippingMethodConfig(name string, key string, description string, lo
localized_name = {
en = "{{ .localizedName }}"
}
active = "{{ .active }}"
is_default = "{{ .isDefault }}"
predicate = "{{ .predicate }}"

Expand All @@ -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,
})
Expand Down