Skip to content

Commit 66db92a

Browse files
author
Aline Abler
committed
Separate cleanup to its own struct in order to remove unneeded parameters
1 parent a515506 commit 66db92a

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

apiserver/billing/odoostorage/odoo/odoo8/odoo8.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,24 @@ func NewOdoo8Storage(odooURL string, debugTransport bool, conf Config) *Odoo8Sto
7676
}
7777
}
7878

79+
func NewFailedRecordScrubber(odooURL string, debugTransport bool) *FailedRecordScrubber {
80+
return &FailedRecordScrubber{
81+
sessionCreator: func(ctx context.Context) (client.QueryExecutor, error) {
82+
return client.Open(ctx, odooURL, client.ClientOptions{UseDebugLogger: debugTransport})
83+
},
84+
}
85+
}
86+
7987
type Odoo8Storage struct {
8088
config Config
8189

8290
sessionCreator func(ctx context.Context) (client.QueryExecutor, error)
8391
}
8492

93+
type FailedRecordScrubber struct {
94+
sessionCreator func(ctx context.Context) (client.QueryExecutor, error)
95+
}
96+
8597
func (s *Odoo8Storage) Get(ctx context.Context, name string) (*billingv1.BillingEntity, error) {
8698
company, accountingContact, err := s.get(ctx, name)
8799
if err != nil {
@@ -278,7 +290,9 @@ func (s *Odoo8Storage) Update(ctx context.Context, be *billingv1.BillingEntity)
278290
return nil
279291
}
280292

281-
func (s *Odoo8Storage) CleanupIncompleteRecords(ctx context.Context, minAge time.Duration) error {
293+
func (s *FailedRecordScrubber) CleanupIncompleteRecords(ctx context.Context, minAge time.Duration) error {
294+
// CleanupIncompleteRecords looks for partner records in Odoo that still have the "inflight" flag set despite being older than `minAge`. Those records are then deleted.
295+
// Such records might come into existence due to a partially failed creation request.
282296
l := klog.FromContext(ctx)
283297
l.Info("Looking for stale inflight partner records...")
284298

@@ -298,9 +312,9 @@ func (s *Odoo8Storage) CleanupIncompleteRecords(ctx context.Context, minAge time
298312
ids := []int{}
299313

300314
for _, record := range inflightRecords {
301-
updateTime := record.CreationTimestamp.ToTime()
315+
createdTime := record.CreationTimestamp.ToTime()
302316

303-
if updateTime.Before(time.Now().Add(-1 * minAge)) {
317+
if createdTime.Before(time.Now().Add(-1 * minAge)) {
304318
ids = append(ids, record.ID)
305319
l.Info("Preparing to delete inflight partner record", "name", record.Name, "id", record.ID)
306320
}

apiserver/billing/odoostorage/odoo/odoo8/odoo8_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,19 @@ func createStorage(t *testing.T) (*gomock.Controller, *clientmock.MockQueryExecu
411411
}
412412
}
413413

414+
func createFailedRecordScrubber(t *testing.T) (*gomock.Controller, *clientmock.MockQueryExecutor, *FailedRecordScrubber) {
415+
ctrl := gomock.NewController(t)
416+
mock := clientmock.NewMockQueryExecutor(ctrl)
417+
418+
return ctrl, mock, &FailedRecordScrubber{
419+
sessionCreator: func(ctx context.Context) (client.QueryExecutor, error) {
420+
return mock, nil
421+
},
422+
}
423+
}
424+
414425
func TestCleanup(t *testing.T) {
415-
ctrl, mock, subject := createStorage(t)
426+
ctrl, mock, subject := createFailedRecordScrubber(t)
416427
defer ctrl.Finish()
417428

418429
tn := time.Now()

cleanup.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
ctrl "sigs.k8s.io/controller-runtime"
1010

1111
"github.com/appuio/control-api/apiserver/billing/odoostorage/odoo/odoo8"
12-
"github.com/appuio/control-api/apiserver/billing/odoostorage/odoo/odoo8/countries"
1312
)
1413

1514
// APICommand creates a new command allowing to start the API server
@@ -20,32 +19,17 @@ func CleanupCommand() *cobra.Command {
2019

2120
odooUrl := cmd.Flags().String("billing-entity-odoo8-url", "http://localhost:8069", "URL of the Odoo instance to use for billing entities")
2221
debugTransport := cmd.Flags().Bool("billing-entity-odoo8-debug-transport", false, "Enable debug logging for the Odoo transport")
23-
countryList := cmd.Flags().String("billing-entity-odoo8-country-list", "countries.yaml", "Path to the country list file in the format of [{name: \"Germany\", code: \"DE\", id: 81},...]")
24-
accountingContactDisplayName := cmd.Flags().String("billing-entity-odoo8-accounting-contact-display-name", "Accounting", "Display name of the accounting contact")
25-
languagePreference := cmd.Flags().String("billing-entity-odoo8-language-preference", "en_US", "Language preference of the Odoo record")
26-
paymentTerm := cmd.Flags().Int("billing-entity-odoo8-payment-term-id", 2, "Payment term ID of the Odoo record")
2722
minAge := cmd.Flags().Duration("billing-entity-odoo8-cleanup-after", time.Hour, "Clean up only records older than this")
2823

2924
cmd.Run = func(cmd *cobra.Command, args []string) {
3025
ctx := ctrl.SetupSignalHandler()
3126
l := klog.FromContext(ctx)
32-
countryIDs, err := countries.LoadCountryIDs(*countryList)
33-
if err != nil {
34-
l.Error(err, "Unable to load country list")
35-
os.Exit(1)
36-
}
37-
storage := odoo8.NewOdoo8Storage(
27+
scrubber := odoo8.NewFailedRecordScrubber(
3828
*odooUrl,
3929
*debugTransport,
40-
odoo8.Config{
41-
AccountingContactDisplayName: *accountingContactDisplayName,
42-
LanguagePreference: *languagePreference,
43-
PaymentTermID: *paymentTerm,
44-
CountryIDs: countryIDs,
45-
},
4630
)
4731

48-
err = storage.CleanupIncompleteRecords(ctx, *minAge)
32+
err := scrubber.CleanupIncompleteRecords(ctx, *minAge)
4933
if err != nil {
5034
l.Error(err, "Unable to clean up incomplete records")
5135
os.Exit(1)

0 commit comments

Comments
 (0)