-
Notifications
You must be signed in to change notification settings - Fork 58
/
policy.go
75 lines (58 loc) · 1.62 KB
/
policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package manager
import (
"time"
"github.com/bricks-cloud/bricksllm/internal/policy"
"github.com/bricks-cloud/bricksllm/internal/util"
)
type PoliciesStorage interface {
CreatePolicy(p *policy.Policy) (*policy.Policy, error)
UpdatePolicy(id string, p *policy.UpdatePolicy) (*policy.Policy, error)
GetPolicyById(id string) (*policy.Policy, error)
GetPoliciesByTags(tags []string) ([]*policy.Policy, error)
}
type PoliciesMemStorage interface {
GetPolicy(id string) *policy.Policy
}
type PolicyManager struct {
Storage PoliciesStorage
Memdb PoliciesMemStorage
}
func NewPolicyManager(s PoliciesStorage, memdb PoliciesMemStorage) *PolicyManager {
return &PolicyManager{
Storage: s,
Memdb: memdb,
}
}
func (m *PolicyManager) CreatePolicy(p *policy.Policy) (*policy.Policy, error) {
err := p.Validate()
if err != nil {
return nil, err
}
p.CreatedAt = time.Now().Unix()
p.UpdatedAt = time.Now().Unix()
p.Id = util.NewUuid()
if p.Config == nil {
p.Config = &policy.Config{}
}
if p.RegexConfig == nil {
p.RegexConfig = &policy.RegexConfig{}
}
if p.CustomConfig == nil {
p.CustomConfig = &policy.CustomConfig{}
}
return m.Storage.CreatePolicy(p)
}
func (m *PolicyManager) UpdatePolicy(id string, p *policy.UpdatePolicy) (*policy.Policy, error) {
err := p.Validate()
if err != nil {
return nil, err
}
p.UpdatedAt = time.Now().Unix()
return m.Storage.UpdatePolicy(id, p)
}
func (m *PolicyManager) GetPoliciesByTags(tags []string) ([]*policy.Policy, error) {
return m.Storage.GetPoliciesByTags(tags)
}
func (m *PolicyManager) GetPolicyByIdFromMemdb(id string) *policy.Policy {
return m.Memdb.GetPolicy(id)
}