From 9298c0983dfe8efdcd935d5e23df3741788d0592 Mon Sep 17 00:00:00 2001 From: Poovamraj T T Date: Fri, 5 Jan 2024 23:16:48 +0100 Subject: [PATCH] Add support for store-id --- cmd/store/import.go | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/cmd/store/import.go b/cmd/store/import.go index 870110d..29ddaec 100644 --- a/cmd/store/import.go +++ b/cmd/store/import.go @@ -19,6 +19,8 @@ package store import ( "context" "fmt" + "github.com/openfga/cli/cmd/model" + "github.com/openfga/cli/internal/authorizationmodel" "os" "path" @@ -38,6 +40,11 @@ var importCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { clientConfig := cmdutils.GetClientConfig(cmd) + storeId, err := cmd.Flags().GetString("store-id") + if err != nil { + return fmt.Errorf("failed to get store-id %w", err) + } + fileName, err := cmd.Flags().GetString("file") if err != nil { return err //nolint:wrapcheck @@ -48,17 +55,38 @@ var importCmd = &cobra.Command{ return err //nolint:wrapcheck } - createStoreAndModelResponse, err := CreateStoreWithModel(clientConfig, storeData.Name, storeData.Model, format) + fgaClient, err := clientConfig.GetFgaClient() if err != nil { - return err + return fmt.Errorf("failed to initialize FGA Client due to %w", err) } - clientConfig.StoreID = createStoreAndModelResponse.Store.Id - fgaClient, err := clientConfig.GetFgaClient() + if storeId == "" { + createStoreAndModelResponse, err := CreateStoreWithModel(clientConfig, storeData.Name, storeData.Model, format) + if err != nil { + return err + } + clientConfig.StoreID = createStoreAndModelResponse.Store.Id + } else { + authModel := authorizationmodel.AuthzModel{} + clientConfig.StoreID = storeId + if format == authorizationmodel.ModelFormatJSON { + err = authModel.ReadFromJSONString(storeData.Model) + } else { + err = authModel.ReadFromDSLString(storeData.Model) + } + if err != nil { + return err //nolint:wrapcheck + } + + _, err := model.Write(fgaClient, authModel) + if err != nil { + return fmt.Errorf("failed to write model due to %w", err) + } + } + fgaClient, err = clientConfig.GetFgaClient() if err != nil { return fmt.Errorf("failed to initialize FGA Client due to %w", err) } - _, err = fgaClient.WriteTuples(context.Background()).Body(storeData.Tuples).Execute() if err != nil { return err //nolint:wrapcheck @@ -70,6 +98,7 @@ var importCmd = &cobra.Command{ func init() { importCmd.Flags().String("file", "", "File Name. The file should have the store") + importCmd.Flags().String("store-id", "", "Store ID") if err := importCmd.MarkFlagRequired("file"); err != nil { fmt.Printf("error setting flag as required - %v: %v\n", "cmd/models/write", err)