forked from goharbor/harbor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.go
115 lines (94 loc) · 2.54 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package dao
import (
"errors"
"time"
"github.com/astaxie/beego/orm"
common_dao "github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/replication/dao/models"
"github.com/goharbor/harbor/src/replication/model"
)
// AddRepPolicy insert new policy to DB.
func AddRepPolicy(policy *models.RepPolicy) (int64, error) {
o := common_dao.GetOrmer()
now := time.Now()
policy.CreationTime = now
policy.UpdateTime = now
return o.Insert(policy)
}
// GetPolicies list polices with given query parameters.
func GetPolicies(queries ...*model.PolicyQuery) (int64, []*models.RepPolicy, error) {
var qs = common_dao.GetOrmer().QueryTable(new(models.RepPolicy))
var policies []*models.RepPolicy
if len(queries) == 0 {
total, err := qs.Count()
if err != nil {
return -1, nil, err
}
_, err = qs.All(&policies)
if err != nil {
return total, nil, err
}
return total, policies, nil
}
query := queries[0]
if len(query.Name) != 0 {
qs = qs.Filter("Name__icontains", query.Name)
}
if len(query.Namespace) != 0 {
// TODO: Namespace filter not implemented yet
}
if query.SrcRegistry > 0 {
qs = qs.Filter("SrcRegistryID__exact", query.SrcRegistry)
}
if query.DestRegistry > 0 {
qs = qs.Filter("DestRegistryID__exact", query.DestRegistry)
}
total, err := qs.Count()
if err != nil {
return -1, nil, err
}
if query.Page > 0 && query.Size > 0 {
qs = qs.Limit(query.Size, (query.Page-1)*query.Size)
}
_, err = qs.All(&policies)
if err != nil {
return total, nil, err
}
return total, policies, nil
}
// GetRepPolicy return special policy by id.
func GetRepPolicy(id int64) (policy *models.RepPolicy, err error) {
policy = new(models.RepPolicy)
err = common_dao.GetOrmer().QueryTable(policy).
Filter("id", id).One(policy)
if err == orm.ErrNoRows {
return nil, nil
}
return
}
// GetRepPolicyByName return special policy by name.
func GetRepPolicyByName(name string) (policy *models.RepPolicy, err error) {
policy = new(models.RepPolicy)
err = common_dao.GetOrmer().QueryTable(policy).
Filter("name", name).One(policy)
if err == orm.ErrNoRows {
return nil, nil
}
return
}
// UpdateRepPolicy update fields by props
func UpdateRepPolicy(policy *models.RepPolicy, props ...string) (err error) {
var o = common_dao.GetOrmer()
if policy != nil {
_, err = o.Update(policy, props...)
} else {
err = errors.New("Nil policy")
}
return
}
// DeleteRepPolicy will hard delete database item
func DeleteRepPolicy(id int64) error {
o := common_dao.GetOrmer()
_, err := o.Delete(&models.RepPolicy{ID: id})
return err
}