From 7387074e18316734e969e491e95896237c6653dd Mon Sep 17 00:00:00 2001 From: Nicolas Bigler Date: Fri, 22 Dec 2023 11:12:14 +0100 Subject: [PATCH] Fix salesOrder being overwritten and therefore creating wrong odoo records Signed-off-by: Nicolas Bigler --- pkg/cloudscale/objectstorage.go | 15 ++++++++------- pkg/exoscale/dbaas.go | 7 ++++--- pkg/exoscale/objectstorage.go | 7 ++++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/pkg/cloudscale/objectstorage.go b/pkg/cloudscale/objectstorage.go index 214c8d8..c5a150f 100644 --- a/pkg/cloudscale/objectstorage.go +++ b/pkg/cloudscale/objectstorage.go @@ -84,15 +84,16 @@ func (o *ObjectStorage) GetMetrics(ctx context.Context, billingDate time.Time) ( continue } appuioManaged := true - if o.salesOrder == "" { + salesOrder := o.salesOrder + if salesOrder == "" { appuioManaged = false - o.salesOrder, err = controlAPI.GetSalesOrder(ctx, o.controlApiClient, nsTenants[bd.Namespace]) + salesOrder, err = controlAPI.GetSalesOrder(ctx, o.controlApiClient, nsTenants[bd.Namespace]) if err != nil { logger.Error(err, "unable to sync bucket", "namespace", bd.Namespace) continue } } - records, err := o.createOdooRecord(bucketMetricsData, bd, appuioManaged) + records, err := o.createOdooRecord(bucketMetricsData, bd, appuioManaged, salesOrder) if err != nil { logger.Error(err, "unable to create Odoo Record", "namespace", bd.Namespace) continue @@ -104,7 +105,7 @@ func (o *ObjectStorage) GetMetrics(ctx context.Context, billingDate time.Time) ( return allRecords, nil } -func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetricsData, b BucketDetail, appuioManaged bool) ([]odoo.OdooMeteredBillingRecord, error) { +func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetricsData, b BucketDetail, appuioManaged bool, salesOrder string) ([]odoo.OdooMeteredBillingRecord, error) { if len(bucketMetricsData.TimeSeries) != 1 { return nil, fmt.Errorf("there must be exactly one metrics data point, found %d", len(bucketMetricsData.TimeSeries)) } @@ -137,7 +138,7 @@ func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetr InstanceID: instanceId, ItemDescription: "AppCat Cloudscale ObjectStorage", ItemGroupDescription: itemGroup, - SalesOrder: o.salesOrder, + SalesOrder: salesOrder, UnitID: o.uomMapping[units[productIdStorage]], ConsumedUnits: storageBytesValue, TimeRange: odoo.TimeRange{ @@ -150,7 +151,7 @@ func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetr InstanceID: instanceId, ItemDescription: "AppCat Cloudscale ObjectStorage", ItemGroupDescription: itemGroup, - SalesOrder: o.salesOrder, + SalesOrder: salesOrder, UnitID: o.uomMapping[units[productIdTrafficOut]], ConsumedUnits: trafficOutValue, TimeRange: odoo.TimeRange{ @@ -163,7 +164,7 @@ func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetr InstanceID: instanceId, ItemDescription: "AppCat Cloudscale ObjectStorage", ItemGroupDescription: itemGroup, - SalesOrder: o.salesOrder, + SalesOrder: salesOrder, UnitID: o.uomMapping[units[productIdQueryRequests]], ConsumedUnits: queryRequestsValue, TimeRange: odoo.TimeRange{ diff --git a/pkg/exoscale/dbaas.go b/pkg/exoscale/dbaas.go index b5288be..92dfeb5 100644 --- a/pkg/exoscale/dbaas.go +++ b/pkg/exoscale/dbaas.go @@ -209,9 +209,10 @@ func (ds *DBaaS) AggregateDBaaS(ctx context.Context, exoscaleDBaaS []*egoscale.D itemGroup := fmt.Sprintf("APPUiO Managed - Zone: %s / Namespace: %s", ds.clusterId, dbaasDetail.Namespace) instanceId := fmt.Sprintf("%s/%s", dbaasDetail.Zone, dbaasDetail.DBName) - if ds.salesOrder == "" { + salesOrder := ds.salesOrder + if salesOrder == "" { itemGroup = fmt.Sprintf("APPUiO Cloud - Zone: %s / Namespace: %s", ds.clusterId, dbaasDetail.Namespace) - ds.salesOrder, err = controlAPI.GetSalesOrder(ctx, ds.controlApiClient, dbaasDetail.Organization) + salesOrder, err = controlAPI.GetSalesOrder(ctx, ds.controlApiClient, dbaasDetail.Organization) if err != nil { logger.Error(err, "Unable to sync DBaaS, cannot get salesOrder", "namespace", dbaasDetail.Namespace) continue @@ -223,7 +224,7 @@ func (ds *DBaaS) AggregateDBaaS(ctx context.Context, exoscaleDBaaS []*egoscale.D InstanceID: instanceId, ItemDescription: "Exoscale DBaaS " + dbaasTypes[*dbaasUsage.Type], ItemGroupDescription: itemGroup, - SalesOrder: ds.salesOrder, + SalesOrder: salesOrder, UnitID: ds.uomMapping[odoo.InstanceHour], ConsumedUnits: 1, TimeRange: odoo.TimeRange{ diff --git a/pkg/exoscale/objectstorage.go b/pkg/exoscale/objectstorage.go index 1e817ea..6fc17ac 100644 --- a/pkg/exoscale/objectstorage.go +++ b/pkg/exoscale/objectstorage.go @@ -112,9 +112,10 @@ func (o *ObjectStorage) getOdooMeteredBillingRecords(ctx context.Context, sosBuc itemGroup := fmt.Sprintf("APPUiO Managed - Zone: %s / Namespace: %s", o.clusterId, bucketDetail.Namespace) instanceId := fmt.Sprintf("%s/%s", bucketDetail.Zone, bucketDetail.BucketName) - if o.salesOrder == "" { + salesOrder := o.salesOrder + if salesOrder == "" { itemGroup = fmt.Sprintf("APPUiO Cloud - Zone: %s / Namespace: %s", o.clusterId, bucketDetail.Namespace) - o.salesOrder, err = controlAPI.GetSalesOrder(ctx, o.controlApiClient, bucketDetail.Organization) + salesOrder, err = controlAPI.GetSalesOrder(ctx, o.controlApiClient, bucketDetail.Organization) if err != nil { logger.Error(err, "unable to sync bucket", "namespace", bucketDetail.Namespace) continue @@ -126,7 +127,7 @@ func (o *ObjectStorage) getOdooMeteredBillingRecords(ctx context.Context, sosBuc InstanceID: instanceId, ItemDescription: "AppCat Exoscale ObjectStorage", ItemGroupDescription: itemGroup, - SalesOrder: o.salesOrder, + SalesOrder: salesOrder, UnitID: o.uomMapping[odoo.GBDay], ConsumedUnits: value, TimeRange: odoo.TimeRange{