-
Notifications
You must be signed in to change notification settings - Fork 48
/
CommonNhnCloudFunc.go
322 lines (267 loc) · 8.64 KB
/
CommonNhnCloudFunc.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
// 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 Example for PoC Test.
//
// by ETRI, Innogrid, 2021.12.
package resources
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"time"
nhnsdk "github.com/cloud-barista/nhncloud-sdk-go"
"github.com/cloud-barista/nhncloud-sdk-go/openstack/compute/v2/extensions/secgroups"
"github.com/cloud-barista/nhncloud-sdk-go/openstack/compute/v2/flavors"
"github.com/cloud-barista/nhncloud-sdk-go/openstack/networking/v2/ports"
"github.com/cloud-barista/nhncloud-sdk-go/openstack/networking/v2/vpcs"
"github.com/cloud-barista/nhncloud-sdk-go/openstack/networking/v2/vpcsubnets"
"github.com/sirupsen/logrus"
cblog "github.com/cloud-barista/cb-log"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)
var once sync.Once
var cblogger *logrus.Logger
var calllogger *logrus.Logger
func InitLog() {
once.Do(func() {
// cblog is a global variable.
cblogger = cblog.GetLogger("CB-SPIDER")
calllogger = call.GetLogger("HISCALL")
})
}
func LoggingError(hiscallInfo call.CLOUDLOGSCHEMA, err error) {
cblogger.Error(err.Error())
hiscallInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(hiscallInfo))
}
func LoggingInfo(hiscallInfo call.CLOUDLOGSCHEMA, start time.Time) {
hiscallInfo.ElapsedTime = call.Elapsed(start)
calllogger.Info(call.String(hiscallInfo))
}
func getCallLogScheme(zone string, resourceType call.RES_TYPE, resourceName string, apiName string) call.CLOUDLOGSCHEMA {
cblogger.Info(fmt.Sprintf("Call %s %s", call.NHNCLOUD, apiName))
return call.CLOUDLOGSCHEMA{
CloudOS: call.NHNCLOUD,
RegionZone: zone,
ResourceType: resourceType,
ResourceName: resourceName,
CloudOSAPI: apiName,
}
}
func logAndReturnError(callLogInfo call.CLOUDLOGSCHEMA, givenErrString string, v interface{}) error {
newErr := fmt.Errorf(givenErrString+" %v", v)
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return newErr
}
func getPublicVPCInfo(networkClient *nhnsdk.ServiceClient, typeName string) (string, error) {
cblogger.Info("NHN Cloud Driver: called getPublicVPCInfo()")
listOpts := vpcs.ListOpts{
RouterExternal: true,
}
allPages, err := vpcs.List(networkClient, listOpts).AllPages()
if err != nil {
newErr := fmt.Errorf("Failed to Get VPC Pages. %s", err.Error())
cblogger.Error(newErr)
return "", newErr
}
// external VPC 필터링
var extVpcList []NetworkWithExt
err = vpcs.ExtractVPCsInto(allPages, &extVpcList)
if err != nil {
newErr := fmt.Errorf("Failed to Get VPC list. %s", err.Error())
cblogger.Error(newErr)
return "", newErr
}
if len(extVpcList) == 0 {
newErr := fmt.Errorf("Failed to Get VPC list, external VPC not exist")
cblogger.Error(newErr)
return "", newErr
}
extVpc := extVpcList[0]
if strings.EqualFold(typeName, "ID") {
return extVpc.ID, nil
} else if strings.EqualFold(typeName, "NAME") {
return extVpc.Name, nil
}
return "", nil
}
func getVMSpecIdWithName(client *nhnsdk.ServiceClient, flavorName string) (string, error) {
cblogger.Info("NHN Cloud Driver: called GetVMSpecIdWithName()")
allPages, err := flavors.ListDetail(client, nil).AllPages()
if err != nil {
return "", err
}
nhnFlavorList, err := flavors.ExtractFlavors(allPages)
if err != nil {
return "", err
}
for _, nhnFlavor := range nhnFlavorList {
if strings.EqualFold(nhnFlavor.Name, flavorName) {
return nhnFlavor.ID, nil
}
}
return "", fmt.Errorf("Failed to Find Flavor with the name [%s]", flavorName)
}
func getSGWithName(networkClient *nhnsdk.ServiceClient, securityGroupName string) (*secgroups.SecurityGroup, error) {
cblogger.Info("NHN Cloud Driver: called GetSGWithName()")
allPages, err := secgroups.List(networkClient).AllPages()
if err != nil {
return nil, err
}
nhnSGList, err := secgroups.ExtractSecurityGroups(allPages)
if err != nil {
return nil, err
}
for _, nhnSG := range nhnSGList {
if strings.EqualFold(nhnSG.Name, securityGroupName) {
return &nhnSG, nil
}
}
return nil, fmt.Errorf("Failed to Find SecurityGroups with the name [%s]", securityGroupName)
}
func getVPCWithName(networkClient *nhnsdk.ServiceClient, vpcName string) (*vpcs.VPC, error) {
cblogger.Info("NHN Cloud Driver: called GetVPCWithName()")
allPages, err := vpcs.List(networkClient, vpcs.ListOpts{Name: vpcName}).AllPages()
if err != nil {
return nil, err
}
nhnVPCList, err := vpcs.ExtractVPCs(allPages)
if err != nil {
return nil, err
}
for _, nhnVPC := range nhnVPCList {
if strings.EqualFold(nhnVPC.Name, vpcName) {
return &nhnVPC, nil
}
}
return nil, fmt.Errorf("Failed to Find VPC Info with name [%s]", vpcName)
}
func getVPCWithId(networkClient *nhnsdk.ServiceClient, vpcId string) (*vpcs.VPC, error) {
cblogger.Info("NHN Cloud Driver: called getVPCWithId()")
allPages, err := vpcs.List(networkClient, vpcs.ListOpts{ID: vpcId}).AllPages()
if err != nil {
return nil, err
}
nhnVPCList, err := vpcs.ExtractVPCs(allPages)
if err != nil {
return nil, err
}
for _, nhnVPC := range nhnVPCList {
if strings.EqualFold(nhnVPC.ID, vpcId) {
return &nhnVPC, nil
}
}
return nil, fmt.Errorf("Failed to Find VPC Info with id [%s]", vpcId)
}
func getVpcsubnetWithId(networkClient *nhnsdk.ServiceClient, vpcsubnetId string) (*vpcsubnets.Vpcsubnet, error) {
cblogger.Info("NHN Cloud Driver: called GetVpcsubnetWithId()")
nhnSubnet, err := vpcsubnets.Get(networkClient, vpcsubnetId).Extract()
if err != nil {
return nil, err
}
return nhnSubnet, nil
}
func getPortWithDeviceId(networkClient *nhnsdk.ServiceClient, deviceID string) (*ports.Port, error) {
cblogger.Info("NHN Cloud Driver: called GetPortWithDeviceId()")
allPages, err := ports.List(networkClient, ports.ListOpts{}).AllPages()
if err != nil {
return nil, err
}
nhnPortList, err := ports.ExtractPorts(allPages)
if err != nil {
return nil, err
}
for _, nhnPort := range nhnPortList {
if strings.EqualFold(nhnPort.DeviceID, deviceID) {
return &nhnPort, nil
}
}
return nil, fmt.Errorf("Failed to Get Port Info. with the DeviceID [%s]", deviceID)
}
func checkIIDValidation(IId irs.IID) bool {
if strings.EqualFold(IId.NameId, "") && strings.EqualFold(IId.SystemId, "") {
newErr := fmt.Errorf("Invalid NameId and SystemId!!")
cblogger.Error(newErr.Error())
return false
}
return true
}
func checkFolderAndCreate(folderPath string) error {
// Check if the Folder Exists and Create it
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
if err := os.Mkdir(folderPath, 0700); err != nil {
return err
}
}
return nil
}
func getOriginalNameId(IID2NameId string) string {
var originalNameId string
if len(IID2NameId) <= 9 { // For local test
originalNameId = IID2NameId
} else { // For CB-Spider IID2 NameId
reversedNameId := reverse(IID2NameId)
originalNameId = reversedNameId[:21]
originalNameId = strings.TrimSuffix(IID2NameId, reverse(originalNameId))
}
cblogger.Infof("# originalNameId : %s", originalNameId)
return originalNameId
}
func reverse(s string) (result string) {
for _, v := range s {
result = string(v) + result
}
return
}
func runCommand(cmdName string, cmdArgs []string) (string, error) {
/*
Ref)
var (
cmdOut []byte
cmdErr error
)
*/
cblogger.Infof("cmdName : %s", cmdName)
cblogger.Infof("cmdArgs : %s", cmdArgs)
//if cmdOut, cmdErr = exec.Command(cmdName, cmdArgs...).Output(); cmdErr != nil {
if cmdOut, cmdErr := exec.Command(cmdName, cmdArgs...).CombinedOutput(); cmdErr != nil {
fmt.Fprintln(os.Stderr, "There was an Error running command : ", cmdErr)
//panic("Can't exec the command: " + cmdErr1.Error())
fmt.Println(fmt.Sprint(cmdErr) + ": " + string(cmdOut))
os.Exit(1)
return string(cmdOut), cmdErr
} else {
fmt.Println("cmdOut : ", string(cmdOut))
return string(cmdOut), nil
}
}
// Convert Cloud Object to JSON String type
func convertJsonString(v interface{}) (string, error) {
jsonBytes, err := json.Marshal(v)
if err != nil {
newErr := fmt.Errorf("Failed to Convert Json to String. [%v]", err.Error())
cblogger.Error(newErr.Error())
return "", newErr
}
jsonString := string(jsonBytes)
return jsonString, nil
}
// Convert time to KTC
func convertTimeToKTC(givenTime time.Time) (time.Time, error) {
loc, err := time.LoadLocation("Asia/Seoul")
if err != nil {
newErr := fmt.Errorf("Failed to Convert the Time to KTC. [%v]", err)
cblogger.Error(newErr.Error())
return givenTime, newErr
}
return givenTime.In(loc), nil
}