fix(glance): image statistics

This commit is contained in:
ioito
2022-02-15 18:47:24 +08:00
parent 7e5b3c890f
commit 86e7c2b661
4 changed files with 89 additions and 63 deletions
+5 -62
View File
@@ -24,11 +24,13 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/cmd/climc/shell"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modulebase"
"yunion.io/x/onecloud/pkg/mcclient/modules/identity"
modules "yunion.io/x/onecloud/pkg/mcclient/modules/image"
"yunion.io/x/onecloud/pkg/mcclient/options"
"yunion.io/x/onecloud/pkg/mcclient/options/glance"
)
type ImageOptionalOptions struct {
@@ -148,69 +150,10 @@ func addImageOptionalOptions(s *mcclient.ClientSession, params *jsonutils.JSONDi
}
func init() {
type ImageListOptions struct {
options.BaseListOptions
IsPublic string `help:"filter images public or not(True, False or None)" choices:"true|false"`
IsStandard string `help:"filter images standard or non-standard" choices:"true|false"`
Protected string `help:"filter images by protected" choices:"true|false"`
IsUefi bool `help:"list uefi image"`
Format []string `help:"Disk formats"`
SubFormats []string `help:"Sub formats"`
Name string `help:"Name filter"`
OsType []string `help:"Type of OS filter e.g. 'Windows, Linux, Freebsd, Android, macOS, VMWare'"`
Distribution []string `help:"Distribution filter, e.g. 'CentOS, Ubuntu, Debian, Windows'"`
}
R(&ImageListOptions{}, "image-list", "List images", func(s *mcclient.ClientSession, args *ImageListOptions) error {
params, err := args.Params()
if err != nil {
return err
}
if len(args.IsPublic) > 0 {
params.Add(jsonutils.NewString(args.IsPublic), "is_public")
}
if len(args.IsStandard) > 0 {
params.Add(jsonutils.NewString(args.IsStandard), "is_standard")
}
if len(args.Protected) > 0 {
params.Add(jsonutils.NewString(args.Protected), "protected")
}
if args.IsUefi {
params.Add(jsonutils.JSONTrue, "uefi")
}
if len(args.Tenant) > 0 {
tid, e := identity.Projects.GetId(s, args.Tenant, nil)
if e != nil {
return e
}
params.Add(jsonutils.NewString(tid), "owner")
}
if len(args.Name) > 0 {
params.Add(jsonutils.NewString(args.Name), "name")
}
if len(args.Format) > 0 {
fs := jsonutils.NewArray()
for _, f := range args.Format {
fs.Add(jsonutils.NewString(f))
}
params.Add(fs, "disk_formats")
}
if len(args.SubFormats) > 0 {
params.Add(jsonutils.Marshal(args.SubFormats), "sub_formats")
}
if len(args.OsType) > 0 {
params.Add(jsonutils.NewStringArray(args.OsType), "os_types")
}
if len(args.Distribution) > 0 {
params.Add(jsonutils.NewStringArray(args.Distribution), "distributions")
}
result, err := modules.Images.List(s, params)
if err != nil {
return err
}
printList(result, modules.Images.GetColumns(s))
return nil
})
cmd := shell.NewResourceCmd(&modules.Images)
cmd.List(&glance.ImageListOptions{})
cmd.GetProperty(&glance.ImageStatusStatisticsOptions{})
type ImageOperationOptions struct {
ID []string `help:"Image id or name" metavar:"IMAGE"`
+1 -1
View File
@@ -98,7 +98,7 @@ func (this *ImageManager) Get(session *mcclient.ClientSession, id string, params
// hack: some GetPropertiesMethod must use HTTP GET action like:
// - GET /images/distinct-field
// hard code this id currently, should found a better solution
if ok, _ := utils.InStringArray(id, []string{"distinct-field"}); ok {
if ok, _ := utils.InStringArray(id, []string{"distinct-field", "statistics"}); ok {
return this.ResourceManager.Get(session, id, params)
}
r, e := this.GetById(session, id, params)
+1
View File
@@ -0,0 +1 @@
package glance // import "yunion.io/x/onecloud/pkg/mcclient/options/glance"
+82
View File
@@ -0,0 +1,82 @@
// 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 glance
import (
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/mcclient/options"
)
type ImageListOptions struct {
options.BaseListOptions
IsPublic string `help:"filter images public or not(True, False or None)" choices:"true|false"`
IsStandard string `help:"filter images standard or non-standard" choices:"true|false"`
Protected string `help:"filter images by protected" choices:"true|false"`
IsUefi bool `help:"list uefi image"`
Format []string `help:"Disk formats"`
SubFormats []string `help:"Sub formats"`
Name string `help:"Name filter"`
OsType []string `help:"Type of OS filter e.g. 'Windows, Linux, Freebsd, Android, macOS, VMWare'"`
Distribution []string `help:"Distribution filter, e.g. 'CentOS, Ubuntu, Debian, Windows'"`
}
func (o *ImageListOptions) Params() (jsonutils.JSONObject, error) {
params, err := o.BaseListOptions.Params()
if err != nil {
return nil, err
}
if len(o.IsPublic) > 0 {
params.Add(jsonutils.NewString(o.IsPublic), "is_public")
}
if len(o.IsStandard) > 0 {
params.Add(jsonutils.NewString(o.IsStandard), "is_standard")
}
if len(o.Protected) > 0 {
params.Add(jsonutils.NewString(o.Protected), "protected")
}
if o.IsUefi {
params.Add(jsonutils.JSONTrue, "uefi")
}
if len(o.Tenant) > 0 {
params.Add(jsonutils.NewString(o.Tenant), "owner")
}
if len(o.Name) > 0 {
params.Add(jsonutils.NewString(o.Name), "name")
}
if len(o.Format) > 0 {
fs := jsonutils.NewArray()
for _, f := range o.Format {
fs.Add(jsonutils.NewString(f))
}
params.Add(fs, "disk_formats")
}
if len(o.SubFormats) > 0 {
params.Add(jsonutils.Marshal(o.SubFormats), "sub_formats")
}
if len(o.OsType) > 0 {
params.Add(jsonutils.NewStringArray(o.OsType), "os_types")
}
if len(o.Distribution) > 0 {
params.Add(jsonutils.NewStringArray(o.Distribution), "distributions")
}
return params, nil
}
type ImageStatusStatisticsOptions struct {
ImageListOptions
options.StatusStatisticsOptions
}