Skip to content

Commit

Permalink
PLT-782:Added pagination serach in get cluster function
Browse files Browse the repository at this point in the history
  • Loading branch information
SivaanandM committed Oct 21, 2023
1 parent bba88ae commit c1b2605
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ func (h *V1Client) SearchClusterSummaries(clusterContext string, filter *models.
return resp.Payload.Items, nil
}

func (h *V1Client) listClusters(clusterContext string) ([]*models.V1SpectroCluster, error) {
func (h *V1Client) listClusters(clusterContext string) ([]*models.V1SpectroCluster, string, error) {
client, err := h.GetClusterClient()
if err != nil {
return nil, err
return nil, "", err
}

var params *clusterC.V1SpectroClustersListParams
Expand All @@ -118,25 +118,49 @@ func (h *V1Client) listClusters(clusterContext string) ([]*models.V1SpectroClust

resp, err := client.V1SpectroClustersList(params)
if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 {
return nil, nil
return nil, "", nil
} else if err != nil {
return nil, err
return nil, "", err
}
return resp.Payload.Items, nil
return resp.Payload.Items, resp.Payload.Listmeta.Continue, nil
}

func (h *V1Client) ListClusters(clusterContext string) ([]*models.V1SpectroCluster, error) {
allClusters, err := h.listClusters(clusterContext)
func (h *V1Client) listClustersWithPagination(clusterContext string, ContinueToken string) ([]*models.V1SpectroCluster, string, error) {
client, err := h.GetClusterClient()
if err != nil {
return nil, err
return nil, "", err
}

var params *clusterC.V1SpectroClustersListParams
switch clusterContext {
case "project":
params = clusterC.NewV1SpectroClustersListParamsWithContext(h.Ctx)
case "tenant":
params = clusterC.NewV1SpectroClustersListParams()
}
params.Continue = &ContinueToken
resp, err := client.V1SpectroClustersList(params)

if e, ok := err.(*transport.TransportError); ok && e.HttpCode == 404 {
return nil, "", nil
} else if err != nil {
return nil, "", err
}
return resp.Payload.Items, resp.Payload.Listmeta.Continue, nil
}

func (h *V1Client) ListClusters(clusterContext string) ([]*models.V1SpectroCluster, string, error) {
allClusters, continueToken, err := h.listClusters(clusterContext)
if err != nil {
return nil, "", err
}
clusters := make([]*models.V1SpectroCluster, 0)
for _, c := range allClusters {
if c.Status.State != "Deleted" {
clusters = append(clusters, c)
}
}
return clusters, nil
return clusters, continueToken, nil
}

func (h *V1Client) GetClusterWithoutStatus(scope, uid string) (*models.V1SpectroCluster, error) {
Expand Down Expand Up @@ -170,17 +194,25 @@ func (h *V1Client) GetClusterWithoutStatus(scope, uid string) (*models.V1Spectro
}

func (h *V1Client) GetClusterByName(name, clusterContext string) (*models.V1SpectroCluster, error) {
clusters, err := h.listClusters(clusterContext)
clusters, continueToken, err := h.listClusters(clusterContext)
if err != nil {
return nil, err
}

// Finding out in page one with the limit of 50
for _, cluster := range clusters {
if cluster.Metadata.Name == name && cluster.Status.State != "Deleted" {
return cluster, nil
}
}

// Pagination loop till continueToken becomes = ""
for continueToken != "" {
clusters, continueToken, err = h.listClustersWithPagination(clusterContext, continueToken)

Check failure on line 209 in client/cluster.go

View workflow job for this annotation

GitHub Actions / build-with-coverage

ineffectual assignment to err (ineffassign)
for _, cluster := range clusters {
if cluster.Metadata.Name == name && cluster.Status.State != "Deleted" {
return cluster, nil
}
}
}
return nil, nil
}

Expand Down

0 comments on commit c1b2605

Please sign in to comment.