diff --git a/accounts/pkg/service/v0/groups.go b/accounts/pkg/service/v0/groups.go index a2b346197d..c7a102d01e 100644 --- a/accounts/pkg/service/v0/groups.go +++ b/accounts/pkg/service/v0/groups.go @@ -228,9 +228,9 @@ func (s Service) DeleteGroup(c context.Context, in *proto.DeleteGroupRequest, ou s.log.Error().Err(err).Str("groupid", id).Str("accountid", g.Members[i].Id).Msg("could not remove account memberof, skipping") } } - if err = os.Remove(path); err != nil { - s.log.Error().Err(err).Str("id", id).Str("path", path).Msg("could not remove group") - return merrors.InternalServerError(s.id, "could not remove group: %v", err.Error()) + + if err = s.repo.DeleteGroup(c, id); err != nil { + return err } if err = s.index.Delete(id); err != nil { diff --git a/accounts/pkg/storage/cs3.go b/accounts/pkg/storage/cs3.go index 71b1ec4789..12f7a55321 100644 --- a/accounts/pkg/storage/cs3.go +++ b/accounts/pkg/storage/cs3.go @@ -14,6 +14,7 @@ import ( merrors "github.com/micro/go-micro/v2/errors" "github.com/owncloud/ocis/accounts/pkg/proto/v0" "google.golang.org/grpc/metadata" + "io/ioutil" "net/http" ) @@ -95,19 +96,147 @@ func (r CS3Repo) LoadAccount(ctx context.Context, id string, a *proto.Account) ( Transport: http.DefaultTransport, } - if _, err = cl.Do(ureq); err != nil { + resp, err := cl.Do(ureq) + if err != nil { + return err + } + + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + defer resp.Body.Close() + + if err := json.Unmarshal(b, &a); err != nil { return err } return nil } +func (r CS3Repo) DeleteAccount(ctx context.Context, id string) (err error) { + t, err := r.authenticate(ctx) + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, token.TokenHeader, t) + + ureq, err := http.NewRequest("DELETE", fmt.Sprintf("http://localhost:9187/data/accounts/%s", id), nil) + if err != nil { + return err + } + + ureq.Header.Add("x-access-token", t) + cl := http.Client{ + Transport: http.DefaultTransport, + } + + resp, err := cl.Do(ureq) + if err != nil { + return err + } + + defer resp.Body.Close() + + return nil +} + +func (r CS3Repo) DeleteGroup(ctx context.Context, id string) (err error) { + t, err := r.authenticate(ctx) + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, token.TokenHeader, t) + + ureq, err := http.NewRequest("DELETE", fmt.Sprintf("http://localhost:9187/data/groups/%s", id), nil) + if err != nil { + return err + } + + ureq.Header.Add("x-access-token", t) + cl := http.Client{ + Transport: http.DefaultTransport, + } + + resp, err := cl.Do(ureq) + if err != nil { + return err + } + + defer resp.Body.Close() + + return nil +} + func (r CS3Repo) WriteGroup(ctx context.Context, g *proto.Group) (err error) { - panic("implement me") + t, err := r.authenticate(ctx) + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, token.TokenHeader, t) + if err := r.makeRootDirIfNotExist(ctx); err != nil { + return err + } + + var by []byte + if by, err = json.Marshal(g); err != nil { + return merrors.InternalServerError(r.serviceID, "could not marshal account: %v", err.Error()) + } + + ureq, err := http.NewRequest("PUT", fmt.Sprintf("http://localhost:9187/data/groups/%s", g.Id), bytes.NewReader(by)) + if err != nil { + return err + } + + ureq.Header.Add("x-access-token", t) + cl := http.Client{ + Transport: http.DefaultTransport, + } + + if _, err := cl.Do(ureq); err != nil { + return err + } + + return nil } func (r CS3Repo) LoadGroup(ctx context.Context, id string, g *proto.Group) (err error) { - panic("implement me") + t, err := r.authenticate(ctx) + if err != nil { + return err + } + + ctx = metadata.AppendToOutgoingContext(ctx, token.TokenHeader, t) + + ureq, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:9187/data/groups/%s", id), nil) + if err != nil { + return err + } + + ureq.Header.Add("x-access-token", t) + cl := http.Client{ + Transport: http.DefaultTransport, + } + + resp, err := cl.Do(ureq) + if err != nil { + return err + } + + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + defer resp.Body.Close() + + if err := json.Unmarshal(b, &g); err != nil { + return err + } + + return nil } func (r CS3Repo) authenticate(ctx context.Context) (token string, err error) { diff --git a/accounts/pkg/storage/disk.go b/accounts/pkg/storage/disk.go index f70fe6ce79..a9ca4b15b5 100644 --- a/accounts/pkg/storage/disk.go +++ b/accounts/pkg/storage/disk.go @@ -7,6 +7,7 @@ import ( "github.com/owncloud/ocis/accounts/pkg/proto/v0" "github.com/rs/zerolog" "io/ioutil" + "os" "path/filepath" "sync" ) @@ -115,6 +116,26 @@ func (r DiskRepo) deflateMemberOf(a *proto.Account) { a.MemberOf = deflated } +func (r DiskRepo) DeleteAccount(ctx context.Context, id string) (err error) { + path := filepath.Join(r.dataPath, "accounts", id) + if err = os.Remove(path); err != nil { + r.log.Error().Err(err).Str("id", id).Str("path", path).Msg("could not remove account") + return merrors.InternalServerError(r.serviceID, "could not remove account: %v", err.Error()) + } + + return nil +} + +func (r DiskRepo) DeleteGroup(ctx context.Context, id string) (err error) { + path := filepath.Join(r.dataPath, "groups", id) + if err = os.Remove(path); err != nil { + r.log.Error().Err(err).Str("id", id).Str("path", path).Msg("could not remove group") + return merrors.InternalServerError(r.serviceID, "could not remove group: %v", err.Error()) + } + + return nil +} + // deflateMembers replaces the users of a group with an instance that only contains the id func (r DiskRepo) deflateMembers(g *proto.Group) { if g == nil { diff --git a/accounts/pkg/storage/repo.go b/accounts/pkg/storage/repo.go index bb060eee75..bac46dc639 100644 --- a/accounts/pkg/storage/repo.go +++ b/accounts/pkg/storage/repo.go @@ -8,6 +8,8 @@ import ( type Repo interface { WriteAccount(ctx context.Context, a *proto.Account) (err error) LoadAccount(ctx context.Context, id string, 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) + DeleteGroup(ctx context.Context, id string) (err error) }