-
Notifications
You must be signed in to change notification settings - Fork 7
/
pricing.go
114 lines (99 loc) · 3.45 KB
/
pricing.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"regexp"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/pricing"
"github.com/aws/aws-sdk-go-v2/service/pricing/types"
)
//Pricing is a data structure we use to unmarshal the data returned by the Pricing API into a native Golang object.
type Pricing struct {
Product struct {
ProductFamily string `json:"productFamily"`
Attributes struct {
StorageMedia string `json:"storageMedia"`
MaxThroughputvolume string `json:"maxThroughputvolume"`
VolumeType string `json:"volumeType"`
MaxIopsvolume string `json:"maxIopsvolume"`
Servicecode string `json:"servicecode"`
Usagetype string `json:"usagetype"`
LocationType string `json:"locationType"`
VolumeAPIName string `json:"volumeApiName"`
Location string `json:"location"`
Servicename string `json:"servicename"`
MaxVolumeSize string `json:"maxVolumeSize"`
Operation string `json:"operation"`
Group string `json:"group"`
} `json:"attributes"`
Sku string `json:"sku"`
} `json:"product"`
ServiceCode string `json:"serviceCode"`
Terms struct {
OnDemand struct {
SKU struct {
PriceDimensions struct {
Dimension struct {
Unit string `json:"unit"`
EndRange string `json:"endRange"`
Description string `json:"description"`
AppliesTo []interface{} `json:"appliesTo"`
RateCode string `json:"rateCode"`
BeginRange string `json:"beginRange"`
PricePerUnit struct {
USD string `json:"USD"`
} `json:"pricePerUnit"`
} `json:"Dimension"`
} `json:"priceDimensions"`
Sku string `json:"sku"`
EffectiveDate time.Time `json:"effectiveDate"`
OfferTermCode string `json:"offerTermCode"`
TermAttributes struct {
} `json:"termAttributes"`
} `json:"SKU"`
} `json:"OnDemand"`
} `json:"terms"`
Version string `json:"version"`
PublicationDate time.Time `json:"publicationDate"`
}
func getPricingData(filters []types.Filter) ([]Pricing, error) {
var priceList []Pricing
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1"))
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
// Create a Pricing client with additional configuration
svc := pricing.NewFromConfig(cfg)
input := &pricing.GetProductsInput{
ServiceCode: aws.String("AmazonEC2"),
Filters: filters,
}
paginator := pricing.NewGetProductsPaginator(svc, input)
// Iterate through the Amazon S3 object pages.
for paginator.HasMorePages() {
// next page takes a context
page, err := paginator.NextPage(context.TODO())
if err != nil {
return nil, fmt.Errorf("failed to get a page, %w", err)
}
debug.Println("page contents: ", page.PriceList)
var p Pricing
for _, item := range page.PriceList {
dimension := regexp.MustCompile(`\"\w{16,}\.\w{10,}\.\w{10,}\"`)
itemDimension := dimension.ReplaceAll([]byte(item), []byte("\"Dimension\""))
sku := regexp.MustCompile(`\"\w{16,}\.\w{10,}\"`)
itemSKU := sku.ReplaceAll(itemDimension, []byte("\"SKU\""))
// fmt.Printf("%v", string(itemSKU))
err := json.Unmarshal(itemSKU, &p)
if err != nil {
fmt.Println("error: ", err.Error())
}
priceList = append(priceList, p)
}
}
return priceList, nil
}