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

feat: support start time parameter on read changes #443

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ type document
| [Write Relationship Tuples](#write-relationship-tuples) | `write` | `--store-id`, `--model-id` | `fga tuple write user:anne can_view document:roadmap --store-id=01H0H015178Y2V4CX10C2KGHF4` |
| [Delete Relationship Tuples](#delete-relationship-tuples) | `delete` | `--store-id`, `--model-id` | `fga tuple delete user:anne can_view document:roadmap --store-id=01H0H015178Y2V4CX10C2KGHF4` |
| [Read Relationship Tuples](#read-relationship-tuples) | `read` | `--store-id`, `--model-id` | `fga tuple read --store-id=01H0H015178Y2V4CX10C2KGHF4 --model-id=01GXSA8YR785C4FYS3C0RTG7B1` |
| [Read Relationship Tuple Changes (Watch)](#read-relationship-tuple-changes-watch) | `changes` | `--store-id`, `--type`, `--continuation-token`, | `fga tuple changes --store-id=01H0H015178Y2V4CX10C2KGHF4 --type=document --continuation-token=M3w=` |
| [Read Relationship Tuple Changes (Watch)](#read-relationship-tuple-changes-watch) | `changes` | `--store-id`, `--type`, `--start-time`, `--continuation-token`, | `fga tuple changes --store-id=01H0H015178Y2V4CX10C2KGHF4 --type=document --start-time=2022-01-01T00:00:00Z --continuation-token=M3w=` |
| [Import Relationship Tuples](#import-relationship-tuples) | `import` | `--store-id`, `--model-id`, `--file` | `fga tuple import --store-id=01H0H015178Y2V4CX10C2KGHF4 --model-id=01GXSA8YR785C4FYS3C0RTG7B1 --file tuples.json` |

##### Write Relationship Tuples
Expand Down Expand Up @@ -896,6 +896,7 @@ fga tuple **changes** --type <type> --store-id=<store-id>
###### Parameters
* `--store-id`: Specifies the store id
* `--type`: Restrict to a specific type (optional)
* `--start-time`: Return changes since a specified time
* `--max-pages`: Max number of pages to retrieve (default: 20)
* `--continuation-token`: Continuation token to start changes from

Expand Down
15 changes: 11 additions & 4 deletions cmd/tuple/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@
var MaxReadChangesPagesLength = 20

func readChanges(
fgaClient client.SdkClient, maxPages int, selectedType string, continuationToken string,
fgaClient client.SdkClient, maxPages int, selectedType string, startTime string, continuationToken string,
) (*openfga.ReadChangesResponse, error) {
changes := []openfga.TupleChange{}
pageIndex := 0

for {
body := &client.ClientReadChangesRequest{
Type: selectedType,
Type: selectedType,
StartTime: startTime,

Check failure on line 43 in cmd/tuple/changes.go

View workflow job for this annotation

GitHub Actions / Security Audits

unknown field StartTime in struct literal of type client.ClientReadChangesRequest

Check failure on line 43 in cmd/tuple/changes.go

View workflow job for this annotation

GitHub Actions / Tests

unknown field StartTime in struct literal of type client.ClientReadChangesRequest

Check failure on line 43 in cmd/tuple/changes.go

View workflow job for this annotation

GitHub Actions / Lints

unknown field StartTime in struct literal of type client.ClientReadChangesRequest) (typecheck)

Check failure on line 43 in cmd/tuple/changes.go

View workflow job for this annotation

GitHub Actions / Lints

unknown field StartTime in struct literal of type client.ClientReadChangesRequest) (typecheck)

Check failure on line 43 in cmd/tuple/changes.go

View workflow job for this annotation

GitHub Actions / Lints

unknown field StartTime in struct literal of type client.ClientReadChangesRequest
}
options := &client.ClientReadChangesOptions{
ContinuationToken: &continuationToken,
Expand Down Expand Up @@ -70,7 +71,7 @@
Use: "changes",
Short: "Read Relationship Tuple Changes (Watch)",
Long: "Get a list of relationship tuple changes (Writes and Deletes) across time.",
Example: "fga tuple changes --store-id=01H0H015178Y2V4CX10C2KGHF4 --type document --continuation-token=MXw=",
Example: "fga tuple changes --store-id=01H0H015178Y2V4CX10C2KGHF4 --type document --start-time 2022-01-01T00:00:00Z --continuation-token=MXw=",
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)

Expand All @@ -89,12 +90,17 @@
return fmt.Errorf("failed to get tuple changes due to %w", err)
}

startTime, err := cmd.Flags().GetString("start-time")
if err != nil {
return fmt.Errorf("failed to get tuple changes due to %w", err)
}

continuationToken, err := cmd.Flags().GetString("continuation-token")
if err != nil {
return fmt.Errorf("failed to get tuple changes due to %w", err)
}

response, err := readChanges(fgaClient, maxPages, selectedType, continuationToken)
response, err := readChanges(fgaClient, maxPages, selectedType, startTime, continuationToken)
if err != nil {
return err
}
Expand All @@ -105,6 +111,7 @@

func init() {
changesCmd.Flags().String("type", "", "Type to restrict the changes by.")
changesCmd.Flags().String("start-time", "", "Time to return changes since.")
changesCmd.Flags().Int("max-pages", MaxReadChangesPagesLength, "Max number of pages to get.")
changesCmd.Flags().String("continuation-token", "", "Continuation token to start changes from.")
}
Loading