Skip to content
Snippets Groups Projects
Commit 5ace99a8 authored by Lyuben Penkovski's avatar Lyuben Penkovski
Browse files

Refactor locking function to reduce code duplication

parent 469fed2d
No related branches found
No related tags found
1 merge request!3HTTP endpoints for policy lock and unlock
Pipeline #49716 passed
...@@ -13,8 +13,7 @@ import ( ...@@ -13,8 +13,7 @@ import (
type Storage interface { type Storage interface {
Policy(ctx context.Context, name, group, version string) (*storage.Policy, error) Policy(ctx context.Context, name, group, version string) (*storage.Policy, error)
LockPolicy(ctx context.Context, name, group, version string) error SetPolicyLock(ctx context.Context, name, group, version string, lock bool) error
UnlockPolicy(ctx context.Context, name, group, version string) error
} }
type Service struct { type Service struct {
...@@ -100,7 +99,7 @@ func (s *Service) Lock(ctx context.Context, req *policy.LockRequest) error { ...@@ -100,7 +99,7 @@ func (s *Service) Lock(ctx context.Context, req *policy.LockRequest) error {
return errors.New(errors.Forbidden, "policy is already locked") 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)) logger.Error("error locking policy", zap.Error(err))
return errors.New("error locking policy", err) return errors.New("error locking policy", err)
} }
...@@ -131,7 +130,7 @@ func (s *Service) Unlock(ctx context.Context, req *policy.UnlockRequest) error { ...@@ -131,7 +130,7 @@ func (s *Service) Unlock(ctx context.Context, req *policy.UnlockRequest) error {
return errors.New(errors.Forbidden, "policy is unlocked") 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)) logger.Error("error unlocking policy", zap.Error(err))
return errors.New("error unlocking policy", err) return errors.New("error unlocking policy", err)
} }
......
...@@ -11,7 +11,7 @@ package storage ...@@ -11,7 +11,7 @@ package storage
// Group: "example", // Group: "example",
// Version: "1.0", // Version: "1.0",
// Locked: false, // Locked: false,
// LastUpdated: time.Now(), // LastUpdate: time.Now(),
// Rego: ` // Rego: `
// package gaiax // package gaiax
// //
......
...@@ -12,31 +12,27 @@ import ( ...@@ -12,31 +12,27 @@ import (
) )
type Policy struct { type Policy struct {
Filename string Filename string
Name string Name string
Group string Group string
Version string Version string
Rego string Rego string
Locked bool Locked bool
LastUpdated time.Time LastUpdate time.Time
} }
type Storage struct { type Storage struct {
db *mongo.Client policy *mongo.Collection
dbname string
collection string
} }
func New(db *mongo.Client, dbname, collection string) *Storage { func New(db *mongo.Client, dbname, collection string) *Storage {
return &Storage{ return &Storage{
db: db, policy: db.Database(dbname).Collection(collection),
dbname: dbname,
collection: collection,
} }
} }
func (s *Storage) Policy(ctx context.Context, name, group, version string) (*Policy, error) { 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, "name": name,
"group": group, "group": group,
"version": version, "version": version,
...@@ -57,8 +53,8 @@ func (s *Storage) Policy(ctx context.Context, name, group, version string) (*Pol ...@@ -57,8 +53,8 @@ func (s *Storage) Policy(ctx context.Context, name, group, version string) (*Pol
return &policy, nil return &policy, nil
} }
func (s *Storage) LockPolicy(ctx context.Context, name, group, version string) error { func (s *Storage) SetPolicyLock(ctx context.Context, name, group, version string, lock bool) error {
_, err := s.db.Database(s.dbname).Collection(s.collection).UpdateOne( _, err := s.policy.UpdateOne(
ctx, ctx,
bson.M{ bson.M{
"name": name, "name": name,
...@@ -67,24 +63,8 @@ func (s *Storage) LockPolicy(ctx context.Context, name, group, version string) e ...@@ -67,24 +63,8 @@ func (s *Storage) LockPolicy(ctx context.Context, name, group, version string) e
}, },
bson.M{ bson.M{
"$set": bson.M{ "$set": bson.M{
"locked": true, "locked": lock,
}, "lastUpdate": time.Now(),
},
)
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,
}, },
}, },
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment