diff --git a/cmd/climc/shell/hosts.go b/cmd/climc/shell/hosts.go index cb579e6a23..b9def8987e 100644 --- a/cmd/climc/shell/hosts.go +++ b/cmd/climc/shell/hosts.go @@ -55,6 +55,8 @@ func init() { CdromBoot *bool `help:"filter hosts list by cdrom_boot=true|false"` + Sn string `help:"find host by sn"` + options.BaseListOptions } R(&HostListOptions{}, "host-list", "List hosts", func(s *mcclient.ClientSession, opts *HostListOptions) error { @@ -76,6 +78,9 @@ func init() { if len(opts.Uuid) > 0 { params.Add(jsonutils.NewString(opts.Uuid), "uuid") } + if len(opts.Sn) > 0 { + params.Add(jsonutils.NewString(opts.Sn), "sn") + } result, err := modules.Hosts.List(s, params) if err != nil { return err diff --git a/pkg/apis/compute/guests.go b/pkg/apis/compute/guests.go index d6a4fa37ee..843aa3cf78 100644 --- a/pkg/apis/compute/guests.go +++ b/pkg/apis/compute/guests.go @@ -80,6 +80,8 @@ type ServerListInput struct { ResourceType string `json:"resource_type"` // 返回开启主备机功能的主机 GetBackupGuestsOnHost *bool `json:"get_backup_guests_on_host"` + // 根据宿主机 SN 过滤 + HostSn string `json:"host_sn"` } func (input *ServerListInput) AfterUnmarshal() { diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 3a076ae664..9ab2bb1d62 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -448,6 +448,13 @@ func (manager *SGuestManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQ } } + hostSNFilter := query.HostSn + if len(hostSNFilter) > 0 { + hosts := HostManager.Query().SubQuery() + sq := hosts.Query(hosts.Field("id")).Filter(sqlchemy.Equals(hosts.Field("sn"), hostSNFilter)).SubQuery() + q = q.In("host_id", sq) + } + orderByHost := query.OrderByHost if orderByHost == "asc" { hosts := HostManager.Query().SubQuery() diff --git a/pkg/mcclient/options/servers.go b/pkg/mcclient/options/servers.go index a0767090fe..d620bb1e74 100644 --- a/pkg/mcclient/options/servers.go +++ b/pkg/mcclient/options/servers.go @@ -50,6 +50,7 @@ type ServerListOptions struct { UsableServerForEip string `help:"Eip id or name"` WithoutUserMeta *bool `help:"Show Servers without user metadata"` Group string `help:"Instance Group ID or Name"` + HostSn string `help:"Host SN"` ResourceType string `help:"Resource type" choices:"shared|prepaid|dedicated"`