From 8b96c42e9722ccd0fc03c20a3028f8a247e15304 Mon Sep 17 00:00:00 2001 From: Anirban Pal Date: Wed, 16 Oct 2024 16:39:30 +0700 Subject: [PATCH 1/4] Fix progress bar error when tuple section is empty in store import --- cmd/store/import.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmd/store/import.go b/cmd/store/import.go index f187e5a..bff3fc2 100644 --- a/cmd/store/import.go +++ b/cmd/store/import.go @@ -138,6 +138,12 @@ func importStore( return nil, fmt.Errorf("failed to initialize FGA Client: %w", err) } + // Check if there are any tuples to import + if len(storeData.Tuples) == 0 { + fmt.Println("No tuples to import.") + return response, nil + } + // Initialize progress bar bar := progressbar.NewOptions(len(storeData.Tuples), progressbar.OptionSetWriter(os.Stderr), From 220d3cedabc7453ff458fe787cc7d8741e5579d6 Mon Sep 17 00:00:00 2001 From: Anirban Pal Date: Thu, 17 Oct 2024 00:51:51 +0700 Subject: [PATCH 2/4] updated code according to checks (Github Actions) --- cmd/model/list.go | 4 +- cmd/store/import.go | 108 ++++++++++++++++++++++++-------------------- 2 files changed, 61 insertions(+), 51 deletions(-) diff --git a/cmd/model/list.go b/cmd/model/list.go index ac61862..516b648 100644 --- a/cmd/model/list.go +++ b/cmd/model/list.go @@ -95,9 +95,9 @@ var listCmd = &cobra.Command{ models := authorizationmodel.AuthzModelList{} authzModels := response.AuthorizationModels - for index := range len(authzModels) { + for i := range len(authzModels) { authModel := authorizationmodel.AuthzModel{} - authModel.Set(authzModels[index]) + authModel.Set(authzModels[i]) models.AuthorizationModels = append(models.AuthorizationModels, authModel.DisplayAsJSON(fields)) } diff --git a/cmd/store/import.go b/cmd/store/import.go index bff3fc2..cb12c58 100644 --- a/cmd/store/import.go +++ b/cmd/store/import.go @@ -26,6 +26,7 @@ import ( "github.com/schollz/progressbar/v3" + openfga "github.com/openfga/go-sdk" "github.com/openfga/go-sdk/client" "github.com/spf13/cobra" @@ -116,36 +117,73 @@ func importStore( maxTuplesPerWrite, maxParallelRequests int32, fileName string, ) (*CreateStoreAndModelResponse, error) { - var ( - response *CreateStoreAndModelResponse - err error - ) + response, err := createOrUpdateStore(clientConfig, fgaClient, storeData, format, storeID, fileName) + if err != nil { + return nil, err + } + + if len(storeData.Tuples) == 0 { + fmt.Println("No tuples to import.") + return response, nil + } + err = importTuples(fgaClient, storeData.Tuples, maxTuplesPerWrite, maxParallelRequests) + if err != nil { + return nil, err + } + + return response, nil +} + +func createOrUpdateStore( + clientConfig *fga.ClientConfig, + fgaClient client.SdkClient, + storeData *storetest.StoreData, + format authorizationmodel.ModelFormat, + storeID string, + fileName string, +) (*CreateStoreAndModelResponse, error) { if storeID == "" { - response, err = createStore(clientConfig, storeData, format, fileName) - if err != nil { - return nil, fmt.Errorf("failed to create store: %w", err) + return createStore(clientConfig, storeData, format, fileName) + } + return updateStore(clientConfig, fgaClient, storeData, format, storeID) +} + +func importTuples( + fgaClient client.SdkClient, + tuples []openfga.TupleKey, + maxTuplesPerWrite, maxParallelRequests int32, +) error { + bar := createProgressBar(len(tuples)) + + for index := 0; index < len(tuples); index += int(maxTuplesPerWrite) { + end := index + int(maxTuplesPerWrite) + if end > len(tuples) { + end = len(tuples) } - } else { - response, err = updateStore(clientConfig, fgaClient, storeData, format, storeID) - if err != nil { - return nil, fmt.Errorf("failed to update store: %w", err) + + writeRequest := client.ClientWriteRequest{ + Writes: tuples[index:end], + } + if _, err := tuple.ImportTuples(fgaClient, writeRequest, maxTuplesPerWrite, maxParallelRequests); err != nil { + return fmt.Errorf("failed to import tuples: %w", err) } - } - fgaClient, err = clientConfig.GetFgaClient() - if err != nil { - return nil, fmt.Errorf("failed to initialize FGA Client: %w", err) + if err := bar.Add(end - index); err != nil { + return fmt.Errorf("failed to update progress bar: %w", err) + } + + time.Sleep(progressBarUpdateDelay) } - // Check if there are any tuples to import - if len(storeData.Tuples) == 0 { - fmt.Println("No tuples to import.") - return response, nil + if err := bar.Finish(); err != nil { + return fmt.Errorf("failed to finish progress bar: %w", err) } + return nil +} - // Initialize progress bar - bar := progressbar.NewOptions(len(storeData.Tuples), +func createProgressBar(total int) *progressbar.ProgressBar { + return progressbar.NewOptions(total, progressbar.OptionSetWriter(os.Stderr), progressbar.OptionSetDescription("Importing tuples"), progressbar.OptionShowCount(), @@ -164,34 +202,6 @@ func importStore( BarEnd: "]", }), ) - - for index := 0; index < len(storeData.Tuples); index += int(maxTuplesPerWrite) { - end := index + int(maxTuplesPerWrite) - if end > len(storeData.Tuples) { - end = len(storeData.Tuples) - } - - writeRequest := client.ClientWriteRequest{ - Writes: storeData.Tuples[index:end], - } - if _, err := tuple.ImportTuples(fgaClient, writeRequest, maxTuplesPerWrite, maxParallelRequests); err != nil { - return nil, fmt.Errorf("failed to import tuples: %w", err) - } - - if err := bar.Add(end - index); err != nil { - return nil, fmt.Errorf("failed to update progress bar: %w", err) - } - - // Introduce a small delay to smooth out the progress bar rendering - time.Sleep(progressBarUpdateDelay) - } - - // Ensure progress bar is completed and cleared - if err := bar.Finish(); err != nil { - return nil, fmt.Errorf("failed to finish progress bar: %w", err) - } - - return response, nil } // importCmd represents the get command. From fe3f1c8c94d35ddd8f7734ae24e98614372a5287 Mon Sep 17 00:00:00 2001 From: Anirban Pal Date: Thu, 17 Oct 2024 14:17:26 +0700 Subject: [PATCH 3/4] resolved issue in list.go --- cmd/model/list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/model/list.go b/cmd/model/list.go index 516b648..ac61862 100644 --- a/cmd/model/list.go +++ b/cmd/model/list.go @@ -95,9 +95,9 @@ var listCmd = &cobra.Command{ models := authorizationmodel.AuthzModelList{} authzModels := response.AuthorizationModels - for i := range len(authzModels) { + for index := range len(authzModels) { authModel := authorizationmodel.AuthzModel{} - authModel.Set(authzModels[i]) + authModel.Set(authzModels[index]) models.AuthorizationModels = append(models.AuthorizationModels, authModel.DisplayAsJSON(fields)) } From d230f187a0a05579f56d574d120429a2ade104a3 Mon Sep 17 00:00:00 2001 From: Anirban Pal Date: Sat, 19 Oct 2024 01:03:13 +0700 Subject: [PATCH 4/4] fix: remove log to prevent JSON parsing issues (#354) --- cmd/store/import.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/store/import.go b/cmd/store/import.go index cb12c58..9baacf7 100644 --- a/cmd/store/import.go +++ b/cmd/store/import.go @@ -123,7 +123,6 @@ func importStore( } if len(storeData.Tuples) == 0 { - fmt.Println("No tuples to import.") return response, nil } @@ -146,6 +145,7 @@ func createOrUpdateStore( if storeID == "" { return createStore(clientConfig, storeData, format, fileName) } + return updateStore(clientConfig, fgaClient, storeData, format, storeID) } @@ -179,6 +179,7 @@ func importTuples( if err := bar.Finish(); err != nil { return fmt.Errorf("failed to finish progress bar: %w", err) } + return nil }