Skip to content

Commit

Permalink
Add support for store-id
Browse files Browse the repository at this point in the history
  • Loading branch information
poovamraj committed Jan 5, 2024
1 parent a0ddd2d commit 9298c09
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions cmd/store/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package store
import (
"context"
"fmt"
"github.com/openfga/cli/cmd/model"
"github.com/openfga/cli/internal/authorizationmodel"
"os"
"path"

Expand All @@ -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")

Check warning on line 43 in cmd/store/import.go

View workflow job for this annotation

GitHub Actions / Lints

var-naming: var storeId should be storeID (revive)
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
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 9298c09

Please sign in to comment.