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

OCM-10207 | Filter wif configs in interactive mode #660

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
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
22 changes: 17 additions & 5 deletions cmd/ocm/create/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func setWifConfigOption(id, name string) string {
return fmt.Sprintf("%s (%s)", name, id)
}

// Returns the name of the WIF config from the option
func parseWifConfigOption(wifConfigOption string) string {
return strings.Split(wifConfigOption, " ")[0]
}
Expand Down Expand Up @@ -1405,7 +1406,21 @@ func promptGcpAuth(fs *pflag.FlagSet, connection *sdk.Connection) error {
}

func promptWifConfig(fs *pflag.FlagSet, connection *sdk.Connection) error {
wifConfigs, err := provider.GetWifConfigs(connection.ClustersMgmt().V1())
flag := fs.Lookup("wif-config")

// if the flag was set, validate the value
if flag.Changed {
wifKey := flag.Value.String()
wifConfig, err := provider.GetWifConfig(connection.ClustersMgmt().V1(), wifKey)
if err != nil {
return err
}
args.gcpAuthentication.Id = wifConfig.ID()
return nil
}

// if the flag was not set, prompt the user
wifConfigs, err := provider.GetUnusedWifConfigs(connection.ClustersMgmt().V1())
if err != nil {
return err
}
Expand All @@ -1417,16 +1432,13 @@ func promptWifConfig(fs *pflag.FlagSet, connection *sdk.Connection) error {
if err != nil {
return err
}
if args.interactive {
args.gcpWifConfig = parseWifConfigOption(args.gcpWifConfig)
}
args.gcpWifConfig = parseWifConfigOption(args.gcpWifConfig)

// map wif name to wif id
wifMapping := map[string]string{}
for _, wc := range wifConfigs {
wifMapping[wc.DisplayName()] = wc.ID()
}

args.gcpAuthentication.Id = wifMapping[args.gcpWifConfig]
return nil
}
Expand Down
34 changes: 31 additions & 3 deletions pkg/provider/wif_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
)

func getWifConfigs(client *cmv1.Client) (wifConfigs []*cmv1.WifConfig, err error) {
const unusedWifsQuery = "cluster.id is null"

func getWifConfigs(client *cmv1.Client, filter string) (wifConfigs []*cmv1.WifConfig, err error) {
collection := client.GCP().WifConfigs()
page := 1
size := 100
Expand All @@ -16,6 +18,7 @@ func getWifConfigs(client *cmv1.Client) (wifConfigs []*cmv1.WifConfig, err error
response, err = collection.List().
Page(page).
Size(size).
Search(filter).
Send()
if err != nil {
return
Expand All @@ -34,13 +37,38 @@ func getWifConfigs(client *cmv1.Client) (wifConfigs []*cmv1.WifConfig, err error
}

func GetWifConfigs(client *cmv1.Client) (wifConfigs []*cmv1.WifConfig, err error) {
return getWifConfigs(client)
return getWifConfigs(client, "")
}

// GetUnusedWifConfigs returns the WIF configurations that are not associated with any cluster
func GetUnusedWifConfigs(client *cmv1.Client) (wifConfigs []*cmv1.WifConfig, err error) {
return getWifConfigs(client, unusedWifsQuery)
}

// GetWifConfig returns the WIF configuration where the key is the wif config id or name
func GetWifConfig(client *cmv1.Client, key string) (wifConfig *cmv1.WifConfig, err error) {
query := fmt.Sprintf(
"id = '%s' or display_name = '%s'",
key, key,
)
wifs, err := getWifConfigs(client, query)
if err != nil {
return nil, err
}

if len(wifs) == 0 {
return nil, fmt.Errorf("WIF configuration with identifier or name '%s' not found", key)
}
if len(wifs) > 1 {
return nil, fmt.Errorf("there are %d WIF configurations found with identifier or name '%s'", len(wifs), key)
}
return wifs[0], nil
}

// GetWifConfigNameOptions returns the wif config options for the cluster
// with display name as the value and id as the description
func GetWifConfigNameOptions(client *cmv1.Client) (options []arguments.Option, err error) {
wifConfigs, err := getWifConfigs(client)
wifConfigs, err := GetUnusedWifConfigs(client)
if err != nil {
err = fmt.Errorf("failed to retrieve WIF configurations: %s", err)
return
Expand Down
Loading