forked from yunionio/cloudmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bingo.go
390 lines (351 loc) · 9.67 KB
/
bingo.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bingocloud
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"net/http"
"net/url"
"sort"
"strings"
"time"
xj "github.com/basgys/goxml2json"
"github.com/nyl1001/pkg/errors"
"github.com/nyl1001/pkg/util/httputils"
"github.com/nyl1001/pkg/jsonutils"
"yunion.io/x/log"
api "github.com/nyl1001/cloudmux/pkg/apis/compute"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
)
const (
CLOUD_PROVIDER_BINGO_CLOUD = api.CLOUD_PROVIDER_BINGO_CLOUD
MAX_RESULT = 20
)
type BingoCloudConfig struct {
cpcfg cloudprovider.ProviderConfig
endpoint string
accessKey string
secretKey string
debug bool
}
func NewBingoCloudClientConfig(endpoint, accessKey, secretKey string) *BingoCloudConfig {
cfg := &BingoCloudConfig{
endpoint: endpoint,
accessKey: accessKey,
secretKey: secretKey,
}
return cfg
}
func (cfg *BingoCloudConfig) CloudproviderConfig(cpcfg cloudprovider.ProviderConfig) *BingoCloudConfig {
cfg.cpcfg = cpcfg
return cfg
}
func (cfg *BingoCloudConfig) Debug(debug bool) *BingoCloudConfig {
cfg.debug = debug
return cfg
}
type SBingoCloudClient struct {
*BingoCloudConfig
regions []SRegion
}
func NewBingoCloudClient(cfg *BingoCloudConfig) (*SBingoCloudClient, error) {
client := &SBingoCloudClient{BingoCloudConfig: cfg}
var err error
client.regions, err = client.GetRegions()
if err != nil {
return nil, err
}
for i := range client.regions {
client.regions[i].client = client
}
return client, nil
}
func (self *SBingoCloudClient) GetAccountId() string {
return self.endpoint
}
func (self *SBingoCloudClient) GetRegion(id string) (*SRegion, error) {
for i := range self.regions {
if self.regions[i].RegionId == id {
return &self.regions[i], nil
}
}
if len(id) == 0 {
return &self.regions[0], nil
}
return nil, cloudprovider.ErrNotFound
}
func (self *SBingoCloudClient) getDefaultClient(timeout time.Duration) *http.Client {
client := httputils.GetDefaultClient()
if timeout > 0 {
client = httputils.GetTimeoutClient(timeout)
}
if self.cpcfg.ProxyFunc != nil {
httputils.SetClientProxyFunc(client, self.cpcfg.ProxyFunc)
}
return client
}
func (self *SBingoCloudClient) sign(query string) string {
uri, _ := url.Parse(self.endpoint)
items := strings.Split(query, "&")
sort.Slice(items, func(i, j int) bool {
x0, y0 := strings.Split(items[i], "=")[0], strings.Split(items[j], "=")[0]
return x0 < y0
})
path := "/"
if len(uri.Path) > 0 {
path = uri.Path
}
stringToSign := fmt.Sprintf("POST\n%s\n%s\n", uri.Host, path) + strings.Join(items, "&")
hmac := hmac.New(sha256.New, []byte(self.secretKey))
hmac.Write([]byte(stringToSign))
return base64.StdEncoding.EncodeToString(hmac.Sum(nil))
}
func setItemToArray(obj jsonutils.JSONObject) jsonutils.JSONObject {
objDict, ok := obj.(*jsonutils.JSONDict)
if ok {
for k, v := range objDict.Value() {
if v.String() == `""` {
objDict.Remove(k)
continue
}
vDict, ok := v.(*jsonutils.JSONDict)
if ok {
if vDict.Contains("item") {
item, _ := vDict.Get("item")
_, ok := item.(*jsonutils.JSONArray)
if !ok {
if k != "instancesSet" {
item = setItemToArray(item)
objDict.Set(k, jsonutils.NewArray(item))
} else {
objDict.Set(k, setItemToArray(item))
}
} else {
items, _ := item.GetArray()
for i := range items {
items[i] = setItemToArray(items[i])
}
objDict.Set(k, jsonutils.NewArray(items...))
}
for _, nk := range []string{"nextToken", "NextToken"} {
nextToken, _ := vDict.GetString(nk)
if len(nextToken) > 0 {
objDict.Set(nk, jsonutils.NewString(nextToken))
}
}
} else {
objDict.Set(k, setItemToArray(v))
}
} else if _, ok = v.(*jsonutils.JSONArray); ok {
if ok {
arr, _ := v.GetArray()
for i := range arr {
arr[i] = setItemToArray(arr[i])
}
objDict.Set(k, jsonutils.NewArray(arr...))
}
}
}
}
_, ok = obj.(*jsonutils.JSONArray)
if ok {
arr, _ := obj.GetArray()
for i := range arr {
arr[i] = setItemToArray(arr[i])
}
return jsonutils.NewArray(arr...)
}
return objDict
}
type sBingoError struct {
Response struct {
Errors struct {
Error struct {
Code string
ErrorNo string
Message string
}
}
}
}
func (e sBingoError) Error() string {
return jsonutils.Marshal(e.Response.Errors.Error).String()
}
func (self *SBingoCloudClient) invoke(action string, params map[string]string) (jsonutils.JSONObject, error) {
if self.cpcfg.ReadOnly {
for _, prefix := range []string{"Get", "List", "Describe"} {
if strings.HasPrefix(action, prefix) {
return nil, errors.Wrapf(cloudprovider.ErrAccountReadOnly, action)
}
}
}
var encode = func(k, v string) string {
d := url.Values{}
d.Set(k, v)
return d.Encode()
}
query := encode("Action", action)
for k, v := range params {
query += "&" + encode(k, v)
}
// 2022-02-11T03:57:37.000Z
sh, _ := time.LoadLocation("Asia/Shanghai")
timeStamp := time.Now().In(sh).Format("2006-01-02T15:04:05.000Z")
query += "&" + encode("Timestamp", timeStamp)
query += "&" + encode("AWSAccessKeyId", self.accessKey)
query += "&" + encode("Version", "2009-08-15")
query += "&" + encode("SignatureVersion", "2")
query += "&" + encode("SignatureMethod", "HmacSHA256")
query += "&" + encode("Signature", self.sign(query))
client := self.getDefaultClient(time.Minute * 5)
resp, err := httputils.Request(client, context.Background(), httputils.POST, self.endpoint, nil, strings.NewReader(query), self.debug)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
result, err := xj.Convert(bytes.NewReader(data))
if err != nil {
return nil, err
}
obj, err := jsonutils.Parse([]byte(result.String()))
if err != nil {
return nil, errors.Wrapf(err, "jsonutils.Parse")
}
obj = setItemToArray(obj)
if self.debug {
log.Debugf("response: %s", obj.PrettyString())
}
be := &sBingoError{}
_ = obj.Unmarshal(be)
if len(be.Response.Errors.Error.Code) > 0 {
return nil, be
}
respKey := action + "Response"
if obj.Contains(respKey) {
obj, err = obj.Get(respKey)
if err != nil {
return nil, err
}
}
return obj, nil
}
func (self *SBingoCloudClient) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
var tags []struct {
ResourceId string `json:"resourceId"`
Value string `json:"value"`
}
filter := map[string]string{}
filter["resource-type"] = "user"
filter["key"] = "ProjectID"
result, err := self.describeTags(filter)
if err != nil {
return nil, err
}
_ = result.Unmarshal(&tags, "tagSet")
var subAccounts []cloudprovider.SSubAccount
for i := range tags {
subAccount := cloudprovider.SSubAccount{
Account: self.accessKey,
Name: tags[i].ResourceId,
DefaultProjectId: tags[i].Value,
HealthStatus: api.CLOUD_PROVIDER_HEALTH_NORMAL,
}
subAccounts = append(subAccounts, subAccount)
}
return subAccounts, nil
}
func (self *SBingoCloudClient) GetEnrollmentAccounts() ([]cloudprovider.SEnrollmentAccount, error) {
params := map[string]string{"Marker": "", "MaxItems": "1000", "AccountName": "paas_app"}
var result struct {
IsTruncated string
Marker string `json:"marker,omitempty"`
Users struct {
Member *SAccount `json:"member,omitempty"`
}
}
var eas []cloudprovider.SEnrollmentAccount
for {
resp, err := self.invoke("ListAccounts", params)
if err != nil {
return nil, err
}
err = resp.Unmarshal(&result, "ListAccountsResult")
if err != nil {
return nil, err
}
ea := cloudprovider.SEnrollmentAccount{
Id: result.Users.Member.UserId,
Name: result.Users.Member.UserName,
}
eas = append(eas, ea)
//for _, user := range result.Users.Member {
// ea := cloudprovider.SEnrollmentAccount{
// Id: user.UserId,
// Name: user.UserName,
// }
// eas = append(eas, ea)
//}
if params["Marker"] == result.Marker {
break
}
params["Marker"] = result.Marker
}
return eas, nil
}
func (self *SBingoCloudClient) GetIRegions() []cloudprovider.ICloudRegion {
var ret []cloudprovider.ICloudRegion
for i := range self.regions {
self.regions[i].client = self
ret = append(ret, &self.regions[i])
}
return ret
}
func (self *SBingoCloudClient) describeTags(filter map[string]string) (jsonutils.JSONObject, error) {
params := map[string]string{"MaxResults": "10000"}
i := 1
for k, v := range filter {
params[fmt.Sprintf("Filter.%v.Name", i)] = k
params[fmt.Sprintf("Filter.%v.Value.1", i)] = v
i++
}
return self.invoke("DescribeTags", params)
}
func (self *SBingoCloudClient) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) {
iregions := self.GetIRegions()
for i := range iregions {
if iregions[i].GetGlobalId() == id {
return iregions[i], nil
}
}
return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
}
func (self *SBingoCloudClient) GetCapabilities() []string {
return []string{
cloudprovider.CLOUD_CAPABILITY_COMPUTE,
cloudprovider.CLOUD_CAPABILITY_NETWORK,
cloudprovider.CLOUD_CAPABILITY_SECURITY_GROUP,
cloudprovider.CLOUD_CAPABILITY_EIP,
cloudprovider.CLOUD_CAPABILITY_LOADBALANCER,
cloudprovider.CLOUD_CAPABILITY_OBJECTSTORE,
}
}