From 5686ae1e71e8cd58caaf9edcc11eb9d846ae1b1f Mon Sep 17 00:00:00 2001 From: Gabriel Saratura Date: Tue, 30 Apr 2024 11:46:14 +0200 Subject: [PATCH] Add exoscale tiers to SOS --- pkg/exoscale/objectstorage.go | 20 ++++++++++++++-- pkg/exoscale/objectstorage_test.go | 37 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 pkg/exoscale/objectstorage_test.go diff --git a/pkg/exoscale/objectstorage.go b/pkg/exoscale/objectstorage.go index 4b25910..8f9c819 100644 --- a/pkg/exoscale/objectstorage.go +++ b/pkg/exoscale/objectstorage.go @@ -17,7 +17,9 @@ import ( k8s "sigs.k8s.io/controller-runtime/pkg/client" ) -const productIdStorage = "appcat-exoscale-objectstorage-storage" +const productIdStorageTier1 = "appcat-exoscale-objectstorage-storage-tier-1" +const productIdStorageTier2 = "appcat-exoscale-objectstorage-storage-tier-2" +const productIdStorageTier3 = "appcat-exoscale-objectstorage-storage-tier-3" // ObjectStorage gathers bucket data from exoscale provider and cluster and saves to the database type ObjectStorage struct { @@ -125,7 +127,7 @@ func (o *ObjectStorage) getOdooMeteredBillingRecords(ctx context.Context, sosBuc } o := odoo.OdooMeteredBillingRecord{ - ProductID: productIdStorage, + ProductID: getProductId(value), InstanceID: instanceId + "/storage", ItemDescription: bucketDetail.BucketName, ItemGroupDescription: itemGroup, @@ -147,6 +149,20 @@ func (o *ObjectStorage) getOdooMeteredBillingRecords(ctx context.Context, sosBuc return aggregatedBuckets, nil } +// getProductId calculates the tier based on the bucket storage consumption +// For more details https://www.exoscale.com/object-storage/ +// Value is passed as GiB +func getProductId(value float64) string { + valueTB := value / 1024 + if valueTB < 512 { + return productIdStorageTier1 + } else if valueTB > 1024 { + return productIdStorageTier3 + } else { + return productIdStorageTier2 + } +} + func (o *ObjectStorage) fetchManagedBucketsAndNamespaces(ctx context.Context) ([]BucketDetail, error) { logger := log.Logger(ctx) logger.Info("Fetching buckets and namespaces from cluster") diff --git a/pkg/exoscale/objectstorage_test.go b/pkg/exoscale/objectstorage_test.go new file mode 100644 index 0000000..d8cacaf --- /dev/null +++ b/pkg/exoscale/objectstorage_test.go @@ -0,0 +1,37 @@ +package exoscale + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestObjectStorage_getProductId(t *testing.T) { + tests := map[string]struct { + value float64 // in GiB + expectTier string + }{ + "given SOS with below 512TiB capacity, we should get the Product Tier 1": { + value: 300.1, // in GiB + expectTier: productIdStorageTier1, + }, + "given SOS with above 512 TiB and below 1Pib capacity, we should get the Product Tier 2": { + value: 813000.4, // in GiB + expectTier: productIdStorageTier2, + }, + "given SOS with above 1Pib capacity, we should get the Product Tier 3": { + value: 1300345.6, // in GiB + expectTier: productIdStorageTier3, + }, + "given SOS with below 0 capacity, we should get the Product Tier 1": { + value: 0, // in GiB + expectTier: productIdStorageTier1, + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + tier := getProductId(tc.value) + assert.Equal(t, tc.expectTier, tier) + }) + } +}