-
Notifications
You must be signed in to change notification settings - Fork 7
/
entity.go
166 lines (155 loc) · 4.29 KB
/
entity.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
package main
import (
"encoding/json"
"github.com/briandowns/spinner"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"os"
"strings"
"time"
)
var skipLogs = []string{
"already exists in the parent folder",
"Duplicate identifier, please try again with a new identifier",
"already exists in the account",
"already exists in this scope",
"] already exists appId=",
"that already exists. Please enter a unique ID",
"] already exists",
}
func CreateEntities(body RequestBody) {
reqId, err := QueueCreateEntity(body)
if err != nil {
return
}
PollForCompletion(reqId)
}
func QueueCreateEntity(body RequestBody) (reqId string, err error) {
url := GetUrl(migrationReq.Environment, MigratorService, "save/async", migrationReq.Account)
resp, err := Post(url, migrationReq.Auth, body)
if err != nil {
log.Fatal("Failed to create the entities", err)
}
resource, err := getResource(resp.Resource)
if err != nil || len(resource.RequestId) == 0 {
log.Fatal("Failed to create the entities", err)
return
}
reqId = resource.RequestId
log.Infof("The request id is - %s", reqId)
return
}
func PollForCompletion(reqId string) {
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = " Processing"
s.Start()
for {
time.Sleep(time.Second * 10)
url := GetUrlWithQueryParams(migrationReq.Environment, MigratorService, "save/async-result", map[string]string{
AccountIdentifier: migrationReq.Account,
"requestId": reqId,
})
resp, err := Get(url, migrationReq.Auth)
if err != nil {
s.Stop()
log.Fatal("Failed to create the entities", err)
}
resource, err := getResource(resp.Resource)
if err != nil {
s.Stop()
log.Fatal("Failed to create the entities", err)
}
if resource.Status == "ERROR" {
s.Stop()
log.Fatal("Failed to create the entities")
}
if resource.Status == "DONE" {
s.Stop()
saveSummary, err := getSaveSummary(resource)
if err != nil {
log.Fatal("Failed to create the entities", err)
}
renderSaveSummary(saveSummary)
break
}
}
}
func getSaveSummary(resource Resource) (summary SaveSummary, err error) {
byteData, err := json.Marshal(resource.ResponsePayload)
if err != nil {
return
}
err = json.Unmarshal(byteData, &summary)
if err != nil {
return
}
return
}
func renderSaveSummary(saveSummary SaveSummary) {
if len(saveSummary.Stats) > 0 {
var rows []table.Row
for k, v := range saveSummary.Stats {
rows = append(rows, table.Row{k, v.SuccessfullyMigrated, v.AlreadyMigrated})
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"", "Successfully Migrated", "Already Migrated"})
t.AppendRows(rows)
t.AppendSeparator()
t.SetStyle(table.StyleLight)
t.SortBy([]table.SortBy{
{Number: 1, Mode: table.Asc},
})
t.Render()
}
if len(saveSummary.SkipDetails) > 0 {
log.Info("Here are the details of entities that got skipped while migrating - ")
for i := range saveSummary.SkipDetails {
w := saveSummary.SkipDetails[i]
logWithDetails(log.WarnLevel, w.Entity, w.Reason)
}
}
if len(saveSummary.Errors) > 0 {
log.Info("Here are the errors while migrating - ")
for i := range saveSummary.Errors {
e := saveSummary.Errors[i]
level := log.ErrorLevel
// log as debug if the error is in skipLogs
for _, v := range skipLogs {
if strings.Contains(e.Message, v) {
level = log.DebugLevel
break
}
}
logWithDetails(level, e.Entity, e.Message)
}
}
if len(saveSummary.SkippedExpressionsList) > 0 {
var rows []table.Row
for _, v := range saveSummary.SkippedExpressionsList {
rows = append(rows, table.Row{v.EntityType, v.Identifier, v.OrgIdentifier, v.ProjectIdentifier, strings.Join(v.Expressions, "\n")})
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Type", "Identifier", "Org", "Project", "Expressions"})
t.AppendRows(rows)
t.AppendSeparator()
t.SetStyle(table.StyleLight)
t.SortBy([]table.SortBy{
{Number: 1, Mode: table.Asc},
})
t.Render()
}
}
func logWithDetails(level log.Level, entity CurrentGenEntity, message string) {
if len(entity.Id) > 0 {
log.WithFields(log.Fields{
"type": entity.Type,
"appId": entity.AppId,
"id": entity.Id,
"name": entity.Name,
}).Log(level, message)
} else {
log.WithFields(log.Fields{}).Log(level, message)
}
}