-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathcommon.go
102 lines (83 loc) · 2.71 KB
/
common.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
/*
Copyright © 2020 iiusky [email protected]
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 common
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ram"
"github.com/bndr/gotabulate"
)
var AccessKey string
var SecretKey string
var STSAccessKey string
var STSSecretKey string
var STSToken string
var UseSTS bool
var Verbose bool
var ECSRegions []ecs.Region
var APPVersion string
// InitEcsRegions 初始化区域信息表
func InitEcsRegions() bool {
client, err := GetEcsClient("cn-hangzhou")
request := ecs.CreateDescribeRegionsRequest()
request.Scheme = "https"
if Verbose {
requestByte, _ := json.Marshal(request)
fmt.Println(fmt.Sprintf("\r\n InitEcsRegions request is: %s", string(requestByte)))
}
if err != nil {
Logger().Error(fmt.Sprintf("【初始化区域信息表】创建客户端发生异常,异常信息为 %s", err.Error()))
return false
}
response, err := client.DescribeRegions(request)
if err != nil {
Logger().Error(fmt.Sprintf("【初始化区域信息表】创建获取区域信息请求发生异常,异常信息为 %s", err.Error()))
return false
}
if Verbose {
fmt.Println(fmt.Sprintf("\r\n InitEcsRegions response is: %s", response.String()))
}
ECSRegions = response.Regions.Region
return true
}
// GetEcsClient 获取ECS 客户端
func GetEcsClient(regionId string) (*ecs.Client, error) {
if UseSTS {
return ecs.NewClientWithStsToken(regionId, STSAccessKey, STSSecretKey, STSToken)
} else {
return ecs.NewClientWithAccessKey(regionId, AccessKey, SecretKey)
}
}
// GetRamClient 获取RAM客户端
func GetRamClient() (*ram.Client, error) {
if UseSTS {
return ram.NewClientWithStsToken("cn-beijing", STSAccessKey, STSSecretKey, STSToken)
} else {
return ram.NewClientWithAccessKey("cn-beijing", AccessKey, SecretKey)
}
}
// ShowRegions 显示地域信息
func ShowRegions() {
var dates [][]string
count := 0
for _, region := range ECSRegions {
data := []string{fmt.Sprintf("#%d", count+1), region.LocalName, region.RegionId}
dates = append(dates, data)
count = count + 1
}
t := gotabulate.Create(dates)
t.SetHeaders([]string{"#", "名称", "区域ID"})
t.SetAlign("center")
fmt.Println(t.Render("grid"))
}