-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathwatch_list.go
65 lines (53 loc) · 1.54 KB
/
watch_list.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
package trigger
import (
"github.com/vmware/harbor/src/common/dao"
"github.com/vmware/harbor/src/common/models"
)
//DefaultWatchList is the default instance of WatchList
var DefaultWatchList = &WatchList{}
//WatchList contains the items which should be evaluated for replication
//when image pushing or deleting happens.
type WatchList struct{}
//WatchItem keeps the related data for evaluation in WatchList.
type WatchItem struct {
//ID of policy
PolicyID int64
//Corresponding namespace
Namespace string
//For deletion event
OnDeletion bool
//For pushing event
OnPush bool
}
//Add item to the list and persist into DB
func (wl *WatchList) Add(item WatchItem) error {
_, err := dao.DefaultDatabaseWatchItemDAO.Add(
&models.WatchItem{
PolicyID: item.PolicyID,
Namespace: item.Namespace,
OnPush: item.OnPush,
OnDeletion: item.OnDeletion,
})
return err
}
//Remove the specified watch item from list
func (wl *WatchList) Remove(policyID int64) error {
return dao.DefaultDatabaseWatchItemDAO.DeleteByPolicyID(policyID)
}
//Get the watch items according to the namespace and operation
func (wl *WatchList) Get(namespace, operation string) ([]WatchItem, error) {
items, err := dao.DefaultDatabaseWatchItemDAO.Get(namespace, operation)
if err != nil {
return nil, err
}
watchItems := []WatchItem{}
for _, item := range items {
watchItems = append(watchItems, WatchItem{
PolicyID: item.PolicyID,
Namespace: item.Namespace,
OnPush: item.OnPush,
OnDeletion: item.OnDeletion,
})
}
return watchItems, nil
}