Skip to content

Commit 9094d0b

Browse files
author
Nicholas M. Iodice
authored
Prevent secret variables from being lost from the state (#291)
This change fixes an issue where variables marked as secret are always set to be updated by the provider
1 parent 8de1c96 commit 9094d0b

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

azuredevops/resource_variable_group.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,28 +259,50 @@ func flattenVariableGroup(d *schema.ResourceData, variableGroup *taskagent.Varia
259259
d.SetId(fmt.Sprintf("%d", *variableGroup.Id))
260260
d.Set("name", *variableGroup.Name)
261261
d.Set("description", *variableGroup.Description)
262-
d.Set("variable", flattenVariables(variableGroup))
262+
d.Set("variable", flattenVariables(d, variableGroup))
263263
d.Set("project_id", projectID)
264264
}
265265

266266
// Convert AzDO Variables data structure to Terraform TypeSet
267-
func flattenVariables(variableGroup *taskagent.VariableGroup) interface{} {
267+
//
268+
// Note: The AzDO API does not return the value for variables marked as a secret. For this reason
269+
// variables marked as secret will need to be pulled from the state itself
270+
func flattenVariables(d *schema.ResourceData, variableGroup *taskagent.VariableGroup) interface{} {
268271
// Preallocate list of variable prop maps
269272
variables := make([]map[string]interface{}, len(*variableGroup.Variables))
270273

271274
index := 0
272-
for k, v := range *variableGroup.Variables {
273-
variables[index] = map[string]interface{}{
274-
"name": k,
275-
"value": converter.ToString(v.Value, ""),
276-
"is_secret": converter.ToBool(v.IsSecret, false),
275+
for varName, varVal := range *variableGroup.Variables {
276+
var variable map[string]interface{}
277+
if converter.ToBool(varVal.IsSecret, false) {
278+
variable = findVariableFromState(d, varName)
279+
} else {
280+
variable = map[string]interface{}{
281+
"name": varName,
282+
"value": converter.ToString(varVal.Value, ""),
283+
"is_secret": false,
284+
}
277285
}
286+
variables[index] = variable
278287
index = index + 1
279288
}
280289

281290
return variables
282291
}
283292

293+
// Pulls a variable with a given name from the state. If no such variable is found, nil
294+
// will be returned.
295+
func findVariableFromState(d *schema.ResourceData, name string) map[string]interface{} {
296+
for _, variable := range d.Get("variable").(*schema.Set).List() {
297+
asMap := variable.(map[string]interface{})
298+
// Note: casing matters here so we will use `==` over `strings.EqualFold`
299+
if asMap["name"] == name {
300+
return asMap
301+
}
302+
}
303+
return nil
304+
}
305+
284306
// Convert internal Terraform data structure to an AzDO data structure for Allow Access
285307
func expandDefinitionResourceAuth(d *schema.ResourceData, createdVariableGroup *taskagent.VariableGroup) []build.DefinitionResourceReference {
286308
resourceRefType := "variablegroup"

azuredevops/resource_variable_group_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,13 @@ func TestAccAccAzureDevOpsVariableGroup_CreateAndUpdate(t *testing.T) {
9494
resource.TestCheckResourceAttr(tfVarGroupNode, "name", vargroupNameFirst),
9595
testAccCheckVariableGroupResourceExists(vargroupNameFirst, allowAccessFirst),
9696
),
97-
// due to the value of "secret" variables not being returned in the API response.
98-
ExpectNonEmptyPlan: true,
9997
}, {
10098
Config: testhelper.TestAccVariableGroupResource(projectName, vargroupNameSecond, allowAccessSecond),
10199
Check: resource.ComposeTestCheckFunc(
102100
resource.TestCheckResourceAttrSet(tfVarGroupNode, "project_id"),
103101
resource.TestCheckResourceAttr(tfVarGroupNode, "name", vargroupNameSecond),
104102
testAccCheckVariableGroupResourceExists(vargroupNameSecond, allowAccessSecond),
105103
),
106-
// due to the value of "secret" variables not being returned in the API response.
107-
ExpectNonEmptyPlan: true,
108104
}, {
109105
Config: testhelper.TestAccVariableGroupResourceNoSecrets(projectName, vargroupNameSecond, allowAccessSecond),
110106
Check: resource.ComposeTestCheckFunc(

0 commit comments

Comments
 (0)