Skip to content

Instantly share code, notes, and snippets.

@owulveryck

owulveryck/putpoducts.go

Last active Apr 4, 2018
Embed
What would you like to do?
package main
import (
"encoding/json"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/kelseyhightower/envconfig"
"log"
"net/http"
"time"
)
// Configuration holds the environment variables for configuration
type Configuration struct {
TableName string `default:"products"`
OfferIndex string `default:"https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/index.json"`
BaseURL string `default:"https://pricing.us-east-1.amazonaws.com"`
}
// offerIndex represents the structure as returned by the offerindex
type offerIndex struct {
FormatVersion string `json:"formatVersion"`
Disclaimer string `json:"disclaimer"`
PublicationDate time.Time `json:"publicationDate"`
Offers map[string]offer `json:"offers"`
}
type offer struct {
OfferCode string `json:"offerCode:"`
VersionIndexURL string `json:"versionIndexUrl"`
CurrentVersionURL string `json:"currentVersionUrl"`
}
// ProductDetails represents a call to the API
type productDetails struct {
Products map[string]struct { // the key is the product code
//Sku string `json:"sku"`
ProductFamily string `json:"productFamily"`
Attributes map[string]interface{} `json:"attributes"`
} `json:"products"`
}
func main() {
// Create the session for dynamodb
sess, err := session.NewSession()
if err != nil {
log.Fatal("failed to create session,", err)
}
svc := dynamodb.New(sess, &aws.Config{Region: aws.String("us-east-1")})
var config Configuration
err = envconfig.Process("AWSBILLING", &config)
if err != nil {
log.Fatal(err.Error())
}
// At first get the offer index and fill a structure
resp, err := http.Get(config.OfferIndex)
if err != nil {
log.Fatal(err)
}
var oi offerIndex
err = json.NewDecoder(resp.Body).Decode(&oi)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
// Now, for each offer, get the offer details
for _, o := range oi.Offers {
resp, err := http.Get(config.BaseURL + o.CurrentVersionURL)
if err != nil {
log.Fatal(err)
}
var pd productDetails
err = json.NewDecoder(resp.Body).Decode(&pd)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
// Declare the Items we will upload in the DB
var item = make(map[string]*dynamodb.AttributeValue, 3)
for k, v := range pd.Products {
item["SKU"], err = dynamodbattribute.Marshal(k)
item["ProductFamily"], err = dynamodbattribute.Marshal(v.ProductFamily)
item["Attributes"], err = dynamodbattribute.Marshal(v.Attributes)
// Prepare the parameters for the Put operation
params := &dynamodb.PutItemInput{
Item: item,
TableName: aws.String(config.TableName),
}
// Now put the item, discarding the result
_, err = svc.PutItem(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Fatal(err)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment