Skip to content

Commit 869df36

Browse files
authored
Merge pull request #455 from labd/454-update-to-latest-commercetools-go-sdk
454 update to latest commercetools go sdk
2 parents 06013ef + 5e75b3e commit 869df36

File tree

55 files changed

+729
-683
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+729
-683
lines changed

commercetools/custom_fields.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,19 @@ func getTypeResource(ctx context.Context, client *platform.ByProjectKeyRequestBu
241241
return nil, nil
242242
}
243243

244-
if type_id, ok := data["type_id"].(string); ok {
244+
if typeId, ok := data["type_id"].(string); ok {
245245
if cacheTypes == nil {
246246
cacheTypes = make(map[string]*platform.Type)
247247
}
248-
if t, exists := cacheTypes[type_id]; exists {
248+
if t, exists := cacheTypes[typeId]; exists {
249249
if t == nil {
250-
return nil, fmt.Errorf("type %s not in cache due to previous error", type_id)
250+
return nil, fmt.Errorf("type %s not in cache due to previous error", typeId)
251251
}
252252
return t, nil
253253
}
254254

255-
t, err := client.Types().WithId(type_id).Get().Execute(ctx)
256-
cacheTypes[type_id] = t
255+
t, err := client.Types().WithId(typeId).Get().Execute(ctx)
256+
cacheTypes[typeId] = t
257257
return t, err
258258
}
259259
return nil, fmt.Errorf("missing type_id for custom fields")

commercetools/custom_fields_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestAccCustomField_SetAndRemove(t *testing.T) {
124124

125125
// Remove Custom fields from the resource one by one
126126
for index := range customFieldTypes {
127-
var customFieldTypesReduced = []string{}
127+
var customFieldTypesReduced []string
128128
for i := range customFieldTypes {
129129
if i == index {
130130
continue
@@ -164,9 +164,9 @@ func TestAccCustomField_SetAndRemove(t *testing.T) {
164164
})
165165

166166
resource.Test(t, resource.TestCase{
167-
PreCheck: func() { testAccPreCheck(t) },
168-
Providers: testAccProviders,
169-
Steps: customFieldsAccTestSteps,
167+
PreCheck: func() { testAccPreCheck(t) },
168+
ProviderFactories: testAccProviders,
169+
Steps: customFieldsAccTestSteps,
170170
})
171171
}
172172
}
@@ -225,7 +225,7 @@ func getResourceCustomFields(s *terraform.State, resourceType, identifier string
225225
func testGetCategory(s *terraform.State, identifier string) (*platform.Category, error) {
226226
rs, ok := s.RootModule().Resources[identifier]
227227
if !ok {
228-
return nil, fmt.Errorf("Category %s not found", identifier)
228+
return nil, fmt.Errorf("category %s not found", identifier)
229229
}
230230

231231
client := getClient(testAccProvider.Meta())
@@ -239,7 +239,7 @@ func testGetCategory(s *terraform.State, identifier string) (*platform.Category,
239239
func testGetShippingMethod(s *terraform.State, identifier string) (*platform.ShippingMethod, error) {
240240
rs, ok := s.RootModule().Resources[identifier]
241241
if !ok {
242-
return nil, fmt.Errorf("Shipping Method %s not found", identifier)
242+
return nil, fmt.Errorf("shipping Method %s not found", identifier)
243243
}
244244

245245
client := getClient(testAccProvider.Meta())

commercetools/marshalling.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ func expandTypedMoney(d map[string]any) []platform.Money {
5656
return result
5757
}
5858

59+
func expandTypedMoneyDraft(d map[string]any) []platform.TypedMoneyDraft {
60+
input := d["money"].([]any)
61+
var result []platform.TypedMoneyDraft
62+
63+
for _, raw := range input {
64+
i := raw.(map[string]any)
65+
priceCurrencyCode := i["currency_code"].(string)
66+
67+
result = append(result, platform.Money{
68+
CurrencyCode: priceCurrencyCode,
69+
CentAmount: i["cent_amount"].(int),
70+
})
71+
}
72+
73+
return result
74+
}
75+
5976
func expandLocalizedString(val any) platform.LocalizedString {
6077
values, ok := val.(map[string]any)
6178
if !ok {
@@ -69,26 +86,6 @@ func expandLocalizedString(val any) platform.LocalizedString {
6986
return result
7087
}
7188

72-
func expandCentPrecisionMoneyDraft(d map[string]any) []platform.CentPrecisionMoneyDraft {
73-
input := d["money"].([]any)
74-
var result []platform.CentPrecisionMoneyDraft
75-
for _, raw := range input {
76-
data := raw.(map[string]any)
77-
item := platform.CentPrecisionMoneyDraft{}
78-
if currencyCode, ok := data["currency_code"].(string); ok {
79-
item.CurrencyCode = currencyCode
80-
}
81-
if centAmount, ok := data["cent_amount"].(int); ok {
82-
item.CentAmount = centAmount
83-
}
84-
if fractionDigits, ok := data["fraction_digits"].(int); ok {
85-
item.FractionDigits = &fractionDigits
86-
}
87-
result = append(result, item)
88-
}
89-
return result
90-
}
91-
9289
func expandMoneyDraft(d map[string]any) []platform.Money {
9390
input := d["money"].([]any)
9491
var result []platform.Money

commercetools/provider.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import (
1818
)
1919

2020
func init() {
21-
// Set descriptions to support markdown syntax, this will be used in document generation
21+
// Set descriptions to support Markdown syntax, this will be used in document generation
2222
// and the language server.
2323
schema.DescriptionKind = schema.StringMarkdown
2424
}
2525

26-
// Provider returns a terraform.ResourceProvider.
26+
// New returns a new terraform.ResourceProvider.
2727
func New(version string) func() *schema.Provider {
2828
return func() *schema.Provider {
2929
p := &schema.Provider{
@@ -88,7 +88,7 @@ func New(version string) func() *schema.Provider {
8888
// "commercetools_subscription": resourceSubscription(),
8989
},
9090
}
91-
p.ConfigureContextFunc = providerConfigure(version, p)
91+
p.ConfigureContextFunc = providerConfigure(version)
9292
return p
9393
}
9494
}
@@ -100,7 +100,7 @@ func getDefault(d *schema.ResourceData, key string, envKey string) string {
100100
return os.Getenv(envKey)
101101
}
102102

103-
func providerConfigure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (any, diag.Diagnostics) {
103+
func providerConfigure(version string) func(context.Context, *schema.ResourceData) (any, diag.Diagnostics) {
104104
return func(ctx context.Context, d *schema.ResourceData) (any, diag.Diagnostics) {
105105
clientID := getDefault(d, "client_id", "CTP_CLIENT_ID")
106106
clientSecret := getDefault(d, "client_secret", "CTP_CLIENT_SECRET")

commercetools/provider_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1010
)
1111

12-
var testAccProviders map[string]*schema.Provider
12+
var testAccProviders map[string]func() (*schema.Provider, error)
1313
var testAccProvider *schema.Provider
1414

1515
func init() {
1616
testAccProvider = New("snapshot")()
17-
testAccProviders = map[string]*schema.Provider{
18-
"commercetools": testAccProvider,
17+
testAccProviders = map[string]func() (*schema.Provider, error){
18+
"commercetools": func() (*schema.Provider, error) {
19+
return testAccProvider, nil
20+
},
1921
}
2022
}
2123

commercetools/resource_api_client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88

99
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
"github.com/labd/commercetools-go-sdk/platform"
1313

@@ -67,7 +67,7 @@ func resourceAPIClientCreate(ctx context.Context, d *schema.ResourceData, m any)
6767

6868
var apiClient *platform.ApiClient
6969

70-
err := resource.RetryContext(ctx, 20*time.Second, func() *resource.RetryError {
70+
err := retry.RetryContext(ctx, 20*time.Second, func() *retry.RetryError {
7171
var err error
7272
apiClient, err = client.ApiClients().Post(draft).Execute(ctx)
7373
return utils.ProcessRemoteError(err)
@@ -78,7 +78,7 @@ func resourceAPIClientCreate(ctx context.Context, d *schema.ResourceData, m any)
7878
}
7979

8080
d.SetId(apiClient.ID)
81-
d.Set("secret", apiClient.Secret)
81+
_ = d.Set("secret", apiClient.Secret)
8282

8383
return resourceAPIClientRead(ctx, d, m)
8484
}
@@ -96,17 +96,17 @@ func resourceAPIClientRead(ctx context.Context, d *schema.ResourceData, m any) d
9696
}
9797

9898
d.SetId(apiClient.ID)
99-
d.Set("name", apiClient.Name)
99+
_ = d.Set("name", apiClient.Name)
100100
scopes := strings.Split(apiClient.Scope, " ")
101101
sort.Strings(scopes)
102-
d.Set("scope", scopes)
102+
_ = d.Set("scope", scopes)
103103
return nil
104104
}
105105

106106
func resourceAPIClientDelete(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
107107
client := getClient(m)
108108

109-
err := resource.RetryContext(ctx, 20*time.Second, func() *resource.RetryError {
109+
err := retry.RetryContext(ctx, 20*time.Second, func() *retry.RetryError {
110110
_, err := client.ApiClients().WithId(d.Id()).Delete().Execute(ctx)
111111
return utils.ProcessRemoteError(err)
112112
})

commercetools/resource_api_extension.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package commercetools
33
import (
44
"context"
55
"fmt"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
67
"log"
78
"strings"
89
"time"
910

1011
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313
"github.com/labd/commercetools-go-sdk/platform"
1414

@@ -17,7 +17,7 @@ import (
1717

1818
func resourceAPIExtension() *schema.Resource {
1919
return &schema.Resource{
20-
Description: "Create a new API extension to extend the bevahiour of an API with business logic. " +
20+
Description: "Create a new API extension to extend the behaviour of an API with business logic. " +
2121
"Note that API extensions affect the performance of the API it is extending. If it fails, the whole API " +
2222
"call fails \n\n" +
2323
"Also see the [API Extension API Documentation](https://docs.commercetools.com/api/projects/api-extensions)",
@@ -184,7 +184,7 @@ func resourceAPIExtensionCreate(ctx context.Context, d *schema.ResourceData, m a
184184
}
185185

186186
var extension *platform.Extension
187-
err = resource.RetryContext(ctx, 20*time.Second, func() *resource.RetryError {
187+
err = retry.RetryContext(ctx, 20*time.Second, func() *retry.RetryError {
188188
var err error
189189
extension, err = client.Extensions().Post(draft).Execute(ctx)
190190
return utils.ProcessRemoteError(err)
@@ -266,7 +266,7 @@ func resourceAPIExtensionUpdate(ctx context.Context, d *schema.ResourceData, m a
266266
&platform.ExtensionSetTimeoutInMsAction{TimeoutInMs: &newTimeout})
267267
}
268268

269-
err := resource.RetryContext(ctx, 20*time.Second, func() *resource.RetryError {
269+
err := retry.RetryContext(ctx, 20*time.Second, func() *retry.RetryError {
270270
_, err := client.Extensions().WithId(d.Id()).Post(input).Execute(ctx)
271271
return utils.ProcessRemoteError(err)
272272
})
@@ -360,10 +360,10 @@ func expandExtensionDestinationAuthentication(destInput map[string]any) (platfor
360360
// commercetools to write it in the state file.
361361
func flattenExtensionDestination(dst platform.Destination, d *schema.ResourceData) []map[string]string {
362362
// Special handling is required here since the destination contains a secret
363-
// value which is returned as a masked value by the commercetools API. This
364-
// means we need to extract the value from the current raw state file.
365-
// However when importing a resource we don't have the value so we need to
366-
// handle that scenario as well.
363+
// value which is returned as a masked value by the commercetools API. This means
364+
// we need to extract the value from the current raw state file. However, when
365+
// importing a resource we don't have the value, so we need to handle that
366+
// scenario as well.
367367
isExisting := true
368368
rawState := d.GetRawState()
369369
if !rawState.IsNull() {

commercetools/resource_api_extension_migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func resourceAPIExtensionResourceV0() *schema.Resource {
5858
}
5959
}
6060

61-
func migrateAPIExtensionStateV0toV1(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
61+
func migrateAPIExtensionStateV0toV1(_ context.Context, rawState map[string]any, _ any) (map[string]any, error) {
6262
transformToList(rawState, "destination")
6363
return rawState, nil
6464
}

commercetools/resource_api_extension_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ func TestAccAPIExtension_basic(t *testing.T) {
100100
resourceName := "commercetools_api_extension.ext"
101101

102102
resource.Test(t, resource.TestCase{
103-
PreCheck: func() { testAccPreCheck(t) },
104-
Providers: testAccProviders,
105-
CheckDestroy: testAccCheckAPIExtensionDestroy,
103+
PreCheck: func() { testAccPreCheck(t) },
104+
ProviderFactories: testAccProviders,
105+
CheckDestroy: testAccCheckAPIExtensionDestroy,
106106
Steps: []resource.TestStep{
107107
{
108108
Config: testAccAPIExtensionGCFConfig(identifier, name, timeoutInMs),
@@ -309,9 +309,9 @@ func TestAccAPIExtension_azure_authentication(t *testing.T) {
309309
resourceName := "commercetools_api_extension.ext"
310310

311311
resource.Test(t, resource.TestCase{
312-
PreCheck: func() { testAccPreCheck(t) },
313-
Providers: testAccProviders,
314-
CheckDestroy: testAccCheckAPIExtensionDestroy,
312+
PreCheck: func() { testAccPreCheck(t) },
313+
ProviderFactories: testAccProviders,
314+
CheckDestroy: testAccCheckAPIExtensionDestroy,
315315
Steps: []resource.TestStep{
316316
{
317317
Config: testAccAPIExtensionAzureFunctionsConfig(identifier, name, timeoutInMs),

commercetools/resource_cart_discount.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package commercetools
33
import (
44
"context"
55
"fmt"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
67
"time"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1111
"github.com/labd/commercetools-go-sdk/ctutils"
1212
"github.com/labd/commercetools-go-sdk/platform"
@@ -324,7 +324,7 @@ func resourceCartDiscountCreate(ctx context.Context, d *schema.ResourceData, m a
324324
}
325325

326326
var cartDiscount *platform.CartDiscount
327-
err = resource.RetryContext(ctx, 1*time.Minute, func() *resource.RetryError {
327+
err = retry.RetryContext(ctx, 1*time.Minute, func() *retry.RetryError {
328328
var err error
329329
cartDiscount, err = client.CartDiscounts().Post(draft).Execute(ctx)
330330
return utils.ProcessRemoteError(err)
@@ -506,7 +506,7 @@ func resourceCartDiscountUpdate(ctx context.Context, d *schema.ResourceData, m a
506506
}
507507
}
508508

509-
err := resource.RetryContext(ctx, 1*time.Minute, func() *resource.RetryError {
509+
err := retry.RetryContext(ctx, 1*time.Minute, func() *retry.RetryError {
510510
_, err := client.CartDiscounts().WithId(d.Id()).Post(input).Execute(ctx)
511511
return utils.ProcessRemoteError(err)
512512
})
@@ -525,7 +525,7 @@ func resourceCartDiscountDelete(ctx context.Context, d *schema.ResourceData, m a
525525
client := getClient(m)
526526
version := d.Get("version").(int)
527527

528-
err := resource.RetryContext(ctx, 1*time.Minute, func() *resource.RetryError {
528+
err := retry.RetryContext(ctx, 1*time.Minute, func() *retry.RetryError {
529529
_, err := client.CartDiscounts().WithId(d.Id()).Delete().Version(version).Execute(ctx)
530530
return utils.ProcessRemoteError(err)
531531
})
@@ -599,7 +599,7 @@ func expandCartDiscountValue(d *schema.ResourceData) (platform.CartDiscountValue
599599
Money: money,
600600
}, nil
601601
case "fixed":
602-
money := expandTypedMoney(value)
602+
money := expandTypedMoneyDraft(value)
603603
return platform.CartDiscountValueFixedDraft{
604604
Money: money,
605605
}, nil

0 commit comments

Comments
 (0)