Implement Creater and Deleter interfaces for AWS EBS.

Also mark the created EBS volumes with tags, so the admin knows
who/what created the volumes.
This commit is contained in:
Jan Safranek
2015-12-15 10:22:49 +01:00
parent 700d92c2a8
commit 6ff5286df9
9 changed files with 250 additions and 5 deletions

View File

@@ -136,6 +136,7 @@ type EC2Metadata interface {
type VolumeOptions struct {
CapacityGB int
Tags *map[string]string
}
// Volumes is an interface for managing cloud-provisioned volumes
@@ -1216,15 +1217,15 @@ func (aws *AWSCloud) DetachDisk(instanceName string, diskName string) error {
}
// Implements Volumes.CreateVolume
func (aws *AWSCloud) CreateVolume(volumeOptions *VolumeOptions) (string, error) {
func (s *AWSCloud) CreateVolume(volumeOptions *VolumeOptions) (string, error) {
// TODO: Should we tag this with the cluster id (so it gets deleted when the cluster does?)
// This is only used for testing right now
request := &ec2.CreateVolumeInput{}
request.AvailabilityZone = &aws.availabilityZone
request.AvailabilityZone = &s.availabilityZone
volSize := int64(volumeOptions.CapacityGB)
request.Size = &volSize
response, err := aws.ec2.CreateVolume(request)
response, err := s.ec2.CreateVolume(request)
if err != nil {
return "", err
}
@@ -1234,6 +1235,28 @@ func (aws *AWSCloud) CreateVolume(volumeOptions *VolumeOptions) (string, error)
volumeName := "aws://" + az + "/" + awsID
// apply tags
if volumeOptions.Tags != nil {
tags := []*ec2.Tag{}
for k, v := range *volumeOptions.Tags {
tag := &ec2.Tag{}
tag.Key = aws.String(k)
tag.Value = aws.String(v)
tags = append(tags, tag)
}
tagRequest := &ec2.CreateTagsInput{}
tagRequest.Resources = []*string{&awsID}
tagRequest.Tags = tags
if _, err := s.createTags(tagRequest); err != nil {
// delete the volume and hope it succeeds
delerr := s.DeleteVolume(volumeName)
if delerr != nil {
// delete did not succeed, we have a stray volume!
return "", fmt.Errorf("error tagging volume %s, could not delete the volume: %v", volumeName, delerr)
}
return "", fmt.Errorf("error tagging volume %s: %v", volumeName, err)
}
}
return volumeName, nil
}