From 5c3ca04ebda9635d47c785641a45864c757081ae Mon Sep 17 00:00:00 2001 From: Julien Date: Mon, 30 Mar 2020 13:19:12 -0400 Subject: [PATCH] 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)