Skip to content

Commit

Permalink
adding comments
Browse files Browse the repository at this point in the history
Signed-off-by: Vishwanath Hiremath <[email protected]>
  • Loading branch information
vishwahiremat committed Feb 21, 2024
1 parent 2149aad commit f66fcc7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
69 changes: 54 additions & 15 deletions pkg/recipes/terraform/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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.
Expand All @@ -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())
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
6 changes: 6 additions & 0 deletions pkg/recipes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand All @@ -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
}

0 comments on commit f66fcc7

Please sign in to comment.