Skip to content

Commit

Permalink
Fix distribution channel store update edge cases + bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidweterings committed Jul 21, 2020
1 parent b8344d6 commit 14381b8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.22.1 (2020-07-21)
===================
- Fix edge case where distribution channels were not updated

0.22.0 (2020-07-20)
===================
- Update commercetools-go-sdk dependency to v0.2.0. This version now properly
Expand Down
21 changes: 18 additions & 3 deletions commercetools/resource_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commercetools
import (
"context"
"errors"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -103,12 +104,15 @@ func resourceStoreRead(d *schema.ResourceData, m interface{}) error {
if store.Languages != nil {
d.Set("languages", store.Languages)
}

log.Printf("[DEBUG] Store read, distributionChannels: %+v", store.DistributionChannels)
if store.DistributionChannels != nil {
channelKeys, err := flattenDistributionChannels(store.DistributionChannels)
if err != nil {
return err
}
d.Set("distributionChannels", channelKeys)
log.Printf("[DEBUG] Setting channel keys to: %+v", channelKeys)
d.Set("distribution_channels", channelKeys)
}
return nil
}
Expand Down Expand Up @@ -138,9 +142,11 @@ func resourceStoreUpdate(d *schema.ResourceData, m interface{}) error {
&commercetools.StoreSetLanguagesAction{Languages: languages})
}

if d.HasChange("distributionChannels") {
if d.HasChange("distribution_channels") {
dcIdentifiers := expandDistributionChannels(d.Get("distribution_channels"))

log.Printf("[DEBUG] distributionChannels change, new identifiers: %v", dcIdentifiers)

// set action replaces current values
input.Actions = append(
input.Actions,
Expand Down Expand Up @@ -177,21 +183,30 @@ func convertChannelKeysToIdentifiers(channelKeys []string) []commercetools.Chann
}
identifiers = append(identifiers, channelIdentifier)
}

log.Printf("[DEBUG] Converted keys: %v", identifiers)
return identifiers
}

func expandDistributionChannels(distributionChannelData interface{}) []commercetools.ChannelResourceIdentifier {
log.Printf("[DEBUG] Expanding distribution channels: %v", distributionChannelData)
distributionChannelKeys := expandStringArray(distributionChannelData.([]interface{}))
log.Printf("[DEBUG] Expanding distribution channels, got keys: %v", distributionChannelKeys)
return convertChannelKeysToIdentifiers(distributionChannelKeys)
}

func flattenDistributionChannels(distributionChannels []commercetools.ChannelReference) ([]string, error) {
log.Printf("[DEBUG] flattening: %+v", distributionChannels)
channelKeys := make([]string, 0)
for i := 0; i < len(channelKeys); i++ {
for i := 0; i < len(distributionChannels); i++ {

log.Printf("[DEBUG] flattening checking channel: %s", stringFormatObject(distributionChannels[i]))
log.Printf("[DEBUG] flattening checking channel obj: %s", stringFormatObject(distributionChannels[i].Obj))
if distributionChannels[i].Obj == nil {
return nil, errors.New("failed to expand channel objects")
}
channelKeys = append(channelKeys, distributionChannels[i].Obj.Key)
}
log.Printf("[DEBUG] flattening final keys: %v", channelKeys)
return channelKeys, nil
}
36 changes: 36 additions & 0 deletions commercetools/resource_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ func TestAccStore_createAndUpdateDistributionLanguages(t *testing.T) {
),
),
},
{
Config: testAccNewStoreConfigWithoutChannels(name, key, languages),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"commercetools_store.test", "distribution_channels.#", "0",
),
),
},
{
Config: testAccNewStoreConfigWithChannels(name, key, languages),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"commercetools_store.test", "distribution_channels.#", "1",
),
resource.TestCheckResourceAttr(
"commercetools_store.test", "distribution_channels.0", "TEST",
),
),
},
},
})
}
Expand Down Expand Up @@ -145,7 +164,24 @@ func testAccNewStoreConfigWithChannels(name string, key string, languages []stri
languages = %[3]q
distribution_channels = [commercetools_channel.test_channel.key]
}
`, name, key, languages)
}

func testAccNewStoreConfigWithoutChannels(name string, key string, languages []string) string {
return fmt.Sprintf(`
resource "commercetools_channel" "test_channel" {
key = "TEST"
roles = ["ProductDistribution"]
}
resource "commercetools_store" "test" {
name = {
en = "%[1]s"
nl = "%[1]s"
}
key = "%[2]s"
languages = %[3]q
}
`, name, key, languages)
}

Expand Down

0 comments on commit 14381b8

Please sign in to comment.