github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/builder/amazon/common/block_device.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/goamz/ec2"
     7  	"github.com/mitchellh/packer/packer"
     8  )
     9  
    10  // BlockDevice
    11  type BlockDevice struct {
    12  	DeleteOnTermination bool   `mapstructure:"delete_on_termination"`
    13  	DeviceName          string `mapstructure:"device_name"`
    14  	Encrypted           bool   `mapstructure:"encrypted"`
    15  	IOPS                int64  `mapstructure:"iops"`
    16  	NoDevice            bool   `mapstructure:"no_device"`
    17  	SnapshotId          string `mapstructure:"snapshot_id"`
    18  	VirtualName         string `mapstructure:"virtual_name"`
    19  	VolumeType          string `mapstructure:"volume_type"`
    20  	VolumeSize          int64  `mapstructure:"volume_size"`
    21  }
    22  
    23  type BlockDevices struct {
    24  	AMIMappings    []BlockDevice `mapstructure:"ami_block_device_mappings"`
    25  	LaunchMappings []BlockDevice `mapstructure:"launch_block_device_mappings"`
    26  }
    27  
    28  func buildBlockDevices(b []BlockDevice) []ec2.BlockDeviceMapping {
    29  	var blockDevices []ec2.BlockDeviceMapping
    30  
    31  	for _, blockDevice := range b {
    32  		blockDevices = append(blockDevices, ec2.BlockDeviceMapping{
    33  			DeviceName:          blockDevice.DeviceName,
    34  			VirtualName:         blockDevice.VirtualName,
    35  			SnapshotId:          blockDevice.SnapshotId,
    36  			VolumeType:          blockDevice.VolumeType,
    37  			VolumeSize:          blockDevice.VolumeSize,
    38  			DeleteOnTermination: blockDevice.DeleteOnTermination,
    39  			IOPS:                blockDevice.IOPS,
    40  			NoDevice:            blockDevice.NoDevice,
    41  			Encrypted:           blockDevice.Encrypted,
    42  		})
    43  	}
    44  	return blockDevices
    45  }
    46  
    47  func (b *BlockDevices) Prepare(t *packer.ConfigTemplate) []error {
    48  	if t == nil {
    49  		var err error
    50  		t, err = packer.NewConfigTemplate()
    51  		if err != nil {
    52  			return []error{err}
    53  		}
    54  	}
    55  
    56  	lists := map[string][]BlockDevice{
    57  		"ami_block_device_mappings":    b.AMIMappings,
    58  		"launch_block_device_mappings": b.LaunchMappings,
    59  	}
    60  
    61  	var errs []error
    62  	for outer, bds := range lists {
    63  		for i := 0; i < len(bds); i++ {
    64  			templates := map[string]*string{
    65  				"device_name":  &bds[i].DeviceName,
    66  				"snapshot_id":  &bds[i].SnapshotId,
    67  				"virtual_name": &bds[i].VirtualName,
    68  				"volume_type":  &bds[i].VolumeType,
    69  			}
    70  
    71  			errs := make([]error, 0)
    72  			for n, ptr := range templates {
    73  				var err error
    74  				*ptr, err = t.Process(*ptr, nil)
    75  				if err != nil {
    76  					errs = append(
    77  						errs, fmt.Errorf(
    78  							"Error processing %s[%d].%s: %s",
    79  							outer, i, n, err))
    80  				}
    81  			}
    82  		}
    83  	}
    84  
    85  	if len(errs) > 0 {
    86  		return errs
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  func (b *BlockDevices) BuildAMIDevices() []ec2.BlockDeviceMapping {
    93  	return buildBlockDevices(b.AMIMappings)
    94  }
    95  
    96  func (b *BlockDevices) BuildLaunchDevices() []ec2.BlockDeviceMapping {
    97  	return buildBlockDevices(b.LaunchMappings)
    98  }