From 305d9df7d426a9190cfbf9667d3ed12183ab9284 Mon Sep 17 00:00:00 2001 From: lakshmimsft Date: Wed, 17 Jul 2024 16:14:13 -0700 Subject: [PATCH] cleanup upon bicep testing --- .../environment_conversion.go | 121 +++++------------- .../environment_conversion_test.go | 86 ++----------- .../resources/recipe_terraform_test.go | 10 +- 3 files changed, 50 insertions(+), 167 deletions(-) diff --git a/pkg/corerp/api/v20231001preview/environment_conversion.go b/pkg/corerp/api/v20231001preview/environment_conversion.go index c41b43a1d70..74769422882 100644 --- a/pkg/corerp/api/v20231001preview/environment_conversion.go +++ b/pkg/corerp/api/v20231001preview/environment_conversion.go @@ -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) @@ -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 { @@ -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 { @@ -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 { @@ -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, + }) } } @@ -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 diff --git a/pkg/corerp/api/v20231001preview/environment_conversion_test.go b/pkg/corerp/api/v20231001preview/environment_conversion_test.go index c9cb5172981..477f398805c 100644 --- a/pkg/corerp/api/v20231001preview/environment_conversion_test.go +++ b/pkg/corerp/api/v20231001preview/environment_conversion_test.go @@ -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) }) } } @@ -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"), @@ -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"), @@ -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) }) } diff --git a/test/functional-portable/corerp/noncloud/resources/recipe_terraform_test.go b/test/functional-portable/corerp/noncloud/resources/recipe_terraform_test.go index d61145fa5e8..2f30a720d55 100644 --- a/test/functional-portable/corerp/noncloud/resources/recipe_terraform_test.go +++ b/test/functional-portable/corerp/noncloud/resources/recipe_terraform_test.go @@ -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-@#$'" @@ -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{ @@ -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).