forked from cloud66-oss/cloud66
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
171 lines (147 loc) · 5.11 KB
/
server.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
package cloud66
import (
"errors"
"strconv"
"strings"
"time"
)
type ServerNotification struct {
Value interface{} `json:"value"`
Reason string `json:"reason"`
AsOf time.Time `json:"asof"`
}
type Server struct {
Uid string `json:"uid"`
VendorUid string `json:"vendor_uid"`
Name string `json:"name"`
Address string `json:"address"`
Distro string `json:"distro"`
DistroVersion string `json:"distro_version"`
DnsRecord string `json:"dns_record"`
ServerType string `json:"server_type"`
ServerGroupId int `json:"server_group_id"`
Roles []string `json:"server_roles"`
StackUid string `json:"stack_uid"`
HasAgent bool `json:"has_agent"`
Params interface{} `json:"params"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Region string `json:"region"`
AvailabilityZone string `json:"availability_zone"`
ExtIpV4 string `json:"ext_ipv4"`
HealthCode int `json:"health_state"`
UserName string `json:"user_name"`
PersonalKey bool `json:"personal_key"`
SshPrivateKey *string `json:"ssh_private_key"`
Notifications map[string]ServerNotification `json:"notifications"`
HasDeployGateway bool `json:"has_deploy_gateway"`
DeployGatewayAddress string `json:"deploy_gateway_address"`
DeployGatewayUsername string `json:"deploy_gateway_user_name"`
IsKubernetesMaster bool `json:"is_kubernetes_master"`
}
func (s Server) Health() string {
return healthStatus[s.HealthCode]
}
func (c *Client) Servers(stackUid string) ([]Server, error) {
query_strings := make(map[string]string)
query_strings["page"] = "1"
var p Pagination
var result []Server
var serversRes []Server
for {
req, err := c.NewRequest("GET", "/stacks/"+stackUid+"/servers.json", nil, query_strings)
if err != nil {
return nil, err
}
serversRes = nil
err = c.DoReq(req, &serversRes, &p)
if err != nil {
return nil, err
}
result = append(result, serversRes...)
if p.Current < p.Next {
query_strings["page"] = strconv.Itoa(p.Next)
} else {
break
}
}
return result, nil
}
func (c *Client) ServerKeyInformation(stackUid string, serverUid string) (string, error) {
server, err := c.GetServer(stackUid, serverUid, 1)
if err != nil {
return "", err
}
if server.SshPrivateKey == nil {
return "", errors.New("SshPrivateKey not returned by server")
}
return *server.SshPrivateKey, nil
}
func (c *Client) GetServer(stackUid string, serverUid string, includeSshKey int) (*Server, error) {
params := struct {
Value int `json:"include_private_key"`
}{
Value: includeSshKey,
}
req, err := c.NewRequest("GET", "/stacks/"+stackUid+"/servers/"+serverUid+".json", params, nil)
if err != nil {
return nil, err
}
var serverRes *Server
return serverRes, c.DoReq(req, &serverRes, nil)
}
func (c *Client) ServerSettings(stackUid string, serverUid string) ([]StackSetting, error) {
query_strings := make(map[string]string)
query_strings["page"] = "1"
var p Pagination
var result []StackSetting
var settingsRes []StackSetting
for {
req, err := c.NewRequest("GET", "/stacks/"+stackUid+"/servers/"+serverUid+"/settings.json", nil, query_strings)
if err != nil {
return nil, err
}
settingsRes = nil
err = c.DoReq(req, &settingsRes, &p)
if err != nil {
return nil, err
}
result = append(result, settingsRes...)
if p.Current < p.Next {
query_strings["page"] = strconv.Itoa(p.Next)
} else {
break
}
}
return result, nil
}
func (c *Client) ServerSet(stackUid string, serverUid string, key string, value string) (*AsyncResult, error) {
key = strings.Replace(key, ".", "-", -1)
params := struct {
Value string `json:"value"`
}{
Value: value,
}
req, err := c.NewRequest("PUT", "/stacks/"+stackUid+"/servers/"+serverUid+"/settings/"+key+".json", params, nil)
if err != nil {
return nil, err
}
var asyncRes *AsyncResult
return asyncRes, c.DoReq(req, &asyncRes, nil)
}
func (c *Client) ServerReboot(stackUid string, serverUid string) (*AsyncResult, error) {
req, err := c.NewRequest("POST", "/stacks/"+stackUid+"/servers/"+serverUid+"/reboot_server.json", nil, nil)
if err != nil {
return nil, err
}
var asyncRes *AsyncResult
return asyncRes, c.DoReq(req, &asyncRes, nil)
}
func (s *Server) HasRole(searchRole string) bool {
for _, role := range s.Roles {
if role == searchRole {
return true
}
}
return false
}