-
Notifications
You must be signed in to change notification settings - Fork 47
/
ImageHandler.go
289 lines (249 loc) · 9.08 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Cloud Driver Interface 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 Resouces interfaces of Cloud Driver.
//
// by CB-Spider Team, 2019.06.
package resources
import (
"errors"
"reflect"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
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"
//irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/new-resources"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
)
type AwsImageHandler struct {
Region idrv.RegionInfo
Client *ec2.EC2
}
//@TODO : 작업해야 함.
func (imageHandler *AwsImageHandler) CreateImage(imageReqInfo irs.ImageReqInfo) (irs.ImageInfo, error) {
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.AWS,
RegionZone: imageHandler.Region.Zone,
ResourceType: call.VMIMAGE,
ResourceName: imageReqInfo.IId.NameId,
CloudOSAPI: "-",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
imageReqInfo.IId.SystemId = imageReqInfo.IId.NameId
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
callogger.Info(call.String(callLogInfo))
return irs.ImageInfo{imageReqInfo.IId, "", "", nil}, nil
}
//@TODO : 목록이 너무 많기 때문에 amazon 계정으로 공유된 퍼블릭 이미지중 AMI만 조회 함.
//20210607 - Tumblebug에서 필터할 수 있도록 state는 모든 이미지를 대상으로 하며, 이미지가 너무 많기 때문에 AWS 소유의 이미지만 제공 함.
func (imageHandler *AwsImageHandler) ListImage() ([]*irs.ImageInfo, error) {
cblogger.Debug("Start")
var imageInfoList []*irs.ImageInfo
input := &ec2.DescribeImagesInput{
//ImageIds: []*string{aws.String("ami-0d097db2fb6e0f05e")},
Owners: []*string{
aws.String("amazon"), //사용자 계정 번호를 넣으면 사용자의 이미지를 대상으로 조회 함.
},
Filters: []*ec2.Filter{
{
Name: aws.String("image-type"),
Values: aws.StringSlice([]string{"machine"}),
},
{
Name: aws.String("is-public"),
Values: aws.StringSlice([]string{"true"}),
},
/*
{
Name: aws.String("state"),
Values: aws.StringSlice([]string{"available"}),
},
*/
},
}
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.AWS,
RegionZone: imageHandler.Region.Zone,
ResourceType: call.VMIMAGE,
ResourceName: "ListImage()",
CloudOSAPI: "DescribeImages",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
result, err := imageHandler.Client.DescribeImages(input)
//spew.Dump(result) //출력 정보가 너무 많아서 생략
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Error(call.String(callLogInfo))
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
cblogger.Error(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
cblogger.Error(err.Error())
}
return nil, err
}
callogger.Info(call.String(callLogInfo))
cnt := 0
for _, cur := range result.Images {
//spew.Dump(cur)
if reflect.ValueOf(cur.State).IsNil() {
cblogger.Errorf("===>[%s] AMI는 State 정보가 없어서 Skip함.", *cur.ImageId)
continue
}
if reflect.ValueOf(cur.Name).IsNil() {
cblogger.Infof("===>[%s] AMI는 Name 정보가 없어서 Skip함.", *cur.ImageId)
continue
}
cblogger.Debugf("[%s] - [%s] - [%s] AMI 정보 처리", *cur.ImageId, *cur.State, *cur.Name)
//cblogger.Infof("[%s] - [%s] - [%s] - [%s] AMI 정보 처리", *cur.ImageId, *cur.State, *cur.Name, *cur.UsageOperation)
imageInfo := ExtractImageDescribeInfo(cur)
imageInfoList = append(imageInfoList, &imageInfo)
cnt++
/*
if cnt > 20 {
break
}
*/
}
cblogger.Info("%d개의 이미지가 조회됨.", cnt)
//spew.Dump(imageInfoList)
return imageInfoList, nil
}
//Image 정보를 추출함
//@TODO : GuestOS 쳌크할 것
func ExtractImageDescribeInfo(image *ec2.Image) irs.ImageInfo {
//spew.Dump(image)
imageInfo := irs.ImageInfo{
//IId: irs.IID{*image.Name, *image.ImageId},
IId: irs.IID{*image.ImageId, *image.ImageId},
//Id: *image.ImageId,
//Name: *image.Name,
Status: *image.State,
}
keyValueList := []irs.KeyValue{
//{Key: "Name", Value: *image.Name}, //20200723-Name이 없는 이미지 존재 - 예)ami-0008a301
{Key: "CreationDate", Value: *image.CreationDate},
{Key: "Architecture", Value: *image.Architecture}, //x86_64
{Key: "OwnerId", Value: *image.OwnerId},
{Key: "ImageType", Value: *image.ImageType},
{Key: "ImageLocation", Value: *image.ImageLocation},
{Key: "VirtualizationType", Value: *image.VirtualizationType},
{Key: "Public", Value: strconv.FormatBool(*image.Public)},
}
//주로 윈도우즈는 Platform 정보가 존재하며 리눅스 계열은 PlatformDetails만 존재하는 듯. - "Linux/UNIX"
//윈도우즈 계열은 PlatformDetails에는 "Windows with SQL Server Standard"처럼 SQL정보도 포함되어있음.
if !reflect.ValueOf(image.Platform).IsNil() {
imageInfo.GuestOS = *image.Platform //Linux/UNIX
keyValueList = append(keyValueList, irs.KeyValue{Key: "Platform", Value: *image.Platform})
} else {
// Platform 정보가 없는 경우 PlatformDetails 정보가 존재하면 PlatformDetails 값을 이용함.
if !reflect.ValueOf(image.PlatformDetails).IsNil() {
imageInfo.GuestOS = *image.PlatformDetails //Linux/UNIX
}
}
// 일부 이미지들은 아래 정보가 없어서 예외 처리 함.
if !reflect.ValueOf(image.PlatformDetails).IsNil() {
keyValueList = append(keyValueList, irs.KeyValue{Key: "PlatformDetails", Value: *image.PlatformDetails})
}
// 일부 이미지들은 아래 정보가 없어서 예외 처리 함.
if !reflect.ValueOf(image.Name).IsNil() {
keyValueList = append(keyValueList, irs.KeyValue{Key: "Name", Value: *image.Name})
}
if !reflect.ValueOf(image.Description).IsNil() {
keyValueList = append(keyValueList, irs.KeyValue{Key: "Description", Value: *image.Description})
}
if !reflect.ValueOf(image.ImageOwnerAlias).IsNil() {
keyValueList = append(keyValueList, irs.KeyValue{Key: "ImageOwnerAlias", Value: *image.ImageOwnerAlias})
}
if !reflect.ValueOf(image.RootDeviceName).IsNil() {
keyValueList = append(keyValueList, irs.KeyValue{Key: "RootDeviceName", Value: *image.RootDeviceName})
keyValueList = append(keyValueList, irs.KeyValue{Key: "RootDeviceType", Value: *image.RootDeviceType})
}
if !reflect.ValueOf(image.EnaSupport).IsNil() {
keyValueList = append(keyValueList, irs.KeyValue{Key: "EnaSupport", Value: strconv.FormatBool(*image.EnaSupport)})
}
imageInfo.KeyValueList = keyValueList
return imageInfo
}
//func (imageHandler *AwsImageHandler) GetImage(imageID string) (irs.ImageInfo, error) {
func (imageHandler *AwsImageHandler) GetImage(imageIID irs.IID) (irs.ImageInfo, error) {
cblogger.Infof("imageID : [%s]", imageIID.SystemId)
input := &ec2.DescribeImagesInput{
ImageIds: []*string{
aws.String(imageIID.SystemId),
},
}
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.AWS,
RegionZone: imageHandler.Region.Zone,
ResourceType: call.VMIMAGE,
ResourceName: imageIID.SystemId,
CloudOSAPI: "DescribeImages",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
result, err := imageHandler.Client.DescribeImages(input)
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
//spew.Dump(result)
cblogger.Info(result)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
cblogger.Error(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
cblogger.Error(err.Error())
}
return irs.ImageInfo{}, err
}
callogger.Info(call.String(callLogInfo))
if len(result.Images) > 0 {
imageInfo := ExtractImageDescribeInfo(result.Images[0])
return imageInfo, nil
} else {
return irs.ImageInfo{}, errors.New("조회된 Image 정보가 없습니다.")
}
}
//@TODO : 삭제 API 찾아야 함.
func (imageHandler *AwsImageHandler) DeleteImage(imageIID irs.IID) (bool, error) {
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.AWS,
RegionZone: imageHandler.Region.Zone,
ResourceType: call.VMIMAGE,
ResourceName: imageIID.SystemId,
CloudOSAPI: "-",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
callogger.Info(call.String(callLogInfo))
return false, nil
}