github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/aws/list_volumes.go (about) 1 package aws 2 3 import ( 4 "github.com/sirupsen/logrus" 5 "github.com/aws/aws-sdk-go/aws" 6 "github.com/aws/aws-sdk-go/service/ec2" 7 "github.com/emc-advanced-dev/pkg/errors" 8 "github.com/solo-io/unik/pkg/types" 9 ) 10 11 func (p *AwsProvider) ListVolumes() ([]*types.Volume, error) { 12 if len(p.state.GetVolumes()) < 1 { 13 return []*types.Volume{}, nil 14 } 15 volumeIds := []*string{} 16 for volumeId := range p.state.GetVolumes() { 17 volumeIds = append(volumeIds, aws.String(volumeId)) 18 } 19 param := &ec2.DescribeVolumesInput{ 20 VolumeIds: volumeIds, 21 } 22 output, err := p.newEC2().DescribeVolumes(param) 23 if err != nil { 24 return nil, errors.New("running ec2 describe volumes ", err) 25 } 26 volumes := []*types.Volume{} 27 for _, ec2Volume := range output.Volumes { 28 volumeId := *ec2Volume.VolumeId 29 if volumeId == "" { 30 continue 31 } 32 volume, ok := p.state.GetVolumes()[volumeId] 33 if !ok { 34 logrus.WithFields(logrus.Fields{"ec2Volume": ec2Volume}).Errorf("found a volume that unik has no record of") 35 continue 36 } 37 if len(ec2Volume.Attachments) > 0 { 38 if len(ec2Volume.Attachments) > 1 { 39 return nil, errors.New("ec2 reports volume to have >1 attachments. wut", nil) 40 } 41 volume.Attachment = *ec2Volume.Attachments[0].InstanceId 42 } else { 43 volume.Attachment = "" 44 } 45 if err := p.state.ModifyVolumes(func(volumes map[string]*types.Volume) error { 46 volumes[volume.Id] = volume 47 return nil 48 }); err != nil { 49 return nil, errors.New("modifying volume map in state", err) 50 } 51 volumes = append(volumes, volume) 52 } 53 return volumes, nil 54 }