-
Notifications
You must be signed in to change notification settings - Fork 47
/
MyImageHandler.go
238 lines (202 loc) · 8.11 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
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 {
return irs.MyImageInfo{}, errors.New(fmt.Sprintf("MyImage Name ID cannot be longer than 55 characters"))
}
if strings.Contains(snapshotReqInfo.IId.NameId, DEV) {
return irs.MyImageInfo{}, errors.New(fmt.Sprintf("MyImage Name ID cannot include reserved string : %s", DEV))
}
attachedDiskList, listAttachedDiskErr := listRawAttachedDiskByVmIID(myImageHandler.VpcService, myImageHandler.Ctx, snapshotReqInfo.SourceVM)
if listAttachedDiskErr != nil {
return irs.MyImageInfo{}, errors.New(fmt.Sprintf("Failed to List Attached Disk. err = %s", listAttachedDiskErr.Error()))
}
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)
cblogger.Error(createSnapshotErr)
}
LoggingInfo(hiscallInfo, start)
// get myimage info
converted, convertErr := myImageHandler.GetMyImage(irs.IID{NameId: snapshotReqInfo.IId.NameId})
if convertErr != nil {
return irs.MyImageInfo{}, convertErr
}
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 {
return nil, listSnapshotErr
}
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 {
return nil, toMyImageErr
}
myImageInfoList = append(myImageInfoList, &myImage)
}
LoggingInfo(hiscallInfo, start)
return myImageInfoList, nil
}
func (myImageHandler *IbmMyImageHandler) GetMyImage(myImageIID irs.IID) (irs.MyImageInfo, error) {
if myImageIID.NameId == "" && myImageIID.SystemId == "" {
return irs.MyImageInfo{}, errors.New("Failed to Get MyImage. err = MyImage Name ID or System ID is required")
}
hiscallInfo := GetCallLogScheme(myImageHandler.Region, call.MYIMAGE, myImageIID.NameId, "GetMyImage()")
start := call.Start()
myImageList, err := myImageHandler.ListMyImage()
if err != nil {
return irs.MyImageInfo{}, errors.New(fmt.Sprintf("Failed to Get MyImage. err = %s", err))
}
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("MyImage not found")
}
func (myImageHandler *IbmMyImageHandler) DeleteMyImage(myImageIID irs.IID) (bool, error) {
if myImageIID.NameId == "" && myImageIID.SystemId == "" {
return false, errors.New("Failed to Delete MyImage. err = MyImage Name ID or System ID is required")
}
hiscallInfo := GetCallLogScheme(myImageHandler.Region, call.MYIMAGE, myImageIID.NameId, "DeleteMyImage()")
start := call.Start()
if err := myImageHandler.cleanSnapshotByMyImage(myImageIID); err != nil {
return false, errors.New(fmt.Sprintf("Failed to Delte MyImage. err = %s", err))
}
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) {
var getMyImageErr error
myImage, getMyImageErr := myImageHandler.GetMyImage(myImageIID)
if getMyImageErr != nil {
return false, getMyImageErr
}
if myImage.Status != irs.MyImageAvailable {
myImageUnavailableErr := errors.New("Failed to Check Image OS. err = Source Image status is not Available")
return false, myImageUnavailableErr
}
rawSnapshot, _, getRawSnapshotErr := myImageHandler.VpcService.GetSnapshotWithContext(myImageHandler.Ctx, &vpcv1.GetSnapshotOptions{ID: &myImage.IId.SystemId})
if getRawSnapshotErr != nil {
return false, getRawSnapshotErr
}
isWindows := strings.Contains(strings.ToLower(*rawSnapshot.OperatingSystem.Name), "windows")
return isWindows, nil
}