Skip to content

Commit

Permalink
use floats to allow decimal monthly_hrs values
Browse files Browse the repository at this point in the history
  • Loading branch information
jessecureton committed Jul 10, 2022
1 parent 313cdd0 commit a207a1f
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 40 deletions.
10 changes: 5 additions & 5 deletions internal/providers/terraform/azure/linux_virtual_machine.go
Expand Up @@ -25,9 +25,9 @@ func NewAzureRMLinuxVirtualMachine(d *schema.ResourceData, u *schema.UsageData)

instanceType := d.Get("size").String()

var monthlyHours *int64 = nil
var monthlyHours *float64 = nil
if u != nil {
monthlyHours = u.GetInt("monthly_hrs")
monthlyHours = u.GetFloat("monthly_hrs")
}

costComponents := []*schema.CostComponent{linuxVirtualMachineCostComponent(region, instanceType, monthlyHours)}
Expand All @@ -50,7 +50,7 @@ func NewAzureRMLinuxVirtualMachine(d *schema.ResourceData, u *schema.UsageData)
}
}

func linuxVirtualMachineCostComponent(region string, instanceType string, monthlyHours *int64) *schema.CostComponent {
func linuxVirtualMachineCostComponent(region string, instanceType string, monthlyHours *float64) *schema.CostComponent {
purchaseOption := "Consumption"
purchaseOptionLabel := "pay as you go"

Expand All @@ -61,9 +61,9 @@ func linuxVirtualMachineCostComponent(region string, instanceType string, monthl
instanceType = fmt.Sprintf("Standard_%s", instanceType)
}

qty := decimal.NewFromInt(730)
qty := decimal.NewFromFloat(730)
if monthlyHours != nil {
qty = decimal.NewFromInt(*monthlyHours)
qty = decimal.NewFromFloat(*monthlyHours)
}

return &schema.CostComponent{
Expand Down
4 changes: 2 additions & 2 deletions internal/providers/terraform/azure/virtual_machine.go
Expand Up @@ -32,9 +32,9 @@ func NewAzureRMVirtualMachine(d *schema.ResourceData, u *schema.UsageData) *sche
os = "Windows"
}

var monthlyHours *int64 = nil
var monthlyHours *float64 = nil
if u != nil {
monthlyHours = u.GetInt("monthly_hrs")
monthlyHours = u.GetFloat("monthly_hrs")
}

if strings.ToLower(os) == "windows" {
Expand Down
10 changes: 5 additions & 5 deletions internal/providers/terraform/azure/windows_virtual_machine.go
Expand Up @@ -24,9 +24,9 @@ func NewAzureRMWindowsVirtualMachine(d *schema.ResourceData, u *schema.UsageData
instanceType := d.Get("size").String()
licenseType := d.Get("license_type").String()

var monthlyHours *int64 = nil
var monthlyHours *float64 = nil
if u != nil {
monthlyHours = u.GetInt("monthly_hrs")
monthlyHours = u.GetFloat("monthly_hrs")
}

costComponents := []*schema.CostComponent{windowsVirtualMachineCostComponent(region, instanceType, licenseType, monthlyHours)}
Expand All @@ -49,7 +49,7 @@ func NewAzureRMWindowsVirtualMachine(d *schema.ResourceData, u *schema.UsageData
}
}

func windowsVirtualMachineCostComponent(region string, instanceType string, licenseType string, monthlyHours *int64) *schema.CostComponent {
func windowsVirtualMachineCostComponent(region string, instanceType string, licenseType string, monthlyHours *float64) *schema.CostComponent {
purchaseOption := "Consumption"
purchaseOptionLabel := "pay as you go"

Expand All @@ -66,9 +66,9 @@ func windowsVirtualMachineCostComponent(region string, instanceType string, lice
purchaseOptionLabel = "hybrid benefit"
}

qty := decimal.NewFromInt(730)
qty := decimal.NewFromFloat(730)
if monthlyHours != nil {
qty = decimal.NewFromInt(*monthlyHours)
qty = decimal.NewFromFloat(*monthlyHours)
}

return &schema.CostComponent{
Expand Down
28 changes: 14 additions & 14 deletions internal/resources/aws/instance.go
Expand Up @@ -33,13 +33,13 @@ type Instance struct {
EBSBlockDevices []*EBSVolume

// "usage" args
OperatingSystem *string `infracost_usage:"operating_system"`
ReservedInstanceType *string `infracost_usage:"reserved_instance_type"`
ReservedInstanceTerm *string `infracost_usage:"reserved_instance_term"`
ReservedInstancePaymentOption *string `infracost_usage:"reserved_instance_payment_option"`
MonthlyCPUCreditHours *int64 `infracost_usage:"monthly_cpu_credit_hrs"`
VCPUCount *int64 `infracost_usage:"vcpu_count"`
MonthlyHours *int64 `infracost_usage:"monthly_hrs"`
OperatingSystem *string `infracost_usage:"operating_system"`
ReservedInstanceType *string `infracost_usage:"reserved_instance_type"`
ReservedInstanceTerm *string `infracost_usage:"reserved_instance_term"`
ReservedInstancePaymentOption *string `infracost_usage:"reserved_instance_payment_option"`
MonthlyCPUCreditHours *int64 `infracost_usage:"monthly_cpu_credit_hrs"`
VCPUCount *int64 `infracost_usage:"vcpu_count"`
MonthlyHours *float64 `infracost_usage:"monthly_hrs"`
}

var InstanceUsageSchema = []*schema.UsageItem{
Expand All @@ -49,7 +49,7 @@ var InstanceUsageSchema = []*schema.UsageItem{
{Key: "reserved_instance_payment_option", DefaultValue: "", ValueType: schema.String},
{Key: "monthly_cpu_credit_hrs", DefaultValue: 0, ValueType: schema.Int64},
{Key: "vcpu_count", DefaultValue: 0, ValueType: schema.Int64},
{Key: "monthly_hrs", DefaultValue: 730, ValueType: schema.Int64},
{Key: "monthly_hrs", DefaultValue: 730, ValueType: schema.Float64},
}

func (a *Instance) PopulateUsage(u *schema.UsageData) {
Expand Down Expand Up @@ -175,9 +175,9 @@ func (a *Instance) computeCostComponent() *schema.CostComponent {
purchaseOptionLabel = "reserved"
}

qty := decimal.NewFromInt(730)
qty := decimal.NewFromFloat(730)
if a.MonthlyHours != nil {
qty = decimal.NewFromInt(*a.MonthlyHours)
qty = decimal.NewFromFloat(*a.MonthlyHours)
}

return &schema.CostComponent{
Expand Down Expand Up @@ -215,9 +215,9 @@ func (a *Instance) ebsOptimizedCostComponent() *schema.CostComponent {
* > The hourly price for EBS-optimized instances is in addition to the hourly usage fee
* > for supported instance types.
*/
qty := decimal.NewFromInt(730)
qty := decimal.NewFromFloat(730)
if a.MonthlyHours != nil {
qty = decimal.NewFromInt(*a.MonthlyHours)
qty = decimal.NewFromFloat(*a.MonthlyHours)
}

return &schema.CostComponent{
Expand Down Expand Up @@ -266,9 +266,9 @@ func (a *Instance) elasticInferenceAcceleratorCostComponent() *schema.CostCompon
* > With Amazon Elastic Inference, you pay only for the accelerator hours you use.
* > There are no upfront costs or minimum fees.
*/
qty := decimal.NewFromInt(730)
qty := decimal.NewFromFloat(730)
if a.MonthlyHours != nil {
qty = decimal.NewFromInt(*a.MonthlyHours)
qty = decimal.NewFromFloat(*a.MonthlyHours)
}

return &schema.CostComponent{
Expand Down
12 changes: 6 additions & 6 deletions internal/resources/google/compute_cost_component_helpers.go
Expand Up @@ -25,7 +25,7 @@ type ContainerNodeConfig struct {
}

// computeCostComponent returns a cost component for Compute instance usage.
func computeCostComponent(region, machineType string, purchaseOption string, instanceCount int64, monthlyHours *int64) *schema.CostComponent {
func computeCostComponent(region, machineType string, purchaseOption string, instanceCount int64, monthlyHours *float64) *schema.CostComponent {
sustainedUseDiscount := 0.0
if strings.ToLower(purchaseOption) == "on_demand" {
switch strings.ToLower(strings.Split(machineType, "-")[0]) {
Expand All @@ -36,9 +36,9 @@ func computeCostComponent(region, machineType string, purchaseOption string, ins
}
}

qty := decimal.NewFromInt(730)
qty := decimal.NewFromFloat(730)
if monthlyHours != nil {
qty = decimal.NewFromInt(*monthlyHours)
qty = decimal.NewFromFloat(*monthlyHours)
}

return &schema.CostComponent{
Expand Down Expand Up @@ -131,7 +131,7 @@ func computeDiskCostComponent(region string, diskType string, diskSize float64,

// guestAcceleratorCostComponent returns a cost component for Guest Accelerator
// usage for Compute resources.
func guestAcceleratorCostComponent(region string, purchaseOption string, guestAcceleratorType string, guestAcceleratorCount int64, instanceCount int64, monthlyHours *int64) *schema.CostComponent {
func guestAcceleratorCostComponent(region string, purchaseOption string, guestAcceleratorType string, guestAcceleratorCount int64, instanceCount int64, monthlyHours *float64) *schema.CostComponent {
var (
name string
descPrefix string
Expand Down Expand Up @@ -170,9 +170,9 @@ func guestAcceleratorCostComponent(region string, purchaseOption string, guestAc
sustainedUseDiscount = 0.3
}

qty := decimal.NewFromInt(730)
qty := decimal.NewFromFloat(730)
if monthlyHours != nil {
qty = decimal.NewFromInt(*monthlyHours)
qty = decimal.NewFromFloat(*monthlyHours)
}
qty = qty.Mul(count)

Expand Down
4 changes: 2 additions & 2 deletions internal/resources/google/compute_instance.go
Expand Up @@ -19,12 +19,12 @@ type ComputeInstance struct {
ScratchDisks int
GuestAccelerators []*ComputeGuestAccelerator

MonthlyHours *int64 `infracost_usage:"monthly_hrs"`
MonthlyHours *float64 `infracost_usage:"monthly_hrs"`
}

// ComputeInstanceUsageSchema defines a list which represents the usage schema of ComputeInstance.
var ComputeInstanceUsageSchema = []*schema.UsageItem{
{Key: "monthly_hrs", DefaultValue: 730, ValueType: schema.Int64},
{Key: "monthly_hrs", DefaultValue: 730, ValueType: schema.Float64},
}

// PopulateUsage parses the u schema.UsageData into the ComputeInstance.
Expand Down
10 changes: 5 additions & 5 deletions internal/usage/testdata/usage_file/usage_file.golden
Expand Up @@ -11,15 +11,15 @@ resource_usage:
# reserved_instance_payment_option: "" # Payment option for Reserved Instances, can be: no_upfront, partial_upfront, all_upfront.
# monthly_cpu_credit_hrs: 0 # Number of hours in the month where the instance is expected to burst. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# vcpu_count: 0 # Number of the vCPUs for the instance type. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# monthly_hrs: 730 # Monthly number of hours the instance ran for.
# monthly_hrs: 730.0 # Monthly number of hours the instance ran for.
aws_instance.instance_counted[0]:
operating_system: linux # Override the operating system of the instance, can be: linux, windows, suse, rhel.
# reserved_instance_type: "" # Offering class for Reserved Instances, can be: convertible, standard.
# reserved_instance_term: "" # Term for Reserved Instances, can be: 1_year, 3_year.
# reserved_instance_payment_option: "" # Payment option for Reserved Instances, can be: no_upfront, partial_upfront, all_upfront.
# monthly_cpu_credit_hrs: 0 # Number of hours in the month where the instance is expected to burst. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# vcpu_count: 0 # Number of the vCPUs for the instance type. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# monthly_hrs: 730 # Monthly number of hours the instance ran for.
# monthly_hrs: 730.0 # Monthly number of hours the instance ran for.
##
## The following usage values are all commented-out, you can uncomment resources and customize as needed.
##
Expand All @@ -30,15 +30,15 @@ resource_usage:
# reserved_instance_payment_option: "" # Payment option for Reserved Instances, can be: no_upfront, partial_upfront, all_upfront.
# monthly_cpu_credit_hrs: 0 # Number of hours in the month where the instance is expected to burst. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# vcpu_count: 0 # Number of the vCPUs for the instance type. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# monthly_hrs: 730 # Monthly number of hours the instance ran for.
# monthly_hrs: 730.0 # Monthly number of hours the instance ran for.
aws_instance.with_usage:
operating_system: windows # Override the operating system of the instance, can be: linux, windows, suse, rhel.
reserved_instance_type: standard # Offering class for Reserved Instances, can be: convertible, standard.
reserved_instance_term: 1_year # Term for Reserved Instances, can be: 1_year, 3_year.
reserved_instance_payment_option: all_upfront # Payment option for Reserved Instances, can be: no_upfront, partial_upfront, all_upfront.
# monthly_cpu_credit_hrs: 0 # Number of hours in the month where the instance is expected to burst. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# vcpu_count: 0 # Number of the vCPUs for the instance type. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# monthly_hrs: 730 # Monthly number of hours the instance ran for.
# monthly_hrs: 730.0 # Monthly number of hours the instance ran for.
aws_s3_bucket.with_usage:
object_tags: 10000000 # This comment shouldn't be overwritten
# standard:
Expand Down Expand Up @@ -208,7 +208,7 @@ resource_usage:
# reserved_instance_payment_option: "" # Payment option for Reserved Instances, can be: no_upfront, partial_upfront, all_upfront.
# monthly_cpu_credit_hrs: 0 # Number of hours in the month where the instance is expected to burst. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# vcpu_count: 0 # Number of the vCPUs for the instance type. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# monthly_hrs: 730 # Monthly number of hours the instance ran for.
# monthly_hrs: 730.0 # Monthly number of hours the instance ran for.
# aws_s3_bucket.no_usage:
# object_tags: 0 # Total object tags. Only for AWS provider V3.
# standard:
Expand Down
Expand Up @@ -64,7 +64,7 @@ version: 0.1
# reserved_instance_payment_option: "" # Payment option for Reserved Instances, can be: no_upfront, partial_upfront, all_upfront.
# monthly_cpu_credit_hrs: 0 # Number of hours in the month where the instance is expected to burst. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# vcpu_count: 0 # Number of the vCPUs for the instance type. Only applicable with t2, t3 & t4 Instance types. T2 requires credit_specification to be unlimited.
# monthly_hrs: 730 # Monthly number of hours the instance ran for.
# monthly_hrs: 730.0 # Monthly number of hours the instance ran for.
# aws_s3_bucket.no_usage:
# object_tags: 0 # Total object tags. Only for AWS provider V3.
# standard:
Expand Down

0 comments on commit a207a1f

Please sign in to comment.