/
apigatewayv2_api.go
185 lines (146 loc) 路 6.19 KB
/
apigatewayv2_api.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package aws
import (
"fmt"
"strings"
"github.com/infracost/infracost/internal/resources"
"github.com/infracost/infracost/internal/schema"
"github.com/shopspring/decimal"
"github.com/infracost/infracost/internal/usage"
)
type APIGatewayV2API struct {
Address string
Region string
ProtocolType string
MessageSizeKB *int64 `infracost_usage:"message_size_kb"`
MonthlyConnectionMins *int64 `infracost_usage:"monthly_connection_mins"`
MonthlyRequests *int64 `infracost_usage:"monthly_requests"`
RequestSizeKB *int64 `infracost_usage:"request_size_kb"`
MonthlyMessages *int64 `infracost_usage:"monthly_messages"`
}
func (r *APIGatewayV2API) CoreType() string {
return "APIGatewayV2API"
}
func (r *APIGatewayV2API) UsageSchema() []*schema.UsageItem {
return []*schema.UsageItem{
{Key: "message_size_kb", ValueType: schema.Int64, DefaultValue: 0},
{Key: "monthly_connection_mins", ValueType: schema.Int64, DefaultValue: 0},
{Key: "monthly_requests", ValueType: schema.Int64, DefaultValue: 0},
{Key: "request_size_kb", ValueType: schema.Int64, DefaultValue: 0},
{Key: "monthly_messages", ValueType: schema.Int64, DefaultValue: 0},
}
}
func (r *APIGatewayV2API) PopulateUsage(u *schema.UsageData) {
resources.PopulateArgsWithUsage(r, u)
}
func (r *APIGatewayV2API) BuildResource() *schema.Resource {
var costComponents []*schema.CostComponent
if strings.ToLower(r.ProtocolType) == "websocket" {
costComponents = r.websocketAPICostComponent()
}
if strings.ToLower(r.ProtocolType) == "http" {
costComponents = r.httpAPICostComponent()
}
return &schema.Resource{
Name: r.Address,
CostComponents: costComponents,
UsageSchema: r.UsageSchema(),
}
}
func (r *APIGatewayV2API) httpAPICostComponent() []*schema.CostComponent {
var monthlyRequests *decimal.Decimal
requestSize := decimal.NewFromInt(512)
billableRequestSize := decimal.NewFromInt(512)
httpAPITiers := []int{300000000}
costComponents := []*schema.CostComponent{}
if r.MonthlyRequests != nil {
monthlyRequests = decimalPtr(decimal.NewFromInt(*r.MonthlyRequests))
if r.RequestSizeKB != nil {
requestSize = decimal.NewFromInt(*r.RequestSizeKB)
}
if requestSize.GreaterThan(billableRequestSize) {
monthlyRequests = calculateBillableRequests(&requestSize, &billableRequestSize, monthlyRequests)
}
apiRequestQuantities := usage.CalculateTierBuckets(*monthlyRequests, httpAPITiers)
costComponents = append(costComponents, r.httpCostComponent("Requests (first 300M)", "0", &apiRequestQuantities[0]))
if apiRequestQuantities[1].GreaterThan(decimal.NewFromInt(0)) {
costComponents = append(costComponents, r.httpCostComponent("Requests (over 300M)", "300000000", &apiRequestQuantities[1]))
}
} else {
costComponents = append(costComponents, r.httpCostComponent("Requests (first 300M)", "0", nil))
}
return costComponents
}
func (r *APIGatewayV2API) websocketAPICostComponent() []*schema.CostComponent {
var monthlyMessages *decimal.Decimal
var monthlyConnectionMinutes *decimal.Decimal
messageSize := decimal.NewFromInt(32)
billableRequestSize := decimal.NewFromInt(32)
websocketAPITiers := []int{1000000000}
costComponents := []*schema.CostComponent{}
if r.MonthlyMessages != nil {
monthlyMessages = decimalPtr(decimal.NewFromInt(*r.MonthlyMessages))
if r.MessageSizeKB != nil {
messageSize = decimal.NewFromInt(*r.MessageSizeKB)
}
if messageSize.GreaterThan(billableRequestSize) {
monthlyMessages = calculateBillableRequests(&messageSize, &billableRequestSize, monthlyMessages)
}
apiRequestQuantities := usage.CalculateTierBuckets(*monthlyMessages, websocketAPITiers)
costComponents = append(costComponents, r.websocketCostComponent("messages", "ApiGatewayMessage", "Messages (first 1B)", "0", &apiRequestQuantities[0]))
if apiRequestQuantities[1].GreaterThan(decimal.NewFromInt(0)) {
costComponents = append(costComponents, r.websocketCostComponent("messages", "ApiGatewayMessage", "Messages (over 1B)", "1000000000", &apiRequestQuantities[1]))
}
} else {
costComponents = append(costComponents, r.websocketCostComponent("messages", "ApiGatewayMessage", "Messages (first 1B)", "0", nil))
}
if r.MonthlyConnectionMins != nil {
monthlyConnectionMinutes = decimalPtr(decimal.NewFromInt(*r.MonthlyConnectionMins))
}
costComponents = append(costComponents, r.websocketCostComponent("minutes", "ApiGatewayMinute", "Connection duration", "0", monthlyConnectionMinutes))
return costComponents
}
func calculateBillableRequests(requestSize *decimal.Decimal, billableRequestSize *decimal.Decimal, requests *decimal.Decimal) *decimal.Decimal {
return decimalPtr(requests.Mul(requestSize.Div(*billableRequestSize).Ceil()))
}
func (r *APIGatewayV2API) httpCostComponent(displayName string, usageTier string, monthlyQuantity *decimal.Decimal) *schema.CostComponent {
return &schema.CostComponent{
Name: displayName,
Unit: "1M requests",
UnitMultiplier: decimal.NewFromInt(1000000),
MonthlyQuantity: monthlyQuantity,
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(r.Region),
Service: strPtr("AmazonApiGateway"),
ProductFamily: strPtr("API Calls"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "usagetype", ValueRegex: strPtr("/ApiGatewayHttpRequest/")},
},
},
PriceFilter: &schema.PriceFilter{
StartUsageAmount: strPtr(usageTier),
},
UsageBased: true,
}
}
func (r *APIGatewayV2API) websocketCostComponent(unit string, usageType string, displayName string, usageTier string, monthlyQuantity *decimal.Decimal) *schema.CostComponent {
return &schema.CostComponent{
Name: displayName,
Unit: "1M " + unit,
UnitMultiplier: decimal.NewFromInt(1000000),
MonthlyQuantity: monthlyQuantity,
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(r.Region),
Service: strPtr("AmazonApiGateway"),
ProductFamily: strPtr("WebSocket"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "usagetype", ValueRegex: strPtr(fmt.Sprintf("/%s/i", usageType))},
},
},
PriceFilter: &schema.PriceFilter{
StartUsageAmount: strPtr(usageTier),
},
UsageBased: true,
}
}