github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/loggo"
     8  
     9  	"github.com/juju/juju/state"
    10  	"github.com/juju/juju/storage"
    11  )
    12  
    13  var logger = loggo.GetLogger("juju.apiserver.storagecommon")
    14  
    15  // BlockDeviceFromState translates a state.BlockDeviceInfo to a
    16  // storage.BlockDevice.
    17  func BlockDeviceFromState(in state.BlockDeviceInfo) storage.BlockDevice {
    18  	return storage.BlockDevice{
    19  		in.DeviceName,
    20  		in.DeviceLinks,
    21  		in.Label,
    22  		in.UUID,
    23  		in.HardwareId,
    24  		in.WWN,
    25  		in.BusAddress,
    26  		in.Size,
    27  		in.FilesystemType,
    28  		in.InUse,
    29  		in.MountPoint,
    30  	}
    31  }
    32  
    33  // MatchingBlockDevice finds the block device that matches the
    34  // provided volume info and volume attachment info.
    35  func MatchingBlockDevice(
    36  	blockDevices []state.BlockDeviceInfo,
    37  	volumeInfo state.VolumeInfo,
    38  	attachmentInfo state.VolumeAttachmentInfo,
    39  	planBlockInfo state.BlockDeviceInfo,
    40  ) (*state.BlockDeviceInfo, bool) {
    41  	logger.Tracef("looking for block device for volume %#v", volumeInfo)
    42  	for _, dev := range blockDevices {
    43  		if planBlockInfo.HardwareId != "" {
    44  			if planBlockInfo.HardwareId == dev.HardwareId {
    45  				logger.Tracef("plan hwid match on %v", volumeInfo.HardwareId)
    46  				return &dev, true
    47  			}
    48  		}
    49  		if planBlockInfo.WWN != "" {
    50  			if planBlockInfo.WWN == dev.WWN {
    51  				logger.Tracef("plan wwn match on %v", volumeInfo.WWN)
    52  				return &dev, true
    53  			}
    54  			continue
    55  		}
    56  		if planBlockInfo.DeviceName != "" {
    57  			if planBlockInfo.DeviceName == dev.DeviceName {
    58  				logger.Tracef("plan device name match on %v", attachmentInfo.DeviceName)
    59  				return &dev, true
    60  			}
    61  			continue
    62  		}
    63  		if volumeInfo.WWN != "" {
    64  			if volumeInfo.WWN == dev.WWN {
    65  				logger.Tracef("wwn match on %v", volumeInfo.WWN)
    66  				return &dev, true
    67  			}
    68  			logger.Tracef("no match for block device WWN: %v", dev.WWN)
    69  			continue
    70  		}
    71  		if volumeInfo.HardwareId != "" {
    72  			if volumeInfo.HardwareId == dev.HardwareId {
    73  				logger.Tracef("hwid match on %v", volumeInfo.HardwareId)
    74  				return &dev, true
    75  			}
    76  			logger.Tracef("no match for block device hardware id: %v", dev.HardwareId)
    77  			continue
    78  		}
    79  		if attachmentInfo.BusAddress != "" {
    80  			if attachmentInfo.BusAddress == dev.BusAddress {
    81  				logger.Tracef("bus address match on %v", attachmentInfo.BusAddress)
    82  				return &dev, true
    83  			}
    84  			logger.Tracef("no match for block device bus address: %v", dev.BusAddress)
    85  			continue
    86  		}
    87  		// Only match on block device link if the block device is published
    88  		// with device link information.
    89  		if attachmentInfo.DeviceLink != "" && len(dev.DeviceLinks) > 0 {
    90  			for _, link := range dev.DeviceLinks {
    91  				if attachmentInfo.DeviceLink == link {
    92  					logger.Tracef("device link match on %v", attachmentInfo.DeviceLink)
    93  					return &dev, true
    94  				}
    95  			}
    96  			logger.Tracef("no match for block device dev links: %v", dev.DeviceLinks)
    97  			continue
    98  		}
    99  		if attachmentInfo.DeviceName == dev.DeviceName {
   100  			logger.Tracef("device name match on %v", attachmentInfo.DeviceName)
   101  			return &dev, true
   102  		}
   103  		logger.Tracef("no match for block device name: %v", dev.DeviceName)
   104  	}
   105  	return nil, false
   106  }