-
Notifications
You must be signed in to change notification settings - Fork 48
/
vm.go
277 lines (213 loc) · 8.57 KB
/
vm.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
// gRPC Runtime 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
//
// by CB-Spider Team, 2020.09.
package service
import (
"context"
gc "github.com/cloud-barista/cb-spider/api-runtime/grpc-runtime/common"
"github.com/cloud-barista/cb-spider/api-runtime/grpc-runtime/logger"
pb "github.com/cloud-barista/cb-spider/api-runtime/grpc-runtime/stub/cbspider"
cmrt "github.com/cloud-barista/cb-spider/api-runtime/common-runtime"
cres "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)
// ===== [ Constants and Variables ] =====
// ===== [ Types ] =====
// ===== [ Implementations ] =====
// StartVM - VM 시작
func (s *CCMService) StartVM(ctx context.Context, req *pb.VMCreateRequest) (*pb.VMInfoResponse, error) {
logger := logger.NewLogger()
logger.Debug("calling CCMService.StartVM()")
// Rest RegInfo => Driver ReqInfo
// (1) create SecurityGroup IID List
sgIIDList := []cres.IID{}
for _, sgName := range req.Item.SecurityGroupNames {
// SG NameID format => {VPC NameID} + cm.SG_DELIMITER + {SG NameID}
// transform: SG NameID => {VPC NameID}-{SG NameID}
// sgIID := cres.IID{NameId: req.Item.VpcName + cm.SG_DELIMITER + sgName, SystemId: ""}
sgIID := cres.IID{sgName, ""}
sgIIDList = append(sgIIDList, sgIID)
}
// (2) create VMReqInfo with SecurityGroup IID List
reqInfo := cres.VMReqInfo{
IId: cres.IID{NameId: req.Item.Name, SystemId: ""},
ImageIID: cres.IID{NameId: req.Item.ImageName, SystemId: ""},
VpcIID: cres.IID{NameId: req.Item.VpcName, SystemId: ""},
SubnetIID: cres.IID{NameId: req.Item.SubnetName, SystemId: ""},
SecurityGroupIIDs: sgIIDList,
VMSpecName: req.Item.VmSpecName,
KeyPairIID: cres.IID{NameId: req.Item.KeyPairName, SystemId: ""},
RootDiskType: req.Item.RootDiskType,
RootDiskSize: req.Item.RootDiskSize,
VMUserId: req.Item.VmUserId,
VMUserPasswd: req.Item.VmUserPasswd,
}
// Call common-runtime API
result, err := cmrt.StartVM(req.ConnectionName, rsVM, reqInfo)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.StartVM()")
}
// CCM 객체에서 GRPC 메시지로 복사
var grpcObj pb.VMInfo
err = gc.CopySrcToDest(result, &grpcObj)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.StartVM()")
}
resp := &pb.VMInfoResponse{Item: &grpcObj}
return resp, nil
}
// ControlVM - VM 제어
func (s *CCMService) ControlVM(ctx context.Context, req *pb.VMActionRequest) (*pb.StatusResponse, error) {
logger := logger.NewLogger()
logger.Debug("calling CCMService.ControlVM()")
// Call common-runtime API
result, err := cmrt.ControlVM(req.ConnectionName, rsVM, req.Name, req.Action)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.ControlVM()")
}
resp := &pb.StatusResponse{Status: string(result)}
return resp, nil
}
// ListVM - VM 목록
func (s *CCMService) ListVM(ctx context.Context, req *pb.VMAllQryRequest) (*pb.ListVMInfoResponse, error) {
logger := logger.NewLogger()
logger.Debug("calling CCMService.ListVM()")
// Call common-runtime API
result, err := cmrt.ListVM(req.ConnectionName, rsVM)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.ListVM()")
}
// CCM 객체에서 GRPC 메시지로 복사
var grpcObj []*pb.VMInfo
err = gc.CopySrcToDest(&result, &grpcObj)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.ListVM()")
}
resp := &pb.ListVMInfoResponse{Items: grpcObj}
return resp, nil
}
// GetVM - VM 조회
func (s *CCMService) GetVM(ctx context.Context, req *pb.VMQryRequest) (*pb.VMInfoResponse, error) {
logger := logger.NewLogger()
logger.Debug("calling CCMService.GetVM()")
// Call common-runtime API
result, err := cmrt.GetVM(req.ConnectionName, rsVM, req.Name)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.GetVM()")
}
// CCM 객체에서 GRPC 메시지로 복사
var grpcObj pb.VMInfo
err = gc.CopySrcToDest(result, &grpcObj)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.GetVM()")
}
resp := &pb.VMInfoResponse{Item: &grpcObj}
return resp, nil
}
// ListVMStatus - VM 상태 목록
func (s *CCMService) ListVMStatus(ctx context.Context, req *pb.VMAllQryRequest) (*pb.ListVMStatusInfoResponse, error) {
logger := logger.NewLogger()
logger.Debug("calling CCMService.ListVMStatus()")
// Call common-runtime API
result, err := cmrt.ListVMStatus(req.ConnectionName, rsVM)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.ListVMStatus()")
}
// CCM 객체에서 GRPC 메시지로 복사
var grpcObj []*pb.VMStatusInfo
err = gc.CopySrcToDest(&result, &grpcObj)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.ListVMStatus()")
}
resp := &pb.ListVMStatusInfoResponse{Items: grpcObj}
return resp, nil
}
// GetVMStatus - VM 상태 조회
func (s *CCMService) GetVMStatus(ctx context.Context, req *pb.VMQryRequest) (*pb.StatusResponse, error) {
logger := logger.NewLogger()
logger.Debug("calling CCMService.GetVMStatus()")
// Call common-runtime API
result, err := cmrt.GetVMStatus(req.ConnectionName, rsVM, req.Name)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.GetVMStatus()")
}
resp := &pb.StatusResponse{Status: string(result)}
return resp, nil
}
// TerminateVM - VM 삭제
func (s *CCMService) TerminateVM(ctx context.Context, req *pb.VMQryRequest) (*pb.StatusResponse, error) {
logger := logger.NewLogger()
logger.Debug("calling CCMService.TerminateVM()")
// Call common-runtime API
_, result, err := cmrt.DeleteResource(req.ConnectionName, rsVM, req.Name, req.Force)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.TerminateVM()")
}
resp := &pb.StatusResponse{Status: string(result)}
return resp, nil
}
// ListAllVM - 관리 VM 목록
func (s *CCMService) ListAllVM(ctx context.Context, req *pb.VMAllQryRequest) (*pb.AllResourceInfoResponse, error) {
logger := logger.NewLogger()
logger.Debug("calling CCMService.ListAllVM()")
// Call common-runtime API
allResourceList, err := cmrt.ListAllResource(req.ConnectionName, rsVM)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.ListAllVM()")
}
// CCM 객체에서 GRPC 메시지로 복사
var grpcObj pb.AllResourceInfoResponse
err = gc.CopySrcToDest(&allResourceList, &grpcObj)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.ListAllVM()")
}
return &grpcObj, nil
}
// TerminateCSPVM - CSP VM 삭제
func (s *CCMService) TerminateCSPVM(ctx context.Context, req *pb.CSPVMQryRequest) (*pb.StatusResponse, error) {
logger := logger.NewLogger()
logger.Debug("calling CCMService.TerminateCSPVM()")
// Call common-runtime API
_, result, err := cmrt.DeleteCSPResource(req.ConnectionName, rsVM, req.Id)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.TerminateCSPVM()")
}
resp := &pb.StatusResponse{Status: string(result)}
return resp, nil
}
// RegisterVM - VM 등록
func (s *CCMService) RegisterVM(ctx context.Context, req *pb.VMRegisterRequest) (*pb.VMInfoResponse, error) {
logger := logger.NewLogger()
logger.Debug("calling CCMService.RegisterVM()")
userIId := cres.IID{req.Item.Name, req.Item.CspId}
// Call common-runtime API
result, err := cmrt.RegisterVM(req.ConnectionName, userIId)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.RegisterVM()")
}
// CCM 객체에서 GRPC 메시지로 복사
var grpcObj pb.VMInfo
err = gc.CopySrcToDest(result, &grpcObj)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.RegisterVM()")
}
resp := &pb.VMInfoResponse{Item: &grpcObj}
return resp, nil
}
// UnregisterVM - VM 제거
func (s *CCMService) UnregisterVM(ctx context.Context, req *pb.VMUnregiserQryRequest) (*pb.BooleanResponse, error) {
logger := logger.NewLogger()
logger.Debug("calling CCMService.UnregisterVM()")
// Call common-runtime API
result, err := cmrt.UnregisterResource(req.ConnectionName, rsVM, req.Name)
if err != nil {
return nil, gc.ConvGrpcStatusErr(err, "", "CCMService.UnregisterVM()")
}
resp := &pb.BooleanResponse{Result: result}
return resp, nil
}
// ===== [ Private Functions ] =====
// ===== [ Public Functions ] =====