-
Notifications
You must be signed in to change notification settings - Fork 48
/
Test_NLBHandler.go
371 lines (329 loc) · 9.91 KB
/
Test_NLBHandler.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
// Proof of Concepts of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// This is a Cloud Driver Tester Example.
//
// by ETRI, 2022.07.
package main
import (
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"os"
cblog "github.com/cloud-barista/cb-log"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
// nhndrv "github.com/cloud-barista/nhncloud/nhncloud"
nhndrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/nhncloud"
)
var cblogger *logrus.Logger
func init() {
// cblog is a global variable.
cblogger = cblog.GetLogger("NHN Cloud Resource Test")
cblog.SetLevel("info")
}
// Test VMSpec
func handleNLB() {
cblogger.Debug("Start NLBHandler Resource Test")
ResourceHandler, err := getResourceHandler("NLB")
if err != nil {
panic(err)
}
nlbHandler := ResourceHandler.(irs.NLBHandler)
for {
fmt.Println("\n============================================================================================")
fmt.Println("[ NLB Resource Test ]")
cblogger.Info("1. ListNLB()")
cblogger.Info("2. GetNLB()")
cblogger.Info("3. CreateNLB()")
cblogger.Info("4. DeleteNLB()")
cblogger.Info("5. ChangeListener()")
cblogger.Info("6. ChangeVMGroupInfo()")
cblogger.Info("7. AddVMs()")
cblogger.Info("8. RemoveVMs()")
cblogger.Info("9. GetVMGroupHealthInfo()")
cblogger.Info("10. ChangeHealthCheckerInfo()")
cblogger.Info("11. Exit")
fmt.Println("============================================================================================")
config := readConfigFile()
cblogger.Infof("\n # NHN Cloud Region : [%s]", config.NhnCloud.Region)
cblogger.Info("\n # Num : ")
nlbIId := irs.IID{
NameId: "new_lb-1",
SystemId: "8637825b-d053-48c6-bdf3-7d3fd355f086",
}
nlbCreateReqInfo := irs.NLBInfo{
IId: irs.IID{
NameId: "new_nlb-1",
},
VpcIID: irs.IID{
NameId: "nhn-vpc-1",
},
Listener: irs.ListenerInfo{
Protocol: "TCP",
Port: "80",
},
VMGroup: irs.VMGroupInfo{
Protocol: "TCP",
Port: "80",
VMs: &[]irs.IID{
// {NameId: "nhn-vm-1"},
{NameId: "nhn-vm-02"},
},
},
HealthChecker: irs.HealthCheckerInfo{
Protocol: "TCP",
Port: "80",
Interval: -1,
Timeout: -1,
Threshold: -1,
},
// HealthChecker: irs.HealthCheckerInfo{
// Protocol: "TCP",
// Port: "8080",
// Interval: 30,
// Timeout: 5,
// Threshold: 3,
// },
}
updateListener := irs.ListenerInfo{
Protocol: "TCP",
Port: "8087",
}
updateVMGroups := irs.VMGroupInfo{
Protocol: "TCP",
Port: "8087",
}
addVMs := []irs.IID{
{NameId: "nhn-vm-3"},
{NameId: "nhn-vm-02"},
}
removeVMs := []irs.IID{
{NameId: "nhn-vm-4"},
// {NameId: "nhn-vm-3"},
// {NameId: "nhn-vm-4"},
}
updateHealthCheckerInfo := irs.HealthCheckerInfo{
Protocol: "HTTP",
Port: "8080",
Interval: 7,
Timeout: 5,
Threshold: 4,
}
var commandNum int
inputCnt, err := fmt.Scan(&commandNum)
if err != nil {
panic(err)
}
if inputCnt == 1 {
switch commandNum {
case 1:
cblogger.Info("Start ListNLB() ...")
if list, err := nlbHandler.ListNLB(); err != nil {
cblogger.Error(err)
} else {
spew.Dump(list)
cblogger.Info("출력 결과 수 : ", len(list))
}
cblogger.Info("Finish ListNLB()")
case 2:
cblogger.Info("Start GetNLB() ...")
if nlbInfo, err := nlbHandler.GetNLB(nlbIId); err != nil {
cblogger.Error(err)
} else {
spew.Dump(nlbInfo)
}
cblogger.Info("Finish GetNLB()")
case 3:
cblogger.Info("Start CreateNLB() ...")
if nlbInfo, err := nlbHandler.CreateNLB(nlbCreateReqInfo); err != nil {
cblogger.Error(err)
} else {
spew.Dump(nlbInfo)
}
cblogger.Info("Finish CreateNLB()")
case 4:
cblogger.Info("Start DeleteNLB() ...")
if nlbStatus, err := nlbHandler.DeleteNLB(nlbIId); err != nil {
cblogger.Error(err)
} else {
spew.Dump(nlbStatus)
}
cblogger.Info("Finish DeleteNLB()")
case 5:
cblogger.Info("Start ChangeListener() ...")
if nlbInfo, err := nlbHandler.ChangeListener(nlbIId, updateListener); err != nil {
cblogger.Error(err)
} else {
spew.Dump(nlbInfo)
}
cblogger.Info("Finish ChangeListener()")
case 6:
cblogger.Info("Start ChangeVMGroupInfo() ...")
if info, err := nlbHandler.ChangeVMGroupInfo(nlbIId, updateVMGroups); err != nil {
cblogger.Error(err)
} else {
spew.Dump(info)
}
cblogger.Info("Finish ChangeVMGroupInfo()")
case 7:
cblogger.Info("Start AddVMs() ...")
if info, err := nlbHandler.AddVMs(nlbIId, &addVMs); err != nil {
cblogger.Error(err)
} else {
spew.Dump(info)
}
cblogger.Info("Finish AddVMs()")
case 8:
cblogger.Info("Start RemoveVMs() ...")
if result, err := nlbHandler.RemoveVMs(nlbIId, &removeVMs); err != nil {
cblogger.Error(err)
} else {
spew.Dump(result)
}
cblogger.Info("Finish RemoveVMs()")
case 9:
cblogger.Info("Start GetVMGroupHealthInfo() ...")
if info, err := nlbHandler.GetVMGroupHealthInfo(nlbIId); err != nil {
cblogger.Error(err)
} else {
spew.Dump(info)
}
cblogger.Info("Finish GetVMGroupHealthInfo()")
case 10:
cblogger.Info("Start ChangeHealthCheckerInfo() ...")
if info, err := nlbHandler.ChangeHealthCheckerInfo(nlbIId, updateHealthCheckerInfo); err != nil {
cblogger.Error(err)
} else {
spew.Dump(info)
}
cblogger.Info("Finish ChangeHealthCheckerInfo()")
case 11:
cblogger.Info("Exit")
}
}
}
}
func main() {
cblogger.Info("NHN Cloud Resource Test")
handleNLB()
}
// handlerType : resources폴더의 xxxHandler.go에서 Handler이전까지의 문자열
// (예) ImageHandler.go -> "Image"
func getResourceHandler(handlerType string) (interface{}, error) {
var cloudDriver idrv.CloudDriver
cloudDriver = new(nhndrv.NhnCloudDriver)
config := readConfigFile()
// spew.Dump(config)
connectionInfo := idrv.ConnectionInfo{
CredentialInfo: idrv.CredentialInfo{
IdentityEndpoint: config.NhnCloud.IdentityEndpoint,
Username: config.NhnCloud.Nhn_Username,
Password: config.NhnCloud.Api_Password,
DomainName: config.NhnCloud.DomainName,
TenantId: config.NhnCloud.TenantId,
},
RegionInfo: idrv.RegionInfo{
Region: config.NhnCloud.Region,
Zone: config.NhnCloud.Zone,
},
}
cloudConnection, errCon := cloudDriver.ConnectCloud(connectionInfo)
if errCon != nil {
return nil, errCon
}
var resourceHandler interface{}
var err error
switch handlerType {
case "Image":
resourceHandler, err = cloudConnection.CreateImageHandler()
case "Security":
resourceHandler, err = cloudConnection.CreateSecurityHandler()
case "VNetwork":
resourceHandler, err = cloudConnection.CreateVPCHandler()
case "VM":
resourceHandler, err = cloudConnection.CreateVMHandler()
case "VMSpec":
resourceHandler, err = cloudConnection.CreateVMSpecHandler()
case "VPC":
resourceHandler, err = cloudConnection.CreateVPCHandler()
case "NLB":
resourceHandler, err = cloudConnection.CreateNLBHandler()
}
if err != nil {
return nil, err
}
return resourceHandler, nil
}
// Region : 사용할 리전명 (ex) ap-northeast-2
// ImageID : VM 생성에 사용할 AMI ID (ex) ami-047f7b46bd6dd5d84
// BaseName : 다중 VM 생성 시 사용할 Prefix이름 ("BaseName" + "_" + "숫자" 형식으로 VM을 생성 함.) (ex) mcloud-barista
// VmID : 라이프 사이트클을 테스트할 EC2 인스턴스ID
// InstanceType : VM 생성시 사용할 인스턴스 타입 (ex) t2.micro
// KeyName : VM 생성시 사용할 키페어 이름 (ex) mcloud-barista-keypair
// MinCount :
// MaxCount :
// SubnetId : VM이 생성될 VPC의 SubnetId (ex) subnet-cf9ccf83
// SecurityGroupID : 생성할 VM에 적용할 보안그룹 ID (ex) sg-0df1c209ea1915e4b
type Config struct {
NhnCloud struct {
IdentityEndpoint string `yaml:"identity_endpoint"`
Nhn_Username string `yaml:"nhn_username"`
Api_Password string `yaml:"api_password"`
DomainName string `yaml:"domain_name"`
TenantId string `yaml:"tenant_id"`
Region string `yaml:"region"`
Zone string `yaml:"zone"`
VMName string `yaml:"vm_name"`
ImageId string `yaml:"image_id"`
VMSpecId string `yaml:"vmspec_id"`
NetworkId string `yaml:"network_id"`
SecurityGroups string `yaml:"security_groups"`
KeypairName string `yaml:"keypair_name"`
VMId string `yaml:"vm_id"`
Image struct {
Name string `yaml:"name"`
} `yaml:"image_info"`
KeyPair struct {
Name string `yaml:"name"`
} `yaml:"keypair_info"`
PublicIP struct {
Name string `yaml:"name"`
} `yaml:"public_info"`
SecurityGroup struct {
Name string `yaml:"name"`
} `yaml:"security_group_info"`
VirtualNetwork struct {
Name string `yaml:"name"`
} `yaml:"vnet_info"`
Subnet struct {
Id string `yaml:"id"`
} `yaml:"subnet_info"`
Router struct {
Name string `yaml:"name"`
GateWayId string `yaml:"gateway_id"`
AdminStateUp bool `yaml:"adminstatup"`
} `yaml:"router_info"`
} `yaml:"nhncloud"`
}
func readConfigFile() Config {
// Set Environment Value of Project Root Path
// rootPath := "/home/sean/go/src/github.com/cloud-barista/nhncloud/nhncloud/main"
rootPath := os.Getenv("CBSPIDER_ROOT")
configPath := rootPath + "/cloud-control-manager/cloud-driver/drivers/nhncloud/main/conf/config.yaml"
cblogger.Info("Config file : " + configPath)
data, err := os.ReadFile(configPath)
if err != nil {
cblogger.Error(err)
}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
cblogger.Error(err)
}
return config
}