Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import tuples job #442

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions cmd/import/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
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 _import

import (
"fmt"
"github.com/openfga/cli/internal/cmdutils"
"github.com/openfga/cli/internal/job"
"github.com/openfga/cli/internal/storage"
"github.com/openfga/cli/internal/tuplefile"
"github.com/openfga/go-sdk/client"
"github.com/spf13/cobra"
_ "modernc.org/sqlite"
)

// ImportJobTuples receives a client.ClientWriteRequest and imports the tuples to the store. It can be used to import
// either writes or deletes.
// It returns a pointer to an ImportResponse and an error.
// The ImportResponse contains the tuples that were successfully imported and the tuples that failed to be imported.
// Deletes and writes are put together in the same ImportResponse.
func ImportJobTuples(
fgaClient client.SdkClient,
tuples []client.ClientTupleKey,
storeID string,
requestRate int,
maxRequests int,
rampIntervalInSeconds int64,
) error {
conn, err := storage.NewDatabase()
if err != nil {
return err
}
bulkJobID, err := job.CreateJob(conn, storeID, tuples)
if err != nil {
return err
}
fmt.Printf("Job created successfully - %s\n", bulkJobID)

err = job.ImportTuples(conn, bulkJobID, fgaClient, requestRate, maxRequests, rampIntervalInSeconds)
if err != nil {
return err
}

success, failed, err := storage.GetSummary(conn, bulkJobID)
fmt.Printf("The status for Job ID - %s: Success - %d, Failed - %d", bulkJobID, success, failed)
return nil
}

// createCmd represents the import command.
var createCmd = &cobra.Command{
Use: "create",
Short: "Import Relationship Tuples as a job",
Long: "Imports Relationship Tuples to the store. " +
"This will write the tuples in chunks and at the end will report the tuple chunks that failed.",
RunE: func(cmd *cobra.Command, _ []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)
}

fgaClient, err := clientConfig.GetFgaClient()
if err != nil {
return fmt.Errorf("failed to initialize FGA Client due to %w", err)
}

fileName, err := cmd.Flags().GetString("file")
if err != nil {
return fmt.Errorf("failed to parse file name due to %w", err)
}

initialRequestRate, err := cmd.Flags().GetInt("initial-request-rate")
if initialRequestRate <= 0 || err != nil {
initialRequestRate = 20
}

maxRequests, err := cmd.Flags().GetInt("max-requests")
if maxRequests <= 0 || err != nil {
maxRequests = 2000
}

rampInterval, err := cmd.Flags().GetInt64("ramp-interval-seconds")
if rampInterval <= 0 || err != nil {
rampInterval = 120
}

var tuples []client.ClientTupleKey
tuples, err = tuplefile.ReadTupleFile(fileName)
if err != nil {
return err //nolint:wrapcheck
}

err = ImportJobTuples(fgaClient, tuples, storeID, initialRequestRate, maxRequests, rampInterval)
if err != nil {
return err
}

return nil
},
}

func init() {
createCmd.Flags().String("file", "", "Tuples file")
createCmd.Flags().String("store-id", "", "Store ID")
}
36 changes: 36 additions & 0 deletions cmd/import/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
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 _import contains commands to manage import job into OpenFGA.
package _import

import (
"github.com/spf13/cobra"
)

// ImportCmd represents the store command.
var ImportCmd = &cobra.Command{
Use: "import",
Short: "Create a job to insert a large volume of tuples into OpenFGA",
Long: "import jobs are backed by database that can track the successful inserts of tuples and we can retry failed inserts or follow the status of the job.",
}

func init() {
ImportCmd.AddCommand(createCmd)
ImportCmd.AddCommand(statusCmd)
ImportCmd.AddCommand(retryCmd)
ImportCmd.AddCommand(listCmd)
}
45 changes: 45 additions & 0 deletions cmd/import/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
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 _import

import (
"fmt"
"github.com/openfga/cli/internal/storage"
"github.com/spf13/cobra"
_ "modernc.org/sqlite"
)

// listCmd represents the import command.
var listCmd = &cobra.Command{
Use: "list",
Short: "List all the import jobs",
Long: "List all the import jobs",
RunE: func(cmd *cobra.Command, _ []string) error {
conn, err := storage.NewDatabase()
if err != nil {
return err
}
results, err := storage.GetAllJobs(conn)
if err != nil {
return err
}
for _, result := range results {
fmt.Println(result)
}
return nil
},
}
74 changes: 74 additions & 0 deletions cmd/import/retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
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 _import

import (
"fmt"
"github.com/openfga/cli/internal/cmdutils"
"github.com/openfga/cli/internal/job"
"github.com/openfga/cli/internal/storage"
"github.com/spf13/cobra"
_ "modernc.org/sqlite"
)

// statusCmd represents the import command.
var retryCmd = &cobra.Command{
Use: "retry",
Short: "Retry a import job",
Long: "Retry a import job",
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)
conn, err := storage.NewDatabase()
bulkJobID, err := cmd.Flags().GetString("job-id")
if err != nil {
return fmt.Errorf("failed to get job-id: %w", err)
}

fgaClient, err := clientConfig.GetFgaClient()
if err != nil {
return fmt.Errorf("failed to initialize FGA Client due to %w", err)
}

initialRequestRate, err := cmd.Flags().GetInt("initial-request-rate")
if initialRequestRate <= 0 || err != nil {
initialRequestRate = 20
}

maxRequests, err := cmd.Flags().GetInt("max-requests")
if maxRequests <= 0 || err != nil {
maxRequests = 2000
}

rampInterval, err := cmd.Flags().GetInt64("ramp-interval-seconds")
if rampInterval <= 0 || err != nil {
rampInterval = 120
}

err = job.ImportTuples(conn, bulkJobID, fgaClient, initialRequestRate, maxRequests, rampInterval)
if err != nil {
return err
}

success, failed, err := storage.GetSummary(conn, bulkJobID)
fmt.Printf("The status for Job ID - %s: Success - %d, Failed - %d", bulkJobID, success, failed)
return nil
},
}

func init() {
retryCmd.Flags().String("job-id", "", "Job ID")
}
51 changes: 51 additions & 0 deletions cmd/import/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
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 _import

import (
"fmt"
"github.com/openfga/cli/internal/storage"
"github.com/spf13/cobra"
_ "modernc.org/sqlite"
)

// statusCmd represents the import command.
var statusCmd = &cobra.Command{
Use: "status",
Short: "Check the status of the import job",
Long: "The status command is used to check if the import job is running.",
RunE: func(cmd *cobra.Command, _ []string) error {
bulkJobID, err := cmd.Flags().GetString("job-id")
if err != nil {
return fmt.Errorf("failed to get job-id: %w", err)
}
conn, err := storage.NewDatabase()
if err != nil {
return err
}
success, failed, err := storage.GetSummary(conn, bulkJobID)
if err != nil {
return err
}
fmt.Printf("The status for Job ID - %s: Success - %d, Failed - %d", bulkJobID, success, failed)
return nil
},
}

func init() {
statusCmd.Flags().String("job-id", "", "Job ID")
}
20 changes: 4 additions & 16 deletions cmd/query/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"

openfga "github.com/openfga/go-sdk"
"github.com/openfga/go-sdk/client"
"github.com/spf13/cobra"

Expand All @@ -35,7 +34,6 @@ func check(
object string,
contextualTuples []client.ClientContextualTupleKey,
queryContext *map[string]interface{},
consistency *openfga.ConsistencyPreference,
) (*client.ClientCheckResponse, error) {
body := &client.ClientCheckRequest{
User: user,
Expand All @@ -46,14 +44,9 @@ func check(
}
options := &client.ClientCheckOptions{}

// Don't set if UNSPECIFIED has been provided, it's the default anyway
if *consistency != openfga.CONSISTENCYPREFERENCE_UNSPECIFIED {
options.Consistency = consistency
}

response, err := fgaClient.Check(context.Background()).Body(*body).Options(*options).Execute()
if err != nil {
return nil, err //nolint:wrapcheck
return nil, fmt.Errorf("failed to check due to %w", err)
}

return response, nil
Expand All @@ -63,7 +56,7 @@ func check(
var checkCmd = &cobra.Command{
Use: "check",
Short: "Check",
Example: `fga query check --store-id="01H4P8Z95KTXXEP6Z03T75Q984" user:anne can_view document:roadmap --context '{"ip_address":"127.0.0.1"}' --consistency "HIGHER_CONSISTENCY"`, //nolint:lll
Example: `fga query check --store-id="01H4P8Z95KTXXEP6Z03T75Q984" user:anne can_view document:roadmap --context '{"ip_address":"127.0.0.1"}'`, //nolint:lll
Long: "Check if a user has a particular relation with an object.",
Args: cobra.ExactArgs(3), //nolint:mnd
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -83,14 +76,9 @@ var checkCmd = &cobra.Command{
return fmt.Errorf("error parsing query context for check: %w", err)
}

consistency, err := cmdutils.ParseConsistencyFromCmd(cmd)
if err != nil {
return fmt.Errorf("error parsing consistency for check: %w", err)
}

response, err := check(fgaClient, args[0], args[1], args[2], contextualTuples, queryContext, consistency)
response, err := check(fgaClient, args[0], args[1], args[2], contextualTuples, queryContext)
if err != nil {
return fmt.Errorf("check failed: %w", err)
return fmt.Errorf("failed to check due to %w", err)
}

return output.Display(*response)
Expand Down
Loading
Loading