-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconstraint.go
69 lines (55 loc) · 1.97 KB
/
constraint.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
package apiserver
import (
"fmt"
"github.com/laincloud/deployd/engine"
"github.com/mijia/sweb/form"
"github.com/mijia/sweb/server"
"golang.org/x/net/context"
"net/http"
)
type RestfulConstraints struct {
server.BaseResource
}
func (rc RestfulConstraints) Get(ctx context.Context, r *http.Request) (int, interface{}) {
cstType := form.ParamString(r, "type", "node")
if constraint, ok := getEngine(ctx).GetConstraints(cstType); !ok {
return http.StatusNotFound, fmt.Sprintf("No constraint found")
} else {
return http.StatusOK, constraint
}
}
func (rc RestfulConstraints) Patch(ctx context.Context, r *http.Request) (int, interface{}) {
cstType := form.ParamString(r, "type", "")
cstValue := form.ParamString(r, "value", "")
equal := form.ParamBoolean(r, "equal", false)
soft := form.ParamBoolean(r, "soft", true)
if cstType == "" {
return http.StatusBadRequest, "constraint type required"
}
if cstValue == "" {
return http.StatusBadRequest, "constraint value required"
}
constraint := engine.ConstraintSpec{cstType, equal, cstValue, soft}
if err := getEngine(ctx).UpdateConstraints(constraint); err != nil {
return http.StatusInternalServerError, err.Error()
}
urlReverser := getUrlReverser(ctx)
return http.StatusAccepted, map[string]string{
"message": "Constraints will be patched",
"check_url": urlReverser.Reverse("Get_RestfulConstraints") + "?type=" + cstType,
}
}
func (rc RestfulConstraints) Delete(ctx context.Context, r *http.Request) (int, interface{}) {
cstType := form.ParamString(r, "type", "node")
if err := getEngine(ctx).DeleteConstraints(cstType); err != nil {
if err == engine.ErrConstraintNotExists {
return http.StatusNotFound, err.Error()
}
return http.StatusInternalServerError, err.Error()
}
urlReverser := getUrlReverser(ctx)
return http.StatusAccepted, map[string]string{
"message": "Constraint will be deleted from the orc engine.",
"check_url": urlReverser.Reverse("Get_RestfulConstraints") + "?type=" + cstType,
}
}