-
Notifications
You must be signed in to change notification settings - Fork 0
/
label_resource.go
104 lines (86 loc) · 2.53 KB
/
label_resource.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
package api
import (
"net/http"
"strconv"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/core/label"
)
// LabelResourceAPI provides the related basic functions to handle marking labels to resources
type LabelResourceAPI struct {
BaseController
labelManager label.Manager
}
// Prepare resources for follow-up actions.
func (lra *LabelResourceAPI) Prepare() {
lra.BaseController.Prepare()
// Create label manager
lra.labelManager = &label.BaseManager{}
}
func (lra *LabelResourceAPI) checkPermissions(project string) bool {
if lra.Ctx.Request.Method == http.MethodPost ||
lra.Ctx.Request.Method == http.MethodDelete {
if lra.SecurityCtx.HasWritePerm(project) {
return true
}
}
if lra.Ctx.Request.Method == http.MethodGet {
if lra.SecurityCtx.HasReadPerm(project) {
return true
}
}
return false
}
func (lra *LabelResourceAPI) getLabelsOfResource(rType string, rIDOrName interface{}) {
labels, err := lra.labelManager.GetLabelsOfResource(rType, rIDOrName)
if err != nil {
lra.handleErrors(err)
return
}
lra.Data["json"] = labels
lra.ServeJSON()
}
func (lra *LabelResourceAPI) markLabelToResource(rl *models.ResourceLabel) {
labelID, err := lra.labelManager.MarkLabelToResource(rl)
if err != nil {
lra.handleErrors(err)
return
}
// return the ID of label and return status code 200 rather than 201 as the label is not created
lra.Redirect(http.StatusOK, strconv.FormatInt(labelID, 10))
}
func (lra *LabelResourceAPI) removeLabelFromResource(rType string, rIDOrName interface{}, labelID int64) {
if err := lra.labelManager.RemoveLabelFromResource(rType, rIDOrName, labelID); err != nil {
lra.handleErrors(err)
return
}
}
// eat the error of validate method of label manager
func (lra *LabelResourceAPI) validate(labelID, projectID int64) (*models.Label, bool) {
label, err := lra.labelManager.Validate(labelID, projectID)
if err != nil {
lra.handleErrors(err)
return nil, false
}
return label, true
}
// eat the error of exists method of label manager
func (lra *LabelResourceAPI) exists(labelID int64) (*models.Label, bool) {
label, err := lra.labelManager.Exists(labelID)
if err != nil {
return nil, false
}
return label, true
}
// Handle different kinds of errors.
func (lra *LabelResourceAPI) handleErrors(err error) {
switch err.(type) {
case *label.ErrLabelBadRequest:
lra.SendBadRequestError(err)
case *label.ErrLabelConflict:
lra.SendConflictError(err)
case *label.ErrLabelNotFound:
lra.SendNotFoundError(err)
default:
lra.SendInternalServerError(err)
}
}