Skip to content

Commit 03b4395

Browse files
authored
Merge pull request #588 from labd/585-too-many-update-actions---should-batch-requests
fix: added chunking options for product_type resource update
2 parents 72981bc + c7d0a9e commit 03b4395

File tree

3 files changed

+71
-13
lines changed

3 files changed

+71
-13
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixed
2+
body: Added chunking option for product type updates
3+
time: 2025-05-09T15:46:16.943248527+02:00

commercetools/resource_product_type.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
77
"reflect"
8+
"slices"
89
"strings"
910
"time"
1011

@@ -367,29 +368,26 @@ func flattenProductTypeAttributeType(attrType platform.AttributeType, setsAllowe
367368
func resourceProductTypeUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
368369
client := getClient(m)
369370

370-
input := platform.ProductTypeUpdate{
371-
Version: d.Get("version").(int),
372-
Actions: []platform.ProductTypeUpdateAction{},
373-
}
371+
var actions []platform.ProductTypeUpdateAction
374372

375373
if d.HasChange("key") {
376374
newKey := d.Get("key").(string)
377-
input.Actions = append(
378-
input.Actions,
375+
actions = append(
376+
actions,
379377
&platform.ProductTypeSetKeyAction{Key: &newKey})
380378
}
381379

382380
if d.HasChange("name") {
383381
newName := d.Get("name").(string)
384-
input.Actions = append(
385-
input.Actions,
382+
actions = append(
383+
actions,
386384
&platform.ProductTypeChangeNameAction{Name: newName})
387385
}
388386

389387
if d.HasChange("description") {
390388
newDescription := d.Get("description").(string)
391-
input.Actions = append(
392-
input.Actions,
389+
actions = append(
390+
actions,
393391
&platform.ProductTypeChangeDescriptionAction{Description: newDescription})
394392
}
395393

@@ -400,13 +398,24 @@ func resourceProductTypeUpdate(ctx context.Context, d *schema.ResourceData, m an
400398
if err != nil {
401399
return diag.FromErr(err)
402400
}
403-
input.Actions = append(input.Actions, attrChangeActions...)
401+
actions = append(actions, attrChangeActions...)
404402
}
405403

406404
err := retry.RetryContext(ctx, 20*time.Second, func() *retry.RetryError {
407-
_, err := client.ProductTypes().WithId(d.Id()).Post(input).Execute(ctx)
408-
return utils.ProcessRemoteError(err)
405+
var version = d.Get("version").(int)
406+
for chunkedActions := range slices.Chunk(actions, 500) {
407+
response, err := client.ProductTypes().WithId(d.Id()).Post(
408+
platform.ProductTypeUpdate{Version: version, Actions: chunkedActions},
409+
).Execute(ctx)
410+
if err != nil {
411+
return utils.ProcessRemoteError(err)
412+
}
413+
version = response.Version
414+
}
415+
416+
return nil
409417
})
418+
410419
if err != nil {
411420
// Workaround invalid state to be written, see
412421
// https://github.com/hashicorp/terraform-plugin-sdk/issues/476

commercetools/resource_product_type_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,52 @@ func TestAccProductTypes_EnumValues(t *testing.T) {
722722
})
723723
}
724724

725+
func TestAccProductTypes_sliced(t *testing.T) {
726+
t.Skip("Skipping test for large number of attributes")
727+
728+
key := "acctest-producttype"
729+
identifier := "acctest_producttype"
730+
resourceName := "commercetools_product_type.acctest_producttype"
731+
732+
var attributes []TestProductTypeAttrData
733+
for i := 0; i < 1000; i++ {
734+
attributes = append(attributes, TestProductTypeAttrData{
735+
Name: fmt.Sprintf("%d", i),
736+
Type: "text",
737+
})
738+
}
739+
740+
resource.Test(t, resource.TestCase{
741+
PreCheck: func() { testAccPreCheck(t) },
742+
ProviderFactories: testAccProviders,
743+
CheckDestroy: testAccCheckProductTypesDestroy,
744+
Steps: []resource.TestStep{
745+
{
746+
Config: testAccConfigAttributes(key, identifier, []TestProductTypeAttrData{}),
747+
Check: func(s *terraform.State) error {
748+
r, err := testGetProductType(s, resourceName)
749+
if err != nil {
750+
return err
751+
}
752+
assert.EqualValues(t, *r.Key, key)
753+
return nil
754+
},
755+
},
756+
{
757+
Config: testAccConfigAttributes(key, identifier, attributes),
758+
Check: func(s *terraform.State) error {
759+
r, err := testGetProductType(s, resourceName)
760+
if err != nil {
761+
return err
762+
}
763+
assert.EqualValues(t, *r.Key, key)
764+
return nil
765+
},
766+
},
767+
},
768+
})
769+
}
770+
725771
func testAccProductTypeConfigLabelChange(identifier, key string) string {
726772
return hclTemplate(`
727773
resource "commercetools_product_type" "{{ .identifier }}" {

0 commit comments

Comments
 (0)