Skip to content

Commit

Permalink
WIP clone-prev-sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-lee-12 committed Nov 10, 2023
1 parent 2919792 commit 2ac90c1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/sync/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func (s ActionOp) String() string {
return "grant-expansion"
case PartialResourceSyncOp:
return "partial-resource-sync"
case ClonePreviousSyncOp:
return "clone-previous-sync"
default:
return "unknown"
}
Expand Down Expand Up @@ -90,6 +92,8 @@ func newActionOp(str string) ActionOp {
return SyncGrantExpansionOp
case PartialResourceSyncOp.String():
return PartialResourceSyncOp
case ClonePreviousSyncOp.String():
return ClonePreviousSyncOp
default:
return UnknownOp
}
Expand All @@ -106,6 +110,7 @@ const (
SyncAssetsOp
SyncGrantExpansionOp
PartialResourceSyncOp
ClonePreviousSyncOp
)

// Action stores the current operation, page token, and optional fields for which resource is being worked with.
Expand Down
52 changes: 52 additions & 0 deletions pkg/sync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"time"

"github.com/davecgh/go-spew/spew"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
Expand All @@ -18,6 +19,7 @@ import (
reader_v2 "github.com/conductorone/baton-sdk/pb/c1/reader/v2"
"github.com/conductorone/baton-sdk/pkg/annotations"
"github.com/conductorone/baton-sdk/pkg/connectorstore"
"github.com/conductorone/baton-sdk/pkg/dotc1z"
"github.com/conductorone/baton-sdk/pkg/dotc1z/manager"
"github.com/conductorone/baton-sdk/pkg/types"
)
Expand Down Expand Up @@ -154,6 +156,8 @@ func (s *syncer) Sync(ctx context.Context) error {
if s.partialSyncResourceId != nil {
s.state.PushAction(ctx, Action{Op: PartialResourceSyncOp})
s.state.PushAction(ctx, Action{Op: SyncResourceTypesOp})
// clones prev sync
s.state.PushAction(ctx, Action{Op: ClonePreviousSyncOp})
} else {
s.state.PushAction(ctx, Action{Op: SyncGrantExpansionOp})
s.state.PushAction(ctx, Action{Op: SyncGrantsOp})
Expand Down Expand Up @@ -223,6 +227,13 @@ func (s *syncer) Sync(ctx context.Context) error {
return err
}
continue
case ClonePreviousSyncOp:
fmt.Printf("\n\n\n Cloning previous sync... \n\n\n")

Check failure on line 231 in pkg/sync/syncer.go

View workflow job for this annotation

GitHub Actions / go-lint

use of `fmt.Printf` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
err := s.clonePreviousSync(ctx)
if err != nil {
return err
}
continue
default:
return fmt.Errorf("unexpected sync step")
}
Expand Down Expand Up @@ -425,6 +436,7 @@ func (s *syncer) partialSyncResource(ctx context.Context) error {

// Try to retrieve the ID that corresponds to the previous sync.
// Partial sync should NOT run if a previous full sync does not exist.
/*
var previousSyncID string

Check failure on line 440 in pkg/sync/syncer.go

View workflow job for this annotation

GitHub Actions / go-lint

File is not `goimports`-ed (goimports)
if psf, ok := s.store.(lastestSyncFetcher); ok {
Expand All @@ -436,6 +448,7 @@ func (s *syncer) partialSyncResource(ctx context.Context) error {
if previousSyncID == "" {
return fmt.Errorf("previous full sync file does not exist")
}
*/

resp, err := s.connector.FetchResource(ctx, req)
if err != nil {
Expand All @@ -450,6 +463,10 @@ func (s *syncer) partialSyncResource(ctx context.Context) error {
}

//err = s.store.PutResourceType(ctx, resp.ResourceType)

Check failure on line 465 in pkg/sync/syncer.go

View workflow job for this annotation

GitHub Actions / go-lint

commentFormatting: put a space between `//` and comment text (gocritic)
// Copy over all existing data
// Modify the corresponding res.
// Create if not exist


err = s.store.PutResource(ctx, resp.Resource)
if err != nil {
Expand All @@ -464,6 +481,41 @@ func (s *syncer) partialSyncResource(ctx context.Context) error {
return nil
}

func (s *syncer) clonePreviousSync(ctx context.Context) error {
// Try to retrieve the ID that corresponds to the previous sync.
// Partial sync should NOT run if a previous full sync does not exist.
var previousSyncID string
var err error

if psf, ok := s.store.(lastestSyncFetcher); ok {
previousSyncID, err = psf.LatestFinishedSync(ctx)
if err != nil {
return fmt.Errorf("unable to fetch previous sync: %s", err.Error())
}
}
if previousSyncID == "" {
return fmt.Errorf("previous full sync file does not exist")
}

c1file, err := dotc1z.NewC1File(ctx, "./sync.txt")

spew.Dump("newc1file erro", err)

c1fileptr := *c1file
err = c1fileptr.CloneSync(ctx, "./bar.txt", previousSyncID)

spew.Dump("clone sync err", err)

if err != nil {
// fmt.Errorf(err)
return err
}

s.state.FinishAction(ctx)

return nil
}

func (s *syncer) validateResourceTraits(ctx context.Context, r *v2.Resource) error {
resourceTypeResponse, err := s.store.GetResourceType(ctx, &reader_v2.ResourceTypesReaderServiceGetResourceTypeRequest{
ResourceTypeId: r.Id.ResourceType,
Expand Down

0 comments on commit 2ac90c1

Please sign in to comment.