Skip to content

Commit

Permalink
Create gcp wif helpers file and remove unnecessary validation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGray committed Sep 20, 2024
1 parent 4455310 commit a077ebb
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 80 deletions.
8 changes: 4 additions & 4 deletions cmd/ocm/gcp/delete-wif-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 7 additions & 12 deletions cmd/ocm/gcp/describe-wif-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
9 changes: 0 additions & 9 deletions cmd/ocm/gcp/gcp-client-shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
21 changes: 8 additions & 13 deletions cmd/ocm/gcp/generate-wif-script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "",
Expand All @@ -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()
Expand Down
47 changes: 47 additions & 0 deletions cmd/ocm/gcp/helpers.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 0 additions & 6 deletions cmd/ocm/gcp/list-wif-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func NewListWorkloadIdentityConfiguration() *cobra.Command {
Aliases: []string{"wif-configs"},
Short: "List wif-configs.",
RunE: listWorkloadIdentityConfigurationCmd,
PreRunE: validationForListWorkloadIdentityConfigurationCmd,
}

fs := listWorkloadIdentityPoolCmd.Flags()
Expand All @@ -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()
Expand Down
43 changes: 7 additions & 36 deletions cmd/ocm/gcp/update-wif-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
Expand Down Expand Up @@ -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
}

0 comments on commit a077ebb

Please sign in to comment.