Skip to content

Commit

Permalink
impl delete manifest to match hosted directory (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
gertd authored Nov 21, 2023
1 parent af598b3 commit a61f4d9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
32 changes: 25 additions & 7 deletions pkg/directory/v3/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"strconv"

mod "github.com/aserto-dev/azm/model"
manifest "github.com/aserto-dev/azm/v3"
dsm3 "github.com/aserto-dev/go-directory/aserto/directory/model/v3"
"github.com/aserto-dev/go-directory/pkg/derr"
Expand Down Expand Up @@ -220,20 +219,39 @@ func (s *Model) DeleteManifest(ctx context.Context, req *dsm3.DeleteManifestRequ
return resp, derr.ErrProtoValidate.Msg(err.Error())
}

md := &dsm3.Metadata{}
h := fnv.New64a()
h.Reset()

data := bytes.NewBuffer([]byte{})
_, _ = h.Write(data.Bytes())

md := &dsm3.Metadata{
UpdatedAt: timestamppb.Now(),
Etag: strconv.FormatUint(h.Sum64(), 10),
}

m, err := manifest.Load(bytes.NewReader(data.Bytes()))
if err != nil {
return resp, derr.ErrInvalidArgument.Msg(err.Error())
}

if err := s.store.DB().Update(func(tx *bolt.Tx) error {
if err := ds.Manifest(md).Delete(ctx, tx); err != nil {
if err := ds.Manifest(&dsm3.Metadata{}).Delete(ctx, tx); err != nil {
return derr.ErrUnknown.Msgf("failed to delete manifest: %s", err.Error())
}

if err := ds.Manifest(md).Set(ctx, tx, data); err != nil {
return derr.ErrUnknown.Msgf("failed to set manifest: %s", err.Error())
}

if err := ds.Manifest(md).SetModel(ctx, tx, m); err != nil {
return derr.ErrUnknown.Msgf("failed to set model: %s", err.Error())
}

return nil
}); err != nil {
return resp, err
}

if err := s.store.MC().UpdateModel(&mod.Model{}); err != nil {
return resp, derr.ErrUnknown.Msgf("failed to update model: %s", err.Error())
}

return &dsm3.DeleteManifestResponse{Result: &emptypb.Empty{}}, nil
}
34 changes: 29 additions & 5 deletions pkg/ds/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,38 @@ func (m *manifest) SetModel(ctx context.Context, tx *bolt.Tx, mod *model.Model)
return nil
}

// Delete, removes the manifest from the _manifest bucket using key=name:version,
// if not version is provided, version will be set to latest.
// !!! NOTE: delete manifest is a destructive operation !!!
//
// Delete:
// sets the manifest to an empty manifest,
// updates the model accordingly,
// deletes and recreates the objects and relations buckets.
func (m *manifest) Delete(ctx context.Context, tx *bolt.Tx) error {
if ok, _ := bdb.BucketExists(tx, bdb.ManifestPath); !ok {
return nil
if err := bdb.DeleteBucket(tx, bdb.ManifestPath); err != nil {
return err
}
if _, err := bdb.CreateBucket(tx, bdb.ManifestPath); err != nil {
return err
}

if err := bdb.DeleteBucket(tx, bdb.ManifestPath); err != nil {
if err := bdb.DeleteBucket(tx, bdb.ObjectsPath); err != nil {
return err
}
if _, err := bdb.CreateBucket(tx, bdb.ObjectsPath); err != nil {
return err
}

if err := bdb.DeleteBucket(tx, bdb.RelationsObjPath); err != nil {
return err
}
if _, err := bdb.CreateBucket(tx, bdb.RelationsObjPath); err != nil {
return err
}

if err := bdb.DeleteBucket(tx, bdb.RelationsSubPath); err != nil {
return err
}
if _, err := bdb.CreateBucket(tx, bdb.RelationsSubPath); err != nil {
return err
}

Expand Down

0 comments on commit a61f4d9

Please sign in to comment.