diff --git a/accounts/pkg/service/v0/index.go b/accounts/pkg/service/v0/index.go index 58517cb802..527af918f8 100644 --- a/accounts/pkg/service/v0/index.go +++ b/accounts/pkg/service/v0/index.go @@ -19,6 +19,17 @@ func (s Service) RebuildIndex(ctx context.Context, request *proto.RebuildIndexRe return err } + accounts := make([]*proto.Account, 0) + if err := s.repo.LoadAccounts(ctx, accounts); err != nil { + return err + } + for i := range accounts { + _, err := s.index.Add(accounts[i]) + if err != nil { + return err + } + } + // TODO: read all the documents and add them to the indexer. return nil diff --git a/accounts/pkg/service/v0/service.go b/accounts/pkg/service/v0/service.go index 01ce8be166..f1b3b22f14 100644 --- a/accounts/pkg/service/v0/service.go +++ b/accounts/pkg/service/v0/service.go @@ -75,7 +75,7 @@ func New(opts ...Option) (s *Service, err error) { func (s Service) buildIndex() (*indexer.Indexer, error) { idx := indexer.CreateIndexer(s.Config) - if err := createIndices(idx, s.Config); err != nil { + if err := recreateContainers(idx, s.Config); err != nil { return nil, err } return idx, nil diff --git a/accounts/pkg/storage/cs3.go b/accounts/pkg/storage/cs3.go index 930144c875..f2b3e06064 100644 --- a/accounts/pkg/storage/cs3.go +++ b/accounts/pkg/storage/cs3.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "net/http" "path" + "path/filepath" "strings" user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" @@ -18,6 +19,7 @@ import ( "github.com/cs3org/reva/pkg/token/manager/jwt" "github.com/owncloud/ocis/accounts/pkg/config" "github.com/owncloud/ocis/accounts/pkg/proto/v0" + olog "github.com/owncloud/ocis/ocis-pkg/log" "google.golang.org/grpc/metadata" ) @@ -90,6 +92,10 @@ func (r CS3Repo) LoadAccount(ctx context.Context, id string, a *proto.Account) ( return err } + return r.loadAccount(id, t, a) +} + +func (r CS3Repo) loadAccount(id string, t string, a *proto.Account) error { resp, err := r.dataProvider.get(r.accountURL(id), t) if err != nil { return err @@ -109,6 +115,36 @@ func (r CS3Repo) LoadAccount(ctx context.Context, id string, a *proto.Account) ( return json.Unmarshal(b, &a) } +// LoadAccounts loads all the accounts from the cs3 api. If ids are given, the result set will be filtered. +func (r CS3Repo) LoadAccounts(ctx context.Context, a []*proto.Account) (err error) { + t, err := r.authenticate(ctx) + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, token.TokenHeader, t) + res, err := r.storageProvider.ListContainer(ctx, &provider.ListContainerRequest{ + Ref: &provider.Reference{ + Spec: &provider.Reference_Path{Path: path.Join("/meta", accountsFolder)}, + }, + }) + if err != nil { + return err + } + + log := olog.NewLogger(olog.Pretty(r.cfg.Log.Pretty), olog.Color(r.cfg.Log.Color), olog.Level(r.cfg.Log.Level)) + for i := range res.Infos { + acc := &proto.Account{} + err := r.loadAccount(filepath.Base(res.Infos[i].Path), t, acc) + if err != nil { + log.Err(err).Msg("could not load account") + continue + } + a = append(a, acc) + } + return nil +} + // DeleteAccount deletes an account via cs3 by id func (r CS3Repo) DeleteAccount(ctx context.Context, id string) (err error) { t, err := r.authenticate(ctx) diff --git a/accounts/pkg/storage/disk.go b/accounts/pkg/storage/disk.go index fddfb6a0ef..ad3b3f1395 100644 --- a/accounts/pkg/storage/disk.go +++ b/accounts/pkg/storage/disk.go @@ -17,8 +17,8 @@ var groupLock sync.Mutex // DiskRepo provides a local filesystem implementation of the Repo interface type DiskRepo struct { - cfg *config.Config - log olog.Logger + cfg *config.Config + log olog.Logger } // NewDiskRepo creates a new disk repo @@ -37,8 +37,8 @@ func NewDiskRepo(cfg *config.Config, log olog.Logger) DiskRepo { } } return DiskRepo{ - cfg: cfg, - log: log, + cfg: cfg, + log: log, } } @@ -70,6 +70,20 @@ func (r DiskRepo) LoadAccount(ctx context.Context, id string, a *proto.Account) return json.Unmarshal(data, a) } +// LoadAccounts loads all the accounts from the local filesystem. If ids are given, the result set will be filtered. +func (r DiskRepo) LoadAccounts(ctx context.Context, a []*proto.Account) (err error) { + root := filepath.Join(r.cfg.Repo.Disk.Path, accountsFolder) + return filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + acc := &proto.Account{} + if err = r.LoadAccount(ctx, filepath.Base(path), acc); err != nil { + r.log.Err(err).Msg("could not load account") + return nil + } + a = append(a, acc) + return nil + }) +} + // DeleteAccount from the local filesystem func (r DiskRepo) DeleteAccount(ctx context.Context, id string) (err error) { path := filepath.Join(r.cfg.Repo.Disk.Path, accountsFolder, id) diff --git a/accounts/pkg/storage/repo.go b/accounts/pkg/storage/repo.go index 27f278876a..6833af76ff 100644 --- a/accounts/pkg/storage/repo.go +++ b/accounts/pkg/storage/repo.go @@ -2,18 +2,20 @@ package storage import ( "context" + "github.com/owncloud/ocis/accounts/pkg/proto/v0" ) const ( accountsFolder = "accounts" - groupsFolder = "groups" + groupsFolder = "groups" ) // Repo defines the storage operations type Repo interface { WriteAccount(ctx context.Context, a *proto.Account) (err error) LoadAccount(ctx context.Context, id string, a *proto.Account) (err error) + LoadAccounts(ctx context.Context, a []*proto.Account) (err error) DeleteAccount(ctx context.Context, id string) (err error) WriteGroup(ctx context.Context, g *proto.Group) (err error) LoadGroup(ctx context.Context, id string, g *proto.Group) (err error)