This commit is contained in:
76
internal/snapshot/policy.go
Normal file
76
internal/snapshot/policy.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package snapshot
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"gitea.avt.data-center.id/othman.suseno/atlas/internal/models"
|
||||
)
|
||||
|
||||
// PolicyStore manages snapshot policies
|
||||
type PolicyStore struct {
|
||||
mu sync.RWMutex
|
||||
policies map[string]*models.SnapshotPolicy
|
||||
}
|
||||
|
||||
// NewPolicyStore creates a new policy store
|
||||
func NewPolicyStore() *PolicyStore {
|
||||
return &PolicyStore{
|
||||
policies: make(map[string]*models.SnapshotPolicy),
|
||||
}
|
||||
}
|
||||
|
||||
// List returns all snapshot policies
|
||||
func (s *PolicyStore) List() []models.SnapshotPolicy {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
policies := make([]models.SnapshotPolicy, 0, len(s.policies))
|
||||
for _, p := range s.policies {
|
||||
policies = append(policies, *p)
|
||||
}
|
||||
return policies
|
||||
}
|
||||
|
||||
// Get returns a policy for a dataset
|
||||
func (s *PolicyStore) Get(dataset string) (*models.SnapshotPolicy, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
policy, exists := s.policies[dataset]
|
||||
if !exists {
|
||||
return nil, nil // Return nil if not found (not an error)
|
||||
}
|
||||
return policy, nil
|
||||
}
|
||||
|
||||
// Set creates or updates a policy
|
||||
func (s *PolicyStore) Set(policy *models.SnapshotPolicy) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
s.policies[policy.Dataset] = policy
|
||||
}
|
||||
|
||||
// Delete removes a policy
|
||||
func (s *PolicyStore) Delete(dataset string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
delete(s.policies, dataset)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListForDataset returns all policies (for future filtering by dataset prefix)
|
||||
func (s *PolicyStore) ListForDataset(datasetPrefix string) []models.SnapshotPolicy {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
var policies []models.SnapshotPolicy
|
||||
for _, p := range s.policies {
|
||||
if datasetPrefix == "" || p.Dataset == datasetPrefix ||
|
||||
(len(p.Dataset) > len(datasetPrefix) && p.Dataset[:len(datasetPrefix)] == datasetPrefix) {
|
||||
policies = append(policies, *p)
|
||||
}
|
||||
}
|
||||
return policies
|
||||
}
|
||||
Reference in New Issue
Block a user