-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsource.go
More file actions
294 lines (239 loc) · 7.3 KB
/
source.go
File metadata and controls
294 lines (239 loc) · 7.3 KB
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
package service
import (
"context"
"errors"
"time"
"github.com/kubev2v/migration-planner/pkg/opa"
"github.com/google/uuid"
"github.com/kubev2v/migration-planner/internal/image"
"github.com/kubev2v/migration-planner/internal/service/mappers"
"github.com/kubev2v/migration-planner/internal/store"
"github.com/kubev2v/migration-planner/internal/store/model"
"github.com/kubev2v/migration-planner/internal/util"
"github.com/kubev2v/migration-planner/pkg/version"
)
type SourceService struct {
store store.Store
opaValidator *opa.Validator
}
func NewSourceService(store store.Store, opaValidator *opa.Validator) *SourceService {
return &SourceService{
store: store,
opaValidator: opaValidator,
}
}
// TODO should be moved to ImageService (to be created)
func (s *SourceService) GetSourceDownloadURL(ctx context.Context, id uuid.UUID) (string, time.Time, error) {
source, err := s.store.Source().Get(ctx, id)
if err != nil {
if errors.Is(err, store.ErrRecordNotFound) {
return "", time.Now(), NewErrSourceNotFound(id)
}
return "", time.Time{}, err
}
// FIXME: refactor the environment vars + config.yaml
baseUrl := util.GetEnv("MIGRATION_PLANNER_IMAGE_URL", "http://localhost:11443")
url, expireAt, err := image.GenerateDownloadURLByToken(baseUrl, source)
if err != nil {
return "", time.Time{}, err
}
return url, time.Time(*expireAt), err
}
func (s *SourceService) ListSources(ctx context.Context, filter *SourceFilter) ([]model.Source, error) {
storeFilter := store.NewSourceQueryFilter().ByUsername(filter.Username).ByOrgID(filter.OrgID)
userResult, err := s.store.Source().List(ctx, storeFilter)
if err != nil {
return nil, err
}
return userResult, nil
}
func (s *SourceService) GetSource(ctx context.Context, id uuid.UUID) (*model.Source, error) {
source, err := s.store.Source().Get(ctx, id)
if err != nil {
if errors.Is(err, store.ErrRecordNotFound) {
return nil, NewErrSourceNotFound(id)
}
return nil, err
}
return source, nil
}
func (s *SourceService) CreateSource(ctx context.Context, sourceForm mappers.SourceCreateForm) (model.Source, error) {
// Generate a signing key for tokens for the source
imageTokenKey, err := image.HMACKey(32)
if err != nil {
return model.Source{}, err
}
ctx, err = s.store.NewTransactionContext(ctx)
if err != nil {
return model.Source{}, err
}
result, err := s.store.Source().Create(ctx, sourceForm.ToSource())
if err != nil {
_, _ = store.Rollback(ctx)
if errors.Is(err, store.ErrDuplicateKey) {
return model.Source{}, NewErrSourceDuplicateName(sourceForm.Name)
}
return model.Source{}, err
}
imageInfra := sourceForm.ToImageInfra(result.ID, imageTokenKey)
if _, err := s.store.ImageInfra().Create(ctx, imageInfra); err != nil {
_, _ = store.Rollback(ctx)
return model.Source{}, err
}
if _, err := store.Commit(ctx); err != nil {
return model.Source{}, err
}
return *result, nil
}
func (s *SourceService) DeleteSources(ctx context.Context) error {
if err := s.store.Source().DeleteAll(ctx); err != nil {
return err
}
return nil
}
func (s *SourceService) DeleteSource(ctx context.Context, id uuid.UUID) error {
if err := s.store.Source().Delete(ctx, id); err != nil {
return err
}
return nil
}
func (s *SourceService) UpdateSource(ctx context.Context, id uuid.UUID, form mappers.SourceUpdateForm) (*model.Source, error) {
ctx, err := s.store.NewTransactionContext(ctx)
if err != nil {
return nil, err
}
defer func() {
_, _ = store.Rollback(ctx)
}()
source, err := s.store.Source().Get(ctx, id)
if err != nil {
if errors.Is(err, store.ErrRecordNotFound) {
return nil, NewErrSourceNotFound(id)
}
return nil, err
}
// Update source fields
form.ToSource(source)
if _, err := s.store.Source().Update(ctx, *source); err != nil {
return nil, err
}
// Update ImageInfra
imageInfra := form.ToImageInfra(source.ID)
if _, err := s.store.ImageInfra().Update(ctx, imageInfra); err != nil {
return nil, err
}
// Update labels
if labels := form.ToLabels(); labels != nil {
if err := s.store.Label().UpdateLabels(ctx, source.ID, labels); err != nil {
return nil, err
}
}
if _, err := store.Commit(ctx); err != nil {
return nil, err
}
// Re-fetch source to get all updated associations
updatedSource, err := s.store.Source().Get(ctx, id)
if err != nil {
return nil, err
}
return updatedSource, nil
}
func (s *SourceService) UpdateInventory(ctx context.Context, form mappers.InventoryUpdateForm) (model.Source, error) {
ctx, err := s.store.NewTransactionContext(ctx)
if err != nil {
return model.Source{}, err
}
source, err := s.store.Source().Get(ctx, form.SourceID)
if err != nil {
if errors.Is(err, store.ErrRecordNotFound) {
return model.Source{}, NewErrSourceNotFound(form.SourceID)
}
return model.Source{}, err
}
// create the agent if missing
var agent *model.Agent
for _, a := range source.Agents {
if a.ID == form.AgentID {
agent = &a
break
}
}
if agent == nil {
newAgent := model.NewAgentForSource(uuid.New(), *source)
if _, err := s.store.Agent().Create(ctx, newAgent); err != nil {
return model.Source{}, err
}
}
if source.VCenterID != "" && source.VCenterID != form.VCenterID {
_, _ = store.Rollback(ctx)
return model.Source{}, NewErrInvalidVCenterID(form.SourceID, form.VCenterID)
}
source.OnPremises = true
source.VCenterID = form.VCenterID
source.Inventory = form.Inventory
if _, err = s.store.Source().Update(ctx, *source); err != nil {
_, _ = store.Rollback(ctx)
return model.Source{}, err
}
if _, err := store.Commit(ctx); err != nil {
return model.Source{}, err
}
return *source, nil
}
type SourceFilterFunc func(s *SourceFilter)
type SourceFilter struct {
Username string
OrgID string
ID uuid.UUID
}
func NewSourceFilter(filters ...SourceFilterFunc) *SourceFilter {
s := &SourceFilter{}
for _, f := range filters {
f(s)
}
return s
}
func (s *SourceFilter) WithOption(o SourceFilterFunc) *SourceFilter {
o(s)
return s
}
func WithUsername(username string) SourceFilterFunc {
return func(s *SourceFilter) {
s.Username = username
}
}
func WithSourceID(id uuid.UUID) SourceFilterFunc {
return func(s *SourceFilter) {
s.ID = id
}
}
func WithOrgID(orgID string) SourceFilterFunc {
return func(s *SourceFilter) {
s.OrgID = orgID
}
}
// CheckAgentVersionWarning compares stored agent version with current and returns warning if they differ.
func CheckAgentVersionWarning(imageInfra *model.ImageInfra) *string {
versionInfo := version.Get()
if !version.IsValidAgentVersion(versionInfo.AgentVersionName) {
return nil
}
currentVersion := versionInfo.AgentVersionName
// Handle missing version (edge case if migration hasn't run)
if imageInfra == nil || imageInfra.AgentVersion == nil || *imageInfra.AgentVersion == "" {
message := "No version information available for this OVA. Current system version: " + currentVersion +
". Consider downloading a new OVA to ensure compatibility."
return &message
}
storedVersion := *imageInfra.AgentVersion
if !version.IsValidAgentVersion(storedVersion) {
return nil
}
if storedVersion == currentVersion {
return nil
}
message := "Agent version mismatch detected. The OVA was downloaded with version " + storedVersion +
", but the current system version is " + currentVersion +
". Consider downloading a new OVA to ensure compatibility."
return &message
}