From f66fcc7d1dd4766c90c8419e5550bf1f1300afe2 Mon Sep 17 00:00:00 2001 From: Vishwanath Hiremath Date: Wed, 21 Feb 2024 02:25:30 -0800 Subject: [PATCH] adding comments Signed-off-by: Vishwanath Hiremath --- pkg/recipes/terraform/config/config_test.go | 69 +++++++++++++++---- .../testdata/module-private-git-repo.tf.json | 11 +++ pkg/recipes/types.go | 6 ++ 3 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 pkg/recipes/terraform/config/testdata/module-private-git-repo.tf.json diff --git a/pkg/recipes/terraform/config/config_test.go b/pkg/recipes/terraform/config/config_test.go index 53d91cac24..a9f1f90174 100644 --- a/pkg/recipes/terraform/config/config_test.go +++ b/pkg/recipes/terraform/config/config_test.go @@ -117,6 +117,7 @@ func Test_NewConfig(t *testing.T) { desc string moduleName string envdef *recipes.EnvironmentDefinition + envConfig *recipes.Configuration metadata *recipes.ResourceMetadata expectedConfigFile string }{ @@ -174,16 +175,49 @@ func Test_NewConfig(t *testing.T) { }, expectedConfigFile: "testdata/module-emptytemplateversion.tf.json", }, + { + desc: "git private repo module", + moduleName: testRecipeName, + envdef: &recipes.EnvironmentDefinition{ + Name: testRecipeName, + TemplatePath: "git::https://dev.azure.com/project/module", + Parameters: envParams, + }, + envConfig: &recipes.Configuration{ + RecipeConfig: datamodel.RecipeConfigProperties{ + Terraform: datamodel.TerraformConfigProperties{ + Authentication: datamodel.AuthConfig{ + Git: datamodel.GitAuthConfig{ + PAT: map[string]datamodel.SecretConfig{ + "dev.azure.com": { + Secret: "secret-store1", + }, + }, + }, + }, + }, + }, + }, + metadata: &recipes.ResourceMetadata{ + Name: testRecipeName, + Parameters: resourceParams, + EnvironmentID: "/planes/radius/local/resourceGroups/test-group/providers/Applications.Environments/testEnv/env", + ApplicationID: "/planes/radius/local/resourceGroups/test-group/providers/Applications.Applications/testApp/app", + ResourceID: "/planes/radius/local/resourceGroups/test-group/providers/Applications.Datastores/redisCaches/redis", + }, + expectedConfigFile: "testdata/module-private-git-repo.tf.json", + }, } for _, tc := range configTests { t.Run(tc.desc, func(t *testing.T) { workingDir := t.TempDir() - tfconfig, _ := New(context.Background(), testRecipeName, tc.envdef, tc.metadata, nil) + tfconfig, err := New(context.Background(), testRecipeName, tc.envdef, tc.metadata, tc.envConfig) + require.NoError(t, err) // validate generated config - err := tfconfig.Save(testcontext.New(t), workingDir) + err = tfconfig.Save(testcontext.New(t), workingDir) require.NoError(t, err) actualConfig, err := os.ReadFile(getMainConfigFilePath(workingDir)) require.NoError(t, err) @@ -273,9 +307,9 @@ func Test_AddRecipeContext(t *testing.T) { ctx := testcontext.New(t) workingDir := t.TempDir() - tfconfig, _ := New(context.Background(), testRecipeName, tc.envdef, tc.metadata, nil) - - err := tfconfig.AddRecipeContext(ctx, tc.moduleName, tc.recipeContext) + tfconfig, err := New(context.Background(), testRecipeName, tc.envdef, tc.metadata, nil) + require.NoError(t, err) + err = tfconfig.AddRecipeContext(ctx, tc.moduleName, tc.recipeContext) if tc.err == "" { require.NoError(t, err) } else { @@ -417,14 +451,15 @@ func Test_AddProviders(t *testing.T) { ctx := testcontext.New(t) workingDir := t.TempDir() - tfconfig, _ := New(context.Background(), testRecipeName, &envRecipe, &resourceRecipe, nil) + tfconfig, err := New(context.Background(), testRecipeName, &envRecipe, &resourceRecipe, nil) + require.NoError(t, err) for _, p := range tc.expectedProviders { mProvider.EXPECT().BuildConfig(ctx, &tc.envConfig).Times(1).Return(p, nil) } if tc.Err != nil { mProvider.EXPECT().BuildConfig(ctx, &tc.envConfig).Times(1).Return(nil, tc.Err) } - err := tfconfig.AddProviders(ctx, tc.requiredProviders, supportedProviders, &tc.envConfig) + err = tfconfig.AddProviders(ctx, tc.requiredProviders, supportedProviders, &tc.envConfig) if tc.Err != nil { require.ErrorContains(t, err, tc.Err.Error()) return @@ -477,9 +512,10 @@ func Test_AddOutputs(t *testing.T) { for _, tc := range tests { t.Run(tc.desc, func(t *testing.T) { - tfconfig, _ := New(context.Background(), testRecipeName, &envRecipe, &resourceRecipe, nil) + tfconfig, err := New(context.Background(), testRecipeName, &envRecipe, &resourceRecipe, nil) + require.NoError(t, err) - err := tfconfig.AddOutputs(tc.moduleName) + err = tfconfig.AddOutputs(tc.moduleName) if tc.expectedErr { require.Error(t, err) require.Nil(t, tfconfig.Output) @@ -506,9 +542,10 @@ func Test_Save_overwrite(t *testing.T) { ctx := testcontext.New(t) testDir := t.TempDir() envRecipe, resourceRecipe := getTestInputs() - tfconfig, _ := New(context.Background(), testRecipeName, &envRecipe, &resourceRecipe, nil) + tfconfig, err := New(context.Background(), testRecipeName, &envRecipe, &resourceRecipe, nil) + require.NoError(t, err) - err := tfconfig.Save(ctx, testDir) + err = tfconfig.Save(ctx, testDir) require.NoError(t, err) err = tfconfig.Save(ctx, testDir) @@ -518,10 +555,11 @@ func Test_Save_overwrite(t *testing.T) { func Test_Save_ConfigFileReadOnly(t *testing.T) { testDir := t.TempDir() envRecipe, resourceRecipe := getTestInputs() - tfconfig, _ := New(context.Background(), testRecipeName, &envRecipe, &resourceRecipe, nil) + tfconfig, err := New(context.Background(), testRecipeName, &envRecipe, &resourceRecipe, nil) + require.NoError(t, err) // Create a test configuration file with read only permission. - err := os.WriteFile(getMainConfigFilePath(testDir), []byte(`{"module":{}}`), 0400) + err = os.WriteFile(getMainConfigFilePath(testDir), []byte(`{"module":{}}`), 0400) require.NoError(t, err) // Assert that Save returns an error. @@ -534,9 +572,10 @@ func Test_Save_InvalidWorkingDir(t *testing.T) { testDir := filepath.Join("invalid", uuid.New().String()) envRecipe, resourceRecipe := getTestInputs() - tfconfig, _ := New(context.Background(), testRecipeName, &envRecipe, &resourceRecipe, nil) + tfconfig, err := New(context.Background(), testRecipeName, &envRecipe, &resourceRecipe, nil) + require.NoError(t, err) - err := tfconfig.Save(testcontext.New(t), testDir) + err = tfconfig.Save(testcontext.New(t), testDir) require.Error(t, err) require.Equal(t, fmt.Sprintf("error creating file: open %s/main.tf.json: no such file or directory", testDir), err.Error()) } diff --git a/pkg/recipes/terraform/config/testdata/module-private-git-repo.tf.json b/pkg/recipes/terraform/config/testdata/module-private-git-repo.tf.json new file mode 100644 index 0000000000..2f006e57ca --- /dev/null +++ b/pkg/recipes/terraform/config/testdata/module-private-git-repo.tf.json @@ -0,0 +1,11 @@ +{ + "terraform": null, + "module": { + "redis-azure": { + "redis_cache_name": "redis-test", + "resource_group_name": "test-rg", + "sku": "P", + "source": "git::https://env-app-redis-dev.azure.com/project/module" + } + } +} \ No newline at end of file diff --git a/pkg/recipes/types.go b/pkg/recipes/types.go index 9d1c505b6b..e607129dba 100644 --- a/pkg/recipes/types.go +++ b/pkg/recipes/types.go @@ -142,6 +142,7 @@ func (ro *RecipeOutput) PrepareRecipeResponse(resultValue map[string]any) error return nil } +// GetSecretStoreID returns secretstore resource ID associated with git private terraform repository source. func GetSecretStoreID(envConfig Configuration, templatePath string) (string, error) { url, err := GetGitURL(templatePath) if err != nil { @@ -151,6 +152,7 @@ func GetSecretStoreID(envConfig Configuration, templatePath string) (string, err return envConfig.RecipeConfig.Terraform.Authentication.Git.PAT[strings.TrimPrefix(url.Hostname(), "www.")].Secret, nil } +// GetGitURL returns git url from generic git module source. func GetGitURL(templatePath string) (*url.URL, error) { paths := strings.Split(templatePath, "git::") gitUrl := paths[len(paths)-1] @@ -162,18 +164,22 @@ func GetGitURL(templatePath string) (*url.URL, error) { return url, nil } +// GetEnvAppResourceNames returns the application, environment and resource names. func GetEnvAppResourceNames(resourceMetadata *ResourceMetadata) (string, string, string, error) { app, err := resources.ParseResource(resourceMetadata.ApplicationID) if err != nil { return "", "", "", err } + env, err := resources.ParseResource(resourceMetadata.EnvironmentID) if err != nil { return "", "", "", err } + resource, err := resources.ParseResource(resourceMetadata.ResourceID) if err != nil { return "", "", "", err } + return env.Name(), app.Name(), resource.Name(), nil }