Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace wif dry-run flag with mode #671

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions cmd/ocm/gcp/create-wif-config.go
Original file line number Diff line number Diff line change
@@ -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())
20 changes: 16 additions & 4 deletions cmd/ocm/gcp/delete-wif-config.go
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion cmd/ocm/gcp/gcp.go
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions cmd/ocm/gcp/helpers.go
Original file line number Diff line number Diff line change
@@ -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] == "" {
20 changes: 16 additions & 4 deletions cmd/ocm/gcp/update-wif-config.go
Original file line number Diff line number Diff line change
@@ -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 {
JakobGray marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("Writing script files to %s", UpdateWifConfigOpts.TargetDir)
projectNumInt64, err := strconv.ParseInt(wifConfig.Gcp().ProjectNumber(), 10, 64)
if err != nil {