Skip to content

Commit

Permalink
Merged in TER-266-fix_features_parameters_updates (pull request #14)
Browse files Browse the repository at this point in the history
TER-266: Fix features.parameters updates

Approved-by: Jordan Caussat <[email protected]>
Approved-by: Patrick Decat <[email protected]>
Approved-by: Jérôme Respaut <[email protected]>
  • Loading branch information
Jordan Caussat authored and Shr3ps committed Apr 19, 2018
2 parents 373731a + 0a9a3ff commit fb153e0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
20 changes: 13 additions & 7 deletions ghost/resource_ghost_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"reflect"
"time"

"cloud-deploy.io/go-st"
Expand Down Expand Up @@ -785,7 +786,10 @@ func flattenGhostAppFeatures(features *[]ghost.Feature) []interface{} {
}

if feature.Parameters != nil {
values["parameters"] = fmt.Sprintf("%v", feature.Parameters)
params_json, err := json.Marshal(feature.Parameters)
if err == nil {
values["parameters"] = string(params_json)
}
}

featureList = append(featureList, values)
Expand All @@ -796,17 +800,19 @@ func flattenGhostAppFeatures(features *[]ghost.Feature) []interface{} {

func SuppressDiffFeatureParameters() schema.SchemaDiffSuppressFunc {
return func(k, old, new string, d *schema.ResourceData) bool {
var jsonDoc interface{}
var oldJson, newJson interface{}

if err := json.Unmarshal([]byte(new), &jsonDoc); err != nil {
log.Printf("Error loading feature paramaters json: %v", err)
if err := json.Unmarshal([]byte(old), &oldJson); err != nil {
log.Printf("Error loading feature parameters json: %v", err)
}

newJsonParameter := fmt.Sprintf("%v", jsonDoc)
if err := json.Unmarshal([]byte(new), &newJson); err != nil {
log.Printf("Error loading feature parameters json: %v", err)
}

// If the new parameters structure is equivalent to the old one or is empty,
// If the new parameters structure is equivalent to the old one,
// ignores the diff during plan
return newJsonParameter == old || old == "map[]"
return reflect.DeepEqual(oldJson, newJson)
}
}

Expand Down
32 changes: 28 additions & 4 deletions ghost/resource_ghost_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"cloud-deploy.io/go-st"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

Expand Down Expand Up @@ -1096,16 +1097,14 @@ func TestFlattenGhostAppFeatures(t *testing.T) {
Name: "feature",
Version: "1",
Provisioner: "ansible",
Parameters: map[string]interface{}{
"package_name": []interface{}{"test", "nano"},
},
Parameters: `{"package_name":["test","nano"]}`,
}},
[]interface{}{
map[string]interface{}{
"name": "feature",
"version": "1",
"provisioner": "ansible",
"parameters": "map[package_name:[test nano]]",
"parameters": `"{\"package_name\":[\"test\",\"nano\"]}"`,
},
},
},
Expand Down Expand Up @@ -1298,3 +1297,28 @@ func TestFlattenGhostSafeDeployment(t *testing.T) {
}
}
}

func TestSuppressDiffFeatureParameters(t *testing.T) {
suppressDiffFeatureParameters := SuppressDiffFeatureParameters()

cases := []struct {
ParameterName string
OldValue string
NewValue string
ExpectedOutput bool
ResourceData *schema.ResourceData
}{
{"features.0.parameters", `{ "name" : "positive", "id" : 1 }`, `{ "name" : "positive", "id" : 1 }`, true, nil},
{"features.0.parameters", `{ "name" : "positive", "id" : 1 }`, `{ "id" : 1, "name" : "positive" }`, true, nil},
{"features.0.parameters", `{ "name" : "negative", "id" : 1 }`, `{ "id" : 1, "name" : "positive" }`, false, nil},
{"features.0.parameters", `{ "name" : "negative", "id" : 1 }`, `{ "name" : "positive", "id" : 1 }`, false, nil},
}

for _, tc := range cases {
output := suppressDiffFeatureParameters(tc.ParameterName, tc.OldValue, tc.NewValue, tc.ResourceData)
if !reflect.DeepEqual(output, tc.ExpectedOutput) {
t.Fatalf("Unexpected output from SuppressDiffFeatureParameters.\nExpected: %#v\nGiven: %#v",
tc.ExpectedOutput, output)
}
}
}

0 comments on commit fb153e0

Please sign in to comment.