From 5c3ca04ebda9635d47c785641a45864c757081ae Mon Sep 17 00:00:00 2001 From: Julien Date: Mon, 30 Mar 2020 13:19:12 -0400 Subject: [PATCH 1/2] Fix indices in for loop to prevent out of bound exception --- launchdarkly/resource_feature_flag.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/launchdarkly/resource_feature_flag.go b/launchdarkly/resource_feature_flag.go index 2e7470b..6590581 100644 --- a/launchdarkly/resource_feature_flag.go +++ b/launchdarkly/resource_feature_flag.go @@ -520,7 +520,8 @@ func applyChangesToVariations(resourceData *schema.ResourceData, client Client) "op": "remove", "path": fmt.Sprintf("/variations/%d", i), } - payloadValue[i] = removeValue + payloadValue[i - newNumberOfVariation] = removeValue + actualNumberOfVariation-- } _, err = client.Patch(getFlagUrl(project, key), payloadValue, []int{200}, NUMBER_OF_RETRY) @@ -531,7 +532,7 @@ func applyChangesToVariations(resourceData *schema.ResourceData, client Client) //Update values off existing variations var payloadValue []interface{} = make([]interface{}, 3*actualNumberOfVariation) - for i := 0; i <= actualNumberOfVariation-1; i++ { + for i := 0; i < actualNumberOfVariation; i++ { replaceValue := map[string]interface{}{ "op": "replace", "path": fmt.Sprintf("/variations/%d/value", i), @@ -559,12 +560,11 @@ func applyChangesToVariations(resourceData *schema.ResourceData, client Client) //Add new variations if newNumberOfVariation > actualNumberOfVariation { var payloadValue []interface{} = make([]interface{}, newNumberOfVariation - actualNumberOfVariation) - for i := actualNumberOfVariation; i < newNumberOfVariation; i++ { - + for i := 0; i < len(payloadValue); i++ { payloadValue[i] = map[string]interface{}{ "op": "add", - "path": fmt.Sprintf("/variations/%d", i), - "value": transformedVariations[i], + "path": fmt.Sprintf("/variations/%d", actualNumberOfVariation + i), + "value": transformedVariations[actualNumberOfVariation + i], } } _, err = client.Patch(getFlagUrl(project, key), payloadValue, []int{200}, NUMBER_OF_RETRY) From d8ca92f14416e08f1b398878652472285190694d Mon Sep 17 00:00:00 2001 From: Julien Date: Mon, 30 Mar 2020 13:19:12 -0400 Subject: [PATCH 2/2] Fix indices in for loop to prevent out of bound exception --- launchdarkly/resource_feature_flag.go | 104 ++++++++++++++++---------- 1 file changed, 64 insertions(+), 40 deletions(-) diff --git a/launchdarkly/resource_feature_flag.go b/launchdarkly/resource_feature_flag.go index 2e7470b..0034666 100644 --- a/launchdarkly/resource_feature_flag.go +++ b/launchdarkly/resource_feature_flag.go @@ -513,61 +513,85 @@ func applyChangesToVariations(resourceData *schema.ResourceData, client Client) //Remove variations if newNumberOfVariation < actualNumberOfVariation { - var payloadValue []interface{} = make([]interface{}, actualNumberOfVariation - newNumberOfVariation) - - for i := actualNumberOfVariation - 1; i >= newNumberOfVariation; i-- { + var deletePayloadValue []interface{} = make([]interface{}, actualNumberOfVariation - newNumberOfVariation) + for i := 0; i < len(deletePayloadValue); i++ { removeValue := map[string]interface{}{ "op": "remove", - "path": fmt.Sprintf("/variations/%d", i), + "path": fmt.Sprintf("/variations/%d", actualNumberOfVariation - 1), } - payloadValue[i] = removeValue + deletePayloadValue[i] = removeValue + actualNumberOfVariation-- } - - _, err = client.Patch(getFlagUrl(project, key), payloadValue, []int{200}, NUMBER_OF_RETRY) + _, err = client.Patch(getFlagUrl(project, key), deletePayloadValue, []int{200}, NUMBER_OF_RETRY) if err != nil { return err } - } - - //Update values off existing variations - var payloadValue []interface{} = make([]interface{}, 3*actualNumberOfVariation) - for i := 0; i <= actualNumberOfVariation-1; i++ { - replaceValue := map[string]interface{}{ - "op": "replace", - "path": fmt.Sprintf("/variations/%d/value", i), - "value": transformedVariations[i].Value, - } - replaceName := map[string]interface{}{ - "op": "replace", - "path": fmt.Sprintf("/variations/%d/name", i), - "value": transformedVariations[i].Name, + //Update values off existing variations that wont be delete + var updatePayloadValue []interface{} = make([]interface{}, 3*newNumberOfVariation) + for i := 0; i < newNumberOfVariation; i++ { + replaceValue := map[string]interface{}{ + "op": "replace", + "path": fmt.Sprintf("/variations/%d/value", i), + "value": transformedVariations[i].Value, + } + replaceName := map[string]interface{}{ + "op": "replace", + "path": fmt.Sprintf("/variations/%d/name", i), + "value": transformedVariations[i].Name, + } + replaceDescription := map[string]interface{}{ + "op": "replace", + "path": fmt.Sprintf("/variations/%d/description", i), + "value": transformedVariations[i].Description, + } + updatePayloadValue[i*3] = replaceValue + updatePayloadValue[(i*3)+1] = replaceName + updatePayloadValue[(i*3)+2] = replaceDescription } - replaceDescription := map[string]interface{}{ - "op": "replace", - "path": fmt.Sprintf("/variations/%d/description", i), - "value": transformedVariations[i].Description, + _, err = client.Patch(getFlagUrl(project, key), updatePayloadValue, []int{200}, NUMBER_OF_RETRY) + if err != nil { + return err } - payloadValue[i*3] = replaceValue - payloadValue[(i*3)+1] = replaceName - payloadValue[(i*3)+2] = replaceDescription - } - _, err = client.Patch(getFlagUrl(project, key), payloadValue, []int{200}, NUMBER_OF_RETRY) - if err != nil { - return err } //Add new variations - if newNumberOfVariation > actualNumberOfVariation { - var payloadValue []interface{} = make([]interface{}, newNumberOfVariation - actualNumberOfVariation) - for i := actualNumberOfVariation; i < newNumberOfVariation; i++ { - - payloadValue[i] = map[string]interface{}{ + if newNumberOfVariation >= actualNumberOfVariation { + //Update values off existing variations + var updatePayloadValue []interface{} = make([]interface{}, 3*actualNumberOfVariation) + for i := 0; i < actualNumberOfVariation; i++ { + replaceValue := map[string]interface{}{ + "op": "replace", + "path": fmt.Sprintf("/variations/%d/value", i), + "value": transformedVariations[i].Value, + } + replaceName := map[string]interface{}{ + "op": "replace", + "path": fmt.Sprintf("/variations/%d/name", i), + "value": transformedVariations[i].Name, + } + replaceDescription := map[string]interface{}{ + "op": "replace", + "path": fmt.Sprintf("/variations/%d/description", i), + "value": transformedVariations[i].Description, + } + updatePayloadValue[i*3] = replaceValue + updatePayloadValue[(i*3)+1] = replaceName + updatePayloadValue[(i*3)+2] = replaceDescription + } + _, err = client.Patch(getFlagUrl(project, key), updatePayloadValue, []int{200}, NUMBER_OF_RETRY) + if err != nil { + return err + } + + var createPayloadValue []interface{} = make([]interface{}, newNumberOfVariation - actualNumberOfVariation) + for i := 0; i < len(createPayloadValue); i++ { + createPayloadValue[i] = map[string]interface{}{ "op": "add", - "path": fmt.Sprintf("/variations/%d", i), - "value": transformedVariations[i], + "path": fmt.Sprintf("/variations/%d", actualNumberOfVariation + i), + "value": transformedVariations[actualNumberOfVariation + i], } } - _, err = client.Patch(getFlagUrl(project, key), payloadValue, []int{200}, NUMBER_OF_RETRY) + _, err = client.Patch(getFlagUrl(project, key), createPayloadValue, []int{200}, NUMBER_OF_RETRY) if err != nil { return err }