/
node.go
116 lines (102 loc) · 3.79 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
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
116
package service
import (
"fmt"
"github.com/OnFinality-io/onf-cli/pkg/api"
"k8s.io/apimachinery/pkg/api/resource"
)
func GetNodeList(wsID uint64) ([]NodeItem, error) {
var nodes []NodeItem
path := fmt.Sprintf("/workspaces/%d/nodes", wsID)
resp, d, errs := instance.Request(api.MethodGet, path, nil).EndStruct(&nodes)
return nodes, checkError(resp, d, errs)
}
func GetNodeDetail(wsID uint64, nodeID uint64, extrasFlag ...int) (*Node, error) {
// refer to mx-api for the available flags, flags = 1 means load endpoints
flags := 1
if len(extrasFlag) > 0 {
flags = extrasFlag[0]
}
node := Node{}
path := fmt.Sprintf("/workspaces/%d/nodes/%d?extrasFlag=%d", wsID, nodeID, flags)
resp, d, errs := instance.Request(api.MethodGet, path, nil).EndStruct(&node)
return &node, checkError(resp, d, errs)
}
func StopNode(wsID uint64, nodeID uint64) error {
path := fmt.Sprintf("/workspaces/%d/nodes/%d/stop", wsID, nodeID)
resp, d, errs := instance.Request(api.MethodPost, path, nil).End()
return checkError(resp, []byte(d), errs)
}
func ResumeNode(wsID uint64, nodeID uint64) error {
path := fmt.Sprintf("/workspaces/%d/nodes/%d/resume", wsID, nodeID)
resp, d, errs := instance.Request(api.MethodPost, path, nil).End()
return checkError(resp, []byte(d), errs)
}
func RestartNode(wsID uint64, nodeID uint64) error {
path := fmt.Sprintf("/workspaces/%d/nodes/%d/restart", wsID, nodeID)
resp, d, errs := instance.Request(api.MethodPost, path, nil).End()
return checkError(resp, []byte(d), errs)
}
func TerminateNode(wsID uint64, nodeID uint64) error {
path := fmt.Sprintf("/workspaces/%d/nodes/%d", wsID, nodeID)
resp, d, errs := instance.Request(api.MethodDelete, path, nil).End()
return checkError(resp, []byte(d), errs)
}
func UpdateNode(wsID uint64, nodeID uint64, data *UpdateNodePayload) error {
//transformSection(data.Config)
path := fmt.Sprintf("/workspaces/%d/nodes/%d/update", wsID, nodeID)
resp, d, errs := instance.Ver2().Request(api.MethodPost, path, &api.RequestOptions{
Body: data,
}).End()
return checkError(resp, []byte(d), errs)
}
func UpdateImage(wsID uint64, nodeID uint64, data *UpdateNodeImagePayload) error {
path := fmt.Sprintf("/workspaces/%d/nodes/%d/update-image", wsID, nodeID)
resp, d, errs := instance.Request(api.MethodPost, path, &api.RequestOptions{
Body: data,
}).End()
return checkError(resp, []byte(d), errs)
}
func CreateNode(wsID uint64, data *CreateNodePayload) (*Node, error) {
//transformSection(data.Config)
path := fmt.Sprintf("/workspaces/%d/nodes", wsID)
node := &Node{}
resp, d, errs := instance.Ver2().Request(api.MethodPost, path, &api.RequestOptions{
Body: data,
}).EndStruct(node)
return node, checkError(resp, d, errs)
}
func ExpandNodeStorage(wsID uint64, nodeID uint64, size string) error {
_, err := resource.ParseQuantity(size)
if err != nil {
return err
}
path := fmt.Sprintf("/workspaces/%d/nodes/%d/expand-storage", wsID, nodeID)
resp, d, errs := instance.Request(api.MethodPost, path, &api.RequestOptions{
Body: map[string]string{
"storage": size,
},
}).End()
return checkError(resp, []byte(d), errs)
}
func GetNodeStatus(wsID uint64, nodeID uint64) (*NodeStatus, error) {
node := NodeStatus{}
path := fmt.Sprintf("/workspaces/%d/nodes/%d/status", wsID, nodeID)
resp, d, errs := instance.Request(api.MethodGet, path, nil).EndStruct(&node)
return &node, checkError(resp, d, errs)
}
//func transformSection(config *NodeLaunchConfig) {
// if config != nil {
// sectionNum := 0
// Section := sectionNum*200 + 100
// var extraArgs []*ExtraArgs
// for _, arg := range config.ExtraArgs {
// if strings.Compare(*arg.Key, "--") == 0 {
// sectionNum++
// Section = sectionNum*200 + 100
// continue
// }
// extraArgs = append(extraArgs, &ExtraArgs{Key: arg.Key, Value: arg.Value, Section: Section})
// }
// config.ExtraArgs = extraArgs
// }
//}