Skip to content

Commit

Permalink
Make TemplateKind required (#5580)
Browse files Browse the repository at this point in the history
# Description

Added a new property for recipe templateKind in here
#5575. Switching the
property from optional to required and updating functional test
templates.

## Issue reference

Fixes Support multiple template types user story under
https://github.com/project-radius/radius/issues/4446.

## Checklist

Please make sure you've completed the relevant tasks for this PR, out of
the following list:

* [x] Code compiles correctly
* [x] Adds necessary unit tests for change
* [x] Adds necessary E2E tests for change
* [x] Unit tests passing
* [x] Extended the documentation / Created issue for it -- Created issue

---------

Co-authored-by: karishma-chawla <[email protected]>
  • Loading branch information
kachawla and kachawla authored May 22, 2023
1 parent d8cce54 commit 45e95ce
Show file tree
Hide file tree
Showing 31 changed files with 490 additions and 209 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
## EnvironmentRecipeProperties
### Properties
* **parameters**: any: Any object
* **templateKind**: string: Format of the template provided by the recipe. Allowed values: bicep
* **templateKind**: string (Required): Format of the template provided by the recipe. Allowed values: bicep
* **templatePath**: string (Required): Path to the template provided by the recipe. Currently only link to Azure Container Registry is supported.

## TrackedResourceTags
Expand Down
21 changes: 12 additions & 9 deletions pkg/cli/cmd/recipe/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/project-radius/radius/pkg/cli"
"github.com/project-radius/radius/pkg/cli/cmd/commonflags"
types "github.com/project-radius/radius/pkg/cli/cmd/recipe"
"github.com/project-radius/radius/pkg/cli/connections"
"github.com/project-radius/radius/pkg/cli/framework"
"github.com/project-radius/radius/pkg/cli/objectformats"
Expand Down Expand Up @@ -91,14 +92,22 @@ func (r *Runner) Run(ctx context.Context) error {
if err != nil {
return err
}
var envRecipes []EnvironmentRecipe
var envRecipes []types.EnvironmentRecipe
for link, recipes := range envResource.Properties.Recipes {
for recipeName, recipeDetails := range recipes {
envRecipes = append(envRecipes, EnvironmentRecipe{
recipe := types.EnvironmentRecipe{
Name: recipeName,
LinkType: link,
TemplatePath: *recipeDetails.TemplatePath,
})
}
// Check to ensure backwards compatibility with existing environments.
// Remove this in next release once users have migrated their existing environments.
// https://dev.azure.com/azure-octo/Incubations/_workitems/edit/7939
if recipeDetails.TemplateKind != nil {
recipe.TemplateKind = *recipeDetails.TemplateKind
}

envRecipes = append(envRecipes, recipe)
}
}
err = r.Output.WriteFormatted(r.Format, envRecipes, objectformats.GetEnvironmentRecipesTableFormat())
Expand All @@ -108,9 +117,3 @@ func (r *Runner) Run(ctx context.Context) error {

return nil
}

type EnvironmentRecipe struct {
Name string `json:"name,omitempty"`
LinkType string `json:"linkType,omitempty"`
TemplatePath string `json:"templatePath,omitempty"`
}
152 changes: 103 additions & 49 deletions pkg/cli/cmd/recipe/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/golang/mock/gomock"
v1 "github.com/project-radius/radius/pkg/armrpc/api/v1"
"github.com/project-radius/radius/pkg/cli/clients"
types "github.com/project-radius/radius/pkg/cli/cmd/recipe"
"github.com/project-radius/radius/pkg/cli/connections"
"github.com/project-radius/radius/pkg/cli/framework"
"github.com/project-radius/radius/pkg/cli/objectformats"
Expand Down Expand Up @@ -63,58 +64,111 @@ func Test_Validate(t *testing.T) {
}

func Test_Run(t *testing.T) {
t.Run("List recipes linked to the environment", func(t *testing.T) {
t.Run("Success", func(t *testing.T) {
ctrl := gomock.NewController(t)

envResource := v20220315privatepreview.EnvironmentResource{
ID: to.Ptr("/planes/radius/local/resourcegroups/kind-kind/providers/applications.core/environments/kind-kind"),
Name: to.Ptr("kind-kind"),
Type: to.Ptr("applications.core/environments"),
Location: to.Ptr(v1.LocationGlobal),
Properties: &v20220315privatepreview.EnvironmentProperties{
Recipes: map[string]map[string]*v20220315privatepreview.EnvironmentRecipeProperties{
linkrp.MongoDatabasesResourceType: {
"cosmosDB": {
TemplatePath: to.Ptr("testpublicrecipe.azurecr.io/bicep/modules/mongodatabases:v1"),
},
t.Run("List recipes linked to the environment - Success", func(t *testing.T) {
ctrl := gomock.NewController(t)

envResource := v20220315privatepreview.EnvironmentResource{
ID: to.Ptr("/planes/radius/local/resourcegroups/kind-kind/providers/applications.core/environments/kind-kind"),
Name: to.Ptr("kind-kind"),
Type: to.Ptr("applications.core/environments"),
Location: to.Ptr(v1.LocationGlobal),
Properties: &v20220315privatepreview.EnvironmentProperties{
Recipes: map[string]map[string]*v20220315privatepreview.EnvironmentRecipeProperties{
linkrp.MongoDatabasesResourceType: {
"cosmosDB": {
TemplateKind: to.Ptr(types.TemplateKindBicep),
TemplatePath: to.Ptr("testpublicrecipe.azurecr.io/bicep/modules/mongodatabases:v1"),
},
},
},
}
recipes := []EnvironmentRecipe{
{
Name: "cosmosDB",
LinkType: linkrp.MongoDatabasesResourceType,
TemplatePath: "testpublicrecipe.azurecr.io/bicep/modules/mongodatabases:v1",
},
}

appManagementClient := clients.NewMockApplicationsManagementClient(ctrl)
appManagementClient.EXPECT().
GetEnvDetails(gomock.Any(), gomock.Any()).
Return(envResource, nil).Times(1)

outputSink := &output.MockOutput{}

runner := &Runner{
ConnectionFactory: &connections.MockFactory{ApplicationsManagementClient: appManagementClient},
Output: outputSink,
Workspace: &workspaces.Workspace{},
Format: "table",
}

err := runner.Run(context.Background())
require.NoError(t, err)

expected := []any{
output.FormattedOutput{
Format: "table",
Obj: recipes,
Options: objectformats.GetEnvironmentRecipesTableFormat(),
},
}
recipes := []types.EnvironmentRecipe{
{
Name: "cosmosDB",
LinkType: linkrp.MongoDatabasesResourceType,
TemplateKind: types.TemplateKindBicep,
TemplatePath: "testpublicrecipe.azurecr.io/bicep/modules/mongodatabases:v1",
},
}

appManagementClient := clients.NewMockApplicationsManagementClient(ctrl)
appManagementClient.EXPECT().
GetEnvDetails(gomock.Any(), gomock.Any()).
Return(envResource, nil).Times(1)

outputSink := &output.MockOutput{}

runner := &Runner{
ConnectionFactory: &connections.MockFactory{ApplicationsManagementClient: appManagementClient},
Output: outputSink,
Workspace: &workspaces.Workspace{},
Format: "table",
}

err := runner.Run(context.Background())
require.NoError(t, err)

expected := []any{
output.FormattedOutput{
Format: "table",
Obj: recipes,
Options: objectformats.GetEnvironmentRecipesTableFormat(),
},
}
require.Equal(t, expected, outputSink.Writes)
})

t.Run("List recipes linked to the environment - empty template kind", func(t *testing.T) {
ctrl := gomock.NewController(t)

envResource := v20220315privatepreview.EnvironmentResource{
ID: to.Ptr("/planes/radius/local/resourcegroups/kind-kind/providers/applications.core/environments/kind-kind"),
Name: to.Ptr("kind-kind"),
Type: to.Ptr("applications.core/environments"),
Location: to.Ptr(v1.LocationGlobal),
Properties: &v20220315privatepreview.EnvironmentProperties{
Recipes: map[string]map[string]*v20220315privatepreview.EnvironmentRecipeProperties{
linkrp.MongoDatabasesResourceType: {
"cosmosDB": {
TemplatePath: to.Ptr("testpublicrecipe.azurecr.io/bicep/modules/mongodatabases:v1"),
},
},
},
}
require.Equal(t, expected, outputSink.Writes)
})
},
}
recipes := []types.EnvironmentRecipe{
{
Name: "cosmosDB",
LinkType: linkrp.MongoDatabasesResourceType,
TemplatePath: "testpublicrecipe.azurecr.io/bicep/modules/mongodatabases:v1",
},
}

appManagementClient := clients.NewMockApplicationsManagementClient(ctrl)
appManagementClient.EXPECT().
GetEnvDetails(gomock.Any(), gomock.Any()).
Return(envResource, nil).Times(1)

outputSink := &output.MockOutput{}

runner := &Runner{
ConnectionFactory: &connections.MockFactory{ApplicationsManagementClient: appManagementClient},
Output: outputSink,
Workspace: &workspaces.Workspace{},
Format: "table",
}

err := runner.Run(context.Background())
require.NoError(t, err)

expected := []any{
output.FormattedOutput{
Format: "table",
Obj: recipes,
Options: objectformats.GetEnvironmentRecipesTableFormat(),
},
}
require.Equal(t, expected, outputSink.Writes)
})
}
26 changes: 18 additions & 8 deletions pkg/cli/cmd/recipe/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ You can specify parameters using the '--parameter' flag ('-p' for short). Parame
`,
Example: `
# Add a recipe to an environment
rad recipe register cosmosdb -e env_name -w workspace --template-path template_path --link-type Applications.Link/mongoDatabases
rad recipe register cosmosdb -e env_name -w workspace --template-kind bicep --template-path template_path --link-type Applications.Link/mongoDatabases
# Specify a parameter
rad recipe register cosmosdb -e env_name -w workspace --template-path template_path --link-type Applications.Link/mongoDatabases --parameters throughput=400
rad recipe register cosmosdb -e env_name -w workspace --template-kind bicep --template-path template_path --link-type Applications.Link/mongoDatabases --parameters throughput=400
# specify multiple parameters using a JSON parameter file
rad recipe register cosmosdb -e env_name -w workspace --template-path template_path --link-type Applications.Link/mongoDatabases --parameters @myfile.json
rad recipe register cosmosdb -e env_name -w workspace --template-kind bicep --template-path template_path --link-type Applications.Link/mongoDatabases --parameters @myfile.json
`,
Args: cobra.ExactArgs(1),
RunE: framework.RunCommand(runner),
Expand All @@ -52,6 +52,8 @@ rad recipe register cosmosdb -e env_name -w workspace --template-path template_p
commonflags.AddWorkspaceFlag(cmd)
commonflags.AddResourceGroupFlag(cmd)
commonflags.AddEnvironmentNameFlag(cmd)
cmd.Flags().String("template-kind", "", "specify the kind for the template provided by the recipe.")
_ = cmd.MarkFlagRequired("template-kind")
cmd.Flags().String("template-path", "", "specify the path to the template provided by the recipe.")
_ = cmd.MarkFlagRequired("template-path")
cmd.Flags().String("link-type", "", "specify the type of the link this recipe can be consumed by")
Expand All @@ -67,6 +69,7 @@ type Runner struct {
ConnectionFactory connections.Factory
Output output.Interface
Workspace *workspaces.Workspace
TemplateKind string
TemplatePath string
LinkType string
RecipeName string
Expand Down Expand Up @@ -97,10 +100,11 @@ func (r *Runner) Validate(cmd *cobra.Command, args []string) error {
}
r.Workspace.Environment = environment

templatePath, err := requireTemplatePath(cmd)
templateKind, templatePath, err := requireRecipeProperties(cmd)
if err != nil {
return err
}
r.TemplateKind = templateKind
r.TemplatePath = templatePath

linkType, err := cli.RequireLinkType(cmd)
Expand Down Expand Up @@ -147,6 +151,7 @@ func (r *Runner) Run(ctx context.Context) error {
}

properties := &corerp.EnvironmentRecipeProperties{
TemplateKind: &r.TemplateKind,
TemplatePath: &r.TemplatePath,
Parameters: bicep.ConvertToMapStringInterface(r.Parameters),
}
Expand All @@ -168,11 +173,16 @@ func (r *Runner) Run(ctx context.Context) error {
return nil
}

func requireTemplatePath(cmd *cobra.Command) (string, error) {
templatePath, err := cmd.Flags().GetString("template-path")
func requireRecipeProperties(cmd *cobra.Command) (templateKind, templatePath string, err error) {
templateKind, err = cmd.Flags().GetString("template-kind")
if err != nil {
return templatePath, err
return "", "", err
}

return templatePath, nil
templatePath, err = cmd.Flags().GetString("template-path")
if err != nil {
return "", "", err
}

return templateKind, templatePath, nil
}
Loading

0 comments on commit 45e95ce

Please sign in to comment.