diff --git a/cmd/climc/shell/image/images.go b/cmd/climc/shell/image/images.go index 2941adf77c..1fef1980cb 100644 --- a/cmd/climc/shell/image/images.go +++ b/cmd/climc/shell/image/images.go @@ -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"` diff --git a/pkg/mcclient/modules/image/mod_images.go b/pkg/mcclient/modules/image/mod_images.go index a2c2f64394..c1b0e44be5 100644 --- a/pkg/mcclient/modules/image/mod_images.go +++ b/pkg/mcclient/modules/image/mod_images.go @@ -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) diff --git a/pkg/mcclient/options/glance/doc.go b/pkg/mcclient/options/glance/doc.go new file mode 100644 index 0000000000..dd925dedce --- /dev/null +++ b/pkg/mcclient/options/glance/doc.go @@ -0,0 +1 @@ +package glance // import "yunion.io/x/onecloud/pkg/mcclient/options/glance" diff --git a/pkg/mcclient/options/glance/image.go b/pkg/mcclient/options/glance/image.go new file mode 100644 index 0000000000..762e4d3e46 --- /dev/null +++ b/pkg/mcclient/options/glance/image.go @@ -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 +}