Skip to content

Commit

Permalink
Add clustermgmt flag to create kafka (#1835)
Browse files Browse the repository at this point in the history
* docs: update dedicated docs

* refactor: addresses linting issues

refactor: move multiuse functions to dedicated_utils dir

refactor: addition of the clusterId to the cluster name during cluster selection prompt

refactor: addition of cluster id to cluster selection prompt for deregister cluster

refactor: addition of successful registration prompt

* feat: addition of ocm cluster mgmt url and access token flags to kafka create

docs: Updated docs for new flags in kafka create, amongst other minor doc changes

refactor: moves the error msg about not having quota to after enterprise option is selected not after cluster is selected

fix: fixes a bug around the selection of a cluster for cluster registration

fix: fixes a bug selecting the cluster to deregister

docs: updated docs

refactor: hides the dedicated commands

* feat: add cluster mgmt api url and access token flags to dedicated list

fix: addresses comments in review

* fix: fix a bug with the arguements passed to ocm cluter mgmt and rename list cluster package
  • Loading branch information
dimakis authored Mar 10, 2023
1 parent d78b6d5 commit edbd14d
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 159 deletions.
2 changes: 1 addition & 1 deletion docs/commands/rhoas.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions docs/commands/rhoas_dedicated.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions docs/commands/rhoas_dedicated_deregister-cluster.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/commands/rhoas_dedicated_list.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions docs/commands/rhoas_dedicated_register-cluster.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/commands/rhoas_kafka_list.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pkg/cmd/dedicated/dedicated.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dedicated

import (
"github.com/redhat-developer/app-services-cli/pkg/cmd/dedicated/deregister"
"github.com/redhat-developer/app-services-cli/pkg/cmd/dedicated/listclusters"
"github.com/redhat-developer/app-services-cli/pkg/cmd/dedicated/list"
"github.com/redhat-developer/app-services-cli/pkg/cmd/dedicated/register"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
Expand All @@ -18,9 +18,11 @@ func NewDedicatedCmd(f *factory.Factory) *cobra.Command {

cmd.AddCommand(
register.NewRegisterClusterCommand(f),
listclusters.NewListClusterCommand(f),
list.NewListClusterCommand(f),
deregister.NewDeRegisterClusterCommand(f),
)

cmd.Hidden = true

return cmd
}
28 changes: 27 additions & 1 deletion pkg/cmd/dedicated/dedicatedcmdutil/dedicated_util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package kafkacmdutil
package dedicatedcmdutil

import (
"fmt"
"github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/redhat-developer/app-services-sdk-core/app-services-sdk-go/kafkamgmt/apiv1/client"
"strconv"

"github.com/redhat-developer/app-services-cli/pkg/core/errors"
Expand Down Expand Up @@ -38,3 +40,27 @@ func (v *Validator) ValidatorForMachinePoolNodes(val interface{}) error {
}
return nil
}

func CreatePromptOptionsFromClusters(clusterList *kafkamgmtclient.EnterpriseClusterList, clusterMap *map[string]v1.Cluster) *[]string {
promptOptions := []string{}
validatedClusters := ValidateClusters(clusterList)
for _, cluster := range validatedClusters.Items {
ocmCluster := (*clusterMap)[cluster.GetId()]
display := ocmCluster.Name() + " (" + cluster.GetId() + ")"
promptOptions = append(promptOptions, display)
}
return &promptOptions
}

func ValidateClusters(clusterList *kafkamgmtclient.EnterpriseClusterList) *kafkamgmtclient.EnterpriseClusterList {
// if cluster is in a ready state add it to the list of clusters
items := make([]kafkamgmtclient.EnterpriseClusterListItem, 0, len(clusterList.Items))
for _, cluster := range clusterList.Items {
if *cluster.Status == "ready" {
items = append(items, cluster)
}
}

newClusterList := kafkamgmtclient.NewEnterpriseClusterList(clusterList.Kind, clusterList.Page, int32(len(items)), clusterList.Total, items)
return newClusterList
}
29 changes: 10 additions & 19 deletions pkg/cmd/dedicated/deregister/deregister.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,13 @@ func getListOfClusters(opts *options) ([]*clustersmgmtv1.Cluster, error) {
if err != nil {
if response != nil {
if response.StatusCode == 403 {
return nil, opts.f.Localizer.MustLocalizeError("")
return nil, opts.f.Localizer.MustLocalizeError("dedicated.deregisterCluster.error.403")
}

return nil, fmt.Errorf("%v, %v", response.Status, err)
return nil, fmt.Errorf("%v: %w", response.Status, err)
}

return nil, err
}

ocmClusterList, err := clustermgmt.GetClusterListByIds(opts.f, opts.accessToken, opts.clusterManagementApiUrl, kafkautil.CreateClusterSearchStringFromKafkaList(kfmClusterList), len(kfmClusterList.Items))
ocmClusterList, err := clustermgmt.GetClusterListByIds(opts.f, opts.clusterManagementApiUrl, opts.accessToken, kafkautil.CreateClusterSearchStringFromKafkaList(kfmClusterList), len(kfmClusterList.Items))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -235,7 +232,7 @@ func deleteKafkasPrompt(opts *options, kafkas *[]kafkamgmtclient.KafkaRequest) e

if response.StatusCode == 404 {
// remove this callback from the callback list as the kafka is deleted
// break to restart the loop from the begining as we are modifying the list
// break to restart the loop from the beginning as we are modifying the list
// as we are iterating through it
checkIfDeletedCallbacks = append(checkIfDeletedCallbacks[:i], checkIfDeletedCallbacks[i+1:]...)
break
Expand All @@ -256,28 +253,22 @@ func deleteKafkasPrompt(opts *options, kafkas *[]kafkamgmtclient.KafkaRequest) e
func runClusterSelectionInteractivePrompt(opts *options, clusterList *[]*clustersmgmtv1.Cluster) error {
clusterStringList := make([]string, 0)
for _, cluster := range *clusterList {
clusterStringList = append(clusterStringList, cluster.Name())
display := fmt.Sprintf("%s (%s)", cluster.Name(), cluster.ID())
clusterStringList = append(clusterStringList, display)
}

// TO-DO add page size
prompt := &survey.Select{
Message: opts.f.Localizer.MustLocalize("dedicated.registerCluster.prompt.selectCluster.message"),
Options: clusterStringList,
}

var selectedClusterName string
err := survey.AskOne(prompt, &selectedClusterName)
var idx int
err := survey.AskOne(prompt, &idx)
if err != nil {
return err
}

// get the desired cluster
for _, cluster := range *clusterList {
if cluster.Name() == selectedClusterName {
opts.selectedCluster = cluster
opts.selectedClusterId = cluster.ID()
}
}
opts.selectedCluster = (*clusterList)[idx]
opts.selectedClusterId = opts.selectedCluster.ID()
return nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package listclusters
package list

import (
"fmt"
clustersmgmtv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
kafkaFlagutil "github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
"github.com/redhat-developer/app-services-cli/pkg/shared/connection/api/clustermgmt"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
Expand Down Expand Up @@ -45,7 +46,12 @@ func NewListClusterCommand(f *factory.Factory) *cobra.Command {
},
}

flags := kafkaFlagutil.NewFlagSet(cmd, f.Localizer)

flags.StringVar(&opts.clusterManagementApiUrl, "cluster-mgmt-api-url", "", f.Localizer.MustLocalize("dedicated.registerCluster.flag.clusterMgmtApiUrl.description"))
flags.StringVar(&opts.accessToken, "access-token", "", f.Localizer.MustLocalize("dedicated.registercluster.flag.accessToken.description"))
return cmd

}

func runListClusters(opts *options, f *factory.Factory) error {
Expand All @@ -65,7 +71,7 @@ func runListClusters(opts *options, f *factory.Factory) error {

opts.kfmClusterList = kfmClusterList

clist, err := clustermgmt.GetClusterListByIds(opts.f, opts.accessToken, opts.clusterManagementApiUrl, kafkautil.CreateClusterSearchStringFromKafkaList(opts.kfmClusterList), len(opts.kfmClusterList.Items))
clist, err := clustermgmt.GetClusterListByIds(opts.f, opts.clusterManagementApiUrl, opts.accessToken, kafkautil.CreateClusterSearchStringFromKafkaList(opts.kfmClusterList), len(opts.kfmClusterList.Items))
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package listclusters
package list

import (
"testing"
Expand Down
Loading

0 comments on commit edbd14d

Please sign in to comment.