Skip to content

Commit

Permalink
azure_container_group - Fix flattening issue for `init_container.0.…
Browse files Browse the repository at this point in the history
…secure_environment_variables` (#22832)
magodo authored Aug 7, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 5f09597 commit 1c80246
Showing 2 changed files with 31 additions and 18 deletions.
41 changes: 24 additions & 17 deletions internal/services/containers/container_group_resource.go
Original file line number Diff line number Diff line change
@@ -1579,8 +1579,8 @@ func flattenContainerGroupInitContainers(d *pluginsdk.ResourceData, initContaine

if container.Properties.EnvironmentVariables != nil {
if len(*container.Properties.EnvironmentVariables) > 0 {
containerConfig["environment_variables"] = flattenContainerEnvironmentVariables(container.Properties.EnvironmentVariables, false, d, index)
containerConfig["secure_environment_variables"] = flattenContainerEnvironmentVariables(container.Properties.EnvironmentVariables, true, d, index)
containerConfig["environment_variables"] = flattenContainerEnvironmentVariables(container.Properties.EnvironmentVariables)
containerConfig["secure_environment_variables"] = flattenContainerSecureEnvironmentVariables(container.Properties.EnvironmentVariables, d, index, "init_container")
}
}

@@ -1662,8 +1662,8 @@ func flattenContainerGroupContainers(d *pluginsdk.ResourceData, containers *[]co

if container.Properties.EnvironmentVariables != nil {
if len(*container.Properties.EnvironmentVariables) > 0 {
containerConfig["environment_variables"] = flattenContainerEnvironmentVariables(container.Properties.EnvironmentVariables, false, d, index)
containerConfig["secure_environment_variables"] = flattenContainerEnvironmentVariables(container.Properties.EnvironmentVariables, true, d, index)
containerConfig["environment_variables"] = flattenContainerEnvironmentVariables(container.Properties.EnvironmentVariables)
containerConfig["secure_environment_variables"] = flattenContainerSecureEnvironmentVariables(container.Properties.EnvironmentVariables, d, index, "container")
}
}

@@ -1757,26 +1757,33 @@ func flattenContainerVolume(containerConfig map[string]interface{}, containersCo
containerConfig["volume"] = volumeConfigs
}

func flattenContainerEnvironmentVariables(input *[]containerinstance.EnvironmentVariable, isSecure bool, d *pluginsdk.ResourceData, oldContainerIndex int) map[string]interface{} {
func flattenContainerSecureEnvironmentVariables(input *[]containerinstance.EnvironmentVariable, d *pluginsdk.ResourceData, oldContainerIndex int, rootPropName string) map[string]interface{} {
output := make(map[string]interface{})

if input == nil {
return output
}

if isSecure {
for _, envVar := range *input {
if envVar.Value == nil {
envVarValue := d.Get(fmt.Sprintf("container.%d.secure_environment_variables.%s", oldContainerIndex, envVar.Name))
output[envVar.Name] = envVarValue
}
for _, envVar := range *input {
if envVar.Value == nil {
envVarValue := d.Get(fmt.Sprintf("%s.%d.secure_environment_variables.%s", rootPropName, oldContainerIndex, envVar.Name))
output[envVar.Name] = envVarValue
}
} else {
for _, envVar := range *input {
if envVar.Value != nil {
log.Printf("[DEBUG] NOT SECURE: Name: %s - Value: %s", envVar.Name, *envVar.Value)
output[envVar.Name] = *envVar.Value
}
}

return output
}
func flattenContainerEnvironmentVariables(input *[]containerinstance.EnvironmentVariable) map[string]interface{} {
output := make(map[string]interface{})

if input == nil {
return output
}

for _, envVar := range *input {
if envVar.Value != nil {
log.Printf("[DEBUG] NOT SECURE: Name: %s - Value: %s", envVar.Name, *envVar.Value)
output[envVar.Name] = *envVar.Value
}
}

Original file line number Diff line number Diff line change
@@ -661,7 +661,7 @@ func TestAccContainerGroup_withInitContainer(t *testing.T) {
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("ip_address_type"),
data.ImportStep("ip_address_type", "init_container.0.secure_environment_variables", "container.0.secure_environment_variables"),
})
}

@@ -2239,6 +2239,9 @@ resource "azurerm_container_group" "test" {
name = "init"
image = "busybox"
commands = ["echo", "hello from init"]
secure_environment_variables = {
PASSWORD = "something_very_secure_for_init"
}
}
container {
@@ -2248,6 +2251,9 @@ resource "azurerm_container_group" "test" {
memory = "1.5"
commands = ["echo", "hello from ubuntu"]
secure_environment_variables = {
PASSWORD = "something_very_secure"
}
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)

0 comments on commit 1c80246

Please sign in to comment.