Skip to content

Commit

Permalink
cleanup upon bicep testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshmimsft committed Jul 18, 2024
1 parent 3cd05ed commit 305d9df
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 167 deletions.
121 changes: 32 additions & 89 deletions pkg/corerp/api/v20231001preview/environment_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ func (src *EnvironmentResource) ConvertTo() (v1.DataModelInterface, error) {
return nil, err
}
converted.Properties.Compute = *envCompute

recipeConfig, err := toRecipeConfigDatamodel(src.Properties.RecipeConfig)
if err != nil {
return nil, err
}
converted.Properties.RecipeConfig = recipeConfig
converted.Properties.RecipeConfig = toRecipeConfigDatamodel(src.Properties.RecipeConfig)

if src.Properties.Recipes != nil {
envRecipes := make(map[string]map[string]datamodel.EnvironmentRecipeProperties)
Expand Down Expand Up @@ -187,7 +182,7 @@ func (dst *EnvironmentResource) ConvertFrom(src v1.DataModelInterface) error {
return nil
}

func toRecipeConfigDatamodel(config *RecipeConfigProperties) (datamodel.RecipeConfigProperties, error) {
func toRecipeConfigDatamodel(config *RecipeConfigProperties) datamodel.RecipeConfigProperties {
if config != nil {
recipeConfig := datamodel.RecipeConfigProperties{}
if config.Terraform != nil {
Expand All @@ -209,21 +204,16 @@ func toRecipeConfigDatamodel(config *RecipeConfigProperties) (datamodel.RecipeCo
}
}

recipeTFProviders, err := toRecipeConfigTerraformProvidersDatamodel(config)
if err != nil {
return datamodel.RecipeConfigProperties{}, err
}

recipeConfig.Terraform.Providers = recipeTFProviders
recipeConfig.Terraform.Providers = toRecipeConfigTerraformProvidersDatamodel(config)
}

recipeConfig.Env = toRecipeConfigEnvDatamodel(config)
recipeConfig.EnvSecrets = toRecipeConfigEnvSecretsDatamodel(config)

return recipeConfig, nil
return recipeConfig
}

return datamodel.RecipeConfigProperties{}, nil
return datamodel.RecipeConfigProperties{}
}

func fromRecipeConfigDatamodel(config datamodel.RecipeConfigProperties) *RecipeConfigProperties {
Expand Down Expand Up @@ -420,84 +410,44 @@ func fromRecipePropertiesClassificationDatamodel(e datamodel.EnvironmentRecipePr
return nil
}

func toRecipeConfigTerraformProvidersDatamodel(config *RecipeConfigProperties) (map[string][]datamodel.ProviderConfigProperties, error) {
func toRecipeConfigTerraformProvidersDatamodel(config *RecipeConfigProperties) map[string][]datamodel.ProviderConfigProperties {
if config.Terraform == nil || config.Terraform.Providers == nil {
return nil, nil
return nil
}

dm := map[string][]datamodel.ProviderConfigProperties{}
for k, v := range config.Terraform.Providers {
dm[k] = []datamodel.ProviderConfigProperties{}

for _, providerAdditionalProperties := range v {
var addnlProperties map[string]any
var convertedSecrets map[string]datamodel.SecretReference

convertedSecrets = getSecretsFromProviderProperties(providerAdditionalProperties.Secrets)
addnlProperties = providerAdditionalProperties.AdditionalProperties

// Account for possibility of nested additional properties during unmarshalling
// https://json-schema.org/understanding-json-schema/reference/object#additionalproperties
if tmpAddnlProperties, ok := addnlProperties["additionalProperties"]; ok && addnlProperties["additionalProperties"] != nil {
if tmpMap, ok := tmpAddnlProperties.(map[string]any); ok {
addnlProperties = tmpMap
} else {
return nil, v1.ErrInvalidModelConversion
}
}

// check if additional properties contain Secrets
// get AdditionalProperties from providerAdditionalProperties excluding the 'secrets' key.
for key, value := range addnlProperties {
if key == "secrets" {
if tmpSecret, ok := value.(map[string]*SecretReference); !ok {
return nil, v1.ErrInvalidModelConversion
} else {
propSecrets := getSecretsFromProviderProperties(tmpSecret)

// Add the secrets to the convertedSecrets map
for secretKey, secretValue := range propSecrets {
if convertedSecrets == nil {
convertedSecrets = map[string]datamodel.SecretReference{}
}
convertedSecrets[secretKey] = secretValue
}
}

// remove the 'secrets' key from the additional properties as we've added all
// secrets to convertedSecrets map
delete(addnlProperties, "secrets")
}
}

for _, providerConfigProperties := range v {
dm[k] = append(dm[k], datamodel.ProviderConfigProperties{
AdditionalProperties: addnlProperties,
Secrets: convertedSecrets,
AdditionalProperties: providerConfigProperties.AdditionalProperties,
Secrets: getSecretsFromProviderConfig(providerConfigProperties.Secrets),
})
}
}

return dm, nil
return dm
}

func getSecretsFromProviderProperties(providerProperties map[string]*SecretReference) map[string]datamodel.SecretReference {
var convertedSecrets map[string]datamodel.SecretReference
func getSecretsFromProviderConfig(providerConfigSecrets map[string]*SecretReference) map[string]datamodel.SecretReference {
var providerSecrets map[string]datamodel.SecretReference

for secretKey, secretValue := range providerProperties {
for secretKey, secretValue := range providerConfigSecrets {
if secretValue != nil {
convertedSecret := datamodel.SecretReference{
providerSecret := datamodel.SecretReference{
Source: to.String(secretValue.Source),
Key: to.String(secretValue.Key),
}

if convertedSecrets == nil {
convertedSecrets = map[string]datamodel.SecretReference{}
if providerSecrets == nil {
providerSecrets = map[string]datamodel.SecretReference{}
}
convertedSecrets[secretKey] = convertedSecret
providerSecrets[secretKey] = providerSecret
}
}

return convertedSecrets
return providerSecrets
}

func fromRecipeConfigTerraformProvidersDatamodel(config datamodel.RecipeConfigProperties) map[string][]*ProviderConfigProperties {
Expand All @@ -510,28 +460,23 @@ func fromRecipeConfigTerraformProvidersDatamodel(config datamodel.RecipeConfigPr
providers[k] = []*ProviderConfigProperties{}

for _, provider := range v {
var convertedSecrets map[string]*SecretReference
var providerSecrets map[string]*SecretReference

// Iterate over the input secrets map and convert each value to the output type
for secretKey, secretValue := range provider.Secrets {
tmpSecret := &SecretReference{
Source: to.Ptr(secretValue.Source),
Key: to.Ptr(secretValue.Key),
if providerSecrets == nil {
providerSecrets = map[string]*SecretReference{}
}

if convertedSecrets == nil {
convertedSecrets = map[string]*SecretReference{}
providerSecrets[secretKey] = &SecretReference{
Source: to.Ptr(secretValue.Source),
Key: to.Ptr(secretValue.Key),
}

convertedSecrets[secretKey] = tmpSecret
}

tmpProviderConfig := ProviderConfigProperties{
providers[k] = append(providers[k], &ProviderConfigProperties{
AdditionalProperties: provider.AdditionalProperties,
Secrets: convertedSecrets,
}

providers[k] = append(providers[k], &tmpProviderConfig)
Secrets: providerSecrets,
})
}
}

Expand Down Expand Up @@ -582,16 +527,14 @@ func fromRecipeConfigEnvSecretsDatamodel(config datamodel.RecipeConfigProperties
var envSecrets map[string]*SecretReference

for k, v := range config.EnvSecrets {
convertedEnvSecret := &SecretReference{
Source: to.Ptr(v.Source),
Key: to.Ptr(v.Key),
}

if envSecrets == nil {
envSecrets = map[string]*SecretReference{}
}

envSecrets[k] = convertedEnvSecret
envSecrets[k] = &SecretReference{
Source: to.Ptr(v.Source),
Key: to.Ptr(v.Key),
}
}

return envSecrets
Expand Down
86 changes: 13 additions & 73 deletions pkg/corerp/api/v20231001preview/environment_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,71 +776,11 @@ func Test_toRecipeConfigTerraformProvidersDatamodel(t *testing.T) {
},
},
},
{
name: "Error Provider Configuration",
config: &RecipeConfigProperties{
Terraform: &TerraformConfigProperties{
Providers: map[string][]*ProviderConfigProperties{
"azurerm": {
&ProviderConfigProperties{
AdditionalProperties: map[string]any{
"subscription_id": "00000000-0000-0000-0000-000000000000",
"secrets": "invalid",
},
},
},
},
},
},
expectError: true,
},
{
name: "Provider Configuration Variation With Secret",
config: &RecipeConfigProperties{
Terraform: &TerraformConfigProperties{
Providers: map[string][]*ProviderConfigProperties{
"azurerm": {
&ProviderConfigProperties{
AdditionalProperties: map[string]any{
"subscription_id": "00000000-0000-0000-0000-000000000000",
"secrets": map[string]*SecretReference{
"secret1": {
Source: to.Ptr("/planes/radius/local/resourcegroups/default/providers/Applications.Core/secretStores/secretstore1"),
Key: to.Ptr("key1"),
},
},
},
},
},
},
},
},
want: map[string][]datamodel.ProviderConfigProperties{
"azurerm": {
{
AdditionalProperties: map[string]any{
"subscription_id": "00000000-0000-0000-0000-000000000000",
},
Secrets: map[string]datamodel.SecretReference{
"secret1": {
Source: "/planes/radius/local/resourcegroups/default/providers/Applications.Core/secretStores/secretstore1",
Key: "key1",
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := toRecipeConfigTerraformProvidersDatamodel(tt.config)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tt.want, result)
}
result := toRecipeConfigTerraformProvidersDatamodel(tt.config)
require.Equal(t, tt.want, result)
})
}
}
Expand Down Expand Up @@ -1048,15 +988,15 @@ func Test_fromRecipeConfigEnvDatamodel(t *testing.T) {
}
}

func Test_getSecretsFromProviderProperties(t *testing.T) {
func Test_getSecretsFromProviderConfig(t *testing.T) {
tests := []struct {
name string
providerProperties map[string]*SecretReference
expectedResult map[string]datamodel.SecretReference
name string
providerConfigSecrets map[string]*SecretReference
expectedResult map[string]datamodel.SecretReference
}{
{
name: "multiple provider secrets",
providerProperties: map[string]*SecretReference{
providerConfigSecrets: map[string]*SecretReference{
"secret1": {
Source: to.Ptr("source1"),
Key: to.Ptr("key1"),
Expand All @@ -1078,20 +1018,20 @@ func Test_getSecretsFromProviderProperties(t *testing.T) {
},
},
{
name: "nil provider secrets",
providerProperties: nil,
expectedResult: nil,
name: "nil provider secrets",
providerConfigSecrets: nil,
expectedResult: nil,
},
{
name: "nil secret in provider properties",
providerProperties: map[string]*SecretReference{
providerConfigSecrets: map[string]*SecretReference{
"secret1": nil,
},
expectedResult: nil,
},
{
name: "nil + valid secret in provider properties",
providerProperties: map[string]*SecretReference{
providerConfigSecrets: map[string]*SecretReference{
"secret1": nil,
"secret2": {
Source: to.Ptr("source2"),
Expand All @@ -1109,7 +1049,7 @@ func Test_getSecretsFromProviderProperties(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := getSecretsFromProviderProperties(tt.providerProperties)
result := getSecretsFromProviderConfig(tt.providerConfigSecrets)
require.Equal(t, tt.expectedResult, result)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ func Test_TerraformRecipe_KubernetesPostgres(t *testing.T) {
appName := "corerp-resources-terraform-pg-app"
envName := "corerp-resources-terraform-pg-env"
extenderName := "pgs-resources-terraform-pgsapp"
secretName := "pgs-hostsecret"
secretResourceName := appName + "/" + secretName
// secretName := "pgs-hostsecret"
// secretResourceName := appName + "/" + secretName
userName := "postgres"
password := "abc-123-hgd-@#$'"

Expand Down Expand Up @@ -174,10 +174,10 @@ func Test_TerraformRecipe_KubernetesPostgres(t *testing.T) {
{ID: "/planes/kubernetes/local/namespaces/corerp-resources-terraform-pg-app/providers/core/Service/postgres"},
},
},
{
/*{
Name: secretName,
Type: validation.SecretStoresResource,
},
},*/
},
},
K8sObjects: &validation.K8sObjectSet{
Expand All @@ -187,7 +187,7 @@ func Test_TerraformRecipe_KubernetesPostgres(t *testing.T) {
ValidateLabels(false),
validation.NewK8sPodForResource(appName, "postgres").
ValidateLabels(false),
validation.NewK8sSecretForResourceWithResourceName(secretResourceName),
// validation.NewK8sSecretForResourceWithResourceName(secretResourceName),
},
secretNamespace: {
validation.NewK8sSecretForResourceWithResourceName(secretPrefix + secretSuffix).
Expand Down

0 comments on commit 305d9df

Please sign in to comment.