diff --git a/internal/services/containers/container_group_resource.go b/internal/services/containers/container_group_resource.go index 4a78823b3ceb..e6e60fd2e7c7 100644 --- a/internal/services/containers/container_group_resource.go +++ b/internal/services/containers/container_group_resource.go @@ -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 } } diff --git a/internal/services/containers/container_group_resource_test.go b/internal/services/containers/container_group_resource_test.go index 635ec770d140..231091810f20 100644 --- a/internal/services/containers/container_group_resource_test.go +++ b/internal/services/containers/container_group_resource_test.go @@ -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)