-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract a common method to import and parse the store file Support writing a store Removed public write method Add documentation Rename method name Fix lint issues Update cmd/store/write.go Update cmd/store/write.go Update cmd/store/write.go Update cmd/store/write.go Change the write command to import Fix documentation with import Rename insert to import Add support for store-id rename variable to satisfy linter Running gofumpt Co-Authored-By: Raghd Hamzeh <[email protected]>
- Loading branch information
Showing
8 changed files
with
188 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
Copyright © 2023 OpenFGA | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package store | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
"github.com/openfga/cli/cmd/model" | ||
"github.com/openfga/cli/internal/authorizationmodel" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/openfga/cli/internal/cmdutils" | ||
"github.com/openfga/cli/internal/output" | ||
"github.com/openfga/cli/internal/storetest" | ||
) | ||
|
||
// importCmd represents the get command. | ||
var importCmd = &cobra.Command{ | ||
Use: "import", | ||
Short: "Import Store Data", | ||
Long: `Import a store: updating the name, model and appending the global tuples`, | ||
Example: "fga store import --file=model.fga.yaml", | ||
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 | ||
} | ||
|
||
format, storeData, err := storetest.ReadFromFile(fileName, path.Dir(fileName)) | ||
if err != nil { | ||
return err //nolint:wrapcheck | ||
} | ||
|
||
fgaClient, err := clientConfig.GetFgaClient() | ||
if err != nil { | ||
return fmt.Errorf("failed to initialize FGA Client due to %w", err) | ||
} | ||
|
||
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 | ||
} | ||
|
||
return output.Display(output.EmptyStruct{}) //nolint:wrapcheck | ||
}, | ||
} | ||
|
||
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) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Copyright © 2023 OpenFGA | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package storetest | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/openfga/cli/internal/authorizationmodel" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// ReadFromFile is used to read and parse the Store file. | ||
func ReadFromFile(fileName string, basePath string) (authorizationmodel.ModelFormat, *StoreData, error) { | ||
format := authorizationmodel.ModelFormatDefault | ||
|
||
var storeData StoreData | ||
|
||
testFile, err := os.Open(fileName) | ||
if err != nil { | ||
return format, nil, fmt.Errorf("failed to read file %s due to %w", fileName, err) | ||
} | ||
|
||
decoder := yaml.NewDecoder(testFile) | ||
decoder.KnownFields(true) | ||
err = decoder.Decode(&storeData) | ||
|
||
if err != nil { | ||
return format, nil, fmt.Errorf("failed to unmarshal file %s due to %w", fileName, err) | ||
} | ||
|
||
if format, err := storeData.LoadModel(basePath); err != nil { | ||
return format, nil, err | ||
} | ||
|
||
return format, &storeData, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters