-
Notifications
You must be signed in to change notification settings - Fork 8
/
instance.go
249 lines (217 loc) · 7.63 KB
/
instance.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package bcc
import (
"bytes"
"encoding/json"
"fmt"
"github.com/baidu/baiducloud-sdk-go/bce"
"github.com/baidu/baiducloud-sdk-go/billing"
"github.com/baidu/baiducloud-sdk-go/cds"
"github.com/baidu/baiducloud-sdk-go/eip"
"github.com/baidu/baiducloud-sdk-go/util"
)
const (
InstanceStatusRunning string = "Running"
InstanceStatusStarting string = "Starting"
InstanceStatusStopping string = "Stopping"
InstanceStatusStopped string = "Stopped"
InstanceStatusDeleted string = "Deleted"
InstanceStatusScaling string = "Scaling"
InstanceStatusExpired string = "Expired"
InstanceStatusError string = "Error"
InstanceStatusSnapshotProcessing string = "SnapshotProcessing"
InstanceStatusImageProcessing string = "ImageProcessing"
)
// Instance define instance model
type Instance struct {
InstanceID string `json:"id"`
InstanceName string `json:"name"`
Description string `json:"desc"`
Status string `json:"status"`
PaymentTiming string `json:"paymentTiming"`
CreationTime string `json:"createTime"`
ExpireTime string `json:"expireTime"`
PublicIP string `json:"publicIp"`
InternalIP string `json:"internalIp"`
CPUCount int `json:"cpuCount"`
GPUCount int `json:"gpuCount"`
MemoryCapacityInGB int `json:"memoryCapacityInGB"`
LocalDiskSizeInGB int `json:"localDiskSizeInGB"`
ImageID string `json:"imageId"`
NetworkCapacityInMbps int `json:"networkCapacityInMbps"`
PlacementPolicy string `json:"placementPolicy"`
ZoneName string `json:"zoneName"`
SubnetID string `json:"subnetId"`
VpcID string `json:"vpcId"`
}
type EphemeralDisk struct {
StorageType cds.StorageType `json:"storageType,storageType"`
SizeInGB int `json:"sizeInGB,omitempty"`
FreeSizeInGB int `json:"freeSizeInGB,omitempty"`
}
type CreateCdsModel struct {
StorageType cds.StorageType `json:"storageType,storageType"`
SnapshotID string `json:"snapshotId,omitempty"`
CdsSizeInGB int `json:"cdsSizeInGB,omitempty"`
}
type ListInstancesResponse struct {
Marker string `json:"marker"`
IsTruncated bool `json:"isTruncated"`
NextMarker string `json:"nextMarker"`
MaxKeys int `json:"maxKeys"`
Instances []Instance `json:"instances"`
}
// CreateInstanceArgs is args to create instances
// refers to https://cloud.baidu.com/doc/BCC/API.html#.E5.88.9B.E5.BB.BA.E5.AE.9E.E4.BE.8B
// TODO ssh public key still not supperted
type CreateInstanceArgs struct {
ImageID string `json:"imageId"`
Billing billing.Billing `json:"billing"`
InstanceType string `json:"instanceType,omitempty"`
CPUCount int `json:"cpuCount"`
MemoryCapacityInGB int `json:"memoryCapacityInGB"`
RootDiskSizeInGB int `json:"rootDiskSizeInGb,omitempty"`
RootDiskStorageType int `json:"rootDiskStorageType,omitempty"`
LocalDiskSizeInGB int `json:"localDiskSizeInGB,omitempty"` // deprecated now
EphemeralDisks []EphemeralDisk `json:"ephemeralDisks,omitempty"`
CreateCdsList []CreateCdsModel `json:"createCdsList,omitempty"`
NetworkCapacityInMbps int `json:"networkCapacityInMbps,omitempty"`
DedicatedHostID int `json:"dedicatedHostId,omitempty"`
PurchaseCount int `json:"purchaseCount,omitempty"`
Name string `json:"name,omitempty"`
AdminPass string `json:"adminPass,omitempty"`
ZoneName string `json:"zoneName,omitempty"`
SubnetID string `json:"subnetId,omitempty"`
SecurityGroupID string `json:"securityGroupId,omitempty"`
GPUCard string `json:"gpuCard,omitempty"`
FPGACard string `json:"fpgaCard,omitempty"`
CardCount string `json:"cardCount,omitempty"`
}
// CreateInstanceResponse is response of create instances
type CreateInstanceResponse struct {
InstanceIDs []string `json:"instanceIds"`
}
type GetInstanceResponse struct {
Ins Instance `json:"instance"`
}
func (args *CreateInstanceArgs) validate() error {
if args == nil {
return fmt.Errorf("CreateInstanceArgs cannot be nil")
}
if args.ImageID == "" {
return fmt.Errorf("imageId cannot be empty")
}
if args.CPUCount <= 0 {
return fmt.Errorf("cpuCount must be positive integer")
}
if args.MemoryCapacityInGB <= 0 {
return fmt.Errorf("memoryCapacityInGB must be positive integer")
}
return nil
}
// CreateInstances create instances according to args
func (c *Client) CreateInstances(args *CreateInstanceArgs, option *bce.SignOption) ([]string, error) {
instanceIDs := []string{}
if err := args.validate(); err != nil {
return instanceIDs, err
}
params := map[string]string{
"clientToken": c.GenerateClientToken(),
}
// ref: https://cloud.baidu.com/doc/BCC/API.html#.7A.E6.31.D8.94.C1.A1.C2.1A.8D.92.ED.7F.60.7D.AF
if len(args.AdminPass) != 0 {
encrypted, err := util.AesECBEncryptHex(c.SecretAccessKey, args.AdminPass)
if err != nil {
return instanceIDs, err
}
args.AdminPass = encrypted
}
postContent, err := json.Marshal(args)
if err != nil {
return instanceIDs, err
}
req, err := bce.NewRequest("POST", c.GetURL("v2/instance", params), bytes.NewBuffer(postContent))
if err != nil {
return instanceIDs, err
}
resp, err := c.SendRequest(req, option)
if err != nil {
return instanceIDs, err
}
bodyContent, err := resp.GetBodyContent()
if err != nil {
return instanceIDs, err
}
var ciResp *CreateInstanceResponse
if err := json.Unmarshal(bodyContent, &ciResp); err != nil {
return instanceIDs, err
}
return ciResp.InstanceIDs, nil
}
// ListInstances gets all Instances.
func (c *Client) ListInstances(option *bce.SignOption) ([]Instance, error) {
req, err := bce.NewRequest("GET", c.GetURL("v2/instance", nil), nil)
if err != nil {
return nil, err
}
resp, err := c.SendRequest(req, option)
if err != nil {
return nil, err
}
bodyContent, err := resp.GetBodyContent()
if err != nil {
return nil, err
}
var insList *ListInstancesResponse
err = json.Unmarshal(bodyContent, &insList)
if err != nil {
return nil, err
}
return insList.Instances, nil
}
// DescribeInstance describe a instance
func (c *Client) DescribeInstance(instanceID string, option *bce.SignOption) (*Instance, error) {
req, err := bce.NewRequest("GET", c.GetURL("v2/instance"+"/"+instanceID, nil), nil)
if err != nil {
return nil, err
}
resp, err := c.SendRequest(req, option)
if err != nil {
return nil, err
}
bodyContent, err := resp.GetBodyContent()
if err != nil {
return nil, err
}
var ins GetInstanceResponse
err = json.Unmarshal(bodyContent, &ins)
if err != nil {
return nil, err
}
return &ins.Ins, nil
}
// DeleteInstance delete a instance
func (c *Client) DeleteInstance(instanceID string, option *bce.SignOption) error {
instance, err := c.DescribeInstance(instanceID, nil)
if err != nil {
return err
}
// release eip if necessary
if len(instance.PublicIP) != 0 {
eipClient := eip.NewEIPClient(c.Config)
eipArg := &eip.EipArgs{
Ip: instance.PublicIP,
}
// not return err even if failed
eipClient.UnbindEip(eipArg, nil)
eipClient.DeleteEip(eipArg, nil)
}
req, err := bce.NewRequest("DELETE", c.GetURL("v2/instance"+"/"+instanceID, nil), nil)
if err != nil {
return err
}
_, err = c.SendRequest(req, option)
if err != nil {
return err
}
return nil
}