示例#1
0
// blockDeviceCosts returns the total price of a slice of BlockDevices over the given duration
// by using the EC2 API.
func blockDeviceCosts(handle *ec2.EC2, devices []ec2.BlockDevice, dur time.Duration) (float64, error) {
	cost := 0.0
	if len(devices) > 0 {
		volumeIds := []string{}
		for _, bd := range devices {
			volumeIds = append(volumeIds, bd.EBS.VolumeId)
		}
		vols, err := handle.Volumes(volumeIds, nil)
		if err != nil {
			return 0, err
		}
		for _, v := range vols.Volumes {
			// an amazon region is just the availability zone minus the final letter
			region := azToRegion(v.AvailZone)
			size, err := strconv.Atoi(v.Size)
			if err != nil {
				return 0, fmt.Errorf("reading volume size: %v", err)
			}
			p, err := ebsCost(&pkgEBSFetcher, region, size, dur)
			if err != nil {
				return 0, fmt.Errorf("EBS volume %v: %v", v.VolumeId, err)
			}
			cost += p
		}
	}
	return cost, nil
}