github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/vsphere/attach_volume.go (about)

     1  package vsphere
     2  
     3  import (
     4  	"github.com/sirupsen/logrus"
     5  	"github.com/emc-advanced-dev/pkg/errors"
     6  	"github.com/solo-io/unik/pkg/providers/common"
     7  	"github.com/solo-io/unik/pkg/types"
     8  )
     9  
    10  func (p *VsphereProvider) AttachVolume(id, instanceId, mntPoint string) error {
    11  	volume, err := p.GetVolume(id)
    12  	if err != nil {
    13  		return errors.New("retrieving volume "+id, err)
    14  	}
    15  	instance, err := p.GetInstance(instanceId)
    16  	if err != nil {
    17  		return errors.New("retrieving instance "+instanceId, err)
    18  	}
    19  	image, err := p.GetImage(instance.ImageId)
    20  	if err != nil {
    21  		return errors.New("retrieving image for instance", err)
    22  	}
    23  	controllerPort, err := common.GetControllerPortForMnt(image, mntPoint)
    24  	if err != nil {
    25  		return errors.New("getting controller port for mnt point", err)
    26  	}
    27  	logrus.Infof("attaching %s to %s on controller port %v", volume.Id, instance.Id, controllerPort)
    28  	if err := p.getClient().AttachDisk(instance.Id, getVolumeDatastorePath(volume.Name), controllerPort, image.RunSpec.StorageDriver); err != nil {
    29  		return errors.New("attaching disk to vm", err)
    30  	}
    31  	if err := p.state.ModifyVolumes(func(volumes map[string]*types.Volume) error {
    32  		volume, ok := volumes[volume.Id]
    33  		if !ok {
    34  			return errors.New("no record of "+volume.Id+" in the state", nil)
    35  		}
    36  		volume.Attachment = instance.Id
    37  		return nil
    38  	}); err != nil {
    39  		return errors.New("modifying volumes in state", err)
    40  	}
    41  	return nil
    42  }