Skip to content

Commit

Permalink
Merge pull request #80 from vshn/adjust-price-exo
Browse files Browse the repository at this point in the history
Add exoscale tiers to SOS
  • Loading branch information
zugao committed May 7, 2024
2 parents 5539f19 + 5686ae1 commit b52f485
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkg/exoscale/objectstorage.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand Down
37 changes: 37 additions & 0 deletions 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)
})
}
}

0 comments on commit b52f485

Please sign in to comment.