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`
  • Loading branch information
magodo committed Aug 7, 2023
1 parent d5b3a1d commit c17cdc6
Show file tree
Hide file tree
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
Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
})
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit c17cdc6

Please sign in to comment.