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

refactor: moving list enterprise clusters to util package #1818

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
39 changes: 7 additions & 32 deletions pkg/cmd/dedicated/listclusters/list.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package listclusters

import (
"context"
"fmt"
"github.com/redhat-developer/app-services-cli/pkg/shared/kafkautil"

clustersmgmtv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
Expand Down Expand Up @@ -50,40 +50,24 @@ func NewListClusterCommand(f *factory.Factory) *cobra.Command {
}

func runListClusters(opts *options, f *factory.Factory) error {
err := listEnterpriseClusters(opts, f)
kfmClusterList, err := kafkautil.ListEnterpriseClusters(f)
opts.kfmClusterList = kfmClusterList
if err != nil {
return err
}
clist, err := getPaginatedClusterList(opts)
clist, err := clustermgmt.GetClusterListByIds(opts.f, opts.accessToken, opts.clusterManagementApiUrl, createSearchString(opts), len(opts.kfmClusterList.Items))
if err != nil {
return err
}
if clist == nil {
return opts.f.Localizer.MustLocalizeError("dedicated.list.error.noRegisteredClustersFound")
}
opts.clustermgmtClusterList = clist
opts.registeredClusters = kfmListToClusterRowList(opts)
displayRegisteredClusters(opts)
return nil
}

func listEnterpriseClusters(opts *options, f *factory.Factory) error {
conn, err := f.Connection()
if err != nil {
return err
}
ctx := context.Background()
api := conn.API()
cl := api.KafkaMgmtEnterprise().GetEnterpriseOsdClusters(ctx)
clist, response, err := cl.Execute()
if err != nil {
return err
}
opts.kfmClusterList = &clist
f.Logger.Debug(response)
if len(opts.kfmClusterList.Items) == 0 {
return f.Localizer.MustLocalizeError("dedicated.list.cmd.errorNoRegisteredClusters")
}
return nil
}

func createSearchString(opts *options) string {
searchString := ""
for idx, kfmcluster := range opts.kfmClusterList.Items {
Expand All @@ -95,15 +79,6 @@ func createSearchString(opts *options) string {
return searchString
}

func getPaginatedClusterList(opts *options) (*clustersmgmtv1.ClusterList, error) {
// get ids of clusters and create an ocm call filtering by those ids
ocmcl, err := clustermgmt.GetClusterListByIds(opts.f, opts.accessToken, opts.clusterManagementApiUrl, createSearchString(opts), len(opts.kfmClusterList.Items))
if err != nil {
return nil, err
}
return ocmcl, nil
}

func kfmListToClusterRowList(opts *options) []clusterRow {
var crl []clusterRow

Expand Down
26 changes: 26 additions & 0 deletions pkg/shared/kafkautil/dedicated_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package kafkautil

import (
"context"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-core/app-services-sdk-go/kafkamgmt/apiv1/client"
)

func ListEnterpriseClusters(f *factory.Factory) (*kafkamgmtclient.EnterpriseClusterList, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shared utils is a place where methods defined are used in different subcommands. For example - kafkautil.InteractiveSelect is used in kafka, connectors and cluster subcommands.
ListEnterpriseClusters is used just in dedicated list, will it make sense to move it to pkg/cmd/dedicated/dedicatedcmdutil/dedicated_util.go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will also be used in the create kafka cmd too, so that's the reason I moved it to shared

conn, err := f.Connection()
if err != nil {
return nil, err
}
ctx := context.Background()
api := conn.API()
cl := api.KafkaMgmtEnterprise().GetEnterpriseOsdClusters(ctx)
clist, response, err := cl.Execute()
if err != nil {
return nil, err
}
if len(clist.Items) == 0 {
return &clist, nil
}
f.Logger.Debug(response)
return &clist, nil
}