From 2c043454a52a8fd662975f32dd7802879b9f577a Mon Sep 17 00:00:00 2001 From: lvyangyang Date: Fri, 23 Oct 2020 17:53:02 +0800 Subject: [PATCH] create with tags --- cmd/climc/shell/compute/dbinstances.go | 23 +------ cmd/climc/shell/compute/elasticcache.go | 2 +- cmd/climc/shell/compute/loadbalancers.go | 2 +- pkg/cloudprovider/dbinstance.go | 1 + pkg/cloudprovider/elasticcache.go | 1 + pkg/compute/models/dbinstances.go | 1 + pkg/compute/models/elasticcache_instances.go | 1 + pkg/compute/models/loadbalancers.go | 11 ++-- pkg/compute/regiondrivers/managedvirtual.go | 1 + pkg/mcclient/options/dbinstances.go | 65 ++++++++++++++++++++ pkg/mcclient/options/elasticcache.go | 39 ++++++++++-- pkg/mcclient/options/loadbalancers.go | 31 +++++++++- pkg/multicloud/qcloud/rds_mysql.go | 8 +++ pkg/multicloud/qcloud/region.go | 6 ++ 14 files changed, 159 insertions(+), 33 deletions(-) create mode 100644 pkg/mcclient/options/dbinstances.go diff --git a/cmd/climc/shell/compute/dbinstances.go b/cmd/climc/shell/compute/dbinstances.go index 453324d19d..15f52932ac 100644 --- a/cmd/climc/shell/compute/dbinstances.go +++ b/cmd/climc/shell/compute/dbinstances.go @@ -51,27 +51,8 @@ func init() { return nil }) - type DBInstanceCreateOptions struct { - NAME string `help:"DBInstance Name"` - InstanceType string `help:"InstanceType for DBInstance"` - VcpuCount int `help:"Core of cpu for DBInstance"` - VmemSizeMb int `help:"Memory size of DBInstance"` - Port int `help:"Port of DBInstance"` - Category string `help:"Category of DBInstance"` - Network string `help:"Network of DBInstance"` - Address string `help:"Address of DBInstance"` - Engine string `help:"Engine of DBInstance"` - EngineVersion string `help:"EngineVersion of DBInstance Engine"` - StorageType string `help:"StorageTyep of DBInstance"` - Secgroup string `help:"Secgroup name or Id for DBInstance"` - Zone string `help:"ZoneId or name for DBInstance"` - DiskSizeGB int `help:"Storage size for DBInstance"` - Duration string `help:"Duration for DBInstance"` - AllowDelete *bool `help:"not lock dbinstance" ` - } - - R(&DBInstanceCreateOptions{}, "dbinstance-create", "Create DB instance", func(s *mcclient.ClientSession, opts *DBInstanceCreateOptions) error { - params, err := options.StructToParams(opts) + R(&options.DBInstanceCreateOptions{}, "dbinstance-create", "Create DB instance", func(s *mcclient.ClientSession, opts *options.DBInstanceCreateOptions) error { + params, err := opts.Params() if err != nil { return err } diff --git a/cmd/climc/shell/compute/elasticcache.go b/cmd/climc/shell/compute/elasticcache.go index 25a2eb0669..31bc1cbf9b 100644 --- a/cmd/climc/shell/compute/elasticcache.go +++ b/cmd/climc/shell/compute/elasticcache.go @@ -41,7 +41,7 @@ func init() { }) R(&options.ElasticCacheCreateOptions{}, "elastic-cache-create", "Create elastisc cache instance", func(s *mcclient.ClientSession, opts *options.ElasticCacheCreateOptions) error { - params, err := options.StructToParams(opts) + params, err := opts.Params() if err != nil { return err } diff --git a/cmd/climc/shell/compute/loadbalancers.go b/cmd/climc/shell/compute/loadbalancers.go index 6ec2bfe22e..130d2dba0b 100644 --- a/cmd/climc/shell/compute/loadbalancers.go +++ b/cmd/climc/shell/compute/loadbalancers.go @@ -22,7 +22,7 @@ import ( func init() { R(&options.LoadbalancerCreateOptions{}, "lb-create", "Create lb", func(s *mcclient.ClientSession, opts *options.LoadbalancerCreateOptions) error { - params, err := options.StructToParams(opts) + params, err := opts.Params() if err != nil { return err } diff --git a/pkg/cloudprovider/dbinstance.go b/pkg/cloudprovider/dbinstance.go index 3bd5ac1417..9e5f32ac01 100644 --- a/pkg/cloudprovider/dbinstance.go +++ b/pkg/cloudprovider/dbinstance.go @@ -60,6 +60,7 @@ type SManagedDBInstanceCreateConfig struct { ProjectId string BillingCycle *billing.SBillingCycle + Tags map[string]string } type SManagedDBInstanceChangeConfig struct { diff --git a/pkg/cloudprovider/elasticcache.go b/pkg/cloudprovider/elasticcache.go index d5343c987f..c4a91f92cc 100644 --- a/pkg/cloudprovider/elasticcache.go +++ b/pkg/cloudprovider/elasticcache.go @@ -40,6 +40,7 @@ type SCloudElasticCacheInput struct { MaintainEnd string // 维护时间窗结束时间,格式为HH:mm:ss BC *billing.SBillingCycle // 包年包月 ProjectId string + Tags map[string]string } type SCloudElasticCacheAccountInput struct { diff --git a/pkg/compute/models/dbinstances.go b/pkg/compute/models/dbinstances.go index ee55238488..b777765688 100644 --- a/pkg/compute/models/dbinstances.go +++ b/pkg/compute/models/dbinstances.go @@ -1568,6 +1568,7 @@ func (self *SDBInstance) SyncWithCloudDBInstance(ctx context.Context, userCred m if err != nil { return err } + syncVirtualResourceMetadata(ctx, userCred, self, extInstance) db.OpsLog.LogSyncUpdate(self, diff, userCred) return nil } diff --git a/pkg/compute/models/elasticcache_instances.go b/pkg/compute/models/elasticcache_instances.go index 6ab14ee8e8..8894fc1e77 100644 --- a/pkg/compute/models/elasticcache_instances.go +++ b/pkg/compute/models/elasticcache_instances.go @@ -847,6 +847,7 @@ func (self *SElasticcache) GetCreateAliyunElasticcacheParams(data *jsonutils.JSO input.NetworkId = network.ExternalId } } + input.Tags, _ = self.GetAllUserMetadata() return input, nil } diff --git a/pkg/compute/models/loadbalancers.go b/pkg/compute/models/loadbalancers.go index 22ad7ed97d..a4436fd7d8 100644 --- a/pkg/compute/models/loadbalancers.go +++ b/pkg/compute/models/loadbalancers.go @@ -1034,6 +1034,7 @@ func (lb *SLoadbalancer) SyncWithCloudLoadbalancer(ctx context.Context, userCred if extLb.GetMetadata() != nil { lb.LBInfo = extLb.GetMetadata() } + syncVirtualResourceMetadata(ctx, userCred, lb, extLb) if vpcId := extLb.GetVpcId(); len(vpcId) > 0 { if vpc, err := db.FetchByExternalIdAndManagerId(VpcManager, vpcId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery { @@ -1235,12 +1236,12 @@ func (self *SLoadbalancer) PerformRemoteUpdate(ctx context.Context, userCred mcc return nil, nil } -func (guest *SLoadbalancer) StartRemoteUpdateTask(ctx context.Context, userCred mcclient.TokenCredential, replaceTags bool, parentTaskId string) error { +func (self *SLoadbalancer) StartRemoteUpdateTask(ctx context.Context, userCred mcclient.TokenCredential, replaceTags bool, parentTaskId string) error { data := jsonutils.NewDict() if replaceTags { data.Add(jsonutils.JSONTrue, "replace_tags") } - if task, err := taskman.TaskManager.NewTask(ctx, "LoadbalancerRemoteUpdateTask", guest, userCred, data, parentTaskId, "", nil); err != nil { + if task, err := taskman.TaskManager.NewTask(ctx, "LoadbalancerRemoteUpdateTask", self, userCred, data, parentTaskId, "", nil); err != nil { log.Errorln(err) return errors.Wrap(err, "Start LoadbalancerRemoteUpdateTask") } else { @@ -1249,11 +1250,11 @@ func (guest *SLoadbalancer) StartRemoteUpdateTask(ctx context.Context, userCred return nil } -func (guest *SLoadbalancer) OnMetadataUpdated(ctx context.Context, userCred mcclient.TokenCredential) { - if len(guest.ExternalId) == 0 { +func (self *SLoadbalancer) OnMetadataUpdated(ctx context.Context, userCred mcclient.TokenCredential) { + if len(self.ExternalId) == 0 { return } - err := guest.StartRemoteUpdateTask(ctx, userCred, false, "") + err := self.StartRemoteUpdateTask(ctx, userCred, false, "") if err != nil { log.Errorf("StartRemoteUpdateTask fail: %s", err) } diff --git a/pkg/compute/regiondrivers/managedvirtual.go b/pkg/compute/regiondrivers/managedvirtual.go index 1c4f804327..3c3f63ac46 100644 --- a/pkg/compute/regiondrivers/managedvirtual.go +++ b/pkg/compute/regiondrivers/managedvirtual.go @@ -1665,6 +1665,7 @@ func (self *SManagedVirtualizationRegionDriver) RequestCreateDBInstance(ctx cont Port: dbinstance.Port, Password: passwd, } + desc.Tags, _ = dbinstance.GetAllUserMetadata() _cloudprovider := dbinstance.GetCloudprovider() desc.ProjectId, err = _cloudprovider.SyncProject(ctx, userCred, dbinstance.ProjectId) diff --git a/pkg/mcclient/options/dbinstances.go b/pkg/mcclient/options/dbinstances.go new file mode 100644 index 0000000000..9f8e9eb913 --- /dev/null +++ b/pkg/mcclient/options/dbinstances.go @@ -0,0 +1,65 @@ +// 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 options + +import ( + "fmt" + "strings" + + "yunion.io/x/jsonutils" +) + +type DBInstanceCreateOptions struct { + NAME string `help:"DBInstance Name"` + InstanceType string `help:"InstanceType for DBInstance"` + VcpuCount int `help:"Core of cpu for DBInstance"` + VmemSizeMb int `help:"Memory size of DBInstance"` + Port int `help:"Port of DBInstance"` + Category string `help:"Category of DBInstance"` + Network string `help:"Network of DBInstance"` + Address string `help:"Address of DBInstance"` + Engine string `help:"Engine of DBInstance"` + EngineVersion string `help:"EngineVersion of DBInstance Engine"` + StorageType string `help:"StorageTyep of DBInstance"` + Secgroup string `help:"Secgroup name or Id for DBInstance"` + Zone string `help:"ZoneId or name for DBInstance"` + DiskSizeGB int `help:"Storage size for DBInstance"` + Duration string `help:"Duration for DBInstance"` + AllowDelete *bool `help:"not lock dbinstance" ` + Tags []string `help:"Tags info,prefix with 'user:', eg: user:project=default" json:"-"` +} + +func (opts *DBInstanceCreateOptions) Params() (*jsonutils.JSONDict, error) { + params, err := StructToParams(opts) + if err != nil { + return nil, err + } + Tagparams := jsonutils.NewDict() + for _, tag := range opts.Tags { + info := strings.Split(tag, "=") + if len(info) == 2 { + if len(info[0]) == 0 { + return nil, fmt.Errorf("invalidate tag info %s", tag) + } + Tagparams.Add(jsonutils.NewString(info[1]), info[0]) + } else if len(info) == 1 { + Tagparams.Add(jsonutils.NewString(info[0]), info[0]) + } else { + return nil, fmt.Errorf("invalidate tag info %s", tag) + } + } + params.Add(Tagparams, "__meta__") + return params, nil +} diff --git a/pkg/mcclient/options/elasticcache.go b/pkg/mcclient/options/elasticcache.go index 901acc640e..0a9c2b8383 100644 --- a/pkg/mcclient/options/elasticcache.go +++ b/pkg/mcclient/options/elasticcache.go @@ -14,7 +14,14 @@ package options -import computeapi "yunion.io/x/onecloud/pkg/apis/compute" +import ( + "fmt" + "strings" + + "yunion.io/x/jsonutils" + + computeapi "yunion.io/x/onecloud/pkg/apis/compute" +) type ElasticCacheCreateOptions struct { NAME string @@ -29,9 +36,33 @@ type ElasticCacheCreateOptions struct { PrivateIP string `help:"private ip address in specificated network"` Password string `help:"set auth password"` InstanceType string - CapacityMB string `help:"elastic cache capacity. required by huawei."` - BillingType string `choices:"postpaid|prepaid" default:"postpaid"` - Month int `help:"billing duration (unit:month)"` + CapacityMB string `help:"elastic cache capacity. required by huawei."` + BillingType string `choices:"postpaid|prepaid" default:"postpaid"` + Month int `help:"billing duration (unit:month)"` + Tags []string `help:"Tags info,prefix with 'user:', eg: user:project=default" json:"-"` +} + +func (opts *ElasticCacheCreateOptions) Params() (*jsonutils.JSONDict, error) { + params, err := StructToParams(opts) + if err != nil { + return nil, err + } + Tagparams := jsonutils.NewDict() + for _, tag := range opts.Tags { + info := strings.Split(tag, "=") + if len(info) == 2 { + if len(info[0]) == 0 { + return nil, fmt.Errorf("invalidate tag info %s", tag) + } + Tagparams.Add(jsonutils.NewString(info[1]), info[0]) + } else if len(info) == 1 { + Tagparams.Add(jsonutils.NewString(info[0]), info[0]) + } else { + return nil, fmt.Errorf("invalidate tag info %s", tag) + } + } + params.Add(Tagparams, "__meta__") + return params, nil } type ElasticCacheAccountCreateOptions struct { diff --git a/pkg/mcclient/options/loadbalancers.go b/pkg/mcclient/options/loadbalancers.go index 164fdf2e2b..5329dbf548 100644 --- a/pkg/mcclient/options/loadbalancers.go +++ b/pkg/mcclient/options/loadbalancers.go @@ -15,11 +15,17 @@ package options import ( + "fmt" + "strings" + + "yunion.io/x/jsonutils" + computeapi "yunion.io/x/onecloud/pkg/apis/compute" ) type LoadbalancerCreateOptions struct { NAME string + Vpc string Network string Address string AddressType string `choices:"intranet|internet"` @@ -30,7 +36,30 @@ type LoadbalancerCreateOptions struct { Zone1 string `json:"zone_1" help:"slave zone 1"` Cluster string `json:"cluster_id"` Manager string - Meta map[string]string `json:"__meta__"` + Tags []string `help:"Tags info,prefix with 'user:', eg: user:project=default" json:"-"` +} + +func (opts *LoadbalancerCreateOptions) Params() (*jsonutils.JSONDict, error) { + params, err := StructToParams(opts) + if err != nil { + return nil, err + } + Tagparams := jsonutils.NewDict() + for _, tag := range opts.Tags { + info := strings.Split(tag, "=") + if len(info) == 2 { + if len(info[0]) == 0 { + return nil, fmt.Errorf("invalidate tag info %s", tag) + } + Tagparams.Add(jsonutils.NewString(info[1]), info[0]) + } else if len(info) == 1 { + Tagparams.Add(jsonutils.NewString(info[0]), info[0]) + } else { + return nil, fmt.Errorf("invalidate tag info %s", tag) + } + } + params.Add(Tagparams, "__meta__") + return params, nil } type LoadbalancerGetOptions struct { diff --git a/pkg/multicloud/qcloud/rds_mysql.go b/pkg/multicloud/qcloud/rds_mysql.go index 6bd713b021..84b6851254 100644 --- a/pkg/multicloud/qcloud/rds_mysql.go +++ b/pkg/multicloud/qcloud/rds_mysql.go @@ -759,6 +759,14 @@ func (self *SRegion) CreateMySQLDBInstance(opts *cloudprovider.SManagedDBInstanc } } params["ClientToken"] = utils.GenRequestId(20) + + i := 0 + for k, v := range opts.Tags { + params[fmt.Sprintf("ResourceTags.%d.TagKey", i)] = k + params[fmt.Sprintf("ResourceTags.%d.TagValue", i)] = v + i++ + } + resp, err := self.cdbRequest(action, params) if err != nil { return nil, errors.Wrapf(err, "cdbRequest") diff --git a/pkg/multicloud/qcloud/region.go b/pkg/multicloud/qcloud/region.go index 2ae14b9293..91906ecf9a 100644 --- a/pkg/multicloud/qcloud/region.go +++ b/pkg/multicloud/qcloud/region.go @@ -200,6 +200,12 @@ func (self *SRegion) CreateILoadBalancer(loadbalancer *cloudprovider.SLoadbalanc } } } + i := 0 + for k, v := range loadbalancer.Tags { + params[fmt.Sprintf("Tags.%d.TagKey", i)] = k + params[fmt.Sprintf("Tags.%d.TagValue", i)] = v + i++ + } resp, err := self.clbRequest("CreateLoadBalancer", params) if err != nil {