-
Notifications
You must be signed in to change notification settings - Fork 14
/
aliyun_vswitch.go
256 lines (226 loc) · 8.74 KB
/
aliyun_vswitch.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
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* 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 aliyun
import (
"context"
ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v4/client"
"github.com/alibabacloud-go/tea/tea"
"github.com/chaosblade-io/chaosblade-exec-cloud/exec"
"github.com/chaosblade-io/chaosblade-exec-cloud/exec/category"
"github.com/chaosblade-io/chaosblade-spec-go/log"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
"os"
)
const VSwitchBin = "chaos_aliyun_vswitch"
type VSwitchActionSpec struct {
spec.BaseExpActionCommandSpec
}
func NewVSwitchActionSpec() spec.ExpActionCommandSpec {
return &VSwitchActionSpec{
spec.BaseExpActionCommandSpec{
ActionFlags: []spec.ExpFlagSpec{
&spec.ExpFlag{
Name: "accessKeyId",
Desc: "the accessKeyId of aliyun, if not provided, get from env ACCESS_KEY_ID",
},
&spec.ExpFlag{
Name: "accessKeySecret",
Desc: "the accessKeySecret of aliyun, if not provided, get from env ACCESS_KEY_SECRET",
},
&spec.ExpFlag{
Name: "type",
Desc: "the operation of VSwitch, support delete etc",
},
&spec.ExpFlag{
Name: "vSwitchId",
Desc: "the VSwitchId",
},
},
ActionExecutor: &VSwitchExecutor{},
ActionExample: `
# delete vSwitch which vSwitch id is i-x
blade create aliyun vSwitch --accessKeyId xxx --accessKeySecret yyy --type delete --vSwitchId i-x`,
ActionPrograms: []string{VSwitchBin},
ActionCategories: []string{category.Cloud + "_" + category.Aliyun + "_" + category.VSwitch},
},
}
}
func (*VSwitchActionSpec) Name() string {
return "vSwitch"
}
func (*VSwitchActionSpec) Aliases() []string {
return []string{}
}
func (*VSwitchActionSpec) ShortDesc() string {
return "do some aliyun vSwitchId Operations, like delete"
}
func (b *VSwitchActionSpec) LongDesc() string {
if b.ActionLongDesc != "" {
return b.ActionLongDesc
}
return "do some aliyun vSwitchId Operations, like delete"
}
type VSwitchExecutor struct {
channel spec.Channel
}
func (*VSwitchExecutor) Name() string {
return "vSwitch"
}
func (be *VSwitchExecutor) Exec(uid string, ctx context.Context, model *spec.ExpModel) *spec.Response {
if be.channel == nil {
util.Errorf(uid, util.GetRunFuncName(), spec.ChannelNil.Msg)
return spec.ResponseFailWithFlags(spec.ChannelNil)
}
accessKeyId := model.ActionFlags["accessKeyId"]
accessKeySecret := model.ActionFlags["accessKeySecret"]
operationType := model.ActionFlags["type"]
vSwitchId := model.ActionFlags["vSwitchId"]
regionId := model.ActionFlags["regionId"]
zoneId := model.ActionFlags["zoneId"]
cidrBlock := model.ActionFlags["cidrBlock"]
vpcId := model.ActionFlags["vpcId"]
if accessKeyId == "" {
val, ok := os.LookupEnv("ACCESS_KEY_ID")
if !ok {
log.Errorf(ctx, "could not get ACCESS_KEY_ID from env or parameter!")
return spec.ResponseFailWithFlags(spec.ParameterLess, "accessKeyId")
}
accessKeyId = val
}
if accessKeySecret == "" {
val, ok := os.LookupEnv("ACCESS_KEY_SECRET")
if !ok {
log.Errorf(ctx, "could not get ACCESS_KEY_SECRET from env or parameter!")
return spec.ResponseFailWithFlags(spec.ParameterLess, "accessKeySecret")
}
accessKeySecret = val
}
if regionId == "" {
log.Errorf(ctx, "regionId is required!")
return spec.ResponseFailWithFlags(spec.ParameterLess, "regionId")
}
if vSwitchId == "" {
log.Errorf(ctx, "vSwitchId is required!")
return spec.ResponseFailWithFlags(spec.ParameterLess, "vSwitchId")
}
if zoneId == "" {
log.Errorf(ctx, "zoneId is required!")
return spec.ResponseFailWithFlags(spec.ParameterLess, "zoneId")
}
if cidrBlock == "" {
log.Errorf(ctx, "cidrBlock is required!")
return spec.ResponseFailWithFlags(spec.ParameterLess, "cidrBlock")
}
if vpcId == "" {
log.Errorf(ctx, "vpcId is required!")
return spec.ResponseFailWithFlags(spec.ParameterLess, "vpcId")
}
vSwitchesStatusMap, _err := describeVSwitchesStatus(ctx, accessKeyId, accessKeySecret, regionId)
if _err != nil {
log.Errorf(ctx, "describe VSwitches Status failed")
return spec.ResponseFailWithFlags(spec.ParameterRequestFailed, "describe VSwitches Status failed")
}
if (vSwitchesStatusMap[vSwitchId] != "Available" && operationType == "delete") || (vSwitchesStatusMap[vSwitchId] == "Available" && operationType == "create") {
return be.stop(ctx, operationType, accessKeyId, accessKeySecret, regionId, vSwitchId, zoneId, cidrBlock, vpcId)
}
return be.start(ctx, operationType, accessKeyId, accessKeySecret, regionId, vSwitchId, zoneId, cidrBlock, vpcId)
}
func (be *VSwitchExecutor) start(ctx context.Context, operationType, accessKeyId, accessKeySecret, regionId, vSwitchId, zoneId, cidrBlock, vpcId string) *spec.Response {
switch operationType {
case "delete":
return deleteVSwitch(ctx, accessKeyId, accessKeySecret, regionId, vSwitchId)
case "create":
return createVSwitch(ctx, accessKeyId, accessKeySecret, regionId, zoneId, cidrBlock, vpcId)
default:
return spec.ResponseFailWithFlags(spec.ParameterInvalid, "type is not support(support delete)")
}
select {}
}
func (be *VSwitchExecutor) stop(ctx context.Context, operationType, accessKeyId, accessKeySecret, regionId, vSwitchId, zoneId, cidrBlock, vpcId string) *spec.Response {
switch operationType {
case "delete":
return createVSwitch(ctx, accessKeyId, accessKeySecret, regionId, zoneId, cidrBlock, vpcId)
case "create":
return deleteVSwitch(ctx, accessKeyId, accessKeySecret, regionId, vSwitchId)
default:
return spec.ResponseFailWithFlags(spec.ParameterInvalid, "type is not support(support delete)")
}
ctx = context.WithValue(ctx, "bin", VSwitchBin)
return exec.Destroy(ctx, be.channel, "aliyun vSwitch")
}
func (be *VSwitchExecutor) SetChannel(channel spec.Channel) {
be.channel = channel
}
// delete vSwitch
func deleteVSwitch(ctx context.Context, accessKeyId, accessKeySecret, regionId, vSwitchId string) *spec.Response {
client, _err := CreateClient(tea.String(accessKeyId), tea.String(accessKeySecret), regionId)
if _err != nil {
log.Errorf(ctx, "create aliyun client failed, err: %s", _err.Error())
return spec.ResponseFailWithFlags(spec.ContainerInContextNotFound, "create aliyun client failed")
}
deleteVSwitchRequest := &ecs20140526.DeleteVSwitchRequest{
VSwitchId: tea.String(vSwitchId),
}
_, _err = client.DeleteVSwitch(deleteVSwitchRequest)
if _err != nil {
log.Errorf(ctx, "delete aliyun vSwitch failed, err: %s", _err.Error())
return spec.ResponseFailWithFlags(spec.ContainerInContextNotFound, "delete aliyun vSwitch failed")
}
return spec.Success()
}
// create vSwitch
func createVSwitch(ctx context.Context, accessKeyId, accessKeySecret, regionId, zoneId, cidrBlock, vpcId string) *spec.Response {
client, _err := CreateClient(tea.String(accessKeyId), tea.String(accessKeySecret), regionId)
if _err != nil {
log.Errorf(ctx, "create aliyun client failed, err: %s", _err.Error())
return spec.ResponseFailWithFlags(spec.ContainerInContextNotFound, "create aliyun client failed")
}
createVSwitchRequest := &ecs20140526.CreateVSwitchRequest{
ZoneId: tea.String(zoneId),
CidrBlock: tea.String(cidrBlock),
VpcId: tea.String(vpcId),
}
_, _err = client.CreateVSwitch(createVSwitchRequest)
if _err != nil {
log.Errorf(ctx, "create aliyun vSwitch failed, err: %s", _err.Error())
return spec.ResponseFailWithFlags(spec.ContainerInContextNotFound, "create aliyun vSwitch failed")
}
return spec.Success()
}
// describe VSwitches status
func describeVSwitchesStatus(ctx context.Context, accessKeyId, accessKeySecret, regionId string) (_result map[string]string, _err error) {
client, _err := CreateClient(tea.String(accessKeyId), tea.String(accessKeySecret), regionId)
if _err != nil {
log.Errorf(ctx, "create aliyun client failed, err: %s", _err.Error())
return _result, _err
}
describeVSwitchesRequest := &ecs20140526.DescribeVSwitchesRequest{
RegionId: tea.String(regionId),
}
response, _err := client.DescribeVSwitches(describeVSwitchesRequest)
if _err != nil {
log.Errorf(ctx, "describe aliyun VSwitch status failed, err: %s", _err.Error())
return _result, _err
}
vSwitchStatusList := response.Body.VSwitches.VSwitch
statusMap := map[string]string{}
for _, vSwitchStatus := range vSwitchStatusList {
statusMap[*vSwitchStatus.VSwitchId] = *vSwitchStatus.Status
}
_result = statusMap
return _result, _err
}