From b87bc730bc63d2cf05d357254b17a83bbf126c92 Mon Sep 17 00:00:00 2001 From: Shruthi Kumar Date: Mon, 22 Jul 2024 10:05:49 -0700 Subject: [PATCH] Update rad init to generate bicep config (#7664) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Update rad init to generate bicep configuration Screenshot 2024-06-03 at 10 45 14 PM ## Type of change - This pull request fixes a bug in Radius and has an approved issue (issue link required). - This pull request adds or changes features of Radius and has an approved issue (issue link required). - This pull request is a minor refactor, code cleanup, test improvement, or other maintenance task and doesn't change the functionality of Radius (issue link optional). Fixes: #issue_number --------- Signed-off-by: sk593 --- .gitignore | 1 + pkg/cli/cmd/radinit/display.go | 1 + pkg/cli/setup/application.go | 38 +++++++++++++++++++++++++++++-- pkg/cli/setup/application_test.go | 14 ++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e5db2846b64..de5d23cab40 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ vendor/ # Radius scaffolding app.bicep .rad/ +bicepconfig.json # Build Output bin/ diff --git a/pkg/cli/cmd/radinit/display.go b/pkg/cli/cmd/radinit/display.go index ead182de57b..cd095362ac9 100644 --- a/pkg/cli/cmd/radinit/display.go +++ b/pkg/cli/cmd/radinit/display.go @@ -243,6 +243,7 @@ func (m *summaryModel) View() string { message.WriteString(summaryApplicationHeadingIcon) message.WriteString(fmt.Sprintf(summaryApplicationScaffoldHeadingFmt, highlight(options.Application.Name))) message.WriteString(fmt.Sprintf(summaryApplicationScaffoldFile, highlight("app.bicep"))) + message.WriteString(fmt.Sprintf(summaryApplicationScaffoldFile, highlight("bicepconfig.json"))) message.WriteString(fmt.Sprintf(summaryApplicationScaffoldFile, highlight(filepath.Join(".rad", "rad.yaml")))) } diff --git a/pkg/cli/setup/application.go b/pkg/cli/setup/application.go index f1122a7aae0..00ab1024833 100644 --- a/pkg/cli/setup/application.go +++ b/pkg/cli/setup/application.go @@ -20,10 +20,12 @@ import ( "fmt" "os" "path/filepath" + + "github.com/radius-project/radius/pkg/version" ) const ( - appBicepTemplate = `import radius as radius + appBicepTemplate = `extension radius @description('The Radius Application ID. Injected automatically by the rad CLI.') param application string @@ -47,6 +49,18 @@ resource demo 'Applications.Core/containers@2023-10-01-preview' = { radYamlTemplate = `workspace: application: %q ` // Trailing newline intentional. + + bicepConfigTemplate = `{ + "experimentalFeaturesEnabled": { + "extensibility": true, + "extensionRegistry": true, + "dynamicTypeLoading": true + }, + "extensions": { + "radius": "br:biceptypes.azurecr.io/radius:%s", + "aws": "br:biceptypes.azurecr.io/aws:%s" + } +}` ) // ScaffoldApplication creates a working sample application in the provided directory @@ -60,7 +74,7 @@ func ScaffoldApplication(directory string, name string) error { return err } - // We NEVER overwrite app.bicep if it exists. We assume the user might have changed it, and don't + // We NEVER overwrite app.bicep or the bicepconfig.json if it exists. We assume the user might have changed it, and don't // want them to lose their content. // // On the other hand, we ALWAYS overwrite rad.yaml if it exists. We assume that the reason why @@ -76,6 +90,17 @@ func ScaffoldApplication(directory string, name string) error { return err } + bicepConfigFilepath := filepath.Join(directory, "bicepconfig.json") + _, err = os.Stat(bicepConfigFilepath) + if os.IsNotExist(err) { + err = os.WriteFile(bicepConfigFilepath, []byte(getVersionedBicepConfig()), 0644) + if err != nil { + return err + } + } else if err != nil { + return err + } + radYamlFilepath := filepath.Join(directory, ".rad", "rad.yaml") err = os.WriteFile(radYamlFilepath, []byte(fmt.Sprintf(radYamlTemplate, name)), 0644) if err != nil { @@ -84,3 +109,12 @@ func ScaffoldApplication(directory string, name string) error { return nil } + +func getVersionedBicepConfig() string { + tag := version.Channel() + if version.IsEdgeChannel() { + tag = "latest" + } + + return fmt.Sprintf(bicepConfigTemplate, tag, tag) +} diff --git a/pkg/cli/setup/application_test.go b/pkg/cli/setup/application_test.go index 9b732985f4d..3291df645f0 100644 --- a/pkg/cli/setup/application_test.go +++ b/pkg/cli/setup/application_test.go @@ -17,6 +17,7 @@ limitations under the License. package setup import ( + "fmt" "os" "path/filepath" "testing" @@ -25,6 +26,8 @@ import ( "gopkg.in/yaml.v3" ) +const latest = "latest" + func Test_ScaffoldApplication_CreatesBothFiles(t *testing.T) { directory := t.TempDir() @@ -33,6 +36,7 @@ func Test_ScaffoldApplication_CreatesBothFiles(t *testing.T) { require.FileExists(t, filepath.Join(directory, ".rad", "rad.yaml")) require.FileExists(t, filepath.Join(directory, "app.bicep")) + require.FileExists(t, filepath.Join(directory, "bicepconfig.json")) b, err := os.ReadFile(filepath.Join(directory, ".rad", "rad.yaml")) require.NoError(t, err) @@ -51,6 +55,10 @@ func Test_ScaffoldApplication_CreatesBothFiles(t *testing.T) { b, err = os.ReadFile(filepath.Join(directory, "app.bicep")) require.NoError(t, err) require.Equal(t, appBicepTemplate, string(b)) + + b, err = os.ReadFile(filepath.Join(directory, "bicepconfig.json")) + require.NoError(t, err) + require.Equal(t, fmt.Sprintf(bicepConfigTemplate, latest, latest), string(b)) } func Test_ScaffoldApplication_KeepsAppBicepButWritesRadYaml(t *testing.T) { @@ -63,6 +71,8 @@ func Test_ScaffoldApplication_KeepsAppBicepButWritesRadYaml(t *testing.T) { require.NoError(t, err) err = os.WriteFile(filepath.Join(directory, "app.bicep"), []byte("something else"), 0644) require.NoError(t, err) + err = os.WriteFile(filepath.Join(directory, "bicepconfig.json"), []byte("something else"), 0644) + require.NoError(t, err) err = ScaffoldApplication(directory, "cool-application") require.NoError(t, err) @@ -87,4 +97,8 @@ func Test_ScaffoldApplication_KeepsAppBicepButWritesRadYaml(t *testing.T) { b, err = os.ReadFile(filepath.Join(directory, "app.bicep")) require.NoError(t, err) require.Equal(t, "something else", string(b)) + + b, err = os.ReadFile(filepath.Join(directory, "bicepconfig.json")) + require.NoError(t, err) + require.Equal(t, "something else", string(b)) }