From 573766b4022be41176e5ed80634a9addbf551a13 Mon Sep 17 00:00:00 2001 From: Jakob Gray <20209054+JakobGray@users.noreply.github.com> Date: Thu, 10 Oct 2024 17:22:33 -0400 Subject: [PATCH] Replace wif dry-run flag with mode --- cmd/ocm/gcp/create-wif-config.go | 20 ++++++++++++++++---- cmd/ocm/gcp/delete-wif-config.go | 20 ++++++++++++++++---- cmd/ocm/gcp/gcp.go | 2 +- cmd/ocm/gcp/helpers.go | 7 +++++++ cmd/ocm/gcp/update-wif-config.go | 20 ++++++++++++++++---- 5 files changed, 56 insertions(+), 13 deletions(-) diff --git a/cmd/ocm/gcp/create-wif-config.go b/cmd/ocm/gcp/create-wif-config.go index 67cb7808..c01490f0 100644 --- a/cmd/ocm/gcp/create-wif-config.go +++ b/cmd/ocm/gcp/create-wif-config.go @@ -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: "", @@ -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)") @@ -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 { @@ -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()) diff --git a/cmd/ocm/gcp/delete-wif-config.go b/cmd/ocm/gcp/delete-wif-config.go index 4fb4f96d..6f268679 100644 --- a/cmd/ocm/gcp/delete-wif-config.go +++ b/cmd/ocm/gcp/delete-wif-config.go @@ -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: "", } ) @@ -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)") @@ -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 @@ -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) diff --git a/cmd/ocm/gcp/gcp.go b/cmd/ocm/gcp/gcp.go index 7486dc6e..26ebfe8e 100644 --- a/cmd/ocm/gcp/gcp.go +++ b/cmd/ocm/gcp/gcp.go @@ -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 diff --git a/cmd/ocm/gcp/helpers.go b/cmd/ocm/gcp/helpers.go index 6298d28c..88330ab0 100644 --- a/cmd/ocm/gcp/helpers.go +++ b/cmd/ocm/gcp/helpers.go @@ -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] == "" { diff --git a/cmd/ocm/gcp/update-wif-config.go b/cmd/ocm/gcp/update-wif-config.go index 8e3945f2..b09680b9 100644 --- a/cmd/ocm/gcp/update-wif-config.go +++ b/cmd/ocm/gcp/update-wif-config.go @@ -14,7 +14,7 @@ import ( var ( UpdateWifConfigOpts = options{ - DryRun: false, + Mode: ModeAuto, TargetDir: "", } ) @@ -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)") @@ -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 @@ -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 {