-
Notifications
You must be signed in to change notification settings - Fork 47
/
MyImageHandler.go
281 lines (248 loc) · 10.3 KB
/
MyImageHandler.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
package resources
import (
"context"
"errors"
"fmt"
"github.com/IBM/vpc-go-sdk/vpcv1"
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"
"strings"
"time"
)
type IbmMyImageHandler struct {
CredentialInfo idrv.CredentialInfo
Region idrv.RegionInfo
VpcService *vpcv1.VpcV1
Ctx context.Context
}
const DEV = "-dev-"
func (myImageHandler *IbmMyImageHandler) SnapshotVM(snapshotReqInfo irs.MyImageInfo) (irs.MyImageInfo, error) {
hiscallInfo := GetCallLogScheme(myImageHandler.Region, call.MYIMAGE, snapshotReqInfo.IId.NameId, "SnapshotVM()")
if len(snapshotReqInfo.IId.NameId) > 55 {
createErr := errors.New(fmt.Sprintf("Failed to SnapshotVM VM. err = MyImage Name ID cannot be longer than 55 characters"))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return irs.MyImageInfo{}, createErr
}
if strings.Contains(snapshotReqInfo.IId.NameId, DEV) {
createErr := errors.New(fmt.Sprintf("Failed to SnapshotVM VM. err = MyImage Name ID cannot include reserved string : %s", DEV))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return irs.MyImageInfo{}, createErr
}
attachedDiskList, listAttachedDiskErr := listRawAttachedDiskByVmIID(myImageHandler.VpcService, myImageHandler.Ctx, snapshotReqInfo.SourceVM)
if listAttachedDiskErr != nil {
createErr := errors.New(fmt.Sprintf("Failed to SnapshotVM VM. err = %s", listAttachedDiskErr.Error()))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return irs.MyImageInfo{}, createErr
}
start := call.Start()
mountIndex := 0
for _, attachedDisk := range (*attachedDiskList).VolumeAttachments {
mountIndex++
snapshotName := fmt.Sprintf("%s%s%d", snapshotReqInfo.IId.NameId, DEV, mountIndex)
createSnapshotOptions := vpcv1.CreateSnapshotOptions{
Name: &snapshotName,
SourceVolume: &vpcv1.VolumeIdentityByID{
ID: attachedDisk.Volume.ID,
},
}
_, _, createSnapshotErr := myImageHandler.VpcService.CreateSnapshotWithContext(myImageHandler.Ctx, &createSnapshotOptions)
if createSnapshotErr != nil {
createErr := errors.New(fmt.Sprintf("Failed to SnapshotVM VM. err = %s", createSnapshotErr.Error()))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return irs.MyImageInfo{}, createErr
}
}
LoggingInfo(hiscallInfo, start)
// get myimage info
converted, convertErr := myImageHandler.GetMyImage(irs.IID{NameId: snapshotReqInfo.IId.NameId})
if convertErr != nil {
createErr := errors.New(fmt.Sprintf("Failed to SnapshotVM VM. err = %s", convertErr.Error()))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return irs.MyImageInfo{}, createErr
}
return converted, nil
}
func (myImageHandler *IbmMyImageHandler) ListMyImage() ([]*irs.MyImageInfo, error) {
hiscallInfo := GetCallLogScheme(myImageHandler.Region, call.MYIMAGE, "MYIMAGE", "ListMyImage()")
start := call.Start()
snapshotList, _, listSnapshotErr := myImageHandler.VpcService.ListSnapshotsWithContext(myImageHandler.Ctx, &vpcv1.ListSnapshotsOptions{})
if listSnapshotErr != nil {
createErr := errors.New(fmt.Sprintf("Failed to List MyImage. err = %s", listSnapshotErr.Error()))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return nil, createErr
}
groupByImageResult := make(map[string][]vpcv1.Snapshot)
for _, snapshot := range snapshotList.Snapshots {
if strings.Contains(*snapshot.Name, DEV) {
groupByKey := strings.Split(*snapshot.Name, DEV)[0]
groupByImageResult[groupByKey] = append(groupByImageResult[groupByKey], snapshot)
}
}
var myImageInfoList []*irs.MyImageInfo
for _, associatedSnapshots := range groupByImageResult {
myImage, toMyImageErr := myImageHandler.ToISRMyImage(associatedSnapshots)
if toMyImageErr != nil {
createErr := errors.New(fmt.Sprintf("Failed to List MyImage. err = %s", toMyImageErr.Error()))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return nil, createErr
}
myImageInfoList = append(myImageInfoList, &myImage)
}
LoggingInfo(hiscallInfo, start)
return myImageInfoList, nil
}
func (myImageHandler *IbmMyImageHandler) GetMyImage(myImageIID irs.IID) (irs.MyImageInfo, error) {
hiscallInfo := GetCallLogScheme(myImageHandler.Region, call.MYIMAGE, myImageIID.NameId, "GetMyImage()")
start := call.Start()
if myImageIID.NameId == "" && myImageIID.SystemId == "" {
createErr := errors.New(fmt.Sprintf("Failed to Get MyImage. err = MyImage Name ID or System ID is required"))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return irs.MyImageInfo{}, createErr
}
myImageList, err := myImageHandler.ListMyImage()
if err != nil {
createErr := errors.New(fmt.Sprintf("Failed to Get MyImage. err = %s", err.Error()))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return irs.MyImageInfo{}, createErr
}
for _, myImage := range myImageList {
if myImage.IId.SystemId == myImageIID.SystemId {
return *myImage, nil
} else if myImage.IId.NameId == myImageIID.NameId {
return *myImage, nil
}
}
LoggingInfo(hiscallInfo, start)
return irs.MyImageInfo{}, errors.New(fmt.Sprintf("Failed to Get MyImage. err = MyImage not found"))
}
func (myImageHandler *IbmMyImageHandler) DeleteMyImage(myImageIID irs.IID) (bool, error) {
hiscallInfo := GetCallLogScheme(myImageHandler.Region, call.MYIMAGE, myImageIID.NameId, "DeleteMyImage()")
start := call.Start()
if myImageIID.NameId == "" && myImageIID.SystemId == "" {
delErr := errors.New(fmt.Sprintf("Failed to Delete MyImage. err = MyImage Name ID or System ID is required"))
cblogger.Error(delErr.Error())
LoggingError(hiscallInfo, delErr)
return false, delErr
}
if err := myImageHandler.cleanSnapshotByMyImage(myImageIID); err != nil {
delErr := errors.New(fmt.Sprintf("Failed to Delete MyImage. err = %s", err))
cblogger.Error(delErr.Error())
LoggingError(hiscallInfo, delErr)
return false, delErr
}
LoggingInfo(hiscallInfo, start)
return true, nil
}
func (myImageHandler *IbmMyImageHandler) ToISRMyImage(snapshotList []vpcv1.Snapshot) (irs.MyImageInfo, error) {
if len(snapshotList) == 0 {
return irs.MyImageInfo{}, errors.New("Cannot find MyImage")
}
var myImageNameId string
var myImageSystemId string
var sourceVmNameId string
var sourceVmSystemId string
myImageStatus := irs.MyImageAvailable
var myImageCreatedTime time.Time
for _, snapshot := range snapshotList {
if *snapshot.Bootable == true {
myImageNameId = strings.Split(*snapshot.Name, DEV)[0]
myImageSystemId = *snapshot.ID
getVolumeOptions := vpcv1.GetVolumeOptions{
ID: snapshot.SourceVolume.ID,
}
rawSourceVolume, _, getSourceVolumeErr := myImageHandler.VpcService.GetVolumeWithContext(myImageHandler.Ctx, &getVolumeOptions)
sourceVmNameId = "Deleted"
sourceVmSystemId = "Deleted"
if getSourceVolumeErr == nil && len((*rawSourceVolume).VolumeAttachments) != 0 {
sourceVmNameId = *(*rawSourceVolume).VolumeAttachments[0].Instance.Name
sourceVmSystemId = *(*rawSourceVolume).VolumeAttachments[0].Instance.ID
}
myImageCreatedTime = time.Time(*snapshot.CreatedAt).Local()
}
if myImageStatus == irs.MyImageAvailable && getSnapshotStatus(*snapshot.LifecycleState) == irs.MyImageUnavailable {
myImageStatus = irs.MyImageUnavailable
}
}
return irs.MyImageInfo{
IId: irs.IID{NameId: myImageNameId, SystemId: myImageSystemId},
SourceVM: irs.IID{NameId: sourceVmNameId, SystemId: sourceVmSystemId},
Status: myImageStatus,
CreatedTime: myImageCreatedTime,
}, nil
}
func (myImageHandler *IbmMyImageHandler) cleanSnapshotByMyImage(myImageIID irs.IID) error {
snapshotList, _, listSnapshotErr := myImageHandler.VpcService.ListSnapshotsWithContext(myImageHandler.Ctx, &vpcv1.ListSnapshotsOptions{})
if listSnapshotErr != nil {
return listSnapshotErr
}
myImageNameId := ""
if myImageIID.NameId != "" {
myImageNameId = myImageIID.NameId
} else {
for _, snapshot := range snapshotList.Snapshots {
if *snapshot.ID == myImageIID.SystemId {
myImageNameId = strings.Split(*snapshot.Name, DEV)[0]
}
}
}
if myImageNameId != "" {
for _, snapshot := range snapshotList.Snapshots {
parsed := strings.Split(*snapshot.Name, DEV)[0]
if parsed == myImageNameId {
deleteSnapshotOptions := vpcv1.DeleteSnapshotOptions{
ID: snapshot.ID,
}
myImageHandler.VpcService.DeleteSnapshotWithContext(myImageHandler.Ctx, &deleteSnapshotOptions)
}
}
}
return nil
}
func getSnapshotStatus(status string) irs.MyImageStatus {
switch status {
case "deleting", "failed", "pending", "suspended", "updating", "waiting":
return irs.MyImageUnavailable
case "stable":
return irs.MyImageAvailable
default:
return irs.MyImageUnavailable
}
}
func (myImageHandler *IbmMyImageHandler) CheckWindowsImage(myImageIID irs.IID) (bool, error) {
hiscallInfo := GetCallLogScheme(myImageHandler.Region, call.MYIMAGE, myImageIID.NameId, "CheckWindowsImage()")
start := call.Start()
var getMyImageErr error
myImage, getMyImageErr := myImageHandler.GetMyImage(myImageIID)
if getMyImageErr != nil {
checkWindowsImageErr := errors.New(fmt.Sprintf("Failed to CheckWindowsImage By MyImage. err = %s", getMyImageErr.Error()))
cblogger.Error(checkWindowsImageErr.Error())
LoggingError(hiscallInfo, checkWindowsImageErr)
return false, checkWindowsImageErr
}
if myImage.Status != irs.MyImageAvailable {
checkWindowsImageErr := errors.New(fmt.Sprintf("Failed to CheckWindowsImage By MyImage. err = Source Image status is not Available"))
cblogger.Error(checkWindowsImageErr.Error())
LoggingError(hiscallInfo, checkWindowsImageErr)
return false, checkWindowsImageErr
}
rawSnapshot, _, getRawSnapshotErr := myImageHandler.VpcService.GetSnapshotWithContext(myImageHandler.Ctx, &vpcv1.GetSnapshotOptions{ID: &myImage.IId.SystemId})
if getRawSnapshotErr != nil {
checkWindowsImageErr := errors.New(fmt.Sprintf("Failed to CheckWindowsImage By MyImage. err = %s", getRawSnapshotErr.Error()))
cblogger.Error(checkWindowsImageErr.Error())
LoggingError(hiscallInfo, checkWindowsImageErr)
return false, checkWindowsImageErr
}
isWindows := strings.Contains(strings.ToLower(*rawSnapshot.OperatingSystem.Name), "windows")
LoggingInfo(hiscallInfo, start)
return isWindows, nil
}