Skip to content

Commit

Permalink
Merge pull request #46 from zkauker/fix-product-proxies-order-sensiti…
Browse files Browse the repository at this point in the history
…vity

Fix product proxies order sensitivity on API product resource
  • Loading branch information
zambien authored Nov 14, 2019
2 parents fcf6e33 + 7e9b2b1 commit b78f198
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
12 changes: 10 additions & 2 deletions apigee/helpers.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package apigee

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/zambien/go-apigee-edge"
"reflect"
"sort"

"github.com/hashicorp/terraform/helper/schema"
"github.com/zambien/go-apigee-edge"
)

func flattenStringList(list []string) []interface{} {
Expand Down Expand Up @@ -75,3 +76,10 @@ func arraySortedEqual(a, b []string) bool {

return reflect.DeepEqual(a_copy, b_copy)
}

func updateResourceOnSortedArrayChange(d *schema.ResourceData, key string, newValues []string) {
currentValues := getStringList(key, d)
if currentValues != nil && !arraySortedEqual(currentValues, newValues) {
d.Set(key, newValues)
}
}
14 changes: 5 additions & 9 deletions apigee/resource_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,6 @@ func resourceProductRead(d *schema.ResourceData, meta interface{}) error {
}
}

apiResources := flattenStringList(ProductData.ApiResources)
proxies := flattenStringList(ProductData.Proxies)
scopes := flattenStringList(ProductData.Scopes)
environments := flattenStringList(ProductData.Environments)

d.Set("name", ProductData.Name)

if ProductData.DisplayName == "" {
Expand All @@ -174,13 +169,14 @@ func resourceProductRead(d *schema.ResourceData, meta interface{}) error {
d.Set("description", ProductData.Description)
d.Set("approval_type", ProductData.ApprovalType)
d.Set("attributes", ProductData.Attributes)
d.Set("apiResource", apiResources)
d.Set("proxies", proxies)
d.Set("quota", ProductData.Quota)
d.Set("quota_interval", ProductData.QuotaInterval)
d.Set("quota_time_unit", ProductData.QuotaTimeUnit)
d.Set("environments", environments)
d.Set("scopes", scopes)

updateResourceOnSortedArrayChange(d, "apiResource", ProductData.ApiResources)
updateResourceOnSortedArrayChange(d, "proxies", ProductData.Proxies)
updateResourceOnSortedArrayChange(d, "environments", ProductData.Environments)
updateResourceOnSortedArrayChange(d, "scopes", ProductData.Scopes)

return nil
}
Expand Down
21 changes: 17 additions & 4 deletions apigee/resource_product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package apigee

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/zambien/go-apigee-edge"
"log"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/zambien/go-apigee-edge"
)

func TestAccProduct_Updated(t *testing.T) {
Expand Down Expand Up @@ -108,14 +109,26 @@ resource "apigee_api_proxy" "tf_helloworld" {
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy.zip")}"
}
resource "apigee_api_proxy" "tf_helloworld_2" {
name = "tf_helloworld_2"
bundle = "test-fixtures/helloworld_proxy.zip"
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy.zip")}"
}
resource "apigee_api_proxy" "tf_helloworld_3" {
name = "tf_helloworld_3"
bundle = "test-fixtures/helloworld_proxy.zip"
bundle_sha = "${filebase64sha256("test-fixtures/helloworld_proxy.zip")}"
}
resource "apigee_product" "foo_product" {
name = "foo_product_updated"
display_name = "foo_product_updated_different"
description = "no one ever fills this out"
approval_type = "auto"
api_resources = ["/**"]
proxies = ["${apigee_api_proxy.tf_helloworld.name}"]
proxies = ["${apigee_api_proxy.tf_helloworld.name}", "${apigee_api_proxy.tf_helloworld_2.name}", "${apigee_api_proxy.tf_helloworld_3.name}"]
quota = "1000"
quota_interval = "2"
Expand Down

0 comments on commit b78f198

Please sign in to comment.