-
Notifications
You must be signed in to change notification settings - Fork 523
/
rds_cluster_instance.go
169 lines (148 loc) 路 5.96 KB
/
rds_cluster_instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package aws
import (
"fmt"
"strings"
"github.com/shopspring/decimal"
"github.com/infracost/infracost/internal/logging"
"github.com/infracost/infracost/internal/resources"
"github.com/infracost/infracost/internal/schema"
)
type RDSClusterInstance struct {
Address string
Region string
InstanceClass string
Engine string
Version string
IOOptimized bool
PerformanceInsightsEnabled bool
PerformanceInsightsLongTermRetention bool
MonthlyCPUCreditHrs *int64 `infracost_usage:"monthly_cpu_credit_hrs"`
VCPUCount *int64 `infracost_usage:"vcpu_count"`
MonthlyAdditionalPerformanceInsightsRequests *int64 `infracost_usage:"monthly_additional_performance_insights_requests"`
ReservedInstanceTerm *string `infracost_usage:"reserved_instance_term"`
ReservedInstancePaymentOption *string `infracost_usage:"reserved_instance_payment_option"`
}
func (r *RDSClusterInstance) CoreType() string {
return "RDSClusterInstance"
}
func (r *RDSClusterInstance) UsageSchema() []*schema.UsageItem {
return []*schema.UsageItem{
{Key: "monthly_cpu_credit_hrs", ValueType: schema.Int64, DefaultValue: 0},
{Key: "vcpu_count", ValueType: schema.Int64, DefaultValue: 0},
{Key: "monthly_additional_performance_insights_requests", ValueType: schema.Int64, DefaultValue: 0},
{Key: "reserved_instance_term", DefaultValue: "", ValueType: schema.String},
{Key: "reserved_instance_payment_option", DefaultValue: "", ValueType: schema.String},
}
}
func (r *RDSClusterInstance) PopulateUsage(u *schema.UsageData) {
resources.PopulateArgsWithUsage(r, u)
}
func (r *RDSClusterInstance) BuildResource() *schema.Resource {
databaseEngine := r.databaseEngineValue()
purchaseOptionLabel := "on-demand"
priceFilter := &schema.PriceFilter{
PurchaseOption: strPtr("on_demand"),
}
var err error
if r.ReservedInstanceTerm != nil {
resolver := &rdsReservationResolver{
term: strVal(r.ReservedInstanceTerm),
paymentOption: strVal(r.ReservedInstancePaymentOption),
}
priceFilter, err = resolver.PriceFilter()
if err != nil {
logging.Logger.Warn().Msgf(err.Error())
}
purchaseOptionLabel = "reserved"
}
// Example usage types for Aurora
// InstanceUsage:db.t3.medium
// InstanceUsageIOOptimized:db.t3.medium
// EU-InstanceUsage:db.t3.medium
// EU-InstanceUsageIOOptimized:db.t3.medium
usageTypeFilter := "/InstanceUsage:/"
if r.IOOptimized {
usageTypeFilter = "/InstanceUsageIOOptimized:/"
}
costComponents := []*schema.CostComponent{
{
Name: fmt.Sprintf("Database instance (%s, %s)", purchaseOptionLabel, r.InstanceClass),
Unit: "hours",
UnitMultiplier: decimal.NewFromInt(1),
HourlyQuantity: decimalPtr(decimal.NewFromInt(1)),
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(r.Region),
Service: strPtr("AmazonRDS"),
ProductFamily: strPtr("Database Instance"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "instanceType", Value: strPtr(r.InstanceClass)},
{Key: "databaseEngine", Value: strPtr(databaseEngine)},
{Key: "usagetype", ValueRegex: strPtr(usageTypeFilter)},
},
},
PriceFilter: priceFilter,
},
}
if instanceFamily := getBurstableInstanceFamily([]string{"db.t3", "db.t4g"}, r.InstanceClass); instanceFamily != "" {
instanceCPUCreditHours := decimal.Zero
if r.MonthlyCPUCreditHrs != nil {
instanceCPUCreditHours = decimal.NewFromInt(*r.MonthlyCPUCreditHrs)
}
instanceVCPUCount := decimal.Zero
if r.VCPUCount != nil {
// VCPU count has been set explicitly
instanceVCPUCount = decimal.NewFromInt(*r.VCPUCount)
} else if count, ok := InstanceTypeToVCPU[strings.TrimPrefix(r.InstanceClass, "db.")]; ok {
// We were able to lookup thing VCPU count
instanceVCPUCount = decimal.NewFromInt(count)
}
if instanceCPUCreditHours.GreaterThan(decimal.NewFromInt(0)) {
cpuCreditQuantity := instanceVCPUCount.Mul(instanceCPUCreditHours)
costComponents = append(costComponents, r.cpuCreditsCostComponent(databaseEngine, instanceFamily, cpuCreditQuantity))
}
}
if r.PerformanceInsightsEnabled {
if r.PerformanceInsightsLongTermRetention {
costComponents = append(costComponents, performanceInsightsLongTermRetentionCostComponent(r.Region, r.InstanceClass))
}
if r.MonthlyAdditionalPerformanceInsightsRequests == nil || *r.MonthlyAdditionalPerformanceInsightsRequests > 0 {
costComponents = append(costComponents,
performanceInsightsAPIRequestCostComponent(r.Region, r.MonthlyAdditionalPerformanceInsightsRequests))
}
}
extendedSupport := extendedSupportCostComponent(r.Version, r.Region, r.Engine, r.InstanceClass)
if extendedSupport != nil {
costComponents = append(costComponents, extendedSupport)
}
return &schema.Resource{
Name: r.Address,
CostComponents: costComponents,
UsageSchema: r.UsageSchema(),
}
}
func (r *RDSClusterInstance) databaseEngineValue() string {
if r.Engine == "aurora-postgresql" {
return "Aurora PostgreSQL"
}
return "Aurora MySQL"
}
func (r *RDSClusterInstance) cpuCreditsCostComponent(databaseEngine, instanceFamily string, vCPUCount decimal.Decimal) *schema.CostComponent {
return &schema.CostComponent{
Name: "CPU credits",
Unit: "vCPU-hours",
UnitMultiplier: decimal.NewFromInt(1),
MonthlyQuantity: &vCPUCount,
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(r.Region),
Service: strPtr("AmazonRDS"),
ProductFamily: strPtr("CPU Credits"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "databaseEngine", Value: strPtr(databaseEngine)},
{Key: "usagetype", ValueRegex: regexPtr("CPUCredits:" + instanceFamily + "$")},
},
},
UsageBased: true,
}
}