func parseOndemandUnitPrice()

in pkg/ec2pricing/ec2pricing.go [311:355]


func parseOndemandUnitPrice(priceList aws.JSONValue) (string, float64, error) {
	// TODO: this could probably be cleaned up a bit by adding a couple structs with json tags
	//       We still need to some weird for-loops to get at elements under json keys that are IDs...
	//       But it would probably be cleaner than this.
	attributes, ok := priceList["product"].(map[string]interface{})["attributes"]
	if !ok {
		return "", float64(-1.0), fmt.Errorf("Unable to find product attributes")
	}
	instanceTypeName, ok := attributes.(map[string]interface{})["instanceType"].(string)
	if !ok {
		return "", float64(-1.0), fmt.Errorf("Unable to find instance type name from product attributes")
	}
	terms, ok := priceList["terms"]
	if !ok {
		return instanceTypeName, float64(-1.0), fmt.Errorf("Unable to find pricing terms")
	}
	ondemandTerms, ok := terms.(map[string]interface{})["OnDemand"]
	if !ok {
		return instanceTypeName, float64(-1.0), fmt.Errorf("Unable to find on-demand pricing terms")
	}
	for _, priceDimensions := range ondemandTerms.(map[string]interface{}) {
		dim, ok := priceDimensions.(map[string]interface{})["priceDimensions"]
		if !ok {
			return instanceTypeName, float64(-1.0), fmt.Errorf("Unable to find on-demand pricing dimensions")
		}
		for _, dimension := range dim.(map[string]interface{}) {
			dims := dimension.(map[string]interface{})
			pricePerUnit, ok := dims["pricePerUnit"]
			if !ok {
				return instanceTypeName, float64(-1.0), fmt.Errorf("Unable to find on-demand price per unit in pricing dimensions")
			}
			pricePerUnitInUSDStr, ok := pricePerUnit.(map[string]interface{})["USD"]
			if !ok {
				return instanceTypeName, float64(-1.0), fmt.Errorf("Unable to find on-demand price per unit in USD")
			}
			var err error
			pricePerUnitInUSD, err := strconv.ParseFloat(pricePerUnitInUSDStr.(string), 64)
			if err != nil {
				return instanceTypeName, float64(-1.0), fmt.Errorf("Could not convert price per unit in USD to a float64")
			}
			return instanceTypeName, pricePerUnitInUSD, nil
		}
	}
	return instanceTypeName, float64(-1.0), fmt.Errorf("Unable to parse pricing doc")
}