Skip to content

Commit

Permalink
Replace wif dry-run flag with mode (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGray authored Oct 15, 2024
1 parent 893acd5 commit 664b2c4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
20 changes: 16 additions & 4 deletions cmd/ocm/gcp/create-wif-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
var (
// CreateWifConfigOpts captures the options that affect creation of the workload identity configuration
CreateWifConfigOpts = options{
DryRun: false,
Mode: ModeAuto,
Name: "",
Project: "",
RolePrefix: "",
Expand Down Expand Up @@ -47,8 +47,16 @@ func NewCreateWorkloadIdentityConfiguration() *cobra.Command {
createWifConfigCmd.MarkPersistentFlagRequired("project")
createWifConfigCmd.PersistentFlags().StringVar(&CreateWifConfigOpts.RolePrefix, "role-prefix", "",
"Prefix for naming custom roles")
createWifConfigCmd.PersistentFlags().BoolVar(&CreateWifConfigOpts.DryRun, "dry-run", false,
"Skip creating objects, and just save what would have been created into files")

createWifConfigCmd.PersistentFlags().StringVarP(
&CreateWifConfigOpts.Mode,
"mode",
"m",
ModeAuto,
"How to perform the operation. Valid options are:\n"+
"auto (default): Resource changes will be automatic applied using the current GCP account\n"+
"manual: Commands necessary to modify GCP resources will be output to be run manually",
)
createWifConfigCmd.PersistentFlags().StringVar(&CreateWifConfigOpts.TargetDir, "output-dir", "",
"Directory to place generated files (defaults to current directory)")

Expand All @@ -63,6 +71,10 @@ func validationForCreateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, arg
return fmt.Errorf("Project is required")
}

if CreateWifConfigOpts.Mode != ModeAuto && CreateWifConfigOpts.Mode != ModeManual {
return fmt.Errorf("Invalid mode. Allowed values are %s", Modes)
}

var err error
CreateWifConfigOpts.TargetDir, err = getPathFromFlag(CreateWifConfigOpts.TargetDir)
if err != nil {
Expand Down Expand Up @@ -91,7 +103,7 @@ func createWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) e
return errors.Wrapf(err, "failed to create WIF config")
}

if CreateWifConfigOpts.DryRun {
if CreateWifConfigOpts.Mode == ModeManual {
log.Printf("Writing script files to %s", CreateWifConfigOpts.TargetDir)

projectNum, err := gcpClient.ProjectNumberFromId(ctx, wifConfig.Gcp().ProjectId())
Expand Down
20 changes: 16 additions & 4 deletions cmd/ocm/gcp/delete-wif-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
var (
// DeleteWifConfigOpts captures the options that affect creation of the workload identity configuration
DeleteWifConfigOpts = options{
DryRun: false,
Mode: ModeAuto,
TargetDir: "",
}
)
Expand All @@ -33,8 +33,15 @@ func NewDeleteWorkloadIdentityConfiguration() *cobra.Command {
PreRunE: validationForDeleteWorkloadIdentityConfigurationCmd,
}

deleteWifConfigCmd.PersistentFlags().BoolVar(&DeleteWifConfigOpts.DryRun, "dry-run", false,
"Skip creating objects, and just save what would have been created into files")
deleteWifConfigCmd.PersistentFlags().StringVarP(
&DeleteWifConfigOpts.Mode,
"mode",
"m",
ModeAuto,
"How to perform the operation. Valid options are:\n"+
"auto (default): Resource changes will be automatic applied using the current GCP account\n"+
"manual: Commands necessary to modify GCP resources will be output to be run manually",
)
deleteWifConfigCmd.PersistentFlags().StringVar(&DeleteWifConfigOpts.TargetDir, "output-dir", "",
"Directory to place generated files (defaults to current directory)")

Expand All @@ -43,6 +50,11 @@ func NewDeleteWorkloadIdentityConfiguration() *cobra.Command {

func validationForDeleteWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error {
var err error

if DeleteWifConfigOpts.Mode != ModeAuto && DeleteWifConfigOpts.Mode != ModeManual {
return fmt.Errorf("Invalid mode. Allowed values are %s", Modes)
}

DeleteWifConfigOpts.TargetDir, err = getPathFromFlag(DeleteWifConfigOpts.TargetDir)
if err != nil {
return err
Expand Down Expand Up @@ -77,7 +89,7 @@ func deleteWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) e
return errors.Wrapf(err, "failed to delete wif config %q", wifConfig.ID())
}

if DeleteWifConfigOpts.DryRun {
if DeleteWifConfigOpts.Mode == ModeManual {
log.Printf("Writing script files to %s", DeleteWifConfigOpts.TargetDir)

err := createDeleteScript(DeleteWifConfigOpts.TargetDir, wifConfig)
Expand Down
2 changes: 1 addition & 1 deletion cmd/ocm/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type options struct {
RolePrefix string
WorkloadIdentityPool string
WorkloadIdentityProvider string
DryRun bool
Mode string
}

// NewGcpCmd implements the "gcp" subcommand for the credentials provisioning
Expand Down
7 changes: 7 additions & 0 deletions cmd/ocm/gcp/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"github.com/pkg/errors"
)

const (
ModeAuto = "auto"
ModeManual = "manual"
)

var Modes = []string{ModeAuto, ModeManual}

// Checks for WIF config name or id in input
func wifKeyArgCheck(args []string) error {
if len(args) != 1 || args[0] == "" {
Expand Down
20 changes: 16 additions & 4 deletions cmd/ocm/gcp/update-wif-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

var (
UpdateWifConfigOpts = options{
DryRun: false,
Mode: ModeAuto,
TargetDir: "",
}
)
Expand All @@ -28,8 +28,15 @@ func NewUpdateWorkloadIdentityConfiguration() *cobra.Command {
PreRunE: validationForUpdateWorkloadIdentityConfigurationCmd,
}

updateWifConfigCmd.PersistentFlags().BoolVar(&UpdateWifConfigOpts.DryRun, "dry-run", false,
"Skip creating objects, and just save what would have been created into files")
updateWifConfigCmd.PersistentFlags().StringVarP(
&UpdateWifConfigOpts.Mode,
"mode",
"m",
ModeAuto,
"How to perform the operation. Valid options are:\n"+
"auto (default): Resource changes will be automatic applied using the current GCP account\n"+
"manual: Commands necessary to modify GCP resources will be output to be run manually",
)
updateWifConfigCmd.PersistentFlags().StringVar(&UpdateWifConfigOpts.TargetDir, "output-dir", "",
"Directory to place generated files (defaults to current directory)")

Expand All @@ -38,6 +45,11 @@ func NewUpdateWorkloadIdentityConfiguration() *cobra.Command {

func validationForUpdateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error {
var err error

if UpdateWifConfigOpts.Mode != ModeAuto && UpdateWifConfigOpts.Mode != ModeManual {
return fmt.Errorf("Invalid mode. Allowed values are %s", Modes)
}

UpdateWifConfigOpts.TargetDir, err = getPathFromFlag(UpdateWifConfigOpts.TargetDir)
if err != nil {
return err
Expand Down Expand Up @@ -71,7 +83,7 @@ func updateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) e
return errors.Wrapf(err, "failed to initiate GCP client")
}

if UpdateWifConfigOpts.DryRun {
if UpdateWifConfigOpts.Mode == ModeManual {
log.Printf("Writing script files to %s", UpdateWifConfigOpts.TargetDir)
projectNumInt64, err := strconv.ParseInt(wifConfig.Gcp().ProjectNumber(), 10, 64)
if err != nil {
Expand Down

0 comments on commit 664b2c4

Please sign in to comment.