-
Notifications
You must be signed in to change notification settings - Fork 84
/
operation_maintainer.go
211 lines (185 loc) · 7.41 KB
/
operation_maintainer.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
package systemfetcher
import (
"context"
"encoding/json"
"time"
"github.com/kyma-incubator/compass/components/director/internal/model"
operationsmanager "github.com/kyma-incubator/compass/components/director/internal/operations_manager"
"github.com/kyma-incubator/compass/components/director/pkg/log"
"github.com/kyma-incubator/compass/components/director/pkg/persistence"
tenantpkg "github.com/kyma-incubator/compass/components/director/pkg/tenant"
"github.com/pkg/errors"
)
// OperationMaintainer is responsible for maintaining of different types of operations.
//
//go:generate mockery --name=OperationMaintainer --output=automock --outpkg=automock --case=underscore --disable-version-string
type OperationMaintainer interface {
Maintain(ctx context.Context) error
}
// OperationService is responsible for the service-layer Operation operations.
//
//go:generate mockery --name=OperationService --output=automock --outpkg=automock --case=underscore --disable-version-string
type OperationService interface {
CreateMultiple(ctx context.Context, in []*model.OperationInput) error
DeleteMultiple(ctx context.Context, ids []string) error
ListAllByType(ctx context.Context, opType model.OperationType) ([]*model.Operation, error)
}
// BusinessTenantMappingService responsible for the service-layer tenant operations
//
//go:generate mockery --name=BusinessTenantMappingService --output=automock --outpkg=automock --case=underscore --disable-version-string
type BusinessTenantMappingService interface {
List(ctx context.Context) ([]*model.BusinessTenantMapping, error)
GetTenantByID(ctx context.Context, id string) (*model.BusinessTenantMapping, error)
GetTenantByExternalID(ctx context.Context, id string) (*model.BusinessTenantMapping, error)
}
// SystemFetcherOperationMaintainer consists of various resource services responsible for operations creation.
type SystemFetcherOperationMaintainer struct {
transact persistence.Transactioner
opSvc OperationService
businessTenantMappingSvc BusinessTenantMappingService
}
// NewOperationMaintainer creates OperationMaintainer based on kind
func NewOperationMaintainer(kind model.OperationType, transact persistence.Transactioner, opSvc OperationService, businessTenantMappingSvc BusinessTenantMappingService) OperationMaintainer {
if kind == model.OperationTypeSystemFetching {
return &SystemFetcherOperationMaintainer{
transact: transact,
opSvc: opSvc,
businessTenantMappingSvc: businessTenantMappingSvc,
}
}
return nil
}
// Maintain is responsible to create all missing and remove all obsolete operations
func (om *SystemFetcherOperationMaintainer) Maintain(ctx context.Context) error {
tx, err := om.transact.Begin()
if err != nil {
return err
}
defer om.transact.RollbackUnlessCommitted(ctx, tx)
ctx = persistence.SaveToContext(ctx, tx)
operationsToCreate, operationsToDelete, err := om.buildNonExistingOperationInputs(ctx)
if err != nil {
return errors.Wrap(err, "while building operation inputs")
}
operationsToDeleteIDs := make([]string, 0)
for _, op := range operationsToDelete {
operationsToDeleteIDs = append(operationsToDeleteIDs, op.ID)
}
if err := om.opSvc.CreateMultiple(ctx, operationsToCreate); err != nil {
return errors.Wrap(err, "while creating multiple operations")
}
if err := om.opSvc.DeleteMultiple(ctx, operationsToDeleteIDs); err != nil {
return errors.Wrap(err, "while deleting multiple operations")
}
return tx.Commit()
}
func (om *SystemFetcherOperationMaintainer) buildNonExistingOperationInputs(ctx context.Context) ([]*model.OperationInput, []*model.Operation, error) {
tenants, err := om.listBusinessTenantMappings(ctx)
if err != nil {
return nil, nil, errors.Wrap(err, "while getting tenants")
}
desiredStateOperations := make([]*model.OperationInput, 0)
for _, tenant := range tenants {
switch tenant.Type {
case tenantpkg.Customer, tenantpkg.Account:
op, err := om.tenantToOperation(tenant)
if err != nil {
return nil, nil, errors.Wrapf(err, "while creating operation from account or customer tenant %q", tenant.ID)
}
desiredStateOperations = append(desiredStateOperations, op)
default:
// Unknown type
log.C(ctx).Infof("No operation created for tenant as it is not of type customer or account: %q", tenant.ID)
}
}
existingOperations, err := om.opSvc.ListAllByType(ctx, model.OperationTypeSystemFetching)
if err != nil {
return nil, nil, err
}
operationsToCreate, err := om.getOperationsToCreate(desiredStateOperations, existingOperations)
if err != nil {
return nil, nil, err
}
operationsToDelete, err := om.getOperationsToDelete(existingOperations, desiredStateOperations)
if err != nil {
return nil, nil, err
}
return operationsToCreate, operationsToDelete, nil
}
func (om *SystemFetcherOperationMaintainer) getOperationsToCreate(desiredOperations []*model.OperationInput, existingOperations []*model.Operation) ([]*model.OperationInput, error) {
result := make([]*model.OperationInput, 0)
for _, currentDesiredOperation := range desiredOperations {
found := false
currentDesiredOperationData, err := ParseSystemFetcherOperationData(currentDesiredOperation.Data)
if err != nil {
return nil, err
}
for _, currentExistingOperation := range existingOperations {
currentExistingOperationData, err := ParseSystemFetcherOperationData(currentExistingOperation.Data)
if err != nil {
return nil, err
}
if currentDesiredOperationData.Equal(currentExistingOperationData) {
found = true
break
}
}
if !found {
result = append(result, currentDesiredOperation)
}
}
return result, nil
}
func (om *SystemFetcherOperationMaintainer) getOperationsToDelete(existingOperations []*model.Operation, desiredOperations []*model.OperationInput) ([]*model.Operation, error) {
result := make([]*model.Operation, 0)
for _, currentExistingOperation := range existingOperations {
found := false
currentExistingOperationData, err := ParseSystemFetcherOperationData(currentExistingOperation.Data)
if err != nil {
return nil, err
}
for _, currentDesiredOperation := range desiredOperations {
currentDesiredOperationData, err := ParseSystemFetcherOperationData(currentDesiredOperation.Data)
if err != nil {
return nil, err
}
if currentDesiredOperationData.Equal(currentExistingOperationData) {
found = true
break
}
}
if !found {
result = append(result, currentExistingOperation)
}
}
return result, nil
}
func (om *SystemFetcherOperationMaintainer) listBusinessTenantMappings(ctx context.Context) ([]*model.BusinessTenantMapping, error) {
tenants, err := om.businessTenantMappingSvc.List(ctx)
if err != nil {
log.C(ctx).WithError(err).Error("error while fetching tenants")
return nil, err
}
return tenants, nil
}
func (om *SystemFetcherOperationMaintainer) tenantToOperation(tenant *model.BusinessTenantMapping) (*model.OperationInput, error) {
opData := NewSystemFetcherOperationData(tenant.ID)
data, err := opData.GetData()
if err != nil {
return nil, err
}
operation := buildSystemFetcherOperationInput(data)
return operation, nil
}
func buildSystemFetcherOperationInput(data string) *model.OperationInput {
now := time.Now()
return &model.OperationInput{
OpType: model.OperationTypeSystemFetching,
Status: model.OperationStatusScheduled,
Data: json.RawMessage(data),
Error: nil,
Priority: int(operationsmanager.LowOperationPriority),
CreatedAt: &now,
UpdatedAt: nil,
}
}