Skip to content

Commit

Permalink
Support listing and parameters in 'gcp get wif-config' (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGray authored Aug 8, 2024
1 parent 0724f62 commit ca8d9db
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions cmd/ocm/gcp/get-wif-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@ import (
"fmt"
"os"

"github.com/openshift-online/ocm-cli/pkg/arguments"
"github.com/openshift-online/ocm-cli/pkg/dump"
"github.com/openshift-online/ocm-cli/pkg/ocm"
"github.com/openshift-online/ocm-cli/pkg/urls"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var GetWorkloadIdentityConfigurationOpts struct {
single bool
single bool
parameter []string
}

// NewCreateWorkloadIdentityConfiguration provides the "create-wif-config" subcommand
func NewGetWorkloadIdentityConfiguration() *cobra.Command {
getWorkloadIdentityPoolCmd := &cobra.Command{
Use: "wif-config [ID]",
Short: "Send a GET request for wif-config.",
RunE: getWorkloadIdentityConfigurationCmd,
PreRunE: validationForGetWorkloadIdentityConfigurationCmd,
Aliases: []string{"wif-configs"},
}

fs := getWorkloadIdentityPoolCmd.Flags()
arguments.AddParameterFlag(fs, &GetWorkloadIdentityConfigurationOpts.parameter)
fs.BoolVar(
&GetWorkloadIdentityConfigurationOpts.single,
"single",
Expand All @@ -36,9 +38,14 @@ func NewGetWorkloadIdentityConfiguration() *cobra.Command {
}

func getWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error {
id, err := urls.Expand(argv)
if err != nil {
return errors.Wrapf(err, "could not create URI")
var path string
if len(argv) == 0 {
path = "/api/clusters_mgmt/v1/gcp/wif_configs"
} else if len(argv) == 1 {
id := argv[0]
path = fmt.Sprintf("/api/clusters_mgmt/v1/gcp/wif_configs/%s", id)
} else {
return fmt.Errorf("unexpected number of arguments")
}

connection, err := ocm.NewConnection().Build()
Expand All @@ -47,7 +54,10 @@ func getWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) erro
}
defer connection.Close()

resp, err := connection.Get().Path(fmt.Sprintf("/api/clusters_mgmt/v1/gcp/wif_configs/%s", id)).Send()
request := connection.Get().Path(path)
arguments.ApplyParameterFlag(request, GetWorkloadIdentityConfigurationOpts.parameter)

resp, err := request.Send()
if err != nil {
return errors.Wrapf(err, "can't send request")
}
Expand All @@ -73,8 +83,8 @@ func getWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) erro
}

func validationForGetWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error {
if len(argv) != 1 {
return fmt.Errorf("Expected exactly one command line parameter containing the id of the WIF config.")
if len(argv) > 1 {
return fmt.Errorf("expected at most one command line parameter containing the id of the WIF config")
}
return nil
}

0 comments on commit ca8d9db

Please sign in to comment.