Skip to content

Commit

Permalink
OCM-11868 | feat: Adding --template-dir flag & TEMPLATE_DIR env var
Browse files Browse the repository at this point in the history
  • Loading branch information
den-rgb authored and openshift-cherrypick-robot committed Oct 25, 2024
1 parent deafc8f commit f16d24f
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 18 deletions.
9 changes: 7 additions & 2 deletions cmd/create/network/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewNetworkCommand() *cobra.Command {
interactive.AddModeFlag(cmd)

cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
templateDir := "cmd/create/network/templates"
templateDir := options.TemplateDir
err := filepath.WalkDir(templateDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
Expand Down Expand Up @@ -99,11 +99,16 @@ func NetworkRunner(userOptions *opts.NetworkUserOptions) rosa.CommandRunner {
for _, arg := range argv {
if !strings.HasPrefix(arg, "--param") {
templateCommand = arg
if templateCommand == "rosa-quickstart-default-vpc" {
r.Logger.Debugf("Template command not provided, using default template %s", templateCommand)
}
break
}
}

templateFile := helper.SelectTemplate(templateCommand)
templateDir := options.args.TemplateDir

templateFile := helper.SelectTemplate(templateDir, templateCommand)
if templateFile == "" {
return r.Reporter.Errorf("No suitable template found")
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/create/network/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ var _ = Describe("Validation functions", func() {
Context("selectTemplate", func() {
It("input template to directory path", func() {
templateFile := "test-template.yaml"
templateSelected := network.SelectTemplate(templateFile)
templateDir := "cmd/create/network/templates"
templateSelected := network.SelectTemplate(templateDir, templateFile)
Expect(templateSelected).To(Equal("cmd/create/network/templates/test-template.yaml/cloudformation.yaml"))
})
})
Expand Down
5 changes: 3 additions & 2 deletions cmd/login/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/openshift/rosa/cmd/logout"
"github.com/openshift/rosa/pkg/arguments"
"github.com/openshift/rosa/pkg/config"
"github.com/openshift/rosa/pkg/constants"
"github.com/openshift/rosa/pkg/fedramp"
"github.com/openshift/rosa/pkg/interactive"
"github.com/openshift/rosa/pkg/ocm"
Expand Down Expand Up @@ -268,9 +269,9 @@ func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command, argv []string) error {

// Verify environment variables:
if !haveReqs && !reAttempt && !fedramp.Enabled() {
token = os.Getenv("ROSA_TOKEN")
token = os.Getenv(constants.RosaToken)
if token == "" {
token = os.Getenv("OCM_TOKEN")
token = os.Getenv(constants.OcmToken)
}
haveReqs = token != ""
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- name: mode
- name: param
- name: template-dir
3 changes: 2 additions & 1 deletion pkg/aws/region/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/spf13/pflag"

"github.com/openshift/rosa/pkg/constants"
"github.com/openshift/rosa/pkg/helper"
)

Expand All @@ -41,7 +42,7 @@ func Region() string {
if helper.HandleEscapedEmptyString(region) != "" {
return region
}
awsRegion := os.Getenv("AWS_REGION")
awsRegion := os.Getenv(constants.AwsRegion)
if helper.HandleEscapedEmptyString(awsRegion) != "" {
return awsRegion
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
sdk "github.com/openshift-online/ocm-sdk-go"
"github.com/openshift-online/ocm-sdk-go/authentication/securestore"

"github.com/openshift/rosa/pkg/constants"
"github.com/openshift/rosa/pkg/debug"
"github.com/openshift/rosa/pkg/properties"
)
Expand Down Expand Up @@ -224,7 +225,7 @@ func Remove() error {
// use the XDG config directory.
func Location() (path string, err error) {
// Use env variable
if ocmconfig := os.Getenv("OCM_CONFIG"); ocmconfig != "" {
if ocmconfig := os.Getenv(constants.OcmConfig); ocmconfig != "" {
return ocmconfig, nil
}

Expand Down
9 changes: 8 additions & 1 deletion pkg/constants/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
package constants

const OcmConfig = "OCM_CONFIG"
const (
RosaToken = "ROSA_TOKEN" // Token for ROSA authentication
OcmToken = "OCM_TOKEN" // Token for OCM authentication
AwsProfile = "AWS_PROFILE" // AWS CLI profile to use
AwsRegion = "AWS_REGION" // AWS region to use
OcmConfig = "OCM_CONFIG" // Path to OCM configuration file
OcmTemplateDir = "OCM_TEMPLATE_DIR" // Directory for OCM cloudformation templates
)
4 changes: 2 additions & 2 deletions pkg/network/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func ParseParams(params []string) (map[string]string, map[string]string, error)
}

// SelectTemplate selects the appropriate template file based on the template name
func SelectTemplate(command string) string {
return fmt.Sprintf("cmd/create/network/templates/%s/cloudformation.yaml", command)
func SelectTemplate(templateDir, command string) string {
return fmt.Sprintf("%s/%s/cloudformation.yaml", templateDir, command)
}

func formatParams(params map[string]string) string {
Expand Down
3 changes: 2 additions & 1 deletion pkg/network/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ var _ = Describe("Helper Functions", func() {
Describe("SelectTemplate", func() {
It("should return the correct template file path", func() {
templateName := "test-template"
templateDir := "cmd/create/network/templates"
expectedTemplateFile := "cmd/create/network/templates/test-template/cloudformation.yaml"
Expect(SelectTemplate(templateName)).To(Equal(expectedTemplateFile))
Expect(SelectTemplate(templateDir, templateName)).To(Equal(expectedTemplateFile))
})
})

Expand Down
27 changes: 22 additions & 5 deletions pkg/options/network/create.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package Network
package network

import (
"os"

"github.com/spf13/cobra"

"github.com/openshift/rosa/pkg/constants"
"github.com/openshift/rosa/pkg/helper"
"github.com/openshift/rosa/pkg/reporter"
)

Expand All @@ -17,10 +21,12 @@ const (
` --param Name=quickstart-stack --param AvailabilityZoneCount=1 --param VpcCidr=10.0.0.0/16` +
"\n\n" + ` # To delete the AWS cloudformation stack` +
"\n" + ` aws cloudformation delete-stack --stack-name <name> --region <region>`
defaultTemplateDir = "cmd/create/network/templates"
)

type NetworkUserOptions struct {
Params []string
Params []string
TemplateDir string
}

type NetworkOptions struct {
Expand All @@ -29,13 +35,23 @@ type NetworkOptions struct {
}

func NewNetworkUserOptions() *NetworkUserOptions {
return &NetworkUserOptions{}
options := &NetworkUserOptions{}

// Set template directory from environment variable or use default
templateDir := os.Getenv(constants.OcmTemplateDir)
if helper.HandleEscapedEmptyString(templateDir) != "" {
options.TemplateDir = templateDir
} else {
options.TemplateDir = defaultTemplateDir
}

return options
}

func NewNetworkOptions() *NetworkOptions {
return &NetworkOptions{
reporter: reporter.CreateReporter(),
args: &NetworkUserOptions{},
args: NewNetworkUserOptions(),
}
}

Expand All @@ -56,7 +72,8 @@ func BuildNetworkCommandWithOptions() (*cobra.Command, *NetworkUserOptions) {
}

flags := cmd.Flags()

flags.StringVar(&options.TemplateDir, "template-dir", defaultTemplateDir, "Use a specific template directory,"+
" overriding the OCM_TEMPLATE_DIR environment variable.")
flags.StringArrayVar(&options.Params, "param", []string{}, "List of parameters")

return cmd, options
Expand Down
2 changes: 1 addition & 1 deletion pkg/options/network/create_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Network
package network

import (
. "github.com/onsi/ginkgo/v2"
Expand Down
2 changes: 1 addition & 1 deletion pkg/options/network/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Network
package network

import (
"testing"
Expand Down

0 comments on commit f16d24f

Please sign in to comment.