github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/aws/create_volume.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  	"os"
    10  	"time"
    11  )
    12  
    13  func (p *AwsProvider) CreateVolume(params types.CreateVolumeParams) (*types.Volume, error) {
    14  	logrus.WithField("raw-image", params.ImagePath).WithField("az", p.config.Zone).Infof("creating data volume from raw image")
    15  	s3svc := p.newS3()
    16  	ec2svc := p.newEC2()
    17  	imageFile, err := os.Stat(params.ImagePath)
    18  	if err != nil {
    19  		return nil, errors.New("stat image file", err)
    20  	}
    21  	volumeId, err := createDataVolumeFromRawImage(s3svc, ec2svc, params.ImagePath, imageFile.Size(), types.ImageFormat_RAW, p.config.Zone)
    22  	if err != nil {
    23  		return nil, errors.New("creating aws boot volume", err)
    24  	}
    25  	tagVolumeInput := &ec2.CreateTagsInput{
    26  		Resources: []*string{
    27  			aws.String(volumeId),
    28  		},
    29  		Tags: []*ec2.Tag{
    30  			&ec2.Tag{
    31  				Key:   aws.String("Name"),
    32  				Value: aws.String(params.Name),
    33  			},
    34  		},
    35  	}
    36  	if _, err := ec2svc.CreateTags(tagVolumeInput); err != nil {
    37  		return nil, errors.New("tagging volume", err)
    38  	}
    39  
    40  	rawImageFile, err := os.Stat(params.ImagePath)
    41  	if err != nil {
    42  		return nil, errors.New("statting raw image file", err)
    43  	}
    44  	sizeMb := rawImageFile.Size() >> 20
    45  
    46  	volume := &types.Volume{
    47  		Id:             volumeId,
    48  		Name:           params.Name,
    49  		SizeMb:         sizeMb,
    50  		Attachment:     "",
    51  		Infrastructure: types.Infrastructure_AWS,
    52  		Created:        time.Now(),
    53  	}
    54  
    55  	if err := p.state.ModifyVolumes(func(volumes map[string]*types.Volume) error {
    56  		volumes[volume.Id] = volume
    57  		return nil
    58  	}); err != nil {
    59  		return nil, errors.New("modifying volume map in state", err)
    60  	}
    61  
    62  	return nil, nil
    63  }
    64  func (p *AwsProvider) CreateEmptyVolume(name string, size int) (*types.Volume, error) {
    65  	return nil, nil
    66  }