-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaws.go
233 lines (201 loc) · 7.46 KB
/
aws.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package aws
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/bytebase/dbcost/client"
)
// Client is the client struct
type Client struct{}
var _ client.Client = (*Client)(nil)
// NewClient return a client
func NewClient() *Client {
return &Client{}
}
// pricing is the api message for AWS pricing .json file
type pricing struct {
Product interface{} `json:"products"`
Term interface{} `json:"terms"`
}
// EngineType is the engine type specified in AWS api message.
// we implement its String() method to convert it into the type stored in ours.
type EngineType string
const (
engineTypeMySQL = "MySQL"
engineTypePostgreSQL = "PostgreSQL"
// TODO: Add support to SQLSERVER & ORACLE
// engineTypeSQLServer = "SQL Server"
// engineTypeOracle = "Oracle"
engineTypeUnknown = "UNKNOWN"
)
func (e EngineType) String() string {
switch e {
case engineTypeMySQL:
return "MYSQL"
case engineTypePostgreSQL:
return "POSTGRES"
}
return "UNKNOWN"
}
// instance is the api message of the Instance for AWS specifically
type instance struct {
ID string
// The tag here does not follow small-camel naming style, it is intended for the AWS name it this way.
ServiceCode string `json:"servicecode"`
Location string `json:"location"`
RegionCode string `json:"regionCode"`
Type string `json:"instanceType"`
InstanceFamily string `json:"instanceFamily"`
// Noted that this is a api mmessage from AWS, so we still use vcpu for unmarshaling the info,
// But in our systems, we use CPU over VCPU.
CPU string `json:"vcpu"`
Memory string `json:"memory"`
PhysicalProcessor string `json:"physicalProcessor"`
NetworkPerformance string `json:"networkPerformance"`
DeploymentOption string `json:"deploymentOption"`
DatabaseEngine EngineType `json:"databaseEngine"`
}
// productEntry is the entry of the instance info
type productEntry struct {
ID string `json:"sku"`
ProductFamily string `json:"productFamily"`
Attributes instance `json:"attributes"`
}
// instanceRecord is the Record of the instance info
type instanceRecord map[string]productEntry
// priceDimensionRaw is the raw dimension struct marshaled from the aws json file
type priceDimensionRaw struct {
Description string `json:"description"`
Unit string `json:"unit"`
PricePerUnit map[client.Currency]string `json:"pricePerUnit"`
}
// priceRaw is the raw price struct marshaled from the aws json file
type priceRaw struct {
Dimension map[string]priceDimensionRaw `json:"priceDimensions"`
Term *client.ChargePayload `json:"termAttributes"`
}
// InfoEndPoint is the instance info endpoint
// More infomation, see: https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/reading-an-offer.html
const InfoEndPoint = "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonRDS/current/index.json"
// GetOffer returns the offers provided by AWS.
func (c *Client) GetOffer() ([]*client.Offer, error) {
res, err := http.Get(InfoEndPoint)
if err != nil {
return nil, fmt.Errorf("Fail to fetch the info file, [internal]: %v", err)
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("An http error occur , [internal]: %v", err)
}
data, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Fail when reading response data , [internal]: %v", err)
}
rawData := &pricing{}
if err := json.Unmarshal(data, rawData); err != nil {
return nil, fmt.Errorf("Fail when unmarshaling response data , [internal]: %v", err)
}
offerList, err := extractOffer(rawData)
if err != nil {
return nil, fmt.Errorf("Fail when extrating offer, [internal]: %v", err)
}
productBytes, err := json.Marshal(rawData.Product)
if err != nil {
return nil, fmt.Errorf("Fail to unmarshal the result, [internal]: %v", err)
}
productsDecoder := json.NewDecoder(bytes.NewReader(productBytes))
var rawEntryList instanceRecord
if err := productsDecoder.Decode(&rawEntryList); err != nil {
return nil, fmt.Errorf("Fail to decode the result, [internal]: %v", err)
}
fillInstancePayload(rawEntryList, offerList)
return offerList, nil
}
// extractOffer extracts the client.offer from the rawData.
func extractOffer(rawData *pricing) ([]*client.Offer, error) {
bytePrice, err := json.Marshal(rawData.Term)
if err != nil {
return nil, fmt.Errorf("Fail to unmarshal the result, [internal]: %v", err)
}
offerDecoder := json.NewDecoder(bytes.NewReader(bytePrice))
var rawEntry map[client.OfferType]map[string]map[string]priceRaw
if err := offerDecoder.Decode(&rawEntry); err != nil {
return nil, fmt.Errorf("Fail to decode the result, [internal]: %v", err)
}
var offerList []*client.Offer
incrID := 0
// rawEntry has two charge types, reserved and on-demand.
for chargeType, instanceOfferList := range rawEntry {
// we use skuID here to track the instance relevant to this offer
for instanceSKU, _offerList := range instanceOfferList {
for instanceTermCode, rawOffer := range _offerList {
offer := &client.Offer{
ID: incrID,
// e.g. 9QH3PUGXCYKNCYPB
SKU: instanceSKU,
// e.g. 9QH3PUGXCYKNCYPB.HU7G6KETJZ
TermCode: instanceTermCode,
// AWS only offer instance-wise product
OfferType: client.OfferTypeInstance,
ChargeType: client.ChargeType(chargeType),
ChargePayload: rawOffer.Term,
}
// an offer may have differnet charging dimension, say upfront fee and it relevant fee charged hourly.
for _, dimension := range rawOffer.Dimension {
USDFloat, err := strconv.ParseFloat(dimension.PricePerUnit[client.CurrencyUSD], 64)
if err != nil {
return nil, fmt.Errorf("Fail to parse the price to type FLOAT64, value: %v, [internal]: %v", dimension.PricePerUnit[client.CurrencyUSD], err)
}
if dimension.Unit == "Quantity" {
offer.CommitmentUSD = USDFloat
} else {
offer.HourlyUSD = USDFloat
}
}
incrID++
offerList = append(offerList, offer)
}
}
}
return offerList, nil
}
func fillInstancePayload(instanceRecord instanceRecord, offerList []*client.Offer) {
// There may be many offers that are bind to the same instance with the same SKU.
offerMap := make(map[string][]*client.Offer)
for _, offer := range offerList {
offerMap[offer.SKU] = append(offerMap[offer.SKU], offer)
}
for instanceSKU, entry := range instanceRecord {
if entry.ProductFamily != "Database Instance" /* filter non-db instance */ ||
entry.Attributes.DeploymentOption != "Single-AZ" /* filter multi-region deployment */ {
continue
}
engineType := entry.Attributes.DatabaseEngine.String()
if engineType == engineTypeUnknown {
continue
}
instance := &client.OfferInstancePayload{
Type: entry.Attributes.Type,
InstanceFamily: entry.Attributes.InstanceFamily,
CPU: entry.Attributes.CPU,
Memory: strings.ReplaceAll(entry.Attributes.Memory, "GiB", ""),
PhysicalProcessor: entry.Attributes.PhysicalProcessor,
NetworkPerformance: entry.Attributes.NetworkPerformance,
DatabaseEngine: client.EngineType(engineType),
}
if _, ok := offerMap[instanceSKU]; ok {
for _, offer := range offerMap[instanceSKU] {
if entry.Attributes.RegionCode != "" {
offer.RegionList = []string{entry.Attributes.RegionCode}
} else {
// When we encounter empty region code, use location string directly.
offer.RegionList = []string{entry.Attributes.Location}
}
offer.InstancePayload = instance
}
}
}
}