-
Notifications
You must be signed in to change notification settings - Fork 47
/
ImageHandler.go
269 lines (233 loc) · 8.95 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
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
package resources
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-03-01/compute"
"github.com/Azure/go-autorest/autorest/to"
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"
)
const (
Image = "IMAGE"
)
type AzureImageHandler struct {
Region idrv.RegionInfo
Ctx context.Context
Client *compute.ImagesClient
VMImageClient *compute.VirtualMachineImagesClient
}
func (imageHandler *AzureImageHandler) setterImage(image compute.Image) *irs.ImageInfo {
imageInfo := &irs.ImageInfo{
IId: irs.IID{
NameId: *image.Name,
SystemId: *image.Name,
},
GuestOS: fmt.Sprint(image.ImageProperties.StorageProfile.OsDisk.OsType),
Status: *image.ProvisioningState,
KeyValueList: []irs.KeyValue{{Key: "ResourceGroup", Value: imageHandler.Region.ResourceGroup}},
}
return imageInfo
}
func (imageHandler *AzureImageHandler) setterVMImage(image compute.VirtualMachineImage) *irs.ImageInfo {
imageIdArr := strings.Split(*image.ID, "/")
imageName := fmt.Sprintf("%s:%s:%s:%s", imageIdArr[8], imageIdArr[12], imageIdArr[14], imageIdArr[16])
imageInfo := &irs.ImageInfo{
IId: irs.IID{
NameId: imageName,
SystemId: imageName,
},
GuestOS: fmt.Sprint(image.OsDiskImage.OperatingSystem),
KeyValueList: []irs.KeyValue{{Key: "ResourceGroup", Value: imageHandler.Region.ResourceGroup}},
}
return imageInfo
}
func (imageHandler *AzureImageHandler) setterVMImageforList(image compute.VirtualMachineImageResource) *irs.ImageInfo {
imageIdArr := strings.Split(*image.ID, "/")
imageName := fmt.Sprintf("%s:%s:%s:%s", imageIdArr[8], imageIdArr[12], imageIdArr[14], imageIdArr[16])
imageInfo := &irs.ImageInfo{
IId: irs.IID{
NameId: imageName,
SystemId: imageName,
},
//GuestOS: fmt.Sprint(image.OsDiskImage.OperatingSystem),
KeyValueList: []irs.KeyValue{{Key: "ResourceGroup", Value: imageHandler.Region.ResourceGroup}},
}
return imageInfo
}
func (imageHandler *AzureImageHandler) CreateImage(imageReqInfo irs.ImageReqInfo) (irs.ImageInfo, error) {
// @TODO: PublicIP 생성 요청 파라미터 정의 필요
type ImageReqInfo struct {
OSType string
DiskId string
}
reqInfo := ImageReqInfo{
//BlobUrl: "https://md-ds50xp550wh2.blob.core.windows.net/kt0lhznvgx2h/abcd?sv=2017-04-17&sr=b&si=b9674241-fb8e-4cb2-89c7-614d336dc3a7&sig=uvbqvAZQITSpxas%2BWosG%2FGOf6e%2BIBmWNxlUmvARnxiM%3D",
OSType: "Linux",
DiskId: "/subscriptions/cb592624-b77b-4a8f-bb13-0e5a48cae40f/resourceGroups/INNO-PLATFORM1-RSRC-GRUP/providers/Microsoft.Compute/disks/inno-test-vm_OsDisk_1_61bf675b990f4aa381d7ee3d766974aa",
// edited by powerkim for test, 2019.08.13
//DiskId: "/subscriptions/f1548292-2be3-4acd-84a4-6df079160846/resourceGroups/CB-RESOURCE-GROUP/providers/Microsoft.Compute/disks/vm_name_OsDisk_1_2d63d9cd754c4094b1b1fb6a98c36b71",
}
// Check Image Exists
image, err := imageHandler.Client.Get(imageHandler.Ctx, imageHandler.Region.ResourceGroup, imageReqInfo.IId.NameId, "")
if image.ID != nil {
errMsg := fmt.Sprintf("Image with name %s already exist", imageReqInfo.IId.NameId)
createErr := errors.New(errMsg)
return irs.ImageInfo{}, createErr
}
createOpts := compute.Image{
ImageProperties: &compute.ImageProperties{
StorageProfile: &compute.ImageStorageProfile{
OsDisk: &compute.ImageOSDisk{
//BlobURI: to.StringPtr(reqInfo.BlobUrl),
ManagedDisk: &compute.SubResource{
ID: to.StringPtr(reqInfo.DiskId),
},
OsType: compute.OperatingSystemTypes(reqInfo.OSType),
},
},
},
Location: &imageHandler.Region.Region,
}
future, err := imageHandler.Client.CreateOrUpdate(imageHandler.Ctx, imageHandler.Region.ResourceGroup, imageReqInfo.IId.NameId, createOpts)
if err != nil {
return irs.ImageInfo{}, err
}
err = future.WaitForCompletionRef(imageHandler.Ctx, imageHandler.Client.Client)
if err != nil {
return irs.ImageInfo{}, err
}
// 생성된 Image 정보 리턴
imageInfo, err := imageHandler.GetImage(imageReqInfo.IId)
if err != nil {
return irs.ImageInfo{}, err
}
return imageInfo, nil
}
func (imageHandler *AzureImageHandler) ListImage() ([]*irs.ImageInfo, error) {
// log HisCall
hiscallInfo := GetCallLogScheme(imageHandler.Region, call.VMIMAGE, Image, "ListImage()")
var imageList []*irs.ImageInfo
publishers, err := imageHandler.VMImageClient.ListPublishers(context.TODO(), imageHandler.Region.Region)
if err != nil {
cblogger.Error(err.Error())
LoggingError(hiscallInfo, err)
return nil, err
}
start := call.Start()
var publisherWg sync.WaitGroup
publisherWg.Add(len(*publishers.Value))
for _, p := range *publishers.Value {
go func(publisher compute.VirtualMachineImageResource) {
defer publisherWg.Done()
offers, err := imageHandler.VMImageClient.ListOffers(context.TODO(), imageHandler.Region.Region, *publisher.Name)
if err != nil {
return
}
for _, offer := range *offers.Value {
skus, err := imageHandler.VMImageClient.ListSkus(context.TODO(), imageHandler.Region.Region, *publisher.Name, *offer.Name)
if err != nil {
continue
}
for _, sku := range *skus.Value {
imageVersionList, err := imageHandler.VMImageClient.List(context.TODO(), imageHandler.Region.Region, *publisher.Name, *offer.Name, *sku.Name, "", nil, "")
if err != nil {
continue
}
if len(*imageVersionList.Value) == 0 {
continue
}
for _, version := range *imageVersionList.Value {
imageIdArr := strings.Split(*version.ID, "/")
imageVersion := imageIdArr[len(imageIdArr)-1]
vmImage, err := imageHandler.VMImageClient.Get(context.TODO(), imageHandler.Region.Region, *publisher.Name, *offer.Name, *sku.Name, imageVersion)
if err != nil {
continue
}
vmImageInfo := imageHandler.setterVMImage(vmImage)
imageList = append(imageList, vmImageInfo)
}
}
}
return
}(p)
}
publisherWg.Wait()
LoggingInfo(hiscallInfo, start)
return imageList, nil
}
func (imageHandler *AzureImageHandler) GetImage(imageIID irs.IID) (irs.ImageInfo, error) {
// log HisCall
hiscallInfo := GetCallLogScheme(imageHandler.Region, call.VMIMAGE, imageIID.NameId, "GetImage()")
imageArr := strings.Split(imageIID.NameId, ":")
// 이미지 URN 형식 검사
if len(imageArr) != 4 {
formatErr := errors.New("invalid format for image ID, imageId=" + imageIID.NameId)
cblogger.Error(formatErr.Error())
LoggingError(hiscallInfo, formatErr)
return irs.ImageInfo{}, formatErr
}
// 해당 이미지 publisher, offer, skus 기준 version 목록 조회 (latest 기준 조회 기능 미활용)
/*
imageVersion := imageArr[3]
if strings.EqualFold(imageVersion, "latest") {
vmImageList, err := imageHandler.VMImageClient.List(imageHandler.Ctx, imageHandler.Region.Region, imageArr[0], imageArr[1], imageArr[2], "", to.Int32Ptr(1), "name desc")
if err != nil {
LoggingError(hiscallInfo, err)
return irs.ImageInfo{}, err
}
if &vmImageList == nil {
getErr := errors.New(fmt.Sprintf("could not found image with imageId %s", imageIID.NameId))
LoggingError(hiscallInfo, getErr)
return irs.ImageInfo{}, getErr
}
if vmImageList.Value == nil {
getErr := errors.New(fmt.Sprintf("could not found image with imageId %s", imageIID.NameId))
LoggingError(hiscallInfo, getErr)
return irs.ImageInfo{}, getErr
}
if len(*vmImageList.Value) == 0 {
getErr := errors.New(fmt.Sprintf("could not found image with imageId %s", imageIID.NameId))
LoggingError(hiscallInfo, getErr)
return irs.ImageInfo{}, getErr
} else {
latestVmImage := (*vmImageList.Value)[0]
imageIdArr := strings.Split(*latestVmImage.ID, "/")
imageVersion = imageIdArr[len(imageIdArr)-1]
}
}
*/
// 1개의 버전 정보를 기준으로 이미지 정보 조회
start := call.Start()
vmImage, err := imageHandler.VMImageClient.Get(imageHandler.Ctx, imageHandler.Region.Region, imageArr[0], imageArr[1], imageArr[2], imageArr[3])
if err != nil {
cblogger.Error(err.Error())
LoggingError(hiscallInfo, err)
return irs.ImageInfo{}, err
}
LoggingInfo(hiscallInfo, start)
imageInfo := imageHandler.setterVMImage(vmImage)
return *imageInfo, nil
}
func (imageHandler *AzureImageHandler) DeleteImage(imageIID irs.IID) (bool, error) {
// log HisCall
hiscallInfo := GetCallLogScheme(imageHandler.Region, call.VMIMAGE, imageIID.NameId, "DeleteImage()")
start := call.Start()
future, err := imageHandler.Client.Delete(imageHandler.Ctx, imageHandler.Region.ResourceGroup, imageIID.NameId)
if err != nil {
cblogger.Error(err.Error())
LoggingError(hiscallInfo, err)
return false, err
}
LoggingInfo(hiscallInfo, start)
err = future.WaitForCompletionRef(imageHandler.Ctx, imageHandler.Client.Client)
if err != nil {
cblogger.Error(err.Error())
LoggingError(hiscallInfo, err)
return false, err
}
return true, nil
}