Skip to content

Commit

Permalink
feat: updated deprecated code, removed unused, made some general fixe…
Browse files Browse the repository at this point in the history
…s to styling
  • Loading branch information
demeyerthom committed Nov 15, 2023
1 parent 13b9050 commit 0e62d5e
Show file tree
Hide file tree
Showing 48 changed files with 434 additions and 511 deletions.
10 changes: 5 additions & 5 deletions commercetools/custom_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,19 @@ func getTypeResource(ctx context.Context, client *platform.ByProjectKeyRequestBu
return nil, nil
}

if type_id, ok := data["type_id"].(string); ok {
if typeId, ok := data["type_id"].(string); ok {
if cacheTypes == nil {
cacheTypes = make(map[string]*platform.Type)
}
if t, exists := cacheTypes[type_id]; exists {
if t, exists := cacheTypes[typeId]; exists {
if t == nil {
return nil, fmt.Errorf("type %s not in cache due to previous error", type_id)
return nil, fmt.Errorf("type %s not in cache due to previous error", typeId)

Check warning on line 250 in commercetools/custom_fields.go

View check run for this annotation

Codecov / codecov/patch

commercetools/custom_fields.go#L250

Added line #L250 was not covered by tests
}
return t, nil
}

t, err := client.Types().WithId(type_id).Get().Execute(ctx)
cacheTypes[type_id] = t
t, err := client.Types().WithId(typeId).Get().Execute(ctx)
cacheTypes[typeId] = t
return t, err
}
return nil, fmt.Errorf("missing type_id for custom fields")
Expand Down
12 changes: 6 additions & 6 deletions commercetools/custom_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestAccCustomField_SetAndRemove(t *testing.T) {

// Remove Custom fields from the resource one by one
for index := range customFieldTypes {
var customFieldTypesReduced = []string{}
var customFieldTypesReduced []string
for i := range customFieldTypes {
if i == index {
continue
Expand Down Expand Up @@ -164,9 +164,9 @@ func TestAccCustomField_SetAndRemove(t *testing.T) {
})

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: customFieldsAccTestSteps,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
Steps: customFieldsAccTestSteps,
})
}
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func getResourceCustomFields(s *terraform.State, resourceType, identifier string
func testGetCategory(s *terraform.State, identifier string) (*platform.Category, error) {
rs, ok := s.RootModule().Resources[identifier]
if !ok {
return nil, fmt.Errorf("Category %s not found", identifier)
return nil, fmt.Errorf("category %s not found", identifier)
}

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

client := getClient(testAccProvider.Meta())
Expand Down
20 changes: 0 additions & 20 deletions commercetools/marshalling.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,6 @@ func expandLocalizedString(val any) platform.LocalizedString {
return result
}

func expandCentPrecisionMoneyDraft(d map[string]any) []platform.CentPrecisionMoneyDraft {
input := d["money"].([]any)
var result []platform.CentPrecisionMoneyDraft
for _, raw := range input {
data := raw.(map[string]any)
item := platform.CentPrecisionMoneyDraft{}
if currencyCode, ok := data["currency_code"].(string); ok {
item.CurrencyCode = currencyCode
}
if centAmount, ok := data["cent_amount"].(int); ok {
item.CentAmount = &centAmount
}
if fractionDigits, ok := data["fraction_digits"].(int); ok {
item.FractionDigits = &fractionDigits
}
result = append(result, item)
}
return result
}

func expandMoneyDraft(d map[string]any) []platform.Money {
input := d["money"].([]any)
var result []platform.Money
Expand Down
8 changes: 4 additions & 4 deletions commercetools/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
)

func init() {
// Set descriptions to support markdown syntax, this will be used in document generation
// Set descriptions to support Markdown syntax, this will be used in document generation
// and the language server.
schema.DescriptionKind = schema.StringMarkdown
}

// Provider returns a terraform.ResourceProvider.
// New returns a new terraform.ResourceProvider.
func New(version string) func() *schema.Provider {
return func() *schema.Provider {
p := &schema.Provider{
Expand Down Expand Up @@ -88,7 +88,7 @@ func New(version string) func() *schema.Provider {
// "commercetools_subscription": resourceSubscription(),
},
}
p.ConfigureContextFunc = providerConfigure(version, p)
p.ConfigureContextFunc = providerConfigure(version)
return p
}
}
Expand All @@ -100,7 +100,7 @@ func getDefault(d *schema.ResourceData, key string, envKey string) string {
return os.Getenv(envKey)
}

func providerConfigure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (any, diag.Diagnostics) {
func providerConfigure(version string) func(context.Context, *schema.ResourceData) (any, diag.Diagnostics) {
return func(ctx context.Context, d *schema.ResourceData) (any, diag.Diagnostics) {
clientID := getDefault(d, "client_id", "CTP_CLIENT_ID")
clientSecret := getDefault(d, "client_secret", "CTP_CLIENT_SECRET")
Expand Down
8 changes: 5 additions & 3 deletions commercetools/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

var testAccProviders map[string]*schema.Provider
var testAccProviders map[string]func() (*schema.Provider, error)
var testAccProvider *schema.Provider

func init() {
testAccProvider = New("snapshot")()
testAccProviders = map[string]*schema.Provider{
"commercetools": testAccProvider,
testAccProviders = map[string]func() (*schema.Provider, error){
"commercetools": func() (*schema.Provider, error) {
return testAccProvider, nil
},
}
}

Expand Down
12 changes: 6 additions & 6 deletions commercetools/resource_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/labd/commercetools-go-sdk/platform"

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

var apiClient *platform.ApiClient

err := resource.RetryContext(ctx, 20*time.Second, func() *resource.RetryError {
err := retry.RetryContext(ctx, 20*time.Second, func() *retry.RetryError {

Check warning on line 70 in commercetools/resource_api_client.go

View check run for this annotation

Codecov / codecov/patch

commercetools/resource_api_client.go#L70

Added line #L70 was not covered by tests
var err error
apiClient, err = client.ApiClients().Post(draft).Execute(ctx)
return utils.ProcessRemoteError(err)
Expand All @@ -78,7 +78,7 @@ func resourceAPIClientCreate(ctx context.Context, d *schema.ResourceData, m any)
}

d.SetId(apiClient.ID)
d.Set("secret", apiClient.Secret)
_ = d.Set("secret", apiClient.Secret)

Check warning on line 81 in commercetools/resource_api_client.go

View check run for this annotation

Codecov / codecov/patch

commercetools/resource_api_client.go#L81

Added line #L81 was not covered by tests

return resourceAPIClientRead(ctx, d, m)
}
Expand All @@ -96,17 +96,17 @@ func resourceAPIClientRead(ctx context.Context, d *schema.ResourceData, m any) d
}

d.SetId(apiClient.ID)
d.Set("name", apiClient.Name)
_ = d.Set("name", apiClient.Name)

Check warning on line 99 in commercetools/resource_api_client.go

View check run for this annotation

Codecov / codecov/patch

commercetools/resource_api_client.go#L99

Added line #L99 was not covered by tests
scopes := strings.Split(apiClient.Scope, " ")
sort.Strings(scopes)
d.Set("scope", scopes)
_ = d.Set("scope", scopes)

Check warning on line 102 in commercetools/resource_api_client.go

View check run for this annotation

Codecov / codecov/patch

commercetools/resource_api_client.go#L102

Added line #L102 was not covered by tests
return nil
}

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

err := resource.RetryContext(ctx, 20*time.Second, func() *resource.RetryError {
err := retry.RetryContext(ctx, 20*time.Second, func() *retry.RetryError {

Check warning on line 109 in commercetools/resource_api_client.go

View check run for this annotation

Codecov / codecov/patch

commercetools/resource_api_client.go#L109

Added line #L109 was not covered by tests
_, err := client.ApiClients().WithId(d.Id()).Delete().Execute(ctx)
return utils.ProcessRemoteError(err)
})
Expand Down
16 changes: 8 additions & 8 deletions commercetools/resource_api_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package commercetools
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/labd/commercetools-go-sdk/platform"

Expand All @@ -17,7 +17,7 @@ import (

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

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

err := resource.RetryContext(ctx, 20*time.Second, func() *resource.RetryError {
err := retry.RetryContext(ctx, 20*time.Second, func() *retry.RetryError {
_, err := client.Extensions().WithId(d.Id()).Post(input).Execute(ctx)
return utils.ProcessRemoteError(err)
})
Expand Down Expand Up @@ -360,10 +360,10 @@ func expandExtensionDestinationAuthentication(destInput map[string]any) (platfor
// commercetools to write it in the state file.
func flattenExtensionDestination(dst platform.Destination, d *schema.ResourceData) []map[string]string {
// Special handling is required here since the destination contains a secret
// value which is returned as a masked value by the commercetools API. This
// means we need to extract the value from the current raw state file.
// However when importing a resource we don't have the value so we need to
// handle that scenario as well.
// value which is returned as a masked value by the commercetools API. This means
// we need to extract the value from the current raw state file. However, when
// importing a resource we don't have the value, so we need to handle that
// scenario as well.
isExisting := true
rawState := d.GetRawState()
if !rawState.IsNull() {
Expand Down
2 changes: 1 addition & 1 deletion commercetools/resource_api_extension_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func resourceAPIExtensionResourceV0() *schema.Resource {
}
}

func migrateAPIExtensionStateV0toV1(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
func migrateAPIExtensionStateV0toV1(_ context.Context, rawState map[string]any, _ any) (map[string]any, error) {

Check warning on line 61 in commercetools/resource_api_extension_migrate.go

View check run for this annotation

Codecov / codecov/patch

commercetools/resource_api_extension_migrate.go#L61

Added line #L61 was not covered by tests
transformToList(rawState, "destination")
return rawState, nil
}
12 changes: 6 additions & 6 deletions commercetools/resource_api_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func TestAccAPIExtension_basic(t *testing.T) {
resourceName := "commercetools_api_extension.ext"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAPIExtensionDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckAPIExtensionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAPIExtensionGCFConfig(identifier, name, timeoutInMs),
Expand Down Expand Up @@ -309,9 +309,9 @@ func TestAccAPIExtension_azure_authentication(t *testing.T) {
resourceName := "commercetools_api_extension.ext"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAPIExtensionDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckAPIExtensionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAPIExtensionAzureFunctionsConfig(identifier, name, timeoutInMs),
Expand Down
8 changes: 4 additions & 4 deletions commercetools/resource_cart_discount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package commercetools
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/labd/commercetools-go-sdk/ctutils"
"github.com/labd/commercetools-go-sdk/platform"
Expand Down Expand Up @@ -324,7 +324,7 @@ func resourceCartDiscountCreate(ctx context.Context, d *schema.ResourceData, m a
}

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

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

err := resource.RetryContext(ctx, 1*time.Minute, func() *resource.RetryError {
err := retry.RetryContext(ctx, 1*time.Minute, func() *retry.RetryError {
_, err := client.CartDiscounts().WithId(d.Id()).Delete().Version(version).Execute(ctx)
return utils.ProcessRemoteError(err)
})
Expand Down
6 changes: 3 additions & 3 deletions commercetools/resource_cart_discount_absolute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func TestAccCartDiscountAbsolute(t *testing.T) {
resourceName := "commercetools_cart_discount.absolute"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCartDiscountDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckCartDiscountDestroy,
Steps: []resource.TestStep{
{
Config: testAccCartDiscountAbsoluteConfig(identifier),
Expand Down
6 changes: 3 additions & 3 deletions commercetools/resource_cart_discount_fixed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func TestAccCartDiscountFixed(t *testing.T) {
resourceName := "commercetools_cart_discount.fixed"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCartDiscountDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckCartDiscountDestroy,
Steps: []resource.TestStep{
{
Config: testAccCartDiscountFixedConfig(identifier),
Expand Down
6 changes: 3 additions & 3 deletions commercetools/resource_cart_discount_giftlineitem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func TestAccCartDiscountGiftLineItem(t *testing.T) {
resourceName := "commercetools_cart_discount.gift_line_item"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCartDiscountDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckCartDiscountDestroy,
Steps: []resource.TestStep{
{
Config: testAccCartDiscountGiftLineItemConfig(identifier),
Expand Down
2 changes: 1 addition & 1 deletion commercetools/resource_cart_discount_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func resourceCartDiscountResourceV0() *schema.Resource {
}
}

func migrateCartDiscountStateV0toV1(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
func migrateCartDiscountStateV0toV1(_ context.Context, rawState map[string]any, _ any) (map[string]any, error) {

Check warning on line 150 in commercetools/resource_cart_discount_migrate.go

View check run for this annotation

Codecov / codecov/patch

commercetools/resource_cart_discount_migrate.go#L150

Added line #L150 was not covered by tests
transformToList(rawState, "target")
return rawState, nil
}
Loading

0 comments on commit 0e62d5e

Please sign in to comment.