Skip to content

Instantly share code, notes, and snippets.

@owulveryck

owulveryck/main.go

Last active Jan 11, 2018
Embed
What would you like to do?
Quick'n'Dirty aws price parser
package main
import (
"encoding/gob"
"encoding/json"
"flag"
"log"
"net/http"
"os"
"time"
)
type product struct {
Sku string `json:"sku"`
ProductFamily string `json:"productFamily"`
Attributes struct {
Servicecode string `json:"servicecode"`
Location string `json:"location"`
LocationType string `json:"locationType"`
InstanceType string `json:"instanceType"`
CurrentGeneration string `json:"currentGeneration"`
InstanceFamily string `json:"instanceFamily"`
Vcpu string `json:"vcpu"`
PhysicalProcessor string `json:"physicalProcessor"`
ClockSpeed string `json:"clockSpeed"`
Memory string `json:"memory"`
Storage string `json:"storage"`
NetworkPerformance string `json:"networkPerformance"`
ProcessorArchitecture string `json:"processorArchitecture"`
Tenancy string `json:"tenancy"`
OperatingSystem string `json:"operatingSystem"`
LicenseModel string `json:"licenseModel"`
Usagetype string `json:"usagetype"`
Operation string `json:"operation"`
EnhancedNetworkingSupported string `json:"enhancedNetworkingSupported"`
PreInstalledSw string `json:"preInstalledSw"`
ProcessorFeatures string `json:"processorFeatures"`
} `json:"attributes"`
}
type priceDimension struct {
RateCode string `json:"rateCode"`
Description string `json:"description"`
BeginRange string `json:"beginRange"`
EndRange string `json:"endRange"`
Unit string `json:"unit"`
PricePerUnit struct {
USD string `json:"USD"`
} `json:"pricePerUnit"`
AppliesTo []interface{} `json:"appliesTo"`
}
type onDemand map[string]struct {
OfferTermCode string `json:"offerTermCode"`
Sku string `json:"sku"`
EffectiveDate time.Time `json:"effectiveDate"`
PriceDimensions map[string]priceDimension `json:"priceDimensions"`
TermAttributes struct {
LeaseContractLength string `json:"LeaseContractLength"`
OfferingClass string `json:"OfferingClass"`
PurchaseOption string `json:"PurchaseOption"`
} `json:"termAttributes"`
}
type reserved map[string]struct {
OfferTermCode string `json:"offerTermCode"`
Sku string `json:"sku"`
EffectiveDate time.Time `json:"effectiveDate"`
PriceDimensions map[string]priceDimension `json:"priceDimensions"`
TermAttributes struct {
LeaseContractLength string `json:"LeaseContractLength"`
OfferingClass string `json:"OfferingClass"`
PurchaseOption string `json:"PurchaseOption"`
} `json:"termAttributes"`
}
type aws struct {
FormatVersion string `json:"formatVersion"`
Disclaimer string `json:"disclaimer"`
OfferCode string `json:"offerCode"`
Version string `json:"version"`
PublicationDate time.Time `json:"publicationDate"`
Products map[string]*product `json:"products"`
Terms struct {
OnDemand map[string]*onDemand `json:"OnDemand"`
Reserved map[string]*reserved `json:"Reserved"`
}
}
var query string
var dbFile string
func init() {
flag.StringVar(&query, "query", "{}", "query to ask server for data")
flag.StringVar(&dbFile, "db", "", "DB Path")
}
func main() {
flag.Parse()
var objects aws
if dbFile == "" {
log.Println("Please provide a dbFile")
return
}
file, err := os.Open(dbFile) // For read access.
if err != nil {
// Cannot open file... Let's create it
resp, err := http.Get("https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/20170224022054/index.json")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&objects)
if err != nil {
log.Fatal(err)
}
file, err := os.Create(dbFile)
defer file.Close()
if err != nil {
log.Fatal(err)
}
enc := gob.NewEncoder(file)
err = enc.Encode(objects)
if err != nil {
log.Fatal("encode error:", err)
}
} else {
defer file.Close()
dec := gob.NewDecoder(file)
err = dec.Decode(&objects)
if err != nil {
log.Fatal("decode error:", err)
}
}
log.Println(objects.Products["HZC9FAP4F9Y8JW67"])
log.Println(objects.Terms.OnDemand["HZC9FAP4F9Y8JW67"])
log.Println(objects.Terms.Reserved["HZC9FAP4F9Y8JW67"])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment