/
tombstone_deleter.go
164 lines (152 loc) · 7.63 KB
/
tombstone_deleter.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
package processor
import (
"context"
"github.com/kyma-incubator/compass/components/director/internal/model"
"github.com/kyma-incubator/compass/components/director/pkg/persistence"
"github.com/kyma-incubator/compass/components/director/pkg/resource"
"github.com/pkg/errors"
)
// BundleService is responsible for the service-layer Bundle operations.
//
//go:generate mockery --name=BundleService --output=automock --outpkg=automock --case=underscore --disable-version-string
type BundleService interface {
CreateBundle(ctx context.Context, resourceType resource.Type, resourceID string, in model.BundleCreateInput, bndlHash uint64) (string, error)
UpdateBundle(ctx context.Context, resourceType resource.Type, id string, in model.BundleUpdateInput, bndlHash uint64) error
Delete(ctx context.Context, resourceType resource.Type, id string) error
ListByApplicationIDNoPaging(ctx context.Context, appID string) ([]*model.Bundle, error)
ListByApplicationTemplateVersionIDNoPaging(ctx context.Context, appTemplateVersionID string) ([]*model.Bundle, error)
}
// TombstonedResourcesDeleter defines tombstoned resources deleter
type TombstonedResourcesDeleter struct {
transact persistence.Transactioner
packageSvc PackageService
apiSvc APIService
eventSvc EventService
entityTypeSvc EntityTypeService
capabilitySvc CapabilityService
integrationDependencySvc IntegrationDependencyService
dataProductSvc DataProductService
vendorSvc VendorService
productSvc ProductService
bundleSvc BundleService
}
// NewTombstonedResourcesDeleter creates new instance of TombstonedResourcesDeleter
func NewTombstonedResourcesDeleter(transact persistence.Transactioner, packageSvc PackageService, apiSvc APIService, eventSvc EventService, entityTypeSvc EntityTypeService, capabilitySvc CapabilityService, integrationDependencySvc IntegrationDependencyService, dataProductSvc DataProductService, vendorSvc VendorService, productSvc ProductService, bundleSvc BundleService) *TombstonedResourcesDeleter {
return &TombstonedResourcesDeleter{
transact: transact,
packageSvc: packageSvc,
apiSvc: apiSvc,
eventSvc: eventSvc,
entityTypeSvc: entityTypeSvc,
capabilitySvc: capabilitySvc,
integrationDependencySvc: integrationDependencySvc,
dataProductSvc: dataProductSvc,
vendorSvc: vendorSvc,
productSvc: productSvc,
bundleSvc: bundleSvc,
}
}
// Delete deletes all tombstoned resources.
func (td *TombstonedResourcesDeleter) Delete(ctx context.Context, resourceType resource.Type, vendorsFromDB []*model.Vendor, productsFromDB []*model.Product, packagesFromDB []*model.Package, bundlesFromDB []*model.Bundle, apisFromDB []*model.APIDefinition, eventsFromDB []*model.EventDefinition, entityTypesFromDB []*model.EntityType, capabilitiesFromDB []*model.Capability, integrationDependenciesFromDB []*model.IntegrationDependency, dataProductsFromDB []*model.DataProduct, tombstonesFromDB []*model.Tombstone, fetchRequests []*OrdFetchRequest) ([]*OrdFetchRequest, error) {
tx, err := td.transact.Begin()
if err != nil {
return nil, err
}
defer td.transact.RollbackUnlessCommitted(ctx, tx)
ctx = persistence.SaveToContext(ctx, tx)
frIdxToExclude := make([]int, 0)
for _, ts := range tombstonesFromDB {
if i, found := searchInSlice(len(packagesFromDB), func(i int) bool {
return packagesFromDB[i].OrdID == ts.OrdID
}); found {
if err := td.packageSvc.Delete(ctx, resourceType, packagesFromDB[i].ID); err != nil {
return nil, errors.Wrapf(err, "error while deleting resource with ORD ID %q based on its tombstone", ts.OrdID)
}
}
if i, found := searchInSlice(len(apisFromDB), func(i int) bool {
return equalStrings(apisFromDB[i].OrdID, &ts.OrdID)
}); found {
if err := td.apiSvc.Delete(ctx, resourceType, apisFromDB[i].ID); err != nil {
return nil, errors.Wrapf(err, "error while deleting resource with ORD ID %q based on its tombstone", ts.OrdID)
}
}
if i, found := searchInSlice(len(eventsFromDB), func(i int) bool {
return equalStrings(eventsFromDB[i].OrdID, &ts.OrdID)
}); found {
if err := td.eventSvc.Delete(ctx, resourceType, eventsFromDB[i].ID); err != nil {
return nil, errors.Wrapf(err, "error while deleting resource with ORD ID %q based on its tombstone", ts.OrdID)
}
}
if i, found := searchInSlice(len(entityTypesFromDB), func(i int) bool {
return equalStrings(&entityTypesFromDB[i].OrdID, &ts.OrdID)
}); found {
if err := td.entityTypeSvc.Delete(ctx, resourceType, entityTypesFromDB[i].ID); err != nil {
return nil, errors.Wrapf(err, "error while deleting resource with ORD ID %q based on its tombstone", ts.OrdID)
}
}
if i, found := searchInSlice(len(capabilitiesFromDB), func(i int) bool {
return equalStrings(capabilitiesFromDB[i].OrdID, &ts.OrdID)
}); found {
if err := td.capabilitySvc.Delete(ctx, resourceType, capabilitiesFromDB[i].ID); err != nil {
return nil, errors.Wrapf(err, "error while deleting resource with ORD ID %q based on its tombstone", ts.OrdID)
}
}
if i, found := searchInSlice(len(integrationDependenciesFromDB), func(i int) bool {
return equalStrings(integrationDependenciesFromDB[i].OrdID, &ts.OrdID)
}); found {
if err := td.integrationDependencySvc.Delete(ctx, resourceType, integrationDependenciesFromDB[i].ID); err != nil {
return nil, errors.Wrapf(err, "error while deleting resource with ORD ID %q based on its tombstone", ts.OrdID)
}
}
if i, found := searchInSlice(len(dataProductsFromDB), func(i int) bool {
return equalStrings(dataProductsFromDB[i].OrdID, &ts.OrdID)
}); found {
if err := td.dataProductSvc.Delete(ctx, resourceType, dataProductsFromDB[i].ID); err != nil {
return nil, errors.Wrapf(err, "error while deleting resource with ORD ID %q based on its tombstone", ts.OrdID)
}
}
if i, found := searchInSlice(len(bundlesFromDB), func(i int) bool {
return equalStrings(bundlesFromDB[i].OrdID, &ts.OrdID)
}); found {
if err := td.bundleSvc.Delete(ctx, resourceType, bundlesFromDB[i].ID); err != nil {
return nil, errors.Wrapf(err, "error while deleting resource with ORD ID %q based on its tombstone", ts.OrdID)
}
}
if i, found := searchInSlice(len(vendorsFromDB), func(i int) bool {
return vendorsFromDB[i].OrdID == ts.OrdID
}); found {
if err := td.vendorSvc.Delete(ctx, resourceType, vendorsFromDB[i].ID); err != nil {
return nil, errors.Wrapf(err, "error while deleting resource with ORD ID %q based on its tombstone", ts.OrdID)
}
}
if i, found := searchInSlice(len(productsFromDB), func(i int) bool {
return productsFromDB[i].OrdID == ts.OrdID
}); found {
if err := td.productSvc.Delete(ctx, resourceType, productsFromDB[i].ID); err != nil {
return nil, errors.Wrapf(err, "error while deleting resource with ORD ID %q based on its tombstone", ts.OrdID)
}
}
for i := range fetchRequests {
if equalStrings(&fetchRequests[i].RefObjectOrdID, &ts.OrdID) {
frIdxToExclude = append(frIdxToExclude, i)
}
}
}
return excludeUnnecessaryFetchRequests(fetchRequests, frIdxToExclude), tx.Commit()
}
func excludeUnnecessaryFetchRequests(fetchRequests []*OrdFetchRequest, frIdxToExclude []int) []*OrdFetchRequest {
finalFetchRequests := make([]*OrdFetchRequest, 0)
for i := range fetchRequests {
shouldExclude := false
for _, idxToExclude := range frIdxToExclude {
if i == idxToExclude {
shouldExclude = true
break
}
}
if !shouldExclude {
finalFetchRequests = append(finalFetchRequests, fetchRequests[i])
}
}
return finalFetchRequests
}