From c1b26052d201e2c15d5c49aff346bf4238b76a57 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Sat, 21 Oct 2023 21:10:34 +0530 Subject: [PATCH 1/4] PLT-782:Added pagination serach in get cluster function --- client/cluster.go | 56 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/client/cluster.go b/client/cluster.go index ba43c99d..29e823c1 100644 --- a/client/cluster.go +++ b/client/cluster.go @@ -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 @@ -118,17 +118,41 @@ 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 { @@ -136,7 +160,7 @@ func (h *V1Client) ListClusters(clusterContext string) ([]*models.V1SpectroClust clusters = append(clusters, c) } } - return clusters, nil + return clusters, continueToken, nil } func (h *V1Client) GetClusterWithoutStatus(scope, uid string) (*models.V1SpectroCluster, error) { @@ -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) + for _, cluster := range clusters { + if cluster.Metadata.Name == name && cluster.Status.State != "Deleted" { + return cluster, nil + } + } + } return nil, nil } From 18affeba7257f2f09b40ec11e4d44b13f80de6fa Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Sat, 21 Oct 2023 21:34:17 +0530 Subject: [PATCH 2/4] set limit as zero --- client/cluster.go | 57 ++++++++++------------------------------------- 1 file changed, 12 insertions(+), 45 deletions(-) diff --git a/client/cluster.go b/client/cluster.go index 29e823c1..4c3acb5d 100644 --- a/client/cluster.go +++ b/client/cluster.go @@ -102,33 +102,10 @@ func (h *V1Client) SearchClusterSummaries(clusterContext string, filter *models. return resp.Payload.Items, nil } -func (h *V1Client) listClusters(clusterContext string) ([]*models.V1SpectroCluster, string, error) { +func (h *V1Client) listClusters(clusterContext string) ([]*models.V1SpectroCluster, error) { client, err := h.GetClusterClient() if err != nil { - return nil, "", err - } - - var params *clusterC.V1SpectroClustersListParams - switch clusterContext { - case "project": - params = clusterC.NewV1SpectroClustersListParamsWithContext(h.Ctx) - case "tenant": - params = clusterC.NewV1SpectroClustersListParams() - } - - 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) 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 @@ -138,21 +115,21 @@ func (h *V1Client) listClustersWithPagination(clusterContext string, ContinueTok case "tenant": params = clusterC.NewV1SpectroClustersListParams() } - params.Continue = &ContinueToken + var limit int64 = 0 + params.Limit = &limit 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, resp.Payload.Listmeta.Continue, nil + return resp.Payload.Items, nil } -func (h *V1Client) ListClusters(clusterContext string) ([]*models.V1SpectroCluster, string, error) { - allClusters, continueToken, err := h.listClusters(clusterContext) +func (h *V1Client) ListClusters(clusterContext string) ([]*models.V1SpectroCluster, error) { + allClusters, err := h.listClusters(clusterContext) if err != nil { - return nil, "", err + return nil, err } clusters := make([]*models.V1SpectroCluster, 0) for _, c := range allClusters { @@ -160,7 +137,7 @@ func (h *V1Client) ListClusters(clusterContext string) ([]*models.V1SpectroClust clusters = append(clusters, c) } } - return clusters, continueToken, nil + return clusters, nil } func (h *V1Client) GetClusterWithoutStatus(scope, uid string) (*models.V1SpectroCluster, error) { @@ -194,25 +171,15 @@ func (h *V1Client) GetClusterWithoutStatus(scope, uid string) (*models.V1Spectro } func (h *V1Client) GetClusterByName(name, clusterContext string) (*models.V1SpectroCluster, error) { - clusters, continueToken, err := h.listClusters(clusterContext) + clusters, 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) - for _, cluster := range clusters { - if cluster.Metadata.Name == name && cluster.Status.State != "Deleted" { - return cluster, nil - } - } - } return nil, nil } From 9c7a30bad640026b5dba52b30c1160146d83c7bf Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Sat, 21 Oct 2023 21:35:25 +0530 Subject: [PATCH 3/4] fix --- client/cluster.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/client/cluster.go b/client/cluster.go index 4c3acb5d..d2b56f05 100644 --- a/client/cluster.go +++ b/client/cluster.go @@ -115,6 +115,7 @@ func (h *V1Client) listClusters(clusterContext string) ([]*models.V1SpectroClust case "tenant": params = clusterC.NewV1SpectroClustersListParams() } + var limit int64 = 0 params.Limit = &limit resp, err := client.V1SpectroClustersList(params) @@ -175,11 +176,13 @@ func (h *V1Client) GetClusterByName(name, clusterContext string) (*models.V1Spec if err != nil { return nil, err } + for _, cluster := range clusters { if cluster.Metadata.Name == name && cluster.Status.State != "Deleted" { return cluster, nil } } + return nil, nil } From c05bf72e1ba6e765f7726fd1eea5c4753c066e27 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Sat, 21 Oct 2023 21:36:05 +0530 Subject: [PATCH 4/4] fix2 --- client/cluster.go | 1 - 1 file changed, 1 deletion(-) diff --git a/client/cluster.go b/client/cluster.go index d2b56f05..0b2135a9 100644 --- a/client/cluster.go +++ b/client/cluster.go @@ -115,7 +115,6 @@ func (h *V1Client) listClusters(clusterContext string) ([]*models.V1SpectroClust case "tenant": params = clusterC.NewV1SpectroClustersListParams() } - var limit int64 = 0 params.Limit = &limit resp, err := client.V1SpectroClustersList(params)