-
Notifications
You must be signed in to change notification settings - Fork 523
/
lb.go
153 lines (132 loc) 路 4.81 KB
/
lb.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
package aws
import (
"github.com/infracost/infracost/internal/resources"
"github.com/infracost/infracost/internal/schema"
"strings"
"github.com/shopspring/decimal"
)
type LB struct {
Address string
LoadBalancerType string
Region string
RuleEvaluations *int64 `infracost_usage:"rule_evaluations"`
NewConnections *int64 `infracost_usage:"new_connections"`
ActiveConnections *int64 `infracost_usage:"active_connections"`
ProcessedBytesGB *float64 `infracost_usage:"processed_bytes_gb"`
}
var LBUsageSchema = []*schema.UsageItem{
{Key: "rule_evaluations", ValueType: schema.Int64, DefaultValue: 0},
{Key: "new_connections", ValueType: schema.Int64, DefaultValue: 0},
{Key: "active_connections", ValueType: schema.Int64, DefaultValue: 0},
{Key: "processed_bytes_gb", ValueType: schema.Float64, DefaultValue: 0},
}
func (r *LB) CoreType() string {
return "LB"
}
func (r *LB) UsageSchema() []*schema.UsageItem {
return LBUsageSchema
}
func (r *LB) PopulateUsage(u *schema.UsageData) {
resources.PopulateArgsWithUsage(r, u)
}
func (r *LB) BuildResource() *schema.Resource {
var maxLCU *decimal.Decimal
var newConnectionsLCU *decimal.Decimal
if r.NewConnections != nil {
newConnections := decimal.NewFromInt(*r.NewConnections)
newConnectionsLCU = decimalPtr(newConnections.Div(decimal.NewFromInt(100)))
maxLCU = newConnectionsLCU
}
var activeConnectionsLCU *decimal.Decimal
if r.ActiveConnections != nil {
activeConnections := decimal.NewFromInt(*r.ActiveConnections)
activeConnectionsLCU = decimalPtr(activeConnections.Div(decimal.NewFromInt(3000)))
maxLCU = decimalPtr(decimal.Max(*maxLCU, *activeConnectionsLCU))
}
var processedBytesLCU *decimal.Decimal
if r.ProcessedBytesGB != nil {
processedBytes := decimal.NewFromFloat(*r.ProcessedBytesGB)
processedBytesLCU = decimalPtr(processedBytes.Div(decimal.NewFromInt(1)))
maxLCU = decimalPtr(decimal.Max(*maxLCU, *processedBytesLCU))
}
var costComponents []*schema.CostComponent
if strings.ToLower(r.LoadBalancerType) == "application" {
var ruleEvaluationsLCU decimal.Decimal
if r.RuleEvaluations != nil && maxLCU != nil {
ruleEvaluations := decimal.NewFromInt(*r.RuleEvaluations)
ruleEvaluationsLCU = ruleEvaluations.Div(decimal.NewFromInt(1000))
maxLCU = decimalPtr(decimal.Max(*maxLCU, ruleEvaluationsLCU))
}
costComponents = r.applicationLBCostComponents(maxLCU)
} else {
costComponents = r.networkLBCostComponents(maxLCU)
}
return &schema.Resource{
Name: r.Address,
CostComponents: costComponents,
UsageSchema: r.UsageSchema(),
}
}
func (r *LB) applicationLBCostComponents(maxLCU *decimal.Decimal) []*schema.CostComponent {
productFamily := "Load Balancer-Application"
return []*schema.CostComponent{
{
Name: "Application load balancer",
Unit: "hours",
HourlyQuantity: decimalPtr(decimal.NewFromInt(1)),
UnitMultiplier: decimal.NewFromInt(1),
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(r.Region),
Service: strPtr("AWSELB"),
ProductFamily: strPtr("Load Balancer-Application"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "locationType", Value: strPtr("AWS Region")},
{Key: "usagetype", ValueRegex: regexPtr("^([A-Z]{3}\\d-|Global-|EU-)?LoadBalancerUsage$")},
},
},
},
r.capacityUnitsCostComponent(productFamily, maxLCU),
}
}
func (r *LB) networkLBCostComponents(maxLCU *decimal.Decimal) []*schema.CostComponent {
productFamily := "Load Balancer-Network"
return []*schema.CostComponent{
{
Name: "Network load balancer",
Unit: "hours",
HourlyQuantity: decimalPtr(decimal.NewFromInt(1)),
UnitMultiplier: decimal.NewFromInt(1),
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(r.Region),
Service: strPtr("AWSELB"),
ProductFamily: strPtr("Load Balancer-Network"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "locationType", Value: strPtr("AWS Region")},
{Key: "usagetype", ValueRegex: strPtr("/LoadBalancerUsage/")},
},
},
},
r.capacityUnitsCostComponent(productFamily, maxLCU),
}
}
func (r *LB) capacityUnitsCostComponent(productFamily string, maxLCU *decimal.Decimal) *schema.CostComponent {
return &schema.CostComponent{
Name: "Load balancer capacity units",
Unit: "LCU",
UnitMultiplier: schema.HourToMonthUnitMultiplier,
MonthlyQuantity: maxLCU,
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(r.Region),
Service: strPtr("AWSELB"),
ProductFamily: strPtr(productFamily),
AttributeFilters: []*schema.AttributeFilter{
{Key: "locationType", Value: strPtr("AWS Region")},
{Key: "usagetype", ValueRegex: strPtr("/LCUUsage/")},
},
},
UsageBased: true,
}
}