Skip to content

fix(container): fix secret management container #2992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions internal/services/container/helpers_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package container
import (
"context"
"errors"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -302,6 +303,30 @@ func expandContainerSecrets(secretsRawMap interface{}) []*container.Secret {
return secrets
}

func convertToMapStringInterface(raw interface{}) map[string]interface{} {
out := make(map[string]interface{})
if raw == nil {
return out
}

m, ok := raw.(map[interface{}]interface{})
if ok {
for k, v := range m {
stringKey := fmt.Sprintf("%v", k)
out[stringKey] = v
}

return out
}

m2, ok := raw.(map[string]interface{})
if ok {
return m2
}

return out
}

func isContainerDNSResolveError(err error) bool {
responseError := &scw.ResponseError{}

Expand Down
23 changes: 22 additions & 1 deletion internal/services/container/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,28 @@ func ResourceContainerNamespaceUpdate(ctx context.Context, d *schema.ResourceDat
}

if d.HasChange("secret_environment_variables") {
req.SecretEnvironmentVariables = expandContainerSecrets(d.Get("secret_environment_variables"))
oldSecretsRaw, newSecretsRaw := d.GetChange("secret_environment_variables")

oldSecretsMap := convertToMapStringInterface(oldSecretsRaw)
newSecretsMap := convertToMapStringInterface(newSecretsRaw)

oldSecrets := expandContainerSecrets(oldSecretsMap)
newSecrets := expandContainerSecrets(newSecretsMap)

deletedSecrets := make([]*container.Secret, 0)

for _, oldSecret := range oldSecrets {
if _, exists := newSecretsMap[oldSecret.Key]; !exists {
deletedSecrets = append(deletedSecrets, &container.Secret{
Key: oldSecret.Key,
Value: nil,
})
}
}

deletedSecrets = append(deletedSecrets, newSecrets...)

req.SecretEnvironmentVariables = deletedSecrets
}

if _, err := api.UpdateNamespace(req, scw.WithContext(ctx)); err != nil {
Expand Down
85 changes: 85 additions & 0 deletions internal/services/container/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,91 @@ func TestAccNamespace_Basic(t *testing.T) {
})
}

func TestAccNamespace_SecretManagement(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: isNamespaceDestroyed(tt),
Steps: []resource.TestStep{
{
Config: `
resource scaleway_container_namespace main {
name = "test-secret-ns"
secret_environment_variables = {
"SECRET_1" = "value1"
}
}
`,
Check: resource.ComposeTestCheckFunc(
isNamespacePresent(tt, "scaleway_container_namespace.main"),
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.SECRET_1", "value1"),
),
},
{
Config: `
resource scaleway_container_namespace main {
name = "test-secret-ns"
secret_environment_variables = {}
}
`,
Check: resource.ComposeTestCheckFunc(
isNamespacePresent(tt, "scaleway_container_namespace.main"),
resource.TestCheckNoResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.SECRET_1"),
),
},
{
Config: `
resource scaleway_container_namespace main {
name = "test-secret-ns"
secret_environment_variables = {
"SECRET_1" = "value1"
"SECRET_2" = "value2"
}
}
`,
Check: resource.ComposeTestCheckFunc(
isNamespacePresent(tt, "scaleway_container_namespace.main"),
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.SECRET_1", "value1"),
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.SECRET_2", "value2"),
),
},
{
Config: `
resource scaleway_container_namespace main {
name = "test-secret-ns"
secret_environment_variables = {
"SECRET_2" = "value2"
}
}
`,
Check: resource.ComposeTestCheckFunc(
isNamespacePresent(tt, "scaleway_container_namespace.main"),
resource.TestCheckNoResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.SECRET_1"),
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.SECRET_2", "value2"),
),
},
{
Config: `
resource scaleway_container_namespace main {
name = "test-secret-ns"
secret_environment_variables = {
"SECRET_3" = "value3"
}
}
`,
Check: resource.ComposeTestCheckFunc(
isNamespacePresent(tt, "scaleway_container_namespace.main"),
resource.TestCheckNoResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.SECRET_2"),
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.SECRET_3", "value3"),
),
},
},
})
}

func TestAccNamespace_DestroyRegistry(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()
Expand Down
Loading
Loading