Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix product proxies order sensitivity on API product resource #46

Merged
merged 3 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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