diff --git a/cmd/ocm/gcp/delete-wif-config.go b/cmd/ocm/gcp/delete-wif-config.go index 51c044b7..d2f3f854 100644 --- a/cmd/ocm/gcp/delete-wif-config.go +++ b/cmd/ocm/gcp/delete-wif-config.go @@ -42,15 +42,15 @@ func NewDeleteWorkloadIdentityConfiguration() *cobra.Command { } func validationForDeleteWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error { - if err := wifKeyArgCheck(argv); err != nil { - return err - } return nil } func deleteWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error { ctx := context.Background() - key := argv[0] + key, err := wifKeyFromArgs(argv) + if err != nil { + return err + } // Create the client for the OCM API: connection, err := ocm.NewConnection().Build() diff --git a/cmd/ocm/gcp/describe-wif-config.go b/cmd/ocm/gcp/describe-wif-config.go index c1df0bd1..26a7a9db 100644 --- a/cmd/ocm/gcp/describe-wif-config.go +++ b/cmd/ocm/gcp/describe-wif-config.go @@ -13,17 +13,19 @@ import ( // NewDescribeWorkloadIdentityConfiguration provides the "gcp describe wif-config" subcommand func NewDescribeWorkloadIdentityConfiguration() *cobra.Command { describeWorkloadIdentityPoolCmd := &cobra.Command{ - Use: "wif-config [ID|Name]", - Short: "Show details of a wif-config.", - RunE: describeWorkloadIdentityConfigurationCmd, - PreRunE: validationForDescribeWorkloadIdentityConfigurationCmd, + Use: "wif-config [ID|Name]", + Short: "Show details of a wif-config.", + RunE: describeWorkloadIdentityConfigurationCmd, } return describeWorkloadIdentityPoolCmd } func describeWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error { - key := argv[0] + key, err := wifKeyFromArgs(argv) + if err != nil { + return err + } // Create the client for the OCM API: connection, err := ocm.NewConnection().Build() @@ -48,10 +50,3 @@ func describeWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) return w.Flush() } - -func validationForDescribeWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error { - if err := wifKeyArgCheck(argv); err != nil { - return err - } - return nil -} diff --git a/cmd/ocm/gcp/gcp-client-shim.go b/cmd/ocm/gcp/gcp-client-shim.go index 9c820ef9..51e444fe 100644 --- a/cmd/ocm/gcp/gcp-client-shim.go +++ b/cmd/ocm/gcp/gcp-client-shim.go @@ -545,12 +545,3 @@ func (c *shim) createMemberRoleBindingForProject( Role: roleName, }) } - -// Checks for WIF config name or id in input -func wifKeyArgCheck(args []string) error { - if len(args) != 1 || args[0] == "" { - return fmt.Errorf("expected exactly one command line parameters containing the name " + - "or ID of the WIF config") - } - return nil -} diff --git a/cmd/ocm/gcp/generate-wif-script.go b/cmd/ocm/gcp/generate-wif-script.go index 1711c252..f6d1ce48 100644 --- a/cmd/ocm/gcp/generate-wif-script.go +++ b/cmd/ocm/gcp/generate-wif-script.go @@ -19,11 +19,10 @@ var ( func NewGenerateCommand() *cobra.Command { generateScriptCmd := &cobra.Command{ - Use: "generate [wif-config ID|Name]", - Short: "Generate script based on a wif-config", - Args: cobra.ExactArgs(1), - RunE: generateCreateScriptCmd, - PreRunE: validationForGenerateCreateScriptCmd, + Use: "generate [wif-config ID|Name]", + Short: "Generate script based on a wif-config", + Args: cobra.ExactArgs(1), + RunE: generateCreateScriptCmd, } generateScriptCmd.PersistentFlags().StringVar(&GenerateScriptOpts.TargetDir, "output-dir", "", @@ -32,16 +31,12 @@ func NewGenerateCommand() *cobra.Command { return generateScriptCmd } -func validationForGenerateCreateScriptCmd(cmd *cobra.Command, argv []string) error { - if err := wifKeyArgCheck(argv); err != nil { - return err - } - return nil -} - func generateCreateScriptCmd(cmd *cobra.Command, argv []string) error { ctx := context.Background() - key := argv[0] + key, err := wifKeyFromArgs(argv) + if err != nil { + return err + } // Create the client for the OCM API: connection, err := ocm.NewConnection().Build() diff --git a/cmd/ocm/gcp/helpers.go b/cmd/ocm/gcp/helpers.go new file mode 100644 index 00000000..19ca216e --- /dev/null +++ b/cmd/ocm/gcp/helpers.go @@ -0,0 +1,47 @@ +package gcp + +import ( + "fmt" + + cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" +) + +// Checks for WIF config name or id in input +func wifKeyArgCheck(args []string) error { + if len(args) != 1 || args[0] == "" { + return fmt.Errorf("expected exactly one command line parameters containing the name " + + "or ID of the WIF config") + } + return nil +} + +// Extracts WIF config name or id from input +func wifKeyFromArgs(args []string) (string, error) { + if err := wifKeyArgCheck(args); err != nil { + return "", err + } + return args[0], nil +} + +// findWifConfig finds the WIF configuration by ID or name +func findWifConfig(client *cmv1.Client, key string) (*cmv1.WifConfig, error) { + collection := client.GCP().WifConfigs() + page := 1 + size := 1 + query := fmt.Sprintf( + "id = '%s' or display_name = '%s'", + key, key, + ) + + response, err := collection.List().Search(query).Page(page).Size(size).Send() + if err != nil { + return nil, err + } + if response.Total() == 0 { + return nil, fmt.Errorf("WIF configuration with identifier or name '%s' not found", key) + } + if response.Total() > 1 { + return nil, fmt.Errorf("there are %d WIF configurations found with identifier or name '%s'", response.Total(), key) + } + return response.Items().Slice()[0], nil +} diff --git a/cmd/ocm/gcp/list-wif-config.go b/cmd/ocm/gcp/list-wif-config.go index 98f88363..025380fa 100644 --- a/cmd/ocm/gcp/list-wif-config.go +++ b/cmd/ocm/gcp/list-wif-config.go @@ -24,7 +24,6 @@ func NewListWorkloadIdentityConfiguration() *cobra.Command { Aliases: []string{"wif-configs"}, Short: "List wif-configs.", RunE: listWorkloadIdentityConfigurationCmd, - PreRunE: validationForListWorkloadIdentityConfigurationCmd, } fs := listWorkloadIdentityPoolCmd.Flags() @@ -44,11 +43,6 @@ func NewListWorkloadIdentityConfiguration() *cobra.Command { return listWorkloadIdentityPoolCmd } -func validationForListWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error { - // No validation needed - return nil -} - func listWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error { // Create a context: ctx := context.Background() diff --git a/cmd/ocm/gcp/update-wif-config.go b/cmd/ocm/gcp/update-wif-config.go index 59cacadc..236484db 100644 --- a/cmd/ocm/gcp/update-wif-config.go +++ b/cmd/ocm/gcp/update-wif-config.go @@ -7,7 +7,6 @@ import ( "github.com/openshift-online/ocm-cli/pkg/gcp" "github.com/openshift-online/ocm-cli/pkg/ocm" - cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -18,26 +17,21 @@ var UpdateWifConfigOpts struct { // NewUpdateWorkloadIdentityConfiguration provides the "gcp update wif-config" subcommand func NewUpdateWorkloadIdentityConfiguration() *cobra.Command { updateWifConfigCmd := &cobra.Command{ - Use: "wif-config [ID|Name]", - Short: "Update wif-config.", - RunE: updateWorkloadIdentityConfigurationCmd, - PreRunE: validationForUpdateWorkloadIdentityConfigurationCmd, + Use: "wif-config [ID|Name]", + Short: "Update wif-config.", + RunE: updateWorkloadIdentityConfigurationCmd, } return updateWifConfigCmd } -func validationForUpdateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error { - if err := wifKeyArgCheck(argv); err != nil { - return err - } - return nil -} - func updateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error { ctx := context.Background() log := log.Default() - key := argv[0] + key, err := wifKeyFromArgs(argv) + if err != nil { + return err + } // Create the client for the OCM API: connection, err := ocm.NewConnection().Build() @@ -81,26 +75,3 @@ func updateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) e return nil } - -// findWifConfig finds the WIF configuration by ID or name -func findWifConfig(client *cmv1.Client, key string) (*cmv1.WifConfig, error) { - collection := client.GCP().WifConfigs() - page := 1 - size := 1 - query := fmt.Sprintf( - "id = '%s' or display_name = '%s'", - key, key, - ) - - response, err := collection.List().Search(query).Page(page).Size(size).Send() - if err != nil { - return nil, err - } - if response.Total() == 0 { - return nil, fmt.Errorf("WIF configuration with identifier or name '%s' not found", key) - } - if response.Total() > 1 { - return nil, fmt.Errorf("there are %d WIF configurations found with identifier or name '%s'", response.Total(), key) - } - return response.Items().Slice()[0], nil -}