Skip to content

Commit

Permalink
extracted fromSecretReferenceDatamodel
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshmimsft committed Jul 18, 2024
1 parent 9f8b2d0 commit 361cfb4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 153 deletions.
111 changes: 42 additions & 69 deletions pkg/corerp/api/v20231001preview/environment_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -460,29 +442,53 @@ 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),
})
}
}

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{}
Expand All @@ -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
}
124 changes: 40 additions & 84 deletions pkg/corerp/api/v20231001preview/environment_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand All @@ -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)
})
}
}

0 comments on commit 361cfb4

Please sign in to comment.