-
Notifications
You must be signed in to change notification settings - Fork 35
/
node.go
108 lines (87 loc) · 2.17 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
package client
import (
"context"
"time"
nodeapi "github.com/ehazlett/stellar/api/services/node/v1"
)
type node struct {
client nodeapi.NodeClient
}
func (n *node) ID() (string, error) {
ctx := context.Background()
resp, err := n.client.Info(ctx, &nodeapi.InfoRequest{})
if err != nil {
return "", err
}
return resp.ID, nil
}
func (n *node) Containers(filters ...string) ([]*nodeapi.Container, error) {
ctx := context.Background()
resp, err := n.client.Containers(ctx, &nodeapi.ContainersRequest{
Filters: filters,
})
if err != nil {
return nil, err
}
return resp.Containers, nil
}
func (n *node) Container(id string) (*nodeapi.Container, error) {
ctx := context.Background()
resp, err := n.client.Container(ctx, &nodeapi.ContainerRequest{
ID: id,
})
if err != nil {
return nil, err
}
return resp.Container, nil
}
func (n *node) CreateContainer(appName string, service *nodeapi.Service) error {
ctx := context.Background()
if _, err := n.client.CreateContainer(ctx, &nodeapi.CreateContainerRequest{
Application: appName,
Service: service,
}); err != nil {
return err
}
return nil
}
func (n *node) RestartContainer(id string) error {
ctx := context.Background()
if _, err := n.client.RestartContainer(ctx, &nodeapi.RestartContainerRequest{
ID: id,
}); err != nil {
return err
}
return nil
}
func (n *node) DeleteContainer(id string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
if _, err := n.client.DeleteContainer(ctx, &nodeapi.DeleteContainerRequest{
ID: id,
}); err != nil {
return err
}
return nil
}
func (n *node) SetupContainerNetwork(id, ip, network, gateway string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
if _, err := n.client.SetupContainerNetwork(ctx, &nodeapi.ContainerNetworkRequest{
ID: id,
IP: ip,
Network: network,
Gateway: gateway,
}); err != nil {
return err
}
return nil
}
func (n *node) Images() ([]*nodeapi.Image, error) {
ctx := context.Background()
resp, err := n.client.Images(ctx, &nodeapi.ImagesRequest{})
if err != nil {
return nil, err
}
return resp.Images, nil
}