Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Cost Calculation for DigitalOcean Volumes #1190

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please shift this var definition inside func

const createdLayout = "2006-01-02T15:04:05Z" // 2020-07-21T18:37:44Z
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the comment


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
}
bishal7679 marked this conversation as resolved.
Show resolved Hide resolved

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