Skip to content
This repository has been archived by the owner on Dec 19, 2017. It is now read-only.

Commit

Permalink
Adds BlockDeviceMappings for LaunchConfigurations
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneiderhan committed Feb 5, 2015
1 parent 0cb2b82 commit 91eb1b9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
58 changes: 58 additions & 0 deletions autoscaling/autoscaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,44 @@ func makeParams(action string) map[string]string {
return params
}

func addBlockDeviceParams(prename string, params map[string]string, blockdevices []BlockDeviceMapping) {
for i, k := range blockdevices {
// Fixup index since Amazon counts these from 1
prefix := prename + "BlockDeviceMappings.member." + strconv.Itoa(i+1) + "."

if k.DeviceName != "" {
params[prefix+"DeviceName"] = k.DeviceName
}

if k.VirtualName != "" {
params[prefix+"VirtualName"] = k.VirtualName
} else if k.NoDevice {
params[prefix+"NoDevice"] = ""
} else {
if k.SnapshotId != "" {
params[prefix+"Ebs.SnapshotId"] = k.SnapshotId
}
if k.VolumeType != "" {
params[prefix+"Ebs.VolumeType"] = k.VolumeType
}
if k.IOPS != 0 {
params[prefix+"Ebs.Iops"] = strconv.FormatInt(k.IOPS, 10)
}
if k.VolumeSize != 0 {
params[prefix+"Ebs.VolumeSize"] = strconv.FormatInt(k.VolumeSize, 10)
}
if k.DeleteOnTermination {
params[prefix+"Ebs.DeleteOnTermination"] = "true"
} else {
params[prefix+"Ebs.DeleteOnTermination"] = "false"
}
if k.Encrypted {
params[prefix+"Ebs.Encrypted"] = "true"
}
}
}
}

// ----------------------------------------------------------------------------
// AutoScaling objects

Expand All @@ -108,6 +146,7 @@ type LaunchConfiguration struct {
Name string `xml:"LaunchConfigurationName"`
SecurityGroups []string `xml:"SecurityGroups>member"`
UserData []byte `xml:"UserData"`
BlockDevices []BlockDeviceMapping `xml:"BlockDeviceMappings>member"`
}

type Instance struct {
Expand Down Expand Up @@ -138,6 +177,23 @@ type AutoScalingGroup struct {
TerminationPolicies []string `xml:"TerminationPolicies>member"`
}

// BlockDeviceMapping represents the association of a block device with an image.
//
// See http://goo.gl/wnDBf for more details.
type BlockDeviceMapping struct {
DeviceName string `xml:"DeviceName"`
VirtualName string `xml:"VirtualName"`
SnapshotId string `xml:"Ebs>SnapshotId"`
VolumeType string `xml:"Ebs>VolumeType"`
VolumeSize int64 `xml:"Ebs>VolumeSize"`
DeleteOnTermination bool `xml:"Ebs>DeleteOnTermination"`
Encrypted bool `xml:"Ebs>Encrypted"`
NoDevice bool `xml:"NoDevice"`

// The number of I/O operations per second (IOPS) that the volume supports.
IOPS int64 `xml:"ebs>iops"`
}

// ----------------------------------------------------------------------------
// Create

Expand Down Expand Up @@ -253,6 +309,7 @@ type CreateLaunchConfiguration struct {
Name string
SecurityGroups []string
UserData string
BlockDevices []BlockDeviceMapping
}

func (autoscaling *AutoScaling) CreateLaunchConfiguration(options *CreateLaunchConfiguration) (resp *SimpleResp, err error) {
Expand Down Expand Up @@ -298,6 +355,7 @@ func (autoscaling *AutoScaling) CreateLaunchConfiguration(options *CreateLaunchC
b64.Encode(userData, []byte(options.UserData))
params["UserData"] = string(userData)
}
addBlockDeviceParams("", params, options.BlockDevices)

resp = &SimpleResp{}

Expand Down
8 changes: 8 additions & 0 deletions autoscaling/autoscaling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (s *S) Test_CreateLaunchConfiguration(c *C) {
KeyName: "foobar",
Name: "i-141421",
UserData: "#!/bin/bash\necho Hello\n",
BlockDevices: []autoscaling.BlockDeviceMapping{
{DeviceName: "/dev/sdb", VirtualName: "ephemeral0"},
{DeviceName: "/dev/sdc", SnapshotId: "snap-a08912c9", DeleteOnTermination: true},
},
}

resp, err := s.autoscaling.CreateLaunchConfiguration(&options)
Expand All @@ -88,6 +92,10 @@ func (s *S) Test_CreateLaunchConfiguration(c *C) {
c.Assert(req.Form["InstanceType"], DeepEquals, []string{"m1.small"})
c.Assert(req.Form["SecurityGroups.member.1"], DeepEquals, []string{"sg-1111"})
c.Assert(req.Form["UserData"], DeepEquals, []string{"IyEvYmluL2Jhc2gKZWNobyBIZWxsbwo="})
c.Assert(req.Form["BlockDeviceMappings.member.1.DeviceName"], DeepEquals, []string{"/dev/sdb"})
c.Assert(req.Form["BlockDeviceMappings.member.1.VirtualName"], DeepEquals, []string{"ephemeral0"})
c.Assert(req.Form["BlockDeviceMappings.member.2.Ebs.SnapshotId"], DeepEquals, []string{"snap-a08912c9"})
c.Assert(req.Form["BlockDeviceMappings.member.2.Ebs.DeleteOnTermination"], DeepEquals, []string{"true"})
c.Assert(err, IsNil)
c.Assert(resp.RequestId, Equals, "7c6e177f-f082-11e1-ac58-3714bEXAMPLE")
}
Expand Down

0 comments on commit 91eb1b9

Please sign in to comment.