-
Notifications
You must be signed in to change notification settings - Fork 84
/
vendor.go
103 lines (88 loc) · 3.75 KB
/
vendor.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
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"
)
// VendorService is responsible for the service-layer Vendor operations.
//
//go:generate mockery --name=VendorService --output=automock --outpkg=automock --case=underscore --disable-version-string
type VendorService interface {
Create(ctx context.Context, resourceType resource.Type, resourceID string, in model.VendorInput) (string, error)
Update(ctx context.Context, resourceType resource.Type, id string, in model.VendorInput) error
Delete(ctx context.Context, resourceType resource.Type, id string) error
ListByApplicationID(ctx context.Context, appID string) ([]*model.Vendor, error)
ListByApplicationTemplateVersionID(ctx context.Context, appTemplateVersionID string) ([]*model.Vendor, error)
}
// VendorProcessor defines vendor processor
type VendorProcessor struct {
transact persistence.Transactioner
vendorSvc VendorService
}
// NewVendorProcessor creates new instance of VendorProcessor
func NewVendorProcessor(transact persistence.Transactioner, vendorSvc VendorService) *VendorProcessor {
return &VendorProcessor{
transact: transact,
vendorSvc: vendorSvc,
}
}
// Process re-syncs the vendors passed as an argument.
func (vp *VendorProcessor) Process(ctx context.Context, resourceType resource.Type, resourceID string, vendors []*model.VendorInput) ([]*model.Vendor, error) {
vendorsFromDB, err := vp.listVendorsInTx(ctx, resourceType, resourceID)
if err != nil {
return nil, err
}
for _, vendor := range vendors {
if err := vp.resyncVendorInTx(ctx, resourceType, resourceID, vendorsFromDB, vendor); err != nil {
return nil, err
}
}
vendorsFromDB, err = vp.listVendorsInTx(ctx, resourceType, resourceID)
if err != nil {
return nil, err
}
return vendorsFromDB, nil
}
func (vp *VendorProcessor) listVendorsInTx(ctx context.Context, resourceType resource.Type, resourceID string) ([]*model.Vendor, error) {
tx, err := vp.transact.Begin()
if err != nil {
return nil, err
}
defer vp.transact.RollbackUnlessCommitted(ctx, tx)
ctx = persistence.SaveToContext(ctx, tx)
var vendorsFromDB []*model.Vendor
switch resourceType {
case resource.Application:
vendorsFromDB, err = vp.vendorSvc.ListByApplicationID(ctx, resourceID)
case resource.ApplicationTemplateVersion:
vendorsFromDB, err = vp.vendorSvc.ListByApplicationTemplateVersionID(ctx, resourceID)
}
if err != nil {
return nil, errors.Wrapf(err, "error while listing vendors for %s with id %q", resourceType, resourceID)
}
return vendorsFromDB, tx.Commit()
}
func (vp *VendorProcessor) resyncVendorInTx(ctx context.Context, resourceType resource.Type, resourceID string, vendorsFromDB []*model.Vendor, vendor *model.VendorInput) error {
tx, err := vp.transact.Begin()
if err != nil {
return err
}
defer vp.transact.RollbackUnlessCommitted(ctx, tx)
ctx = persistence.SaveToContext(ctx, tx)
if err := vp.resyncVendor(ctx, resourceType, resourceID, vendorsFromDB, *vendor); err != nil {
return errors.Wrapf(err, "error while resyncing vendor with ORD ID %q", vendor.OrdID)
}
return tx.Commit()
}
func (vp *VendorProcessor) resyncVendor(ctx context.Context, resourceType resource.Type, resourceID string, vendorsFromDB []*model.Vendor, vendor model.VendorInput) error {
ctx = addFieldToLogger(ctx, "vendor_ord_id", vendor.OrdID)
if i, found := searchInSlice(len(vendorsFromDB), func(i int) bool {
return vendorsFromDB[i].OrdID == vendor.OrdID
}); found {
return vp.vendorSvc.Update(ctx, resourceType, vendorsFromDB[i].ID, vendor)
}
_, err := vp.vendorSvc.Create(ctx, resourceType, resourceID, vendor)
return err
}