-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnode.go
68 lines (59 loc) · 1.78 KB
/
node.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
package apiserver
import (
"fmt"
"net/http"
"github.com/mijia/sweb/form"
"github.com/mijia/sweb/server"
"golang.org/x/net/context"
)
type RestfulNodes struct {
server.BaseResource
}
func (rn RestfulNodes) Get(ctx context.Context, r *http.Request) (int, interface{}) {
nodes, err := getEngine(ctx).GetNodes()
if err != nil {
return http.StatusInternalServerError, err.Error()
}
return http.StatusAccepted, nodes
}
func (rn RestfulNodes) Patch(ctx context.Context, r *http.Request) (int, interface{}) {
fromNode := form.ParamString(r, "from", "")
targetNode := form.ParamString(r, "to", "")
forceDrift := form.ParamBoolean(r, "force", false)
pgName := form.ParamString(r, "pg", "")
pgInstance := form.ParamInt(r, "pg_instance", -1)
if fromNode == "" {
return http.StatusBadRequest, "from node name required"
}
if fromNode == targetNode {
return http.StatusBadRequest, "from node equals to target node"
}
cmd := form.ParamString(r, "cmd", "")
switch cmd {
case "drift":
engine := getEngine(ctx)
engine.DriftNode(fromNode, targetNode, pgName, pgInstance, forceDrift)
return http.StatusAccepted, map[string]interface{}{
"message": "PodGroups will be drifting",
"from": fromNode,
"to": targetNode,
"pgName": pgName,
"pgInstance": pgInstance,
"forceDrift": forceDrift,
}
default:
return http.StatusBadRequest, fmt.Sprintf("Unkown command %s", cmd)
}
}
func (rn RestfulNodes) Delete(ctx context.Context, r *http.Request) (int, interface{}) {
node := form.ParamString(r, "node", "")
if node == "" {
return http.StatusBadRequest, "from node name required"
}
engine := getEngine(ctx)
engine.RemoveNode(node)
return http.StatusAccepted, map[string]interface{}{
"message": "containers in node will be drift",
"node": node,
}
}