-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliyun.go
205 lines (164 loc) · 4.31 KB
/
aliyun.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
package aliyun
import (
"fmt"
"os"
"strings"
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/cs"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/aliyun/alibaba-cloud-sdk-go/services/rds"
"github.com/aliyun/alibaba-cloud-sdk-go/services/slb"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/gogap/config"
"github.com/gogap/context"
alierrors "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
)
type Tag struct {
Key string
Value string
}
type Aliyun struct {
Config config.Configuration
AccessKeyId string
AccessKeySecret string
Region string
Code string
// ZoneId string
vpcClient *vpc.Client
ecsClient *ecs.Client
ossClient *oss.Client
rdsClient *rds.Client
csClient *cs.Client
slbClient *slb.Client
dnsClient *alidns.Client
}
func NewAliyun(ctx context.Context, conf config.Configuration) *Aliyun {
code, _ := ctx.Value("code").(string)
if len(code) == 0 {
code = conf.GetString("code")
}
if len(code) == 0 {
panic(fmt.Errorf("the context of code is empty"))
}
envRegion := os.Getenv("ENV_ALIYUN_REGION")
envAkId := os.Getenv("ENV_ALIYUN_ACCESS_KEY_ID")
envAkSecret := os.Getenv("ENV_ALIYUN_ACCESS_KEY_SECRET")
akId := conf.GetString("aliyun.access-key-id", envAkId)
akSecret := conf.GetString("aliyun.access-key-secret", envAkSecret)
region := conf.GetString("aliyun.region", envRegion)
if len(akId) == 0 || len(akSecret) == 0 {
panic(fmt.Errorf("please set aliyun AccessKeyId into config or set env to ${ENV_ALIYUN_ACCESS_KEY_ID} and accessKeySecret into ${ENV_ALIYUN_ACCESS_KEY_SECRET}"))
}
if len(region) == 0 {
panic("region is empty, please set it to config file or env ${ENV_ALIYUN_REGION}")
}
ali := &Aliyun{
Config: conf,
AccessKeyId: akId,
AccessKeySecret: akSecret,
Region: region,
Code: code,
}
return ali
}
func (p *Aliyun) ECSClient() *ecs.Client {
if p.ecsClient == nil {
var err error
p.ecsClient, err = ecs.NewClientWithAccessKey(p.Region, p.AccessKeyId, p.AccessKeySecret)
if err != nil {
panic(err)
}
}
return p.ecsClient
}
func (p *Aliyun) OSSClient() *oss.Client {
if p.ossClient == nil {
endpoint := fmt.Sprintf("oss-%s.aliyuncs.com", p.Region)
var err error
p.ossClient, err = oss.New(endpoint, p.AccessKeyId, p.AccessKeySecret)
if err != nil {
panic(err)
}
}
return p.ossClient
}
func (p *Aliyun) RDSClient() *rds.Client {
if p.rdsClient == nil {
var err error
p.rdsClient, err = rds.NewClientWithAccessKey(p.Region, p.AccessKeyId, p.AccessKeySecret)
if err != nil {
panic(err)
}
}
return p.rdsClient
}
func (p *Aliyun) VPCClient() *vpc.Client {
if p.vpcClient == nil {
var err error
p.vpcClient, err = vpc.NewClientWithAccessKey(p.Region, p.AccessKeyId, p.AccessKeySecret)
if err != nil {
panic(err)
}
}
return p.vpcClient
}
func (p *Aliyun) CSClient() *cs.Client {
if p.csClient == nil {
p.csClient = cs.NewClient(p.AccessKeyId, p.AccessKeySecret)
}
return p.csClient
}
func (p *Aliyun) SLBClient() *slb.Client {
if p.slbClient == nil {
var err error
p.slbClient, err = slb.NewClientWithAccessKey(p.Region, p.AccessKeyId, p.AccessKeySecret)
if err != nil {
panic(err)
}
}
return p.slbClient
}
func (p *Aliyun) DNSClient() *alidns.Client {
if p.dnsClient == nil {
var err error
p.dnsClient, err = alidns.NewClientWithAccessKey(p.Region, p.AccessKeyId, p.AccessKeySecret)
if err != nil {
panic(err)
}
}
return p.dnsClient
}
func (p *Aliyun) signWithCode(str string) string {
return fmt.Sprintf("%s [%s]", str, p.Code)
}
func (p *Aliyun) isSignd(str string) bool {
return strings.Contains(str, fmt.Sprintf("[%s]", p.Code))
}
func IsAliErrCode(err error, code string) bool {
switch v := err.(type) {
case alierrors.Error:
{
if v.ErrorCode() == code {
return true
}
}
case *common.Error:
{
if v.Code == code {
return true
}
}
}
return false
}
func setENV(key, value string) error {
key = strings.Replace(key, "-", "_", -1)
key = strings.Replace(key, ".", "_", -1)
key = strings.Replace(key, " ", "_", -1)
key = strings.Replace(key, ":", "_", -1)
key = strings.ToUpper(key)
key = "ENV_ALIYUN_" + key
return os.Setenv(key, value)
}