From faddb1a8dbf850bf82c72368cd4ee0a5b234309b Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Mon, 30 Dec 2024 10:30:05 +0800 Subject: [PATCH] fix: add more filters to isolated_device (#21908) Co-authored-by: Qiu Jian --- cmd/climc/shell/compute/isolatedevices.go | 89 ++----------------- pkg/apis/compute/isolated_device.go | 6 ++ pkg/compute/models/isolated_devices.go | 6 ++ .../options/compute/isolated_devices.go | 63 +++++++++++++ 4 files changed, 82 insertions(+), 82 deletions(-) create mode 100644 pkg/mcclient/options/compute/isolated_devices.go diff --git a/cmd/climc/shell/compute/isolatedevices.go b/cmd/climc/shell/compute/isolatedevices.go index 8071276eea..d9b2997a9b 100644 --- a/cmd/climc/shell/compute/isolatedevices.go +++ b/cmd/climc/shell/compute/isolatedevices.go @@ -15,90 +15,15 @@ package compute import ( - "yunion.io/x/jsonutils" - - "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/cmd/climc/shell" modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute" - "yunion.io/x/onecloud/pkg/mcclient/options" + "yunion.io/x/onecloud/pkg/mcclient/options/compute" ) func init() { - type DeviceListOptions struct { - options.BaseListOptions - Unused bool `help:"Only show unused devices"` - Gpu bool `help:"Only show gpu devices"` - Host string `help:"Host ID or Name"` - Region string `help:"Cloudregion ID or Name"` - Zone string `help:"Zone ID or Name"` - Server string `help:"Server ID or Name"` - } - R(&DeviceListOptions{}, "isolated-device-list", "List isolated devices like GPU", func(s *mcclient.ClientSession, args *DeviceListOptions) error { - var params *jsonutils.JSONDict - { - var err error - params, err = args.BaseListOptions.Params() - if err != nil { - return err - - } - } - if len(args.Host) > 0 { - params.Add(jsonutils.NewString(args.Host), "host") - } - if args.Unused { - params.Add(jsonutils.JSONTrue, "unused") - } - if args.Gpu { - params.Add(jsonutils.JSONTrue, "gpu") - } - if len(args.Region) > 0 { - params.Add(jsonutils.NewString(args.Region), "region") - } - if args.Zone != "" { - params.Add(jsonutils.NewString(args.Zone), "zone") - } - if args.Server != "" { - params.Add(jsonutils.NewString(args.Server), "guest_id") - } - result, err := modules.IsolatedDevices.List(s, params) - if err != nil { - return err - } - printList(result, modules.IsolatedDevices.GetColumns(s)) - return nil - }) - - type DeviceShowOptions struct { - ID string `help:"ID of the isolated device"` - } - R(&DeviceShowOptions{}, "isolated-device-show", "Show isolated device details", func(s *mcclient.ClientSession, args *DeviceShowOptions) error { - result, err := modules.IsolatedDevices.Get(s, args.ID, nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - type DeviceDeleteOptions struct { - IDS []string `help:"IDs of the isolated device"` - } - R(&DeviceDeleteOptions{}, "isolated-device-delete", "Delete a isolated device", func(s *mcclient.ClientSession, args *DeviceDeleteOptions) error { - result := modules.IsolatedDevices.BatchDelete(s, args.IDS, nil) - printBatchResults(result, modules.IsolatedDevices.GetColumns(s)) - return nil - }) - - type DeviceUpdateOptions struct { - ID []string `help:"ID of the isolated device" json:"-"` - ReservedCpu *int `help:"reserved cpu for isolated device"` - ReservedMem *int `help:"reserved mem for isolated device"` - ReservedStorage *int `help:"reserved storage for isolated device"` - DevType string `help:"Device type" choices:"GPU-HPC|GPU-VGA"` - } - R(&DeviceUpdateOptions{}, "isolated-device-update", "Update a isolated device", func(s *mcclient.ClientSession, args *DeviceUpdateOptions) error { - res := modules.IsolatedDevices.BatchUpdate(s, args.ID, jsonutils.Marshal(args)) - printBatchResults(res, modules.IsolatedDevices.GetColumns(s)) - return nil - }) + cmd := shell.NewResourceCmd(&modules.IsolatedDevices) + cmd.List(&compute.DeviceListOptions{}) + cmd.Update(&compute.DeviceUpdateOptions{}) + cmd.Show(&compute.DeviceShowOptions{}) + cmd.BatchDelete(&compute.DeviceDeleteOptions{}) } diff --git a/pkg/apis/compute/isolated_device.go b/pkg/apis/compute/isolated_device.go index c613764dc9..0577de07bc 100644 --- a/pkg/apis/compute/isolated_device.go +++ b/pkg/apis/compute/isolated_device.go @@ -58,9 +58,15 @@ type IsolatedDeviceListInput struct { // # pci address of `Bus:Device.Function` format, or usb bus address of `bus.addr` Addr []string `json:"addr"` + // 设备路径 + DevicePath []string `json:"device_path"` + // 设备VENDOE编号 VendorDeviceId []string `json:"vendor_device_id"` + // NUMA节点序号 + NumaNode []uint8 `json:"numa_node"` + // 展示物理机的上的设备 ShowBaremetalIsolatedDevices bool `json:"show_baremetal_isolated_devices"` diff --git a/pkg/compute/models/isolated_devices.go b/pkg/compute/models/isolated_devices.go index 5c658dacd6..c18948f521 100644 --- a/pkg/compute/models/isolated_devices.go +++ b/pkg/compute/models/isolated_devices.go @@ -320,9 +320,15 @@ func (manager *SIsolatedDeviceManager) ListItemFilter( if len(query.Addr) > 0 { q = q.In("addr", query.Addr) } + if len(query.DevicePath) > 0 { + q = q.In("device_path", query.DevicePath) + } if len(query.VendorDeviceId) > 0 { q = q.In("vendor_device_id", query.VendorDeviceId) } + if len(query.NumaNode) > 0 { + q = q.In("numa_node", query.NumaNode) + } if !query.ShowBaremetalIsolatedDevices { sq := HostManager.Query("id").In("host_type", []string{api.HOST_TYPE_HYPERVISOR, api.HOST_TYPE_CONTAINER}).SubQuery() diff --git a/pkg/mcclient/options/compute/isolated_devices.go b/pkg/mcclient/options/compute/isolated_devices.go new file mode 100644 index 0000000000..29a7df2a55 --- /dev/null +++ b/pkg/mcclient/options/compute/isolated_devices.go @@ -0,0 +1,63 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package compute + +import ( + "yunion.io/x/jsonutils" + + "yunion.io/x/onecloud/pkg/mcclient/options" +) + +type DeviceListOptions struct { + options.BaseListOptions + Unused bool `help:"Only show unused devices"` + Gpu bool `help:"Only show gpu devices"` + Host string `help:"Host ID or Name"` + Region string `help:"Cloudregion ID or Name"` + Zone string `help:"Zone ID or Name"` + Server string `help:"Server ID or Name"` + + DevType []string `help:"filter by dev_type"` + Model []string `help:"filter by model"` + Addr []string `help:"filter by addr"` + DevicePath []string `help:"filter by device path"` + VendorDeviceId []string `help:"filter by vendor device id(PCIID)"` + NumaNode []uint8 `help:"fitler by numa node index"` +} + +func (o *DeviceListOptions) Params() (jsonutils.JSONObject, error) { + return options.ListStructToParams(o) +} + +type DeviceShowOptions struct { + options.BaseIdOptions +} + +type DeviceDeleteOptions struct { + options.BaseIdsOptions +} + +type DeviceUpdateOptions struct { + options.BaseIdOptions + + ReservedCpu *int `help:"reserved cpu for isolated device"` + ReservedMem *int `help:"reserved mem for isolated device"` + ReservedStorage *int `help:"reserved storage for isolated device"` + DevType string `help:"Device type"` +} + +func (o *DeviceUpdateOptions) Params() (jsonutils.JSONObject, error) { + return jsonutils.Marshal(o), nil +}