fix: add more filters to isolated_device (#21908)

Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
Jian Qiu
2024-12-30 10:30:05 +08:00
committed by GitHub
parent c46a33213f
commit faddb1a8db
4 changed files with 82 additions and 82 deletions
+7 -82
View File
@@ -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{})
}
+6
View File
@@ -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"`
+6
View File
@@ -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()
@@ -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
}