diff --git a/internal/service/policy/service.go b/internal/service/policy/service.go index 982d9982a68de93b6d435c09f420ed1ab3c27b2f..a9f6616aca71da37771186bf4a345af04aa1fc38 100644 --- a/internal/service/policy/service.go +++ b/internal/service/policy/service.go @@ -13,8 +13,7 @@ import ( type Storage interface { Policy(ctx context.Context, name, group, version string) (*storage.Policy, error) - LockPolicy(ctx context.Context, name, group, version string) error - UnlockPolicy(ctx context.Context, name, group, version string) error + SetPolicyLock(ctx context.Context, name, group, version string, lock bool) error } type Service struct { @@ -100,7 +99,7 @@ func (s *Service) Lock(ctx context.Context, req *policy.LockRequest) error { return errors.New(errors.Forbidden, "policy is already locked") } - if err := s.storage.LockPolicy(ctx, req.PolicyName, req.Group, req.Version); err != nil { + if err := s.storage.SetPolicyLock(ctx, req.PolicyName, req.Group, req.Version, true); err != nil { logger.Error("error locking policy", zap.Error(err)) return errors.New("error locking policy", err) } @@ -131,7 +130,7 @@ func (s *Service) Unlock(ctx context.Context, req *policy.UnlockRequest) error { return errors.New(errors.Forbidden, "policy is unlocked") } - if err := s.storage.UnlockPolicy(ctx, req.PolicyName, req.Group, req.Version); err != nil { + if err := s.storage.SetPolicyLock(ctx, req.PolicyName, req.Group, req.Version, false); err != nil { logger.Error("error unlocking policy", zap.Error(err)) return errors.New("error unlocking policy", err) } diff --git a/internal/storage/policies_tmp_store.go b/internal/storage/policies_tmp_store.go index e3235d50288f8b62a3c3cf18f1d3db7dd7576a4c..e3d696da68f3f59a3380d6a18f1ad7537c406bde 100644 --- a/internal/storage/policies_tmp_store.go +++ b/internal/storage/policies_tmp_store.go @@ -11,7 +11,7 @@ package storage // Group: "example", // Version: "1.0", // Locked: false, -// LastUpdated: time.Now(), +// LastUpdate: time.Now(), // Rego: ` // package gaiax // diff --git a/internal/storage/storage.go b/internal/storage/storage.go index da2ebfe25ba00f0fc53778275bbedb30baf876de..ecdcde80ac061b808725e8210e8f2c4997e3e07f 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -12,31 +12,27 @@ import ( ) type Policy struct { - Filename string - Name string - Group string - Version string - Rego string - Locked bool - LastUpdated time.Time + Filename string + Name string + Group string + Version string + Rego string + Locked bool + LastUpdate time.Time } type Storage struct { - db *mongo.Client - dbname string - collection string + policy *mongo.Collection } func New(db *mongo.Client, dbname, collection string) *Storage { return &Storage{ - db: db, - dbname: dbname, - collection: collection, + policy: db.Database(dbname).Collection(collection), } } func (s *Storage) Policy(ctx context.Context, name, group, version string) (*Policy, error) { - result := s.db.Database(s.dbname).Collection(s.collection).FindOne(ctx, bson.M{ + result := s.policy.FindOne(ctx, bson.M{ "name": name, "group": group, "version": version, @@ -57,8 +53,8 @@ func (s *Storage) Policy(ctx context.Context, name, group, version string) (*Pol return &policy, nil } -func (s *Storage) LockPolicy(ctx context.Context, name, group, version string) error { - _, err := s.db.Database(s.dbname).Collection(s.collection).UpdateOne( +func (s *Storage) SetPolicyLock(ctx context.Context, name, group, version string, lock bool) error { + _, err := s.policy.UpdateOne( ctx, bson.M{ "name": name, @@ -67,24 +63,8 @@ func (s *Storage) LockPolicy(ctx context.Context, name, group, version string) e }, bson.M{ "$set": bson.M{ - "locked": true, - }, - }, - ) - return err -} - -func (s *Storage) UnlockPolicy(ctx context.Context, name, group, version string) error { - _, err := s.db.Database(s.dbname).Collection(s.collection).UpdateOne( - ctx, - bson.M{ - "name": name, - "group": group, - "version": version, - }, - bson.M{ - "$set": bson.M{ - "locked": false, + "locked": lock, + "lastUpdate": time.Now(), }, }, )