diff --git a/cmd/climc/shell/compute/hosts.go b/cmd/climc/shell/compute/hosts.go index 2035edd6c1..14db80e27a 100644 --- a/cmd/climc/shell/compute/hosts.go +++ b/cmd/climc/shell/compute/hosts.go @@ -20,159 +20,43 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/onecloud/cmd/climc/shell" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/modules" "yunion.io/x/onecloud/pkg/mcclient/options" + "yunion.io/x/onecloud/pkg/mcclient/options/compute" "yunion.io/x/onecloud/pkg/util/fileutils2" "yunion.io/x/onecloud/pkg/util/ssh" ) func init() { - type HostListOptions struct { - Schedtag string `help:"List hosts in schedtag"` - Zone string `help:"List hosts in zone"` - Region string `help:"List hosts in region"` - Wire string `help:"List hosts in wire"` - Image string `help:"List hosts cached images" json:"cachedimage"` - Storage string `help:"List hosts attached to storages"` - Baremetal string `help:"List hosts that is managed by baremetal system" choices:"true|false"` - Empty bool `help:"show empty host" json:"-"` - Occupied bool `help:"show occupid host" json:"-"` - Enabled bool `help:"Show enabled host only" json:"-"` - Disabled bool `help:"Show disabled host only" json:"-"` - HostType string `help:"Host type filter" choices:"baremetal|hypervisor|esxi|kubelet|hyperv|aliyun|azure|qcloud|aws|huawei|ucloud|google|ctyun"` - AnyMac string `help:"Mac matches one of the host's interface"` - AnyIp string `help:"IP matches one of the host's interface"` + cmd := shell.NewResourceCmd(&modules.Hosts) + cmd.List(&compute.HostListOptions{}) + cmd.Show(&options.BaseShowOptions{}) + cmd.GetMetadata(&options.BaseIdOptions{}) + cmd.GetProperty(&compute.HostStatusStatisticsOptions{}) - IsBaremetal *bool `help:"filter host list by is_baremetal=true|false"` + cmd.Perform("ping", &options.BaseIdOptions{}) + cmd.Perform("undo-convert", &options.BaseIdOptions{}) + cmd.Perform("maintenance", &options.BaseIdOptions{}) + cmd.Perform("unmaintenance", &options.BaseIdOptions{}) + cmd.Perform("start", &options.BaseIdOptions{}) + cmd.Perform("stop", &options.BaseIdOptions{}) + cmd.Perform("reset", &options.BaseIdOptions{}) + cmd.Perform("delete", &options.BaseIdOptions{}) + cmd.Perform("remove-all-netifs", &options.BaseIdOptions{}) - ResourceType string `help:"Resource type" choices:"shared|prepaid|dedicated"` + cmd.BatchPerform("enable", &options.BaseIdsOptions{}) + cmd.BatchPerform("disable", &options.BaseIdsOptions{}) + cmd.BatchPerform("syncstatus", &options.BaseIdsOptions{}) + cmd.BatchPerform("sync-config", &options.BaseIdsOptions{}) + cmd.BatchPerform("prepare", &options.BaseIdsOptions{}) + cmd.BatchPerform("ipmi-probe", &options.BaseIdsOptions{}) - Usable *bool `help:"List all zones that is usable"` + cmd.Get("ipmi", &options.BaseIdOptions{}) + cmd.Get("vnc", &options.BaseIdOptions{}) - Hypervisor string `help:"filter hosts by hypervisor"` - - StorageNotAttached bool `help:"List hosts not attach specified storage"` - - Uuid string `help:"find host with given system uuid"` - - CdromBoot *bool `help:"filter hosts list by cdrom_boot=true|false"` - - Sn string `help:"find host by sn"` - - OrderByServerCount string `help:"Order by server count" choices:"desc|asc"` - - options.BaseListOptions - } - R(&HostListOptions{}, "host-list", "List hosts", func(s *mcclient.ClientSession, opts *HostListOptions) error { - params, err := options.ListStructToParams(opts) - if err != nil { - return err - } - - if opts.Empty { - params.Add(jsonutils.JSONTrue, "is_empty") - } else if opts.Occupied { - params.Add(jsonutils.JSONFalse, "is_empty") - } - if opts.Enabled { - params.Add(jsonutils.NewInt(1), "enabled") - } else if opts.Disabled { - params.Add(jsonutils.NewInt(0), "enabled") - } - 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 - } - printList(result, modules.Hosts.GetColumns(s)) - return nil - }) - - type HostDetailOptions struct { - ID string `help:"ID or name of host"` - } - R(&HostDetailOptions{}, "host-show", "Show details of a host", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.Get(s, args.ID, nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - R(&HostDetailOptions{}, "host-ping", "Ping a host", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.PerformAction(s, args.ID, "ping", nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - R(&HostDetailOptions{}, "host-metadata", "Show metadata of a host", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.GetMetadata(s, args.ID, nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - type HostOpsOptions struct { - ID []string `help:"ID or name of hosts"` - } - R(&HostOpsOptions{}, "host-enable", "Enable a host", func(s *mcclient.ClientSession, args *HostOpsOptions) error { - results := modules.Hosts.BatchPerformAction(s, args.ID, "enable", nil) - printBatchResults(results, modules.Hosts.GetColumns(s)) - return nil - }) - - R(&HostOpsOptions{}, "host-disable", "Disable a host", func(s *mcclient.ClientSession, args *HostOpsOptions) error { - results := modules.Hosts.BatchPerformAction(s, args.ID, "disable", nil) - printBatchResults(results, modules.Hosts.GetColumns(s)) - return nil - }) - - R(&HostOpsOptions{}, "host-syncstatus", "Synchronize status of a host", func(s *mcclient.ClientSession, args *HostOpsOptions) error { - results := modules.Hosts.BatchPerformAction(s, args.ID, "syncstatus", nil) - printBatchResults(results, modules.Hosts.GetColumns(s)) - return nil - }) - - R(&HostOpsOptions{}, "host-sync-config", "Synchronize config of a host", func(s *mcclient.ClientSession, args *HostOpsOptions) error { - results := modules.Hosts.BatchPerformAction(s, args.ID, "sync-config", nil) - printBatchResults(results, modules.Hosts.GetColumns(s)) - return nil - }) - - R(&HostOpsOptions{}, "host-prepare", "Prepare a host for installation", func(s *mcclient.ClientSession, args *HostOpsOptions) error { - results := modules.Hosts.BatchPerformAction(s, args.ID, "prepare", nil) - printBatchResults(results, modules.Hosts.GetColumns(s)) - return nil - }) - - R(&HostOpsOptions{}, "host-ipmi-probe", "Do ipmi probe host", func(s *mcclient.ClientSession, args *HostOpsOptions) error { - results := modules.Hosts.BatchPerformAction(s, args.ID, "ipmi-probe", nil) - printBatchResults(results, modules.Hosts.GetColumns(s)) - return nil - }) - - R(&HostDetailOptions{}, "host-ipmi", "Get IPMI information of a host", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.GetSpecific(s, args.ID, "ipmi", nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - R(&HostDetailOptions{}, "host-logininfo", "Get SSH login information of a host", func(s *mcclient.ClientSession, args *HostDetailOptions) error { + R(&options.BaseIdOptions{}, "host-logininfo", "Get SSH login information of a host", func(s *mcclient.ClientSession, args *options.BaseIdOptions) error { srvid, e := modules.Hosts.GetId(s, args.ID, nil) if e != nil { return e @@ -185,15 +69,6 @@ func init() { return nil }) - R(&HostDetailOptions{}, "host-vnc", "Get VNC information of a host", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.GetSpecific(s, args.ID, "vnc", nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - type HostPropertyOptions struct { } @@ -206,7 +81,7 @@ func init() { return nil }) - R(&HostListOptions{}, "host-node-count", "Get host node count", func(s *mcclient.ClientSession, opts *HostListOptions) error { + R(&compute.HostListOptions{}, "host-node-count", "Get host node count", func(s *mcclient.ClientSession, opts *compute.HostListOptions) error { params, err := options.ListStructToParams(opts) if err != nil { return err @@ -348,67 +223,6 @@ func init() { printObject(result) return nil }) - - R(&HostDetailOptions{}, "host-undo-convert", "Undo converting a host to hypervisor", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.PerformAction(s, args.ID, "undo-convert", nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - R(&HostDetailOptions{}, "host-maintenance", "Reboot host into PXE offline OS, do maintenance jobs", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.PerformAction(s, args.ID, "maintenance", nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - R(&HostDetailOptions{}, "host-unmaintenance", "Reboot host back into disk installed OS", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.PerformAction(s, args.ID, "unmaintenance", nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - R(&HostDetailOptions{}, "host-start", "Power on host", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.PerformAction(s, args.ID, "start", nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - R(&HostDetailOptions{}, "host-stop", "Power off host", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.PerformAction(s, args.ID, "stop", nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - R(&HostDetailOptions{}, "host-reset", "Power reset host", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.PerformAction(s, args.ID, "reset", nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - R(&HostOpsOptions{}, "host-delete", "Delete host record", func(s *mcclient.ClientSession, args *HostOpsOptions) error { - results := modules.Hosts.BatchDelete(s, args.ID, nil) - printBatchResults(results, modules.Hosts.GetColumns(s)) - return nil - }) - type HostAddNetIfOptions struct { ID string `help:"ID or Name of host"` WIRE string `help:"ID or Name of wire to attach"` @@ -506,15 +320,6 @@ func init() { return nil }) - R(&HostDetailOptions{}, "host-remove-all-netifs", "Remvoe all netifs expect admin&ipmi netifs", func(s *mcclient.ClientSession, args *HostDetailOptions) error { - result, err := modules.Hosts.PerformAction(s, args.ID, "remove-all-netifs", nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - type HostCacheImageActionOptions struct { ID string `help:"ID or name of host"` IMAGE string `help:"ID or name of image"` diff --git a/cmd/climc/shell/compute/servers.go b/cmd/climc/shell/compute/servers.go index 6a44065c88..2a035ef43a 100644 --- a/cmd/climc/shell/compute/servers.go +++ b/cmd/climc/shell/compute/servers.go @@ -98,7 +98,7 @@ func init() { cmd.Get("make-sshable-cmd", new(options.ServerIdOptions)) cmd.Get("change-owner-candidate-domains", new(options.ServerChangeOwnerCandidateDomainsOptions)) cmd.Get("change-owner-candidate-domains", new(options.ServerChangeOwnerCandidateDomainsOptions)) - cmd.GetProperty(&options.StatusStatisticsOptions{}) + cmd.GetProperty(&options.ServerStatusStatisticsOptions{}) type ServerTaskShowOptions struct { ID string `help:"ID or name of server" json:"-"` diff --git a/pkg/cloudcommon/db/enabledstatusinfrasresource.go b/pkg/cloudcommon/db/enabledstatusinfrasresource.go index 0f8e6df0e4..88b4f7fd24 100644 --- a/pkg/cloudcommon/db/enabledstatusinfrasresource.go +++ b/pkg/cloudcommon/db/enabledstatusinfrasresource.go @@ -24,6 +24,7 @@ import ( "yunion.io/x/onecloud/pkg/apis" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/util/rbacutils" "yunion.io/x/onecloud/pkg/util/stringutils2" ) @@ -69,6 +70,51 @@ func (self *SEnabledStatusInfrasResourceBase) PerformDisable(ctx context.Context return nil, nil } +func (manager *SEnabledStatusInfrasResourceBaseManager) AllowGetPropertyStatistics(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool { + return IsAdminAllowGetSpec(userCred, manager, "statistics") +} + +func (manager *SEnabledStatusInfrasResourceBaseManager) GetPropertyStatistics(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (map[string]apis.StatusStatistic, error) { + im, ok := manager.GetVirtualObject().(IModelManager) + if !ok { + im = manager + } + + var err error + q := manager.Query() + q, err = ListItemFilter(im, ctx, q, userCred, query) + if err != nil { + return nil, err + } + + sq := q.SubQuery() + statQ := sq.Query(sq.Field("status"), sqlchemy.COUNT("total_count", sq.Field("id"))) + _, queryScope, err := FetchCheckQueryOwnerScope(ctx, userCred, query, im, rbacutils.ActionList, true) + if err != nil { + return nil, httperrors.NewGeneralError(err) + } + statQ = manager.FilterByOwner(statQ, userCred, queryScope) + statQ = manager.FilterBySystemAttributes(statQ, userCred, query, queryScope) + statQ = manager.FilterByHiddenSystemAttributes(statQ, userCred, query, queryScope) + statQ = statQ.GroupBy(sq.Field("status")) + + ret := []struct { + Status string + TotalCount int64 + }{} + err = statQ.All(&ret) + if err != nil { + return nil, errors.Wrapf(err, "q.All") + } + result := map[string]apis.StatusStatistic{} + for _, s := range ret { + result[s.Status] = apis.StatusStatistic{ + TotalCount: s.TotalCount, + } + } + return result, nil +} + func (manager *SEnabledStatusInfrasResourceBaseManager) ValidateCreateData( ctx context.Context, userCred mcclient.TokenCredential, diff --git a/pkg/cloudcommon/db/statusinfrasresource.go b/pkg/cloudcommon/db/statusinfrasresource.go index 7df93ff687..db73acda74 100644 --- a/pkg/cloudcommon/db/statusinfrasresource.go +++ b/pkg/cloudcommon/db/statusinfrasresource.go @@ -24,6 +24,7 @@ import ( "yunion.io/x/onecloud/pkg/apis" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/util/rbacutils" "yunion.io/x/onecloud/pkg/util/stringutils2" ) @@ -55,6 +56,51 @@ func (self *SStatusInfrasResourceBase) GetIStatusInfrasModel() IStatusInfrasMode return self.GetVirtualObject().(IStatusInfrasModel) } +func (manager *SStatusInfrasResourceBaseManager) AllowGetPropertyStatistics(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool { + return IsAdminAllowGetSpec(userCred, manager, "statistics") +} + +func (manager *SStatusInfrasResourceBaseManager) GetPropertyStatistics(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (map[string]apis.StatusStatistic, error) { + im, ok := manager.GetVirtualObject().(IModelManager) + if !ok { + im = manager + } + + var err error + q := manager.Query() + q, err = ListItemFilter(im, ctx, q, userCred, query) + if err != nil { + return nil, err + } + + sq := q.SubQuery() + statQ := sq.Query(sq.Field("status"), sqlchemy.COUNT("total_count", sq.Field("id"))) + _, queryScope, err := FetchCheckQueryOwnerScope(ctx, userCred, query, im, rbacutils.ActionList, true) + if err != nil { + return nil, httperrors.NewGeneralError(err) + } + statQ = manager.FilterByOwner(statQ, userCred, queryScope) + statQ = manager.FilterBySystemAttributes(statQ, userCred, query, queryScope) + statQ = manager.FilterByHiddenSystemAttributes(statQ, userCred, query, queryScope) + statQ = statQ.GroupBy(sq.Field("status")) + + ret := []struct { + Status string + TotalCount int64 + }{} + err = statQ.All(&ret) + if err != nil { + return nil, errors.Wrapf(err, "q.All") + } + result := map[string]apis.StatusStatistic{} + for _, s := range ret { + result[s.Status] = apis.StatusStatistic{ + TotalCount: s.TotalCount, + } + } + return result, nil +} + // 更新资源状态 func (self *SStatusInfrasResourceBase) PerformStatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformStatusInput) (jsonutils.JSONObject, error) { err := StatusBasePerformStatus(self.GetIStatusInfrasModel(), userCred, input) diff --git a/pkg/cloudcommon/db/statusstandalone.go b/pkg/cloudcommon/db/statusstandalone.go index 0c38e9fca9..7cd27917a9 100644 --- a/pkg/cloudcommon/db/statusstandalone.go +++ b/pkg/cloudcommon/db/statusstandalone.go @@ -61,22 +61,34 @@ func (manager *SStatusStandaloneResourceBaseManager) AllowGetPropertyStatistics( } func (manager *SStatusStandaloneResourceBaseManager) GetPropertyStatistics(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (map[string]apis.StatusStatistic, error) { - sq := manager.Query().SubQuery() - q := sq.Query(sq.Field("status"), sqlchemy.COUNT("total_count", sq.Field("id"))) - _, queryScope, err := FetchCheckQueryOwnerScope(ctx, userCred, query, manager, rbacutils.ActionList, true) + im, ok := manager.GetVirtualObject().(IModelManager) + if !ok { + im = manager + } + + var err error + q := manager.Query() + q, err = ListItemFilter(im, ctx, q, userCred, query) + if err != nil { + return nil, err + } + + sq := q.SubQuery() + statQ := sq.Query(sq.Field("status"), sqlchemy.COUNT("total_count", sq.Field("id"))) + _, queryScope, err := FetchCheckQueryOwnerScope(ctx, userCred, query, im, rbacutils.ActionList, true) if err != nil { return nil, httperrors.NewGeneralError(err) } - q = manager.FilterByOwner(q, userCred, queryScope) - q = manager.FilterBySystemAttributes(q, userCred, query, queryScope) - q = manager.FilterByHiddenSystemAttributes(q, userCred, query, queryScope) - q = q.GroupBy(sq.Field("status")) + statQ = manager.FilterByOwner(statQ, userCred, queryScope) + statQ = manager.FilterBySystemAttributes(statQ, userCred, query, queryScope) + statQ = manager.FilterByHiddenSystemAttributes(statQ, userCred, query, queryScope) + statQ = statQ.GroupBy(sq.Field("status")) ret := []struct { Status string TotalCount int64 }{} - err = q.All(&ret) + err = statQ.All(&ret) if err != nil { return nil, errors.Wrapf(err, "q.All") } diff --git a/pkg/cloudcommon/db/virtualresource.go b/pkg/cloudcommon/db/virtualresource.go index c496af5e7c..86d4693183 100644 --- a/pkg/cloudcommon/db/virtualresource.go +++ b/pkg/cloudcommon/db/virtualresource.go @@ -92,23 +92,35 @@ func (manager *SVirtualResourceBaseManager) AllowGetPropertyStatistics(ctx conte } func (manager *SVirtualResourceBaseManager) GetPropertyStatistics(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (map[string]apis.StatusStatistic, error) { - sq := manager.Query().SubQuery() - q := sq.Query(sq.Field("status"), sqlchemy.COUNT("total_count", sq.Field("id")), sqlchemy.SUM("pending_deleted_count", sq.Field("pending_deleted"))) + im, ok := manager.GetVirtualObject().(IModelManager) + if !ok { + im = manager + } + + var err error + q := manager.Query() + q, err = ListItemFilter(im, ctx, q, userCred, query) + if err != nil { + return nil, err + } + + sq := q.SubQuery() + statQ := sq.Query(sq.Field("status"), sqlchemy.COUNT("total_count", sq.Field("id")), sqlchemy.SUM("pending_deleted_count", sq.Field("pending_deleted"))) _, queryScope, err := FetchCheckQueryOwnerScope(ctx, userCred, query, manager, rbacutils.ActionList, true) if err != nil { return nil, httperrors.NewGeneralError(err) } - q = manager.FilterByOwner(q, userCred, queryScope) - q = manager.FilterBySystemAttributes(q, userCred, query, queryScope) - q = manager.FilterByHiddenSystemAttributes(q, userCred, query, queryScope) - q = q.GroupBy(sq.Field("status")) + statQ = manager.FilterByOwner(statQ, userCred, queryScope) + statQ = manager.FilterBySystemAttributes(statQ, userCred, query, queryScope) + statQ = manager.FilterByHiddenSystemAttributes(statQ, userCred, query, queryScope) + statQ = statQ.GroupBy(sq.Field("status")) ret := []struct { Status string TotalCount int64 PendingDeletedCount int64 }{} - err = q.All(&ret) + err = statQ.All(&ret) if err != nil { return nil, errors.Wrapf(err, "q.All") } diff --git a/pkg/mcclient/options/base.go b/pkg/mcclient/options/base.go index b8b14a4dd0..f55a4fbac9 100644 --- a/pkg/mcclient/options/base.go +++ b/pkg/mcclient/options/base.go @@ -397,6 +397,18 @@ func (o *BaseIdOptions) Params() (jsonutils.JSONObject, error) { return nil, nil } +type BaseIdsOptions struct { + ID []string `json:"-"` +} + +func (o *BaseIdsOptions) GetIds() []string { + return o.ID +} + +func (o *BaseIdsOptions) Params() (jsonutils.JSONObject, error) { + return nil, nil +} + type BaseShowOptions struct { BaseIdOptions WithMeta *bool `help:"With meta data"` @@ -420,25 +432,6 @@ func (o ChangeOwnerOptions) Params() (jsonutils.JSONObject, error) { } type StatusStatisticsOptions struct { - PendingDelete *bool `help:"Show only pending deleted resources"` - PendingDeleteAll *bool `help:"Show also pending-deleted resources" json:"-"` - DeleteAll *bool `help:"Show also deleted resources" json:"-"` - ShowEmulated *bool `help:"Show all resources including the emulated resources"` - Scope string `help:"resource scope" choices:"system|domain|project|user"` -} - -func (o StatusStatisticsOptions) Params() (jsonutils.JSONObject, error) { - params, err := ListStructToParams(o) - if err != nil { - return nil, err - } - if BoolV(o.DeleteAll) { - params.Set("delete", jsonutils.NewString("all")) - } - if BoolV(o.PendingDeleteAll) { - params.Set("pending_delete", jsonutils.NewString("all")) - } - return params, nil } func (o StatusStatisticsOptions) Property() string { diff --git a/pkg/mcclient/options/compute/host.go b/pkg/mcclient/options/compute/host.go new file mode 100644 index 0000000000..12a8b67228 --- /dev/null +++ b/pkg/mcclient/options/compute/host.go @@ -0,0 +1,87 @@ +// 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 HostListOptions struct { + Schedtag string `help:"List hosts in schedtag"` + Zone string `help:"List hosts in zone"` + Region string `help:"List hosts in region"` + Wire string `help:"List hosts in wire"` + Image string `help:"List hosts cached images" json:"cachedimage"` + Storage string `help:"List hosts attached to storages"` + Baremetal string `help:"List hosts that is managed by baremetal system" choices:"true|false"` + Empty bool `help:"show empty host" json:"-"` + Occupied bool `help:"show occupid host" json:"-"` + Enabled bool `help:"Show enabled host only" json:"-"` + Disabled bool `help:"Show disabled host only" json:"-"` + HostType string `help:"Host type filter" choices:"baremetal|hypervisor|esxi|kubelet|hyperv|aliyun|azure|qcloud|aws|huawei|ucloud|google|ctyun"` + AnyMac string `help:"Mac matches one of the host's interface"` + AnyIp string `help:"IP matches one of the host's interface"` + + IsBaremetal *bool `help:"filter host list by is_baremetal=true|false"` + + ResourceType string `help:"Resource type" choices:"shared|prepaid|dedicated"` + + Usable *bool `help:"List all zones that is usable"` + + Hypervisor string `help:"filter hosts by hypervisor"` + + StorageNotAttached bool `help:"List hosts not attach specified storage"` + + Uuid string `help:"find host with given system uuid"` + + CdromBoot *bool `help:"filter hosts list by cdrom_boot=true|false"` + + Sn string `help:"find host by sn"` + + OrderByServerCount string `help:"Order by server count" choices:"desc|asc"` + + options.BaseListOptions +} + +func (opts *HostListOptions) Params() (jsonutils.JSONObject, error) { + params, err := options.ListStructToParams(opts) + if err != nil { + return nil, err + } + if opts.Empty { + params.Add(jsonutils.JSONTrue, "is_empty") + } else if opts.Occupied { + params.Add(jsonutils.JSONFalse, "is_empty") + } + if opts.Enabled { + params.Add(jsonutils.NewInt(1), "enabled") + } else if opts.Disabled { + params.Add(jsonutils.NewInt(0), "enabled") + } + if len(opts.Uuid) > 0 { + params.Add(jsonutils.NewString(opts.Uuid), "uuid") + } + if len(opts.Sn) > 0 { + params.Add(jsonutils.NewString(opts.Sn), "sn") + } + return params, nil +} + +type HostStatusStatisticsOptions struct { + HostListOptions + options.StatusStatisticsOptions +} diff --git a/pkg/mcclient/options/servers.go b/pkg/mcclient/options/servers.go index 5f6c5dd8a8..dbe1d02efd 100644 --- a/pkg/mcclient/options/servers.go +++ b/pkg/mcclient/options/servers.go @@ -1132,3 +1132,8 @@ type ServerMigrateNetworkOptions struct { func (opts *ServerMigrateNetworkOptions) Params() (jsonutils.JSONObject, error) { return jsonutils.Marshal(opts), nil } + +type ServerStatusStatisticsOptions struct { + ServerListOptions + StatusStatisticsOptions +}