From 4adcabdd0fec384cbd4e6d88b441423e33f81ab5 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Tue, 31 Dec 2019 16:21:39 +0800 Subject: [PATCH] image batch update --- cmd/climc/shell/images.go | 13 +++++-------- pkg/mcclient/modules/mod_images.go | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/cmd/climc/shell/images.go b/cmd/climc/shell/images.go index 21587e2998..a892602625 100644 --- a/cmd/climc/shell/images.go +++ b/cmd/climc/shell/images.go @@ -221,9 +221,9 @@ func init() { }) type ImageUpdateOptions struct { - ID string `help:"ID or Name of Image"` - Name string `help:"New name of the image"` - Description string `help:"Description of image"` + ID []string `help:"ID or Name of Image"` + Name string `help:"New name of the image"` + Description string `help:"Description of image"` ImageOptionalOptions } @@ -239,11 +239,8 @@ func init() { if err != nil { return err } - img, err := modules.Images.Update(s, args.ID, params) - if err != nil { - return err - } - printObject(img) + result := modules.Images.BatchUpdate(s, args.ID, params) + printBatchResults(result, modules.Images.GetColumns(s)) return nil }) diff --git a/pkg/mcclient/modules/mod_images.go b/pkg/mcclient/modules/mod_images.go index a9b37c9b20..f253297051 100644 --- a/pkg/mcclient/modules/mod_images.go +++ b/pkg/mcclient/modules/mod_images.go @@ -540,6 +540,31 @@ func (this *ImageManager) _update(s *mcclient.ClientSession, id string, params j return json.Get("image") } +func (this *ImageManager) BatchUpdate( + session *mcclient.ClientSession, idlist []string, params jsonutils.JSONObject, +) []modulebase.SubmitResult { + return modulebase.BatchDo(idlist, func(id string) (jsonutils.JSONObject, error) { + var curParams = params.(*jsonutils.JSONDict).Copy() + img, err := this.Get(session, id, nil) + if err != nil { + return nil, err + } + properties, _ := img.Get("properties") + if properties != nil { + propDict := properties.(*jsonutils.JSONDict) + propMap, _ := propDict.GetMap() + if propMap != nil { + for k, val := range propMap { + if !curParams.Contains("properties", k) { + curParams.Add(val, "properties", k) + } + } + } + } + return this._update(session, id, curParams, nil) + }) +} + func (this *ImageManager) Download(s *mcclient.ClientSession, id string, format string, torrent bool) (jsonutils.JSONObject, io.Reader, int64, error) { query := jsonutils.NewDict() if len(format) > 0 {