Skip to content

Commit

Permalink
added cost calculation for digitalocean volumes
Browse files Browse the repository at this point in the history
Signed-off-by: bishal7679 <[email protected]>
  • Loading branch information
bishal7679 committed Nov 13, 2023
1 parent 5983512 commit 6958325
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions providers/digitalocean/storage/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"github.com/tailwarden/komiser/providers"
)

var hourlyPrice float64
const createdLayout = "2006-01-02T15:04:05Z" // 2020-07-21T18:37:44Z

func Volumes(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
resources := make([]models.Resource, 0)
volumes, _, err := client.DigitalOceanClient.Storage.ListVolumes(ctx, &godo.ListVolumeParams{})
Expand All @@ -37,13 +40,41 @@ func Volumes(ctx context.Context, client providers.ProviderClient) ([]models.Res
}
}

sizeInGB := volume.SizeGigaBytes
if sizeInGB <= 100 {
hourlyPrice = 0.015
}
if sizeInGB <= 500 && sizeInGB > 100 {
hourlyPrice = 0.075
} else {
hourlyPrice = 0.150
}

currentTime := time.Now()
currentMonth := time.Date(currentTime.Year(), currentTime.Month(), 1, 0, 0, 0, 0, time.UTC)

created, err := time.Parse(createdLayout, volume.CreatedAt.String())
if err != nil {
return nil, err
}

var duration time.Duration
if created.Before(currentMonth) {
duration = currentTime.Sub(currentMonth)
} else {
duration = currentTime.Sub(created)
}

monthlyCost := hourlyPrice * float64(duration.Hours())

resources = append(resources, models.Resource{
Provider: "DigitalOcean",
Account: client.Name,
Service: "Volume",
ResourceId: volume.ID,
Region: volume.Region.Name,
Name: volume.Name,
Cost: monthlyCost,
Tags: tags,
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://cloud.digitalocean.com/volumes/%s", volume.ID),
Expand Down

0 comments on commit 6958325

Please sign in to comment.