-
Notifications
You must be signed in to change notification settings - Fork 2
/
migration_model.go
440 lines (375 loc) · 13.8 KB
/
migration_model.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package model
import (
"encoding/json"
"fmt"
"os"
"strings"
// "errors"
cblog "github.com/cloud-barista/cb-log"
"github.com/davecgh/go-spew/spew"
)
const (
migrationModelDir string = "/.migration_model"
)
func init() {
// cblog is a global variable.
cblogger = cblog.GetLogger("Model Handler")
}
type JSON_Model struct {
MigrationModel MigrationModel `json:"migrationModel"`
}
type MigrationModel struct {
Description string `json:"description"`
Name string `json:"name"`
Version string `json:"version"`
ModelId string `json:"modelId"`
MigrationStep string `json:"migrationStep"`
TargetEnvironment TargetEnvironment `json:"targetEnvironment"`
}
type TargetEnvironment struct {
Provider string `json:"provider"`
Details EnvironmentDetails `json:"details"`
}
type EnvironmentDetails struct {
Region string `json:"region"`
Zone string `json:"zone"`
Resources []Resource `json:"resources"`
}
type Resource struct {
Type string `json:"type"`
NsSpecifications *Namespace `json:"nsSpecifications,omitempty"`
VNetSpecifications *VNet `json:"vNetSpecifications,omitempty"`
McisSpecifications *MCIS `json:"mcisSpecifications,omitempty"`
ImgSpecifications *VMImage `json:"imgSpecifications,omitempty"`
SgSpecifications *SG `json:"sgSpecifications,omitempty"`
KeySpecifications *KeyPair `json:"keySpecifications,omitempty"`
DbSpecifications *Database `json:"dbSpecifications,omitempty"`
DiskSpecifications *DataDisk `json:"diskSpecifications,omitempty"`
NlbSpecifications *NLB `json:"nlbSpecifications,omitempty"`
AppSpecifications *Application `json:"appSpecifications,omitempty"`
WebSvrSpecifications *WebServer `json:"webSvrSpecifications,omitempty"`
}
// Define structs for each specification
type Namespace struct {
Description string `json:"description"`
Name string `json:"name"`
}
type VNet struct {
CidrBlock string `json:"cidrBlock"`
CspVNetId string `json:"cspVNetId"`
Description string `json:"description"`
Name string `json:"name"`
SubnetInfoList []SubnetInfo `json:"subnetInfoList"`
}
type MCIS struct {
Description string `json:"description"`
InstallMonAgent string `json:"installMonAgent"`
Label string `json:"label"`
Name string `json:"name"`
PlacementAlgo string `json:"placementAlgo"`
SystemLabel string `json:"systemLabel"`
VM []VM `json:"vm"`
}
type VMImage struct {
CspImageId string `json:"cspImageId"`
Description string `json:"description"`
Name string `json:"name"`
}
type SG struct {
CspSecurityGroupId string `json:"cspSecurityGroupId"`
Description string `json:"description"`
FirewallRules []FirewallRule `json:"firewallRules"`
Name string `json:"name"`
VNetId string `json:"vNetId"`
}
type KeyPair struct {
ConnectionName string `json:"connectionName"`
CspSshKeyId string `json:"cspSshKeyId"`
Description string `json:"description"`
Fingerprint string `json:"fingerprint"`
Name string `json:"name"`
PrivateKey string `json:"privateKey"`
PublicKey string `json:"publicKey"`
Username string `json:"username"`
VerifiedUsername string `json:"verifiedUsername"`
}
type Database struct {
DatabaseType string `json:"databaseType"`
Version string `json:"version"`
Size string `json:"size"`
Tables string `json:"tables"`
StoredProcedures string `json:"storedProcedures"`
}
type DataDisk struct {
ConnectionName string `json:"connectionName"`
CspDataDiskId string `json:"cspDataDiskId"`
Description string `json:"description"`
DiskSize string `json:"diskSize"`
DiskType string `json:"diskType"`
Name string `json:"name"`
}
type NLB struct {
CspNLBId string `json:"cspNLBId"`
Description string `json:"description"`
HealthChecker HealthChecker `json:"healthChecker"`
Listener Listener `json:"listener"`
Scope string `json:"scope"`
TargetGroup TargetGroup `json:"targetGroup"`
Type string `json:"type"`
}
type Application struct {
Name string `json:"name"`
Version string `json:"version"`
Language string `json:"language"`
Dependencies []string `json:"dependencies"`
}
type WebServer struct {
Software string `json:"software"`
Version string `json:"version"`
HostedWebsites string `json:"hostedWebsites"`
Traffic string `json:"traffic"`
}
// Sub structs for nested objects in specifications
type SubnetInfo struct {
Description string `json:"description,omitempty"`
Ipv4CIDR string `json:"ipv4_CIDR,omitempty"`
KeyValueList []KeyValue `json:"keyValueList,omitempty"`
Name string `json:"name,omitempty"`
}
type VM struct {
DataDiskIds []string `json:"dataDiskIds,omitempty"`
Description string `json:"description,omitempty"`
IdByCsp string `json:"idByCsp,omitempty"`
ImageId string `json:"imageId,omitempty"`
Label string `json:"label,omitempty"`
Name string `json:"name,omitempty"`
RootDiskSize string `json:"rootDiskSize,omitempty"`
RootDiskType string `json:"rootDiskType,omitempty"`
SecurityGroupIds []string `json:"securityGroupIds,omitempty"`
SpecId string `json:"specId,omitempty"`
SshKeyId string `json:"sshKeyId,omitempty"`
SubGroupSize string `json:"subGroupSize,omitempty"`
VNetId string `json:"vNetId,omitempty"`
SubnetId string `json:"subnetId,omitempty"`
VmUserAccount string `json:"vmUserAccount,omitempty"`
VmUserPassword string `json:"vmUserPassword,omitempty"`
}
type FirewallRule struct {
Cidr string `json:"cidr,omitempty"`
Direction string `json:"direction,omitempty"`
FromPort string `json:"fromPort,omitempty"`
IpProtocol string `json:"ipprotocol,omitempty"`
ToPort string `json:"toPort,omitempty"`
}
type HealthChecker struct {
Interval string `json:"interval,omitempty"`
Threshold string `json:"threshold,omitempty"`
Timeout string `json:"timeout,omitempty"`
}
type LbListener struct {
Port string `json:"port,omitempty"`
Protocol string `json:"protocol,omitempty"`
}
type TargetGroup struct {
Port string `json:"port,omitempty"`
Protocol string `json:"protocol,omitempty"`
SubGroupId string `json:"subGroupId,omitempty"`
}
type KeyValue struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
type IID struct {
NameId string
SystemId string
}
func WriteModel(modelName string, rsInfo JSON_Model) (JSON_Model, error){
cblogger.Info("Model Handler called CreateModel()!")
var modelIId IID
modelIId.NameId = modelName
// Check if the Model Name already Exists
modelInfo, _ := GetModel(modelIId)
if modelInfo.MigrationModel.Name != "" {
newErr := fmt.Errorf("The Model Name already exists!!")
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
modelFilePath := os.Getenv("MODEL_ROOT") + migrationModelDir + "/"
jsonFileName := modelFilePath + modelName + ".json"
// Check if the Model file folder Exists, and Create it
if err := CheckFolderAndCreate(modelFilePath); err != nil {
newErr := fmt.Errorf("Failed to Create the Model Path : [%s] : [%v]" + modelFilePath, err)
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
rsInfo.MigrationModel.Name = modelName
file, err := json.MarshalIndent(rsInfo, "", " ")
if err != nil {
return JSON_Model{}, err
}
writeErr := os.WriteFile(jsonFileName, file, 0644)
if writeErr != nil {
newErr := fmt.Errorf("Failed to write the file : [%s] : [%v]", jsonFileName, writeErr)
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
// Return the created Model Info.
modelInfo, modelErr := GetModel(modelIId)
if modelErr != nil {
newErr := fmt.Errorf("Failed to Get the Model Info : [%v]", modelErr)
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
return modelInfo, nil
}
func GetModel(modelIID IID) (JSON_Model, error) {
cblogger.Info("Model Handler called GetModel()!")
if !strings.EqualFold(modelIID.SystemId,"") {
modelIID.NameId = modelIID.SystemId
}
modelFilePath := os.Getenv("MODEL_ROOT") + migrationModelDir + "/"
jsonFileName := modelFilePath + modelIID.NameId + ".json"
// Check if the Model file Folder Exists, and Create it
if err := CheckFolderAndCreate(modelFilePath); err != nil {
newErr := fmt.Errorf("Failed to Create the Model Path : [%s] : [%v]" + modelFilePath, err)
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
file, err := os.ReadFile(jsonFileName)
if err != nil {
return JSON_Model{}, err
}
var rsData JSON_Model
err = json.Unmarshal([]byte(file), &rsData)
if err != nil {
return JSON_Model{}, err
}
return rsData, nil
}
func ListModel() ([]*JSON_Model, error) {
cblogger.Info("Model Handler : called ListModel()!")
var modelIID IID
var resourceInfoList []*JSON_Model
medelFilePath := os.Getenv("MODEL_ROOT") + migrationModelDir + "/"
dirFiles, readRrr := os.ReadDir(medelFilePath)
if readRrr != nil {
return nil, readRrr
}
spew.Dump(dirFiles)
for _, file := range dirFiles {
fileName := strings.TrimSuffix(file.Name(), ".json") // 접미사 제거
modelIID.NameId = fileName
cblogger.Infof("# Model Name : " + modelIID.NameId)
rsInfo, getErr := GetModel(modelIID)
if getErr != nil {
newErr := fmt.Errorf("Failed to Get the Model Info : [%v]", getErr)
cblogger.Error(newErr.Error())
return nil, newErr
}
resourceInfoList = append(resourceInfoList, &rsInfo)
}
return resourceInfoList, nil
}
func UpdateModel(modelIId IID, rsInfo JSON_Model) (JSON_Model, error){
cblogger.Info("Model Handler called UpdateModel()!")
var modelName = modelIId.NameId
// Find the created Model Info.
modelInfo, modelErr := GetModel(modelIId)
if modelErr != nil {
newErr := fmt.Errorf("Failed to Find the Model Info!! : [%v]", modelErr)
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
if modelInfo.MigrationModel.Name == "" {
newErr := fmt.Errorf("Failed to Find the Model Info!!")
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
modelFilePath := os.Getenv("MODEL_ROOT") + migrationModelDir + "/"
jsonFileName := modelFilePath + modelName + ".json"
// Check if the Model file Folder Exists, and Create it
if err := CheckFolderAndCreate(modelFilePath); err != nil {
newErr := fmt.Errorf("Failed to Create the Model Path : [%s] : [%v]" + modelFilePath, err)
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
rsInfo.MigrationModel.Name = modelName
file, err := json.MarshalIndent(rsInfo, "", " ")
if err != nil {
return JSON_Model{}, err
}
writeErr := os.WriteFile(jsonFileName, file, 0644)
if writeErr != nil {
newErr := fmt.Errorf("Failed to write the file : [%s] : [%v]", jsonFileName, writeErr)
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
// Return the updated Model Info.
modelInfo, getErr := GetModel(modelIId)
if getErr != nil {
newErr := fmt.Errorf("Failed to Get the Model Info : [%v]", getErr)
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
return modelInfo, nil
}
func DeleteModel(modelIID IID) (bool, error) {
cblogger.Info("Model Handler called DelModel()!")
if !strings.EqualFold(modelIID.SystemId,"") {
modelIID.NameId = modelIID.SystemId
}
//To check whether the Model exists.
_, modelErr := GetModel(modelIID)
if modelErr != nil {
newErr := fmt.Errorf("Failed to Get the Model Info : [%v]", modelErr)
cblogger.Error(newErr.Error())
return false, newErr
}
modelFilePath := os.Getenv("MODEL_ROOT") + migrationModelDir + "/"
jsonFileName := modelFilePath + modelIID.NameId + ".json"
// Check if the Model file Folder Exists, and Create it
if err := CheckFolderAndCreate(modelFilePath); err != nil {
newErr := fmt.Errorf("Failed to Create the Model Path : [%s] : [%v]" + modelFilePath, err)
cblogger.Error(newErr.Error())
return false, newErr
}
// To Remove the Model file on the Local machine.
delErr := os.Remove(jsonFileName)
if delErr != nil {
newErr := fmt.Errorf("Failed to Delete the Model : [%s] : [%v]", jsonFileName, delErr)
cblogger.Error(newErr.Error())
return false, newErr
}
return true, nil
}
// Function to update SubnetInfoList in the first vNet resource
func UpdateSubnetInfoList(modelIId IID, newSubnetInfoList []SubnetInfo) (JSON_Model, error){
modelInfo, getErr := GetModel(modelIId)
if getErr != nil {
newErr := fmt.Errorf("Failed to Get the Model Info : [%v]", getErr)
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
for i, resource := range modelInfo.MigrationModel.TargetEnvironment.Details.Resources {
cblogger.Infof("# resource.Type : [%s]", resource.Type)
if resource.Type == "vNet" {
modelInfo.MigrationModel.TargetEnvironment.Details.Resources[i].VNetSpecifications.SubnetInfoList = newSubnetInfoList
break
}
}
_, err := UpdateModel(modelIId, modelInfo)
if err != nil {
newErr := fmt.Errorf("Failed to Update the Model Info : [%v]", err)
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
// spew.Dump(model)
// Return the updated Model Info.
model, getErr := GetModel(modelIId)
if getErr != nil {
newErr := fmt.Errorf("Failed to Get the Model Info : [%v]", getErr)
cblogger.Error(newErr.Error())
return JSON_Model{}, newErr
}
return model, nil
}