From a1f7cbbc9004e786aa05c9b911b665c3a003e401 Mon Sep 17 00:00:00 2001 From: Ryan Quinn Date: Wed, 22 Jan 2025 11:40:53 -0600 Subject: [PATCH] feat: support start time parameter on read changes feat: support start time parameter on read changes --- README.md | 3 ++- cmd/tuple/changes.go | 37 ++++++++++++++++++++++++++++++------ cmd/tuple/changes_test.go | 40 +++++++++++++++++++++++++++++++-------- go.mod | 2 +- go.sum | 2 ++ 5 files changed, 68 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b38ff51..26e2a65 100644 --- a/README.md +++ b/README.md @@ -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 @@ -896,6 +896,7 @@ fga tuple **changes** --type --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 diff --git a/cmd/tuple/changes.go b/cmd/tuple/changes.go index 1a3fab0..538dae9 100644 --- a/cmd/tuple/changes.go +++ b/cmd/tuple/changes.go @@ -19,6 +19,7 @@ package tuple import ( "context" "fmt" + "time" openfga "github.com/openfga/go-sdk" "github.com/openfga/go-sdk/client" @@ -32,15 +33,32 @@ import ( 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 + var startTimeObj *time.Time + + if startTime != "" { + layout := "2006-01-02T15:04:05Z" + + parsedTime, err := time.Parse(layout, startTime) + if err != nil { + return nil, fmt.Errorf("failed to parse startTime: %w", err) + } + + startTimeObj = &parsedTime + } + for { body := &client.ClientReadChangesRequest{ Type: selectedType, } + if startTimeObj != nil { + body.StartTime = *startTimeObj + } + options := &client.ClientReadChangesOptions{ ContinuationToken: &continuationToken, } @@ -67,10 +85,11 @@ func readChanges( // changesCmd represents the changes command. var changesCmd = &cobra.Command{ - 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=", + 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 + --start-time 2022-01-01T00:00:00Z --continuation-token=MXw=`, RunE: func(cmd *cobra.Command, _ []string) error { clientConfig := cmdutils.GetClientConfig(cmd) @@ -89,12 +108,17 @@ var changesCmd = &cobra.Command{ 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 } @@ -105,6 +129,7 @@ var changesCmd = &cobra.Command{ 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.") } diff --git a/cmd/tuple/changes_test.go b/cmd/tuple/changes_test.go index 6dbfbff..fbecc86 100644 --- a/cmd/tuple/changes_test.go +++ b/cmd/tuple/changes_test.go @@ -44,7 +44,7 @@ func TestReadChangesError(t *testing.T) { mockFgaClient.EXPECT().ReadChanges(context.Background()).Return(mockBody) - _, err := readChanges(mockFgaClient, 5, "document", "") + _, err := readChanges(mockFgaClient, 5, "document", "", "") if err == nil { t.Error("Expect error but there is none") } @@ -82,7 +82,7 @@ func TestReadChangesEmpty(t *testing.T) { mockFgaClient.EXPECT().ReadChanges(context.Background()).Return(mockBody) - output, err := readChanges(mockFgaClient, 5, "document", "") + output, err := readChanges(mockFgaClient, 5, "document", "", "") if err != nil { t.Error(err) } @@ -102,6 +102,8 @@ func TestReadChangesEmpty(t *testing.T) { func TestReadChangesSinglePage(t *testing.T) { t.Parallel() + const layout = "2006-01-02T15:04:05Z" + mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() mockFgaClient := mock_client.NewMockSdkClient(mockCtrl) @@ -139,14 +141,20 @@ func TestReadChangesSinglePage(t *testing.T) { mockBody := mock_client.NewMockSdkClientReadChangesRequestInterface(mockCtrl) + sTime, err := time.Parse(layout, "2022-01-01T00:00:00Z") + if err != nil { + t.Error(err) + } + body := client.ClientReadChangesRequest{ - Type: "document", + Type: "document", + StartTime: sTime, } mockBody.EXPECT().Body(body).Return(mockRequest) mockFgaClient.EXPECT().ReadChanges(context.Background()).Return(mockBody) - output, err := readChanges(mockFgaClient, 5, "document", "") + output, err := readChanges(mockFgaClient, 5, "document", "2022-01-01T00:00:00Z", "") if err != nil { t.Error(err) } @@ -166,6 +174,8 @@ func TestReadChangesSinglePage(t *testing.T) { func TestReadChangesMultiPages(t *testing.T) { t.Parallel() + const layout = "2006-01-02T15:04:05Z" + mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() @@ -232,8 +242,14 @@ func TestReadChangesMultiPages(t *testing.T) { mockBody1 := mock_client.NewMockSdkClientReadChangesRequestInterface(mockCtrl) mockBody2 := mock_client.NewMockSdkClientReadChangesRequestInterface(mockCtrl) + sTime, err := time.Parse(layout, "2022-01-01T00:00:00Z") + if err != nil { + t.Error(err) + } + body := client.ClientReadChangesRequest{ - Type: "document", + Type: "document", + StartTime: sTime, } gomock.InOrder( @@ -246,7 +262,7 @@ func TestReadChangesMultiPages(t *testing.T) { mockFgaClient.EXPECT().ReadChanges(context.Background()).Return(mockBody2), ) - output, err := readChanges(mockFgaClient, 5, "document", "") + output, err := readChanges(mockFgaClient, 5, "document", "2022-01-01T00:00:00Z", "") if err != nil { t.Error(err) } @@ -266,6 +282,8 @@ func TestReadChangesMultiPages(t *testing.T) { func TestReadChangesMultiPagesLimit(t *testing.T) { t.Parallel() + const layout = "2006-01-02T15:04:05Z" + mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() mockFgaClient := mock_client.NewMockSdkClient(mockCtrl) @@ -303,14 +321,20 @@ func TestReadChangesMultiPagesLimit(t *testing.T) { mockBody := mock_client.NewMockSdkClientReadChangesRequestInterface(mockCtrl) + sTime, err := time.Parse(layout, "2022-01-01T00:00:00Z") + if err != nil { + t.Error(err) + } + body := client.ClientReadChangesRequest{ - Type: "document", + Type: "document", + StartTime: sTime, } mockBody.EXPECT().Body(body).Return(mockRequest) mockFgaClient.EXPECT().ReadChanges(context.Background()).Return(mockBody) - output, err := readChanges(mockFgaClient, 1, "document", "") + output, err := readChanges(mockFgaClient, 1, "document", "2022-01-01T00:00:00Z", "") if err != nil { t.Error(err) } diff --git a/go.mod b/go.mod index 8d49505..9707dfb 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/nwidger/jsoncolor v0.3.2 github.com/oklog/ulid/v2 v2.1.0 github.com/openfga/api/proto v0.0.0-20250107154247-c22e6db5c4f5 - github.com/openfga/go-sdk v0.6.4-0.20250107171931-2adebcc8c8bc + github.com/openfga/go-sdk v0.6.4 github.com/openfga/language/pkg/go v0.2.0-beta.2.0.20241115164311-10e575c8e47c github.com/openfga/openfga v1.8.4 github.com/spf13/cobra v1.8.1 diff --git a/go.sum b/go.sum index e9000ef..9c94166 100644 --- a/go.sum +++ b/go.sum @@ -182,6 +182,8 @@ github.com/openfga/api/proto v0.0.0-20250107154247-c22e6db5c4f5 h1:z9jaRoo+NIN1A github.com/openfga/api/proto v0.0.0-20250107154247-c22e6db5c4f5/go.mod h1:m74TNgnAAIJ03gfHcx+xaRWnr+IbQy3y/AVNwwCFrC0= github.com/openfga/go-sdk v0.6.4-0.20250107171931-2adebcc8c8bc h1:E7x5UZbNIbcCOhBRD3cqrmQz35qNNceMnZPb6UxZRnw= github.com/openfga/go-sdk v0.6.4-0.20250107171931-2adebcc8c8bc/go.mod h1:zui7pHE3eLAYh2fFmEMrWg9XbxYns2WW5Xr/GEgili4= +github.com/openfga/go-sdk v0.6.4 h1:VAeH9exZuG1qaBt41+mjIK5RxTtuL9maS4d47YsYGZw= +github.com/openfga/go-sdk v0.6.4/go.mod h1:zui7pHE3eLAYh2fFmEMrWg9XbxYns2WW5Xr/GEgili4= github.com/openfga/language/pkg/go v0.2.0-beta.2.0.20241115164311-10e575c8e47c h1:1y84C0V4NRfPtRi4MqQ7+gnFtYgeBKPIeIAPLdVJ7j4= github.com/openfga/language/pkg/go v0.2.0-beta.2.0.20241115164311-10e575c8e47c/go.mod h1:12RMe/HuRNyOzS33RQa53jwdcxE2znr8ycXMlVbgQN4= github.com/openfga/openfga v1.8.4 h1:OqyRpuxMCxcS7irTFYFkhAIYzmAnczNwxUqjnuZOQyo=