-
Notifications
You must be signed in to change notification settings - Fork 46
/
RegionZoneHandler.go
262 lines (221 loc) · 8.76 KB
/
RegionZoneHandler.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
// Proof of Concepts for the Cloud-Barista Multi-Cloud Project.
// * Cloud-Barista: https://github.com/cloud-barista
//
// NCP VPC RegionZone Handler
//
// Created by ETRI, 2023.09.
//==================================================================================================
// RegionZoneInfo Fetch Speed Improvement and KeyValueList Omission Issue :
// https://github.com/cloud-barista/cb-spider/issues/930#issuecomment-1734817828
package resources
import (
// "errors"
"fmt"
"sync"
"strings"
// "github.com/davecgh/go-spew/spew"
// ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
vserver "github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/vserver"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-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"
)
type NcpRegionZoneHandler struct {
CredentialInfo idrv.CredentialInfo
RegionInfo idrv.RegionInfo
VMClient *vserver.APIClient
}
func (regionZoneHandler *NcpRegionZoneHandler) ListRegionZone() ([]*irs.RegionZoneInfo, error) {
cblogger.Info("NCP VPC Cloud Driver: called ListRegionZone()!!")
InitLog()
callLogInfo := GetCallLogScheme(regionZoneHandler.RegionInfo.Zone, call.REGIONZONE, "ListRegionZone()", "ListRegionZone()")
ncpVpcRegionList, err := regionZoneHandler.getNcpVpcRegionList("ListRegionZone()")
if err != nil {
rtnErr := logAndReturnError(callLogInfo, "Failed to Get RegionList from NCP Cloud : ", err)
return nil, rtnErr
}
var regionZoneInfoList []*irs.RegionZoneInfo
var wait sync.WaitGroup
var zoneListError error
for _, region := range ncpVpcRegionList {
wait.Add(1)
go func(region *vserver.Region) {
defer wait.Done()
cblogger.Info("# Search Criteria(NCP RegionCode) : ", *region.RegionCode)
regionZoneInfo := irs.RegionZoneInfo{
Name: *region.RegionCode,
DisplayName: *region.RegionName,
// KeyValueList: []irs.KeyValue{
// {Key: "RegionCode", Value: *region.RegionCode},
// },
}
ncpVpcZoneList, err := regionZoneHandler.getNcpVpcZoneList(region.RegionCode, "ListRegionZone()")
if err != nil {
zoneListError = err
return
}
var zoneInfoList []irs.ZoneInfo
for _, zone := range ncpVpcZoneList {
zoneInfo := irs.ZoneInfo{
Name: *zone.ZoneName,
DisplayName: *zone.ZoneDescription,
Status: irs.NotSupported,
// KeyValueList: []irs.KeyValue{
// {Key: "ZoneCode", Value: *zone.ZoneCode},
// },
}
zoneInfoList = append(zoneInfoList, zoneInfo)
}
regionZoneInfo.ZoneList = zoneInfoList
regionZoneInfoList = append(regionZoneInfoList, ®ionZoneInfo)
}(region)
}
wait.Wait()
if zoneListError != nil {
rtnErr := logAndReturnError(callLogInfo, "Failed to Get Zone List!!", zoneListError)
return nil, rtnErr
}
return regionZoneInfoList, nil
}
func (regionZoneHandler NcpRegionZoneHandler) GetRegionZone(regionCode string) (irs.RegionZoneInfo, error) {
cblogger.Info("NCP VPC Cloud Driver: called GetRegionZone()!!")
InitLog()
callLogInfo := GetCallLogScheme(regionZoneHandler.RegionInfo.Zone, call.REGIONZONE, regionCode, "GetRegionZone()")
if len(regionCode) < 1 {
rtnErr := logAndReturnError(callLogInfo, "Invalid RegionCode!!", "")
return irs.RegionZoneInfo{}, rtnErr
}
ncpVpcRegionList, err := regionZoneHandler.getNcpVpcRegionList("GetRegionZone()")
if err != nil {
rtnErr := logAndReturnError(callLogInfo, "Failed to Get RegionList from NCP Cloud : ", err)
return irs.RegionZoneInfo{}, rtnErr
}
var regionZoneInfo irs.RegionZoneInfo
for _, region := range ncpVpcRegionList {
if strings.EqualFold(regionCode, *region.RegionCode){
cblogger.Info("# Search Criteria(NCP RegionCode) : ", *region.RegionCode)
regionZoneInfo = irs.RegionZoneInfo{
Name: *region.RegionCode,
DisplayName: *region.RegionName,
// KeyValueList: []irs.KeyValue{
// {Key: "RegionCode", Value: *region.RegionCode},
// },
}
ncpVpcZoneList, err := regionZoneHandler.getNcpVpcZoneList(region.RegionCode, "GetRegionZone()")
if err != nil {
rtnErr := logAndReturnError(callLogInfo, "Failed to Get ZoneList from NCP Cloud : ", err)
return irs.RegionZoneInfo{}, rtnErr
}
var zoneInfoList []irs.ZoneInfo
for _, zone := range ncpVpcZoneList {
zoneInfo := irs.ZoneInfo{
Name: *zone.ZoneName,
DisplayName: *zone.ZoneDescription,
Status: irs.NotSupported,
// KeyValueList: []irs.KeyValue{
// {Key: "ZoneCode", Value: *zone.ZoneCode},
// },
}
zoneInfoList = append(zoneInfoList, zoneInfo)
}
regionZoneInfo.ZoneList = zoneInfoList
}
}
return regionZoneInfo, nil
}
func (regionZoneHandler *NcpRegionZoneHandler) ListOrgRegion() (string, error) {
cblogger.Info("NCP VPC Cloud Driver: called ListOrgRegion()!!")
InitLog()
callLogInfo := GetCallLogScheme(regionZoneHandler.RegionInfo.Zone, call.REGIONZONE, "ListOrgRegion()", "ListOrgRegion()")
// To return the results with a style similar to other CSPs.
type Regions struct {
RegionList []*vserver.Region // Must be a capital letter!!
}
ncpVpcRegionList, err := regionZoneHandler.getNcpVpcRegionList("ListOrgRegion()")
if err != nil {
rtnErr := logAndReturnError(callLogInfo, "Failed to Get RegionList from NCP Cloud : ", err)
return "", rtnErr
}
ncpRegionList := Regions{
RegionList: ncpVpcRegionList,
}
jsonString, cvtErr := ConvertJsonString(ncpRegionList)
if cvtErr != nil {
rtnErr := logAndReturnError(callLogInfo, "Failed to Convert the RegionList to Json format string.", cvtErr)
return "", rtnErr
}
return jsonString, cvtErr
}
func (regionZoneHandler *NcpRegionZoneHandler) ListOrgZone() (string, error) {
cblogger.Info("NCP VPC Cloud Driver: called ListOrgZone()!!")
InitLog()
callLogInfo := GetCallLogScheme(regionZoneHandler.RegionInfo.Zone, call.REGIONZONE, "ListOrgZone()", "ListOrgZone()")
if len(regionZoneHandler.RegionInfo.Region) < 1 {
rtnErr := logAndReturnError(callLogInfo, "Invalid Region Info!!", "")
return "", rtnErr
}
// To return the results with a style similar to other CSPs.
type Zones struct {
ZoneList []*vserver.Zone // Must be a capital letter!!
}
ncpVpcZoneList, err := regionZoneHandler.getNcpVpcZoneList(®ionZoneHandler.RegionInfo.Region, "ListOrgZone()")
if err != nil {
newErr := fmt.Errorf("Failed to Get ZoneList from NCP Cloud : [%v]", err)
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return "", newErr
}
ncpZoneList := Zones{
ZoneList: ncpVpcZoneList,
}
jsonString, cvtErr := ConvertJsonString(ncpZoneList)
if cvtErr != nil {
newErr := fmt.Errorf("Failed to Convert the ZoneList to Json format string. : [%v]", cvtErr)
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return "", newErr
}
return jsonString, cvtErr
}
func (regionZoneHandler *NcpRegionZoneHandler) getNcpVpcRegionList(callLogfunc string) ([]*vserver.Region, error) {
cblogger.Info("NCP VPC Cloud Driver: called getNcpVpcRegionList()!!")
InitLog()
callLogInfo := GetCallLogScheme(regionZoneHandler.RegionInfo.Zone, call.REGIONZONE, callLogfunc, callLogfunc)
regionListReq := vserver.GetRegionListRequest{}
callLogStart := call.Start()
regionListResult, err := regionZoneHandler.VMClient.V2Api.GetRegionList(®ionListReq)
if err != nil {
rtnErr := logAndReturnError(callLogInfo, "Failed to Get RegionList from NCP Cloud : ", err)
return nil, rtnErr
}
LoggingInfo(callLogInfo, callLogStart)
if len(regionListResult.RegionList) < 1 {
rtnErr := logAndReturnError(callLogInfo, "Failed to Find Any Region Info.", "")
return nil, rtnErr
} else {
cblogger.Infof("# Supported Region count : [%d]", len(regionListResult.RegionList))
// spew.Dump(regionListResult)
}
return regionListResult.RegionList, nil
}
func (regionZoneHandler *NcpRegionZoneHandler) getNcpVpcZoneList(regionCode *string, callLogfunc string) ([]*vserver.Zone, error) {
cblogger.Info("NCP VPC Cloud Driver: called getNcpVpcZoneList()!!")
InitLog()
callLogInfo := GetCallLogScheme(regionZoneHandler.RegionInfo.Zone, call.REGIONZONE, *regionCode, callLogfunc)
zoneListReq := vserver.GetZoneListRequest{RegionCode: regionCode}
callLogStart := call.Start()
zoneListResult, err := regionZoneHandler.VMClient.V2Api.GetZoneList(&zoneListReq)
if err != nil {
rtnErr := logAndReturnError(callLogInfo, "Failed to Get ZoneList from NCP Cloud : ", err)
return nil, rtnErr
}
LoggingInfo(callLogInfo, callLogStart)
if len(zoneListResult.ZoneList) < 1 {
rtnErr := logAndReturnError(callLogInfo, "Failed to Find Any Zone Info.", "")
return nil, rtnErr
} else {
cblogger.Infof("# Supported Zone count [%s] : [%d]", *regionCode, len(zoneListResult.ZoneList))
// spew.Dump(zoneListResult)
}
return zoneListResult.ZoneList, nil
}