-
Notifications
You must be signed in to change notification settings - Fork 45
/
ImageHandler.go
214 lines (181 loc) · 7.17 KB
/
ImageHandler.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
// Proof of Concepts for the Cloud-Barista Multi-Cloud Project.
// * Cloud-Barista: https://github.com/cloud-barista
//
// NCP Image Handler
//
// by ETRI, 2020.09.
package resources
import (
// "errors"
"fmt"
"strconv"
"strings"
ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
server "github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
cblog "github.com/cloud-barista/cb-log"
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"
"github.com/sirupsen/logrus"
)
type NcpImageHandler struct {
CredentialInfo idrv.CredentialInfo
RegionInfo idrv.RegionInfo
VMClient *server.APIClient
}
var cblogger2 *logrus.Logger
func init() {
cblogger2 = cblog.GetLogger("NCP ImageHandler") // cblog is a global variable.
}
func (imageHandler *NcpImageHandler) GetImage(imageIID irs.IID) (irs.ImageInfo, error) {
cblogger.Info("NCP Classic Cloud Driver: called GetImage()!!")
InitLog()
callLogInfo := GetCallLogScheme(imageHandler.RegionInfo.Zone, call.VMIMAGE, imageIID.SystemId, "GetImage()")
if strings.EqualFold(imageIID.SystemId, "") {
newErr := fmt.Errorf("Invalid Image SystemId")
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return irs.ImageInfo{}, newErr
}
ncpImageInfo, err := imageHandler.GetNcpImageInfo(imageIID)
if err != nil {
newErr := fmt.Errorf("Failed to Get the Image Info from NCP : [%v]", err)
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return irs.ImageInfo{}, newErr
}
imageInfo := MappingImageInfo(*ncpImageInfo)
return imageInfo, nil
}
func (imageHandler *NcpImageHandler) ListImage() ([]*irs.ImageInfo, error) {
cblogger.Info("NCP Classic Cloud Driver: called ListImage()!")
InitLog()
callLogInfo := GetCallLogScheme(imageHandler.RegionInfo.Zone, call.VMIMAGE, "ListImage()", "ListImage()")
vmHandler := NcpVMHandler{
RegionInfo: imageHandler.RegionInfo,
VMClient: imageHandler.VMClient,
}
regionNo, err := vmHandler.GetRegionNo(imageHandler.RegionInfo.Region)
if err != nil {
newErr := fmt.Errorf("Failed to Get NCP Region No of the Region Code: [%v]", err)
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return nil, newErr
}
imageReq := server.GetServerImageProductListRequest{
ProductCode: nil,
RegionNo: regionNo, // CAUTION!! : When searching image Info by RegionNo
}
callLogStart := call.Start()
result, err := imageHandler.VMClient.V2Api.GetServerImageProductList(&imageReq)
if err != nil {
cblogger.Error(*result.ReturnMessage)
newErr := fmt.Errorf("Failed to Find Image list from NCP VPC : [%v]", err)
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return nil, newErr
}
LoggingInfo(callLogInfo, callLogStart)
if len(result.ProductList) < 1 {
newErr := fmt.Errorf("Failed to Find Any Image Info.")
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return nil, newErr
} else {
cblogger.Info("Succeeded in Getting NCP Image list.")
}
var vmImageList []*irs.ImageInfo
for _, image := range result.ProductList {
imageInfo := MappingImageInfo(*image)
vmImageList = append(vmImageList, &imageInfo)
}
cblogger.Info("# Supported Image Product count : ", len(vmImageList))
return vmImageList, nil
}
func MappingImageInfo(serverImage server.Product) irs.ImageInfo {
cblogger.Info("NCP Classic Cloud Driver: called MappingImageInfo()!")
imageInfo := irs.ImageInfo{
IId: irs.IID{ // NOTE 주의 : serverImage.ProductCode -> ProductName 으로
NameId: *serverImage.ProductCode,
SystemId: *serverImage.ProductCode,
},
GuestOS: *serverImage.ProductDescription,
Status: "available",
}
//Image OS Information
keyValueList := []irs.KeyValue{
{Key: "PlatformType", Value: *serverImage.PlatformType.CodeName},
{Key: "InfraResourceType", Value: *serverImage.InfraResourceType.CodeName},
{Key: "BaseBlockStorageSize(GB)", Value: strconv.FormatFloat(float64(*serverImage.BaseBlockStorageSize)/(1024*1024*1024), 'f', 0, 64)},
//{Key: "OsInformation", Value: *serverImage.OsInformation},
//{Key: "DB Type", Value: *serverImage.DbKindCode},
//{Key: "NCP GenerationCode", Value: *serverImage.GenerationCode},
}
keyValueList = append(keyValueList, irs.KeyValue{Key: "Description", Value: *serverImage.ProductDescription})
imageInfo.KeyValueList = keyValueList
return imageInfo
}
func (imageHandler *NcpImageHandler) CreateImage(imageReqInfo irs.ImageReqInfo) (irs.ImageInfo, error) {
cblogger.Info("NCP Classic Cloud Driver: called CreateImage()!")
return irs.ImageInfo{}, fmt.Errorf("Does not support CreateImage() yet!!")
}
func (imageHandler *NcpImageHandler) CheckWindowsImage(imageIID irs.IID) (bool, error) {
cblogger.Info("NCP Classic Cloud Driver: called CheckWindowsImage()")
InitLog()
callLogInfo := GetCallLogScheme(imageHandler.RegionInfo.Region, call.MYIMAGE, imageIID.SystemId, "CheckWindowsImage()")
if strings.EqualFold(imageIID.SystemId, "") {
newErr := fmt.Errorf("Invalid myImage SystemId!!")
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return false, newErr
}
myImageHandler := NcpMyImageHandler{
RegionInfo: imageHandler.RegionInfo,
VMClient: imageHandler.VMClient,
}
myImagePlatform, err := myImageHandler.GetOriginImageOSPlatform(imageIID)
if err != nil {
newErr := fmt.Errorf("Failed to Get MyImage Info. [%v]", err.Error())
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return false, newErr
}
isWindowsImage := false
if strings.Contains(myImagePlatform, "WINDOWS") {
isWindowsImage = true
}
return isWindowsImage, nil
}
func (imageHandler *NcpImageHandler) DeleteImage(imageIID irs.IID) (bool, error) {
cblogger.Info("NCP Classic Cloud Driver: called DeleteImage()!")
return false, fmt.Errorf("Does not support DeleteImage() yet!!")
}
func (imageHandler *NcpImageHandler) GetNcpImageInfo(imageIID irs.IID) (*server.Product, error) {
cblogger.Info("NCP Classic Cloud Driver: called GetNcpImageInfo()!!")
InitLog()
callLogInfo := GetCallLogScheme(imageHandler.RegionInfo.Zone, call.VMIMAGE, imageIID.SystemId, "GetNcpImageInfo()")
if strings.EqualFold(imageIID.SystemId, "") {
createErr := fmt.Errorf("Invalid Image SystemId")
cblogger.Error(createErr.Error())
LoggingError(callLogInfo, createErr)
return nil, createErr
}
imageReq := server.GetServerImageProductListRequest{ProductCode: ncloud.String(imageIID.SystemId)}
callLogStart := call.Start()
result, err := imageHandler.VMClient.V2Api.GetServerImageProductList(&imageReq)
if err != nil {
newErr := fmt.Errorf("Failed to Find Image list from NCP : [%v]", err)
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return nil, newErr
}
LoggingInfo(callLogInfo, callLogStart)
if len(result.ProductList) < 1 {
newErr := fmt.Errorf("Failed to Find Any Image info with the SystemId : [%s]", imageIID.SystemId)
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return nil, newErr
}
cblogger.Info("Succeeded in Getting NCP Image info.")
return result.ProductList[0], nil
}