Git Product home page Git Product logo

aws-pricing-api's Introduction

AWS-Pricing-API

This library allows you to query the amazon pricing api using go types It can cache the results in a database (using gorm)

In memory usage:

package main

import (
        "fmt"
        "github.com/chronojam/aws-pricing-api/types/schema"
        "strings"
)

func main() {
        ec2 := &schema.AmazonEC2{}

        // Populate this object with new pricing data
        err := ec2.Refresh()
        if err != nil {
                panic(err)
        }

        // Get the price of all c4.Large instances,
        // running linux, on shared tenancy
        c4Large := []*schema.AmazonEC2_Product{}
        for _, p := range ec2.Products {
                if p.Attributes.InstanceType == "c4.large" &&
                        p.Attributes.OperatingSystem == "Linux" &&
                        p.Attributes.Tenancy == "Shared" {
                        c4Large = append(c4Large, p)
                }
        }

        // Show the pricing data for each of those.
        for _, p := range c4Large {
                //fmt.Println(p.Sku)
                // Find the correct terms
                for _, term := range ec2.Terms {
                        if term.Sku == p.Sku {
                                for _, pd := range term.PriceDimensions {
                                        // I Stripped out the OnDemand/Reserved field, but maybe ill add it back later
                                        // Only On Demand
                                        if strings.Contains(pd.Description, "On Demand") {
                                                fmt.Printf("%s:\n", p.Sku)
                                                fmt.Printf("\t%s:\n", "PriceDimensions")
                                                fmt.Printf("\t\t%s\n", pd.Description)
                                                fmt.Printf("\t\t%s\n", pd.PricePerUnit.USD)
                                        }
                                }
                        }
                }
        }
}

With a backing DB

import (
        "fmt"
        "github.com/chronojam/aws-pricing-api/types/schema"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)


// Call me to populate with the appropriate product
// Dont call me twice for the same product, because you'll end up with duplicate entries.
func Populate(db *gorm.DB) {
     // p := &schema.AmazonEC2{} or whatever you need
      p := &schema.AWSBudgets{}
      err = p.Refresh()
      if err != nil {
	      panic(err)
      }
      db.Create(p)
}

func main() {
        db, err := gorm.Open("mysql", "root:my-secret-pw@/pricing?charset=utf8&parseTime=True&loc=Local")
        if err != nil {
                panic(err)
        }

        defer db.Close()

        err = schema.Migrate(db)
        if err != nil {
                panic(err)
        }

	// Populate(db)

	// Do some querying
        l := &schema.AWSBudgets{}
        db.First(l)
        db.Model(l).Association("Products").Find(&l.Products)
        //db.Model(l).Association("Terms").Find(l.Terms)
        for _, t := range l.Products {
                fmt.Printf("%v:\n", t)
                db.First(t)
                db.Model(t).Association("Attributes").Find(&t.Attributes)
                fmt.Printf("\t%v\n", t.Attributes)
        }

        db.Model(l).Association("Terms").Find(&l.Terms)
        //db.Model(l).Association("Terms").Find(l.Terms)
        for _, t := range l.Terms {
                fmt.Printf("%v:\n", t)
                db.First(t)
                db.Model(t).Association("PriceDimensions").Find(&t.PriceDimensions)
                for _, a := range t.PriceDimensions {
                        fmt.Printf("\t%v\n", a)
                }
        }
}

Updating generated types

This'll take a little while, as it pulls down everything

cd types/
make generate

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.