-
Notifications
You must be signed in to change notification settings - Fork 9
/
service.go
173 lines (158 loc) · 4.71 KB
/
service.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package cloud66
import (
"strconv"
"net/url"
)
type Service struct {
Name string `json:"name"`
Containers []Container `json:"containers"`
SourceType string `json:"source_type"`
GitRef string `json:"git_ref"`
ImageName string `json:"image_name"`
ImageUid string `json:"image_uid"`
ImageTag string `json:"image_tag"`
Command string `json:"command"`
BuildCommand string `json:"build_command"`
DeployCommand string `json:"deploy_command"`
WrapCommand string `json:"wrap_command"`
DesiredCount int `json:"desired_count"`
}
func (c *Client) GetServices(stackUid string, serverUid *string) ([]Service, error) {
queryStrings := make(map[string]string)
queryStrings["page"] = "1"
if serverUid != nil {
queryStrings["server_uid"] = *serverUid
}
var p Pagination
var result []Service
var serviceRes []Service
for {
req, err := c.NewRequest("GET", "/stacks/"+stackUid+"/services.json", nil, queryStrings)
if err != nil {
return nil, err
}
serviceRes = nil
err = c.DoReq(req, &serviceRes, &p)
if err != nil {
return nil, err
}
result = append(result, serviceRes...)
if p.Current < p.Next {
queryStrings["page"] = strconv.Itoa(p.Next)
} else {
break
}
}
return result, nil
}
func (c *Client) GetService(stackUid string, serviceName string, serverUid *string, wrapCommand *string) (*Service, error) {
queryStrings := make(map[string]string)
queryStrings["page"] = "1"
if serverUid != nil {
queryStrings["server_uid"] = *serverUid
}
if wrapCommand != nil {
queryStrings["wrap_command"] = url.QueryEscape(*wrapCommand)
}
req, err := c.NewRequest("GET", "/stacks/"+stackUid+"/services/"+serviceName+".json", nil, queryStrings)
if err != nil {
return nil, err
}
var servicesRes *Service
return servicesRes, c.DoReq(req, &servicesRes, nil)
}
func (c *Client) StopService(stackUid string, serviceName string, serverUid *string) (*AsyncResult, error) {
var params interface{}
if serverUid == nil {
params = nil
} else {
params = struct {
ServerUid string `json:"server_uid"`
}{
ServerUid: *serverUid,
}
}
req, err := c.NewRequest("DELETE", "/stacks/"+stackUid+"/services/"+serviceName+".json", params, nil)
if err != nil {
return nil, err
}
var asyncRes *AsyncResult
return asyncRes, c.DoReq(req, &asyncRes, nil)
}
func (c *Client) ScaleService(stackUid string, serviceName string, serverCount map[string]int) (*AsyncResult, error) {
params := struct {
ServiceName string `json:"service_name"`
ServerCount map[string]int `json:"server_count"`
}{
ServiceName: serviceName,
ServerCount: serverCount,
}
req, err := c.NewRequest("POST", "/stacks/"+stackUid+"/services.json", params, nil)
if err != nil {
return nil, err
}
var asyncRes *AsyncResult
return asyncRes, c.DoReq(req, &asyncRes, nil)
}
func (c *Client) ScaleServiceByGroup(stackUid string, serviceName string, groupCount map[string]int) (*AsyncResult, error) {
params := struct {
ServiceName string `json:"service_name"`
ServerGroupCount map[string]int `json:"server_group_count"`
}{
ServiceName: serviceName,
ServerGroupCount: groupCount,
}
req, err := c.NewRequest("POST", "/stacks/"+stackUid+"/services.json", params, nil)
if err != nil {
return nil, err
}
var asyncRes *AsyncResult
return asyncRes, c.DoReq(req, &asyncRes, nil)
}
func (s *Service) ServerContainerCountMap() map[string]int {
var serverMap = make(map[string]int)
for _, container := range s.Containers {
if _, present := serverMap[container.ServerName]; present {
serverMap[container.ServerName] = serverMap[container.ServerName] + 1
} else {
serverMap[container.ServerName] = 1
}
}
return serverMap
}
func (c *Client) InvokeServiceAction(stackUid string, serviceName *string, serverUid *string, action string) (*AsyncResult, error) {
var params interface{}
if serverUid != nil && serviceName != nil {
params = struct {
Command string `json:"command"`
ServiceName string `json:"service_name"`
ServerUid string `json:"server_uid"`
}{
Command: action,
ServiceName: *serviceName,
ServerUid: *serverUid,
}
} else if serverUid == nil {
params = struct {
Command string `json:"command"`
ServiceName string `json:"service_name"`
}{
Command: action,
ServiceName: *serviceName,
}
} else if serviceName == nil {
params = struct {
Command string `json:"command"`
ServerUid string `json:"server_uid"`
}{
Command: action,
ServerUid: *serverUid,
}
}
req, err := c.NewRequest("POST", "/stacks/"+stackUid+"/actions.json", params, nil)
if err != nil {
return nil, err
}
var asyncRes *AsyncResult
return asyncRes, c.DoReq(req, &asyncRes, nil)
}