|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +package hypeman |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "slices" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/onkernel/hypeman-go/internal/apijson" |
| 14 | + "github.com/onkernel/hypeman-go/internal/requestconfig" |
| 15 | + "github.com/onkernel/hypeman-go/option" |
| 16 | + "github.com/onkernel/hypeman-go/packages/param" |
| 17 | + "github.com/onkernel/hypeman-go/packages/respjson" |
| 18 | +) |
| 19 | + |
| 20 | +// DeviceService contains methods and other services that help with interacting |
| 21 | +// with the hypeman API. |
| 22 | +// |
| 23 | +// Note, unlike clients, this service does not read variables from the environment |
| 24 | +// automatically. You should not instantiate this service directly, and instead use |
| 25 | +// the [NewDeviceService] method instead. |
| 26 | +type DeviceService struct { |
| 27 | + Options []option.RequestOption |
| 28 | +} |
| 29 | + |
| 30 | +// NewDeviceService generates a new service that applies the given options to each |
| 31 | +// request. These options are applied after the parent client's options (if there |
| 32 | +// is one), and before any request-specific options. |
| 33 | +func NewDeviceService(opts ...option.RequestOption) (r DeviceService) { |
| 34 | + r = DeviceService{} |
| 35 | + r.Options = opts |
| 36 | + return |
| 37 | +} |
| 38 | + |
| 39 | +// Register a device for passthrough |
| 40 | +func (r *DeviceService) New(ctx context.Context, body DeviceNewParams, opts ...option.RequestOption) (res *Device, err error) { |
| 41 | + opts = slices.Concat(r.Options, opts) |
| 42 | + path := "devices" |
| 43 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...) |
| 44 | + return |
| 45 | +} |
| 46 | + |
| 47 | +// Get device details |
| 48 | +func (r *DeviceService) Get(ctx context.Context, id string, opts ...option.RequestOption) (res *Device, err error) { |
| 49 | + opts = slices.Concat(r.Options, opts) |
| 50 | + if id == "" { |
| 51 | + err = errors.New("missing required id parameter") |
| 52 | + return |
| 53 | + } |
| 54 | + path := fmt.Sprintf("devices/%s", id) |
| 55 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...) |
| 56 | + return |
| 57 | +} |
| 58 | + |
| 59 | +// List registered devices |
| 60 | +func (r *DeviceService) List(ctx context.Context, opts ...option.RequestOption) (res *[]Device, err error) { |
| 61 | + opts = slices.Concat(r.Options, opts) |
| 62 | + path := "devices" |
| 63 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...) |
| 64 | + return |
| 65 | +} |
| 66 | + |
| 67 | +// Unregister device |
| 68 | +func (r *DeviceService) Delete(ctx context.Context, id string, opts ...option.RequestOption) (err error) { |
| 69 | + opts = slices.Concat(r.Options, opts) |
| 70 | + opts = append([]option.RequestOption{option.WithHeader("Accept", "*/*")}, opts...) |
| 71 | + if id == "" { |
| 72 | + err = errors.New("missing required id parameter") |
| 73 | + return |
| 74 | + } |
| 75 | + path := fmt.Sprintf("devices/%s", id) |
| 76 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, nil, opts...) |
| 77 | + return |
| 78 | +} |
| 79 | + |
| 80 | +// Discover passthrough-capable devices on host |
| 81 | +func (r *DeviceService) ListAvailable(ctx context.Context, opts ...option.RequestOption) (res *[]AvailableDevice, err error) { |
| 82 | + opts = slices.Concat(r.Options, opts) |
| 83 | + path := "devices/available" |
| 84 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...) |
| 85 | + return |
| 86 | +} |
| 87 | + |
| 88 | +type AvailableDevice struct { |
| 89 | + // PCI device ID (hex) |
| 90 | + DeviceID string `json:"device_id,required"` |
| 91 | + // IOMMU group number |
| 92 | + IommuGroup int64 `json:"iommu_group,required"` |
| 93 | + // PCI address |
| 94 | + PciAddress string `json:"pci_address,required"` |
| 95 | + // PCI vendor ID (hex) |
| 96 | + VendorID string `json:"vendor_id,required"` |
| 97 | + // Currently bound driver (null if none) |
| 98 | + CurrentDriver string `json:"current_driver,nullable"` |
| 99 | + // Human-readable device name |
| 100 | + DeviceName string `json:"device_name"` |
| 101 | + // Human-readable vendor name |
| 102 | + VendorName string `json:"vendor_name"` |
| 103 | + // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. |
| 104 | + JSON struct { |
| 105 | + DeviceID respjson.Field |
| 106 | + IommuGroup respjson.Field |
| 107 | + PciAddress respjson.Field |
| 108 | + VendorID respjson.Field |
| 109 | + CurrentDriver respjson.Field |
| 110 | + DeviceName respjson.Field |
| 111 | + VendorName respjson.Field |
| 112 | + ExtraFields map[string]respjson.Field |
| 113 | + raw string |
| 114 | + } `json:"-"` |
| 115 | +} |
| 116 | + |
| 117 | +// Returns the unmodified JSON received from the API |
| 118 | +func (r AvailableDevice) RawJSON() string { return r.JSON.raw } |
| 119 | +func (r *AvailableDevice) UnmarshalJSON(data []byte) error { |
| 120 | + return apijson.UnmarshalRoot(data, r) |
| 121 | +} |
| 122 | + |
| 123 | +type Device struct { |
| 124 | + // Auto-generated unique identifier (CUID2 format) |
| 125 | + ID string `json:"id,required"` |
| 126 | + // Whether the device is currently bound to the vfio-pci driver, which is required |
| 127 | + // for VM passthrough. |
| 128 | + // |
| 129 | + // - true: Device is bound to vfio-pci and ready for (or currently in use by) a VM. |
| 130 | + // The device's native driver has been unloaded. |
| 131 | + // - false: Device is using its native driver (e.g., nvidia) or no driver. Hypeman |
| 132 | + // will automatically bind to vfio-pci when attaching to an instance. |
| 133 | + BoundToVfio bool `json:"bound_to_vfio,required"` |
| 134 | + // Registration timestamp (RFC3339) |
| 135 | + CreatedAt time.Time `json:"created_at,required" format:"date-time"` |
| 136 | + // PCI device ID (hex) |
| 137 | + DeviceID string `json:"device_id,required"` |
| 138 | + // IOMMU group number |
| 139 | + IommuGroup int64 `json:"iommu_group,required"` |
| 140 | + // PCI address |
| 141 | + PciAddress string `json:"pci_address,required"` |
| 142 | + // Type of PCI device |
| 143 | + // |
| 144 | + // Any of "gpu", "pci". |
| 145 | + Type DeviceType `json:"type,required"` |
| 146 | + // PCI vendor ID (hex) |
| 147 | + VendorID string `json:"vendor_id,required"` |
| 148 | + // Instance ID if attached |
| 149 | + AttachedTo string `json:"attached_to,nullable"` |
| 150 | + // Device name (user-provided or auto-generated from PCI address) |
| 151 | + Name string `json:"name"` |
| 152 | + // JSON contains metadata for fields, check presence with [respjson.Field.Valid]. |
| 153 | + JSON struct { |
| 154 | + ID respjson.Field |
| 155 | + BoundToVfio respjson.Field |
| 156 | + CreatedAt respjson.Field |
| 157 | + DeviceID respjson.Field |
| 158 | + IommuGroup respjson.Field |
| 159 | + PciAddress respjson.Field |
| 160 | + Type respjson.Field |
| 161 | + VendorID respjson.Field |
| 162 | + AttachedTo respjson.Field |
| 163 | + Name respjson.Field |
| 164 | + ExtraFields map[string]respjson.Field |
| 165 | + raw string |
| 166 | + } `json:"-"` |
| 167 | +} |
| 168 | + |
| 169 | +// Returns the unmodified JSON received from the API |
| 170 | +func (r Device) RawJSON() string { return r.JSON.raw } |
| 171 | +func (r *Device) UnmarshalJSON(data []byte) error { |
| 172 | + return apijson.UnmarshalRoot(data, r) |
| 173 | +} |
| 174 | + |
| 175 | +// Type of PCI device |
| 176 | +type DeviceType string |
| 177 | + |
| 178 | +const ( |
| 179 | + DeviceTypeGPU DeviceType = "gpu" |
| 180 | + DeviceTypePci DeviceType = "pci" |
| 181 | +) |
| 182 | + |
| 183 | +type DeviceNewParams struct { |
| 184 | + // PCI address of the device (required, e.g., "0000:a2:00.0") |
| 185 | + PciAddress string `json:"pci_address,required"` |
| 186 | + // Optional globally unique device name. If not provided, a name is auto-generated |
| 187 | + // from the PCI address (e.g., "pci-0000-a2-00-0") |
| 188 | + Name param.Opt[string] `json:"name,omitzero"` |
| 189 | + paramObj |
| 190 | +} |
| 191 | + |
| 192 | +func (r DeviceNewParams) MarshalJSON() (data []byte, err error) { |
| 193 | + type shadow DeviceNewParams |
| 194 | + return param.MarshalObject(r, (*shadow)(&r)) |
| 195 | +} |
| 196 | +func (r *DeviceNewParams) UnmarshalJSON(data []byte) error { |
| 197 | + return apijson.UnmarshalRoot(data, r) |
| 198 | +} |
0 commit comments