github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/apiserver/common/storagecommon/blockdevices.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storagecommon
     5  
     6  import (
     7  	"github.com/juju/juju/state"
     8  	"github.com/juju/juju/storage"
     9  )
    10  
    11  // BlockDeviceFromState translates a state.BlockDeviceInfo to a
    12  // storage.BlockDevice.
    13  func BlockDeviceFromState(in state.BlockDeviceInfo) storage.BlockDevice {
    14  	return storage.BlockDevice{
    15  		in.DeviceName,
    16  		in.DeviceLinks,
    17  		in.Label,
    18  		in.UUID,
    19  		in.HardwareId,
    20  		in.BusAddress,
    21  		in.Size,
    22  		in.FilesystemType,
    23  		in.InUse,
    24  		in.MountPoint,
    25  	}
    26  }
    27  
    28  // MatchingBlockDevice finds the block device that matches the
    29  // provided volume info and volume attachment info.
    30  func MatchingBlockDevice(
    31  	blockDevices []state.BlockDeviceInfo,
    32  	volumeInfo state.VolumeInfo,
    33  	attachmentInfo state.VolumeAttachmentInfo,
    34  ) (*state.BlockDeviceInfo, bool) {
    35  	for _, dev := range blockDevices {
    36  		if volumeInfo.HardwareId != "" {
    37  			if volumeInfo.HardwareId == dev.HardwareId {
    38  				return &dev, true
    39  			}
    40  			continue
    41  		}
    42  		if attachmentInfo.BusAddress != "" {
    43  			if attachmentInfo.BusAddress == dev.BusAddress {
    44  				return &dev, true
    45  			}
    46  			continue
    47  		}
    48  		if attachmentInfo.DeviceLink != "" {
    49  			for _, link := range dev.DeviceLinks {
    50  				if attachmentInfo.DeviceLink == link {
    51  					return &dev, true
    52  				}
    53  			}
    54  			continue
    55  		}
    56  		if attachmentInfo.DeviceName == dev.DeviceName {
    57  			return &dev, true
    58  		}
    59  	}
    60  	return nil, false
    61  }