Skip to content

Commit edbd14d

Browse files
authored
Add clustermgmt flag to create kafka (#1835)
* 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
1 parent d78b6d5 commit edbd14d

File tree

16 files changed

+176
-159
lines changed

16 files changed

+176
-159
lines changed

docs/commands/rhoas.md

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/commands/rhoas_dedicated.md

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/commands/rhoas_dedicated_deregister-cluster.md

+11-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/commands/rhoas_dedicated_list.md

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/commands/rhoas_dedicated_register-cluster.md

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/commands/rhoas_kafka_list.md

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cmd/dedicated/dedicated.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dedicated
22

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

1919
cmd.AddCommand(
2020
register.NewRegisterClusterCommand(f),
21-
listclusters.NewListClusterCommand(f),
21+
list.NewListClusterCommand(f),
2222
deregister.NewDeRegisterClusterCommand(f),
2323
)
2424

25+
cmd.Hidden = true
26+
2527
return cmd
2628
}

pkg/cmd/dedicated/dedicatedcmdutil/dedicated_util.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package kafkacmdutil
1+
package dedicatedcmdutil
22

33
import (
44
"fmt"
5+
"github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
6+
"github.com/redhat-developer/app-services-sdk-core/app-services-sdk-go/kafkamgmt/apiv1/client"
57
"strconv"
68

79
"github.com/redhat-developer/app-services-cli/pkg/core/errors"
@@ -38,3 +40,27 @@ func (v *Validator) ValidatorForMachinePoolNodes(val interface{}) error {
3840
}
3941
return nil
4042
}
43+
44+
func CreatePromptOptionsFromClusters(clusterList *kafkamgmtclient.EnterpriseClusterList, clusterMap *map[string]v1.Cluster) *[]string {
45+
promptOptions := []string{}
46+
validatedClusters := ValidateClusters(clusterList)
47+
for _, cluster := range validatedClusters.Items {
48+
ocmCluster := (*clusterMap)[cluster.GetId()]
49+
display := ocmCluster.Name() + " (" + cluster.GetId() + ")"
50+
promptOptions = append(promptOptions, display)
51+
}
52+
return &promptOptions
53+
}
54+
55+
func ValidateClusters(clusterList *kafkamgmtclient.EnterpriseClusterList) *kafkamgmtclient.EnterpriseClusterList {
56+
// if cluster is in a ready state add it to the list of clusters
57+
items := make([]kafkamgmtclient.EnterpriseClusterListItem, 0, len(clusterList.Items))
58+
for _, cluster := range clusterList.Items {
59+
if *cluster.Status == "ready" {
60+
items = append(items, cluster)
61+
}
62+
}
63+
64+
newClusterList := kafkamgmtclient.NewEnterpriseClusterList(clusterList.Kind, clusterList.Page, int32(len(items)), clusterList.Total, items)
65+
return newClusterList
66+
}

pkg/cmd/dedicated/deregister/deregister.go

+10-19
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,13 @@ func getListOfClusters(opts *options) ([]*clustersmgmtv1.Cluster, error) {
117117
if err != nil {
118118
if response != nil {
119119
if response.StatusCode == 403 {
120-
return nil, opts.f.Localizer.MustLocalizeError("")
120+
return nil, opts.f.Localizer.MustLocalizeError("dedicated.deregisterCluster.error.403")
121121
}
122-
123-
return nil, fmt.Errorf("%v, %v", response.Status, err)
122+
return nil, fmt.Errorf("%v: %w", response.Status, err)
124123
}
125-
126124
return nil, err
127125
}
128-
129-
ocmClusterList, err := clustermgmt.GetClusterListByIds(opts.f, opts.accessToken, opts.clusterManagementApiUrl, kafkautil.CreateClusterSearchStringFromKafkaList(kfmClusterList), len(kfmClusterList.Items))
126+
ocmClusterList, err := clustermgmt.GetClusterListByIds(opts.f, opts.clusterManagementApiUrl, opts.accessToken, kafkautil.CreateClusterSearchStringFromKafkaList(kfmClusterList), len(kfmClusterList.Items))
130127
if err != nil {
131128
return nil, err
132129
}
@@ -235,7 +232,7 @@ func deleteKafkasPrompt(opts *options, kafkas *[]kafkamgmtclient.KafkaRequest) e
235232

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

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

268-
var selectedClusterName string
269-
err := survey.AskOne(prompt, &selectedClusterName)
265+
var idx int
266+
err := survey.AskOne(prompt, &idx)
270267
if err != nil {
271268
return err
272269
}
273-
274-
// get the desired cluster
275-
for _, cluster := range *clusterList {
276-
if cluster.Name() == selectedClusterName {
277-
opts.selectedCluster = cluster
278-
opts.selectedClusterId = cluster.ID()
279-
}
280-
}
270+
opts.selectedCluster = (*clusterList)[idx]
271+
opts.selectedClusterId = opts.selectedCluster.ID()
281272
return nil
282273
}
283274

pkg/cmd/dedicated/listclusters/list.go renamed to pkg/cmd/dedicated/list/list.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package listclusters
1+
package list
22

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

49+
flags := kafkaFlagutil.NewFlagSet(cmd, f.Localizer)
50+
51+
flags.StringVar(&opts.clusterManagementApiUrl, "cluster-mgmt-api-url", "", f.Localizer.MustLocalize("dedicated.registerCluster.flag.clusterMgmtApiUrl.description"))
52+
flags.StringVar(&opts.accessToken, "access-token", "", f.Localizer.MustLocalize("dedicated.registercluster.flag.accessToken.description"))
4853
return cmd
54+
4955
}
5056

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

6672
opts.kfmClusterList = kfmClusterList
6773

68-
clist, err := clustermgmt.GetClusterListByIds(opts.f, opts.accessToken, opts.clusterManagementApiUrl, kafkautil.CreateClusterSearchStringFromKafkaList(opts.kfmClusterList), len(opts.kfmClusterList.Items))
74+
clist, err := clustermgmt.GetClusterListByIds(opts.f, opts.clusterManagementApiUrl, opts.accessToken, kafkautil.CreateClusterSearchStringFromKafkaList(opts.kfmClusterList), len(opts.kfmClusterList.Items))
6975
if err != nil {
7076
return err
7177
}

pkg/cmd/dedicated/listclusters/list_test.go renamed to pkg/cmd/dedicated/list/list_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package listclusters
1+
package list
22

33
import (
44
"testing"

0 commit comments

Comments
 (0)