From 05f50b46e23539f92988001b24af3a6971a20bfe Mon Sep 17 00:00:00 2001 From: srinandan Date: Sun, 22 Dec 2024 06:18:37 +0000 Subject: [PATCH] bug: fixes issues exporting more than 1000 products #611 --- internal/client/products/products.go | 60 ++++++++++++++++++++++------ internal/cmd/products/expprod.go | 1 + 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/internal/client/products/products.go b/internal/client/products/products.go index 01e9cb6cd..54e2a8c06 100644 --- a/internal/client/products/products.go +++ b/internal/client/products/products.go @@ -309,20 +309,9 @@ func Export(conn int) (payload [][]byte, err error) { // parent workgroup var pwg sync.WaitGroup var mu sync.Mutex - const entityType = "apiproducts" - u, _ := url.Parse(apiclient.GetApigeeBaseURL()) - u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), entityType) - // don't print to sysout - apiclient.ClientPrintHttpResponse.Set(false) - respBody, err := apiclient.HttpClient(u.String()) - apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) - if err != nil { - return apiclient.GetEntityPayloadList(), err - } - - products := apiProducts{} - err = json.Unmarshal(respBody, &products) + entityType := "apiproducts" + products, err := listAllProducts() if err != nil { return apiclient.GetEntityPayloadList(), err } @@ -471,3 +460,48 @@ func readProductsFile(filePath string) ([]APIProduct, error) { return products, nil } + +func listAllProducts() (products apiProducts, err error) { + var startKey string + products = apiProducts{} + + u, _ := url.Parse(apiclient.GetApigeeBaseURL()) + u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "apiproducts") + + // don't print to sysout + apiclient.ClientPrintHttpResponse.Set(false) + + for { + + p := apiProducts{} + + if startKey != "" { + q := u.Query() + q.Set("startKey", startKey) + q.Set("count", "1000") + u.RawQuery = q.Encode() + } + + respBody, err := apiclient.HttpClient(u.String()) + startKey = "" + if err != nil { + return products, err + } + + err = json.Unmarshal(respBody, &p) + if err != nil { + return products, err + } + + products.APIProduct = append(products.APIProduct, p.APIProduct...) + + if len(p.APIProduct) == 1000 { + startKey = p.APIProduct[len(p.APIProduct)-1].Name + } else if len(p.APIProduct) < 1000 { + break + } + } + + apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) + return products, nil +} diff --git a/internal/cmd/products/expprod.go b/internal/cmd/products/expprod.go index b1c3d93f0..4e722d3b6 100644 --- a/internal/cmd/products/expprod.go +++ b/internal/cmd/products/expprod.go @@ -34,6 +34,7 @@ var ExpCmd = &cobra.Command{ cmd.SilenceUsage = true const exportFileName = "products.json" + apiclient.DisableCmdPrintHttpResponse() payload, err := products.Export(conn) if err != nil { return err