Skip to content

Commit

Permalink
Update rad init to generate bicep config (#7664)
Browse files Browse the repository at this point in the history
# Description

Update rad init to generate bicep configuration
<img width="413" alt="Screenshot 2024-06-03 at 10 45 14 PM"
src="https://github.com/radius-project/radius/assets/42750942/1fbe214d-91e6-435c-aec0-57e57485a70d">


## Type of change

<!--

Please select **one** of the following options that describes your
change and delete the others. Clearly identifying the type of change you
are making will help us review your PR faster, and is used in authoring
release notes.

If you are making a bug fix or functionality change to Radius and do not
have an associated issue link please create one now.

-->

- 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).

<!--

Please update the following to link the associated issue. This is
required for some kinds of changes (see above).

-->

Fixes: #issue_number

---------

Signed-off-by: sk593 <[email protected]>
  • Loading branch information
sk593 committed Jul 22, 2024
1 parent 282c4c3 commit b87bc73
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ vendor/
# Radius scaffolding
app.bicep
.rad/
bicepconfig.json

# Build Output
bin/
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/cmd/radinit/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))))
}

Expand Down
38 changes: 36 additions & 2 deletions pkg/cli/setup/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
}
14 changes: 14 additions & 0 deletions pkg/cli/setup/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package setup

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -25,6 +26,8 @@ import (
"gopkg.in/yaml.v3"
)

const latest = "latest"

func Test_ScaffoldApplication_CreatesBothFiles(t *testing.T) {
directory := t.TempDir()

Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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))
}

0 comments on commit b87bc73

Please sign in to comment.