github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/client/devicemanager/utils.go (about)

     1  package devicemanager
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  	"github.com/hashicorp/nomad/plugins/device"
     9  	psstructs "github.com/hashicorp/nomad/plugins/shared/structs"
    10  )
    11  
    12  // UnknownDeviceError is returned when an operation is attempted on an unknown
    13  // device.
    14  type UnknownDeviceError struct {
    15  	Err    error
    16  	Name   string
    17  	Vendor string
    18  	Type   string
    19  	IDs    []string
    20  }
    21  
    22  // NewUnknownDeviceError returns a new UnknownDeviceError for the given device.
    23  func NewUnknownDeviceError(err error, name, vendor, devType string, ids []string) *UnknownDeviceError {
    24  	return &UnknownDeviceError{
    25  		Err:    err,
    26  		Name:   name,
    27  		Vendor: vendor, Type: devType,
    28  		IDs: ids,
    29  	}
    30  }
    31  
    32  // Error returns an error formatting that reveals which unknown devices were
    33  // requested
    34  func (u *UnknownDeviceError) Error() string {
    35  	return fmt.Sprintf("operation on unknown device(s) \"%s/%s/%s\" (%v): %v",
    36  		u.Vendor, u.Type, u.Name, u.IDs, u.Err)
    37  }
    38  
    39  // UnknownDeviceErrFromAllocated is a helper that returns an UnknownDeviceError
    40  // populating it via the AllocatedDeviceResource struct.
    41  func UnknownDeviceErrFromAllocated(err string, d *structs.AllocatedDeviceResource) *UnknownDeviceError {
    42  	return NewUnknownDeviceError(errors.New(err), d.Name, d.Vendor, d.Type, d.DeviceIDs)
    43  }
    44  
    45  // convertDeviceGroup converts a device group to a structs NodeDeviceResource
    46  func convertDeviceGroup(d *device.DeviceGroup) *structs.NodeDeviceResource {
    47  	if d == nil {
    48  		return nil
    49  	}
    50  
    51  	return &structs.NodeDeviceResource{
    52  		Vendor:     d.Vendor,
    53  		Type:       d.Type,
    54  		Name:       d.Name,
    55  		Instances:  convertDevices(d.Devices),
    56  		Attributes: psstructs.CopyMapStringAttribute(d.Attributes),
    57  	}
    58  }
    59  
    60  func convertDevices(devs []*device.Device) []*structs.NodeDevice {
    61  	if devs == nil {
    62  		return nil
    63  	}
    64  
    65  	out := make([]*structs.NodeDevice, len(devs))
    66  	for i, dev := range devs {
    67  		out[i] = convertDevice(dev)
    68  	}
    69  	return out
    70  }
    71  
    72  func convertDevice(dev *device.Device) *structs.NodeDevice {
    73  	if dev == nil {
    74  		return nil
    75  	}
    76  
    77  	return &structs.NodeDevice{
    78  		ID:                dev.ID,
    79  		Healthy:           dev.Healthy,
    80  		HealthDescription: dev.HealthDesc,
    81  		Locality:          convertHwLocality(dev.HwLocality),
    82  	}
    83  }
    84  
    85  func convertHwLocality(l *device.DeviceLocality) *structs.NodeDeviceLocality {
    86  	if l == nil {
    87  		return nil
    88  	}
    89  
    90  	return &structs.NodeDeviceLocality{
    91  		PciBusID: l.PciBusID,
    92  	}
    93  }