From 361cfb401e22f77edaec57930fea4b8269e0f017 Mon Sep 17 00:00:00 2001 From: lakshmimsft Date: Wed, 17 Jul 2024 21:01:57 -0700 Subject: [PATCH] extracted fromSecretReferenceDatamodel --- .../environment_conversion.go | 111 ++++++---------- .../environment_conversion_test.go | 124 ++++++------------ 2 files changed, 82 insertions(+), 153 deletions(-) diff --git a/pkg/corerp/api/v20231001preview/environment_conversion.go b/pkg/corerp/api/v20231001preview/environment_conversion.go index d448a225a95..ebc74aa26f0 100644 --- a/pkg/corerp/api/v20231001preview/environment_conversion.go +++ b/pkg/corerp/api/v20231001preview/environment_conversion.go @@ -208,7 +208,9 @@ func toRecipeConfigDatamodel(config *RecipeConfigProperties) datamodel.RecipeCon } recipeConfig.Env = toRecipeConfigEnvDatamodel(config) - recipeConfig.EnvSecrets = toRecipeConfigEnvSecretsDatamodel(config) + if config.EnvSecrets != nil { + recipeConfig.EnvSecrets = toSecretReferenceDatamodel(config.EnvSecrets) + } return recipeConfig } @@ -240,7 +242,7 @@ func fromRecipeConfigDatamodel(config datamodel.RecipeConfigProperties) *RecipeC } recipeConfig.Env = fromRecipeConfigEnvDatamodel(config) - recipeConfig.EnvSecrets = fromRecipeConfigEnvSecretsDatamodel(config) + recipeConfig.EnvSecrets = fromSecretReferenceDatamodel(config.EnvSecrets) return recipeConfig } @@ -430,26 +432,6 @@ func toRecipeConfigTerraformProvidersDatamodel(config *RecipeConfigProperties) m return dm } -func toSecretReferenceDatamodel(providerConfigSecrets map[string]*SecretReference) map[string]datamodel.SecretReference { - var providerSecrets map[string]datamodel.SecretReference - - for secretKey, secretValue := range providerConfigSecrets { - if secretValue != nil { - providerSecret := datamodel.SecretReference{ - Source: to.String(secretValue.Source), - Key: to.String(secretValue.Key), - } - - if providerSecrets == nil { - providerSecrets = map[string]datamodel.SecretReference{} - } - providerSecrets[secretKey] = providerSecret - } - } - - return providerSecrets -} - func fromRecipeConfigTerraformProvidersDatamodel(config datamodel.RecipeConfigProperties) map[string][]*ProviderConfigProperties { if config.Terraform.Providers == nil { return nil @@ -460,22 +442,9 @@ func fromRecipeConfigTerraformProvidersDatamodel(config datamodel.RecipeConfigPr providers[k] = []*ProviderConfigProperties{} for _, provider := range v { - var providerSecrets map[string]*SecretReference - - for secretKey, secretValue := range provider.Secrets { - if providerSecrets == nil { - providerSecrets = map[string]*SecretReference{} - } - - providerSecrets[secretKey] = &SecretReference{ - Source: to.Ptr(secretValue.Source), - Key: to.Ptr(secretValue.Key), - } - } - providers[k] = append(providers[k], &ProviderConfigProperties{ AdditionalProperties: provider.AdditionalProperties, - Secrets: providerSecrets, + Secrets: fromSecretReferenceDatamodel(provider.Secrets), }) } } @@ -483,6 +452,43 @@ func fromRecipeConfigTerraformProvidersDatamodel(config datamodel.RecipeConfigPr return providers } +func toSecretReferenceDatamodel(configSecrets map[string]*SecretReference) map[string]datamodel.SecretReference { + var secrets map[string]datamodel.SecretReference + + for secretKey, secretValue := range configSecrets { + if secretValue != nil { + secret := datamodel.SecretReference{ + Source: to.String(secretValue.Source), + Key: to.String(secretValue.Key), + } + + if secrets == nil { + secrets = map[string]datamodel.SecretReference{} + } + secrets[secretKey] = secret + } + } + + return secrets +} + +func fromSecretReferenceDatamodel(secrets map[string]datamodel.SecretReference) map[string]*SecretReference { + var converted map[string]*SecretReference + + for secretKey, secretValue := range secrets { + if converted == nil { + converted = map[string]*SecretReference{} + } + + converted[secretKey] = &SecretReference{ + Source: to.Ptr(secretValue.Source), + Key: to.Ptr(secretValue.Key), + } + } + + return converted +} + func toRecipeConfigEnvDatamodel(config *RecipeConfigProperties) datamodel.EnvironmentVariables { if config.Env == nil { return datamodel.EnvironmentVariables{} @@ -506,36 +512,3 @@ func fromRecipeConfigEnvDatamodel(config datamodel.RecipeConfigProperties) map[s return env } - -func toRecipeConfigEnvSecretsDatamodel(config *RecipeConfigProperties) map[string]datamodel.SecretReference { - if config.EnvSecrets == nil { - return nil - } - - envSecrets := map[string]datamodel.SecretReference{} - for k, v := range config.EnvSecrets { - envSecrets[k] = datamodel.SecretReference{ - Source: to.String(v.Source), - Key: to.String(v.Key), - } - } - - return envSecrets -} - -func fromRecipeConfigEnvSecretsDatamodel(config datamodel.RecipeConfigProperties) map[string]*SecretReference { - var envSecrets map[string]*SecretReference - - for k, v := range config.EnvSecrets { - if envSecrets == nil { - envSecrets = map[string]*SecretReference{} - } - - envSecrets[k] = &SecretReference{ - Source: to.Ptr(v.Source), - Key: to.Ptr(v.Key), - } - } - - return envSecrets -} diff --git a/pkg/corerp/api/v20231001preview/environment_conversion_test.go b/pkg/corerp/api/v20231001preview/environment_conversion_test.go index 55348f206c4..3d7aa32bb89 100644 --- a/pkg/corerp/api/v20231001preview/environment_conversion_test.go +++ b/pkg/corerp/api/v20231001preview/environment_conversion_test.go @@ -990,13 +990,13 @@ func Test_fromRecipeConfigEnvDatamodel(t *testing.T) { func Test_toSecretReferenceDatamodel(t *testing.T) { tests := []struct { - name string - providerConfigSecrets map[string]*SecretReference - expectedResult map[string]datamodel.SecretReference + name string + configSecrets map[string]*SecretReference + expectedResult map[string]datamodel.SecretReference }{ { - name: "multiple provider secrets", - providerConfigSecrets: map[string]*SecretReference{ + name: "Multiple Provider Secrets", + configSecrets: map[string]*SecretReference{ "secret1": { Source: to.Ptr("source1"), Key: to.Ptr("key1"), @@ -1018,20 +1018,20 @@ func Test_toSecretReferenceDatamodel(t *testing.T) { }, }, { - name: "nil provider secrets", - providerConfigSecrets: nil, - expectedResult: nil, + name: "Nil Provider Secrets", + configSecrets: nil, + expectedResult: nil, }, { - name: "nil secret in provider properties", - providerConfigSecrets: map[string]*SecretReference{ + name: "Nil Secret in Provider Properties", + configSecrets: map[string]*SecretReference{ "secret1": nil, }, expectedResult: nil, }, { - name: "nil + valid secret in provider properties", - providerConfigSecrets: map[string]*SecretReference{ + name: "Nil + Valid Secret in Provider Properties", + configSecrets: map[string]*SecretReference{ "secret1": nil, "secret2": { Source: to.Ptr("source2"), @@ -1049,98 +1049,54 @@ func Test_toSecretReferenceDatamodel(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - result := toSecretReferenceDatamodel(tt.providerConfigSecrets) + result := toSecretReferenceDatamodel(tt.configSecrets) require.Equal(t, tt.expectedResult, result) }) } } -func Test_toRecipeConfigEnvSecretsDatamodel(t *testing.T) { +func Test_fromSecretReferenceDatamodel(t *testing.T) { tests := []struct { - name string - config *RecipeConfigProperties - want map[string]datamodel.SecretReference + name string + secrets map[string]datamodel.SecretReference + expected map[string]*SecretReference }{ { - name: "Empty Recipe Configuration", - config: &RecipeConfigProperties{}, - want: nil, + name: "Empty Secret", + secrets: map[string]datamodel.SecretReference{}, + expected: nil, }, { - name: "With Multiple Environment Secret Variables", - config: &RecipeConfigProperties{ - EnvSecrets: map[string]*SecretReference{ - "secret1": { - Source: to.Ptr("source1"), - Key: to.Ptr("key1"), - }, - "secret2": { - Source: to.Ptr("source2"), - Key: to.Ptr("key2"), - }, - }, - }, - want: map[string]datamodel.SecretReference{ - "secret1": { - Source: "source1", - Key: "key1", - }, - "secret2": { - Source: "source2", - Key: "key2", - }, - }, + name: "Nil Secret", + secrets: nil, + expected: nil, }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := toRecipeConfigEnvSecretsDatamodel(tt.config) - require.Equal(t, tt.want, result) - }) - } -} - -func Test_fromRecipeConfigEnvSecretsDatamodel(t *testing.T) { - tests := []struct { - name string - config datamodel.RecipeConfigProperties - want map[string]*SecretReference - }{ { - name: "Empty Recipe Configuration", - config: datamodel.RecipeConfigProperties{}, - want: nil, + name: "Single Secret", + secrets: map[string]datamodel.SecretReference{ + "secret1": {Source: "source1", Key: "key1"}, + }, + expected: map[string]*SecretReference{ + "secret1": {Source: to.Ptr("source1"), Key: to.Ptr("key1")}, + }, }, { - name: "With Multiple Environment Secret Variables", - config: datamodel.RecipeConfigProperties{ - EnvSecrets: map[string]datamodel.SecretReference{ - "secret1": { - Source: "source1", - Key: "key1", - }, - "secret2": { - Source: "source2", - Key: "key2", - }, - }, + name: "Multiple Secrets", + secrets: map[string]datamodel.SecretReference{ + "secret1": {Source: "source1", Key: "key1"}, + "secret2": {Source: "source2", Key: "key2"}, }, - want: map[string]*SecretReference{ - "secret1": { - Source: to.Ptr("source1"), - Key: to.Ptr("key1"), - }, - "secret2": { - Source: to.Ptr("source2"), - Key: to.Ptr("key2"), - }, + expected: map[string]*SecretReference{ + "secret1": {Source: to.Ptr("source1"), Key: to.Ptr("key1")}, + "secret2": {Source: to.Ptr("source2"), Key: to.Ptr("key2")}, }, }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - result := fromRecipeConfigEnvSecretsDatamodel(tt.config) - require.Equal(t, tt.want, result) + result := fromSecretReferenceDatamodel(tt.secrets) + require.Equal(t, tt.expected, result) }) } }