From 29a0d421fcda83c1822d1419ad2e84415dbae1c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Wed, 19 Feb 2025 16:50:10 +0800 Subject: [PATCH] fix(climc): support create vm with tags (#22127) --- pkg/mcclient/options/compute/servers.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/mcclient/options/compute/servers.go b/pkg/mcclient/options/compute/servers.go index e1f1f1a81d..3827d61fc3 100644 --- a/pkg/mcclient/options/compute/servers.go +++ b/pkg/mcclient/options/compute/servers.go @@ -25,6 +25,7 @@ import ( "yunion.io/x/pkg/util/fileutils" "yunion.io/x/pkg/util/regutils" + "yunion.io/x/onecloud/pkg/apis/cloudcommon/db" computeapi "yunion.io/x/onecloud/pkg/apis/compute" schedapi "yunion.io/x/onecloud/pkg/apis/scheduler" "yunion.io/x/onecloud/pkg/cloudcommon/cmdline" @@ -468,7 +469,8 @@ type ServerCreateOptionalOptions struct { GuestImageID string `help:"create from guest image, need to specify the guest image id"` - EncryptKey string `help:"encryption key"` + EncryptKey string `help:"encryption key"` + Tags []string `help:"tags in the form of key=value"` } func (o *ServerCreateOptions) ToScheduleInput() (*schedapi.ScheduleInput, error) { @@ -633,6 +635,17 @@ func (opts *ServerCreateOptions) Params() (*computeapi.ServerCreateInput, error) if err != nil { return nil, err } + meta := map[string]string{} + for _, v := range opts.Tags { + tag := strings.Split(v, "=") + if len(tag) != 2 { + return nil, fmt.Errorf("invalid tag %s", v) + } + prefix := db.USER_TAG_PREFIX + k, v := prefix+strings.TrimPrefix(tag[0], prefix), tag[1] + meta[k] = v + } + params.Metadata = meta if opts.GenerateName { params.GenerateName = opts.NAME