From baa6557b82ffd42cc4b66217240814bae4ca89f0 Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Tue, 12 Oct 2021 15:36:28 +0800 Subject: [PATCH] fix(region): aliyun rds tags sync --- .../storagehandler/storagehandler.go | 2 +- pkg/multicloud/aliyun/dbinstance.go | 25 +++++++- pkg/multicloud/huawei/dbinstance.go | 59 +++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/pkg/hostman/storageman/storagehandler/storagehandler.go b/pkg/hostman/storageman/storagehandler/storagehandler.go index 4baffec18b..852af631d6 100644 --- a/pkg/hostman/storageman/storagehandler/storagehandler.go +++ b/pkg/hostman/storageman/storagehandler/storagehandler.go @@ -19,9 +19,9 @@ import ( "fmt" "net/http" - "github.com/pkg/errors" "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/appsrv" "yunion.io/x/onecloud/pkg/cloudprovider" diff --git a/pkg/multicloud/aliyun/dbinstance.go b/pkg/multicloud/aliyun/dbinstance.go index f7dc02920b..0e7edcd396 100644 --- a/pkg/multicloud/aliyun/dbinstance.go +++ b/pkg/multicloud/aliyun/dbinstance.go @@ -46,7 +46,6 @@ type SDBInstanceExtra struct { type SDBInstance struct { multicloud.SDBInstanceBase - multicloud.AliyunTags netInfo []SDBInstanceNetwork @@ -852,6 +851,30 @@ func (region *SRegion) RenewDBInstance(instanceId string, bc billing.SBillingCyc return err } +func (self *SDBInstance) GetTags() (map[string]string, error) { + _, tags, err := self.region.ListSysAndUserTags(ALIYUN_SERVICE_RDS, "INSTANCE", self.DBInstanceId) + if err != nil { + return nil, errors.Wrapf(err, "ListTags") + } + tagMaps := map[string]string{} + for k, v := range tags { + tagMaps[strings.ToLower(k)] = v + } + return tagMaps, nil +} + +func (self *SDBInstance) GetSysTags() map[string]string { + tags, _, err := self.region.ListSysAndUserTags(ALIYUN_SERVICE_RDS, "INSTANCE", self.DBInstanceId) + if err != nil { + return nil + } + tagMaps := map[string]string{} + for k, v := range tags { + tagMaps[strings.ToLower(k)] = v + } + return tagMaps +} + func (rds *SDBInstance) SetTags(tags map[string]string, replace bool) error { return rds.region.SetResourceTags(ALIYUN_SERVICE_RDS, "INSTANCE", rds.GetId(), tags, replace) } diff --git a/pkg/multicloud/huawei/dbinstance.go b/pkg/multicloud/huawei/dbinstance.go index 955ade7a88..7841038f56 100644 --- a/pkg/multicloud/huawei/dbinstance.go +++ b/pkg/multicloud/huawei/dbinstance.go @@ -681,6 +681,65 @@ func (region *SRegion) ChangeDBInstanceConfig(instanceId string, instanceType st return nil } +func (self *SDBInstance) SetTags(tags map[string]string, replace bool) error { + existedTags, err := self.GetTags() + if err != nil { + return errors.Wrap(err, "self.GetTags()") + } + deleteTagsKey := []string{} + for k := range existedTags { + if replace { + deleteTagsKey = append(deleteTagsKey, k) + } else { + if _, ok := tags[k]; ok { + deleteTagsKey = append(deleteTagsKey, k) + } + } + } + if len(deleteTagsKey) > 0 { + err := self.region.DeleteRdsTags(self.GetId(), deleteTagsKey) + if err != nil { + return errors.Wrapf(err, "DeleteRdsTags") + } + } + if len(tags) > 0 { + err := self.region.CreateRdsTags(self.GetId(), tags) + if err != nil { + return errors.Wrapf(err, "CreateRdsTags") + } + } + return nil +} + +func (self *SRegion) DeleteRdsTags(instanceId string, tagsKey []string) error { + params := map[string]interface{}{ + "action": "delete", + } + tagsObj := []map[string]string{} + for _, k := range tagsKey { + tagsObj = append(tagsObj, map[string]string{"key": k}) + } + params["tags"] = tagsObj + + _, err := self.ecsClient.DBInstance.PerformAction2("tags/action", instanceId, jsonutils.Marshal(params), "") + return err +} + +func (self *SRegion) CreateRdsTags(instanceId string, tags map[string]string) error { + params := map[string]interface{}{ + "action": "create", + } + + tagsObj := []map[string]string{} + for k, v := range tags { + tagsObj = append(tagsObj, map[string]string{"key": k, "value": v}) + } + params["tags"] = tagsObj + + _, err := self.ecsClient.DBInstance.PerformAction2("tags/action", instanceId, jsonutils.Marshal(params), "") + return err +} + func (rds *SDBInstance) RecoveryFromBackup(conf *cloudprovider.SDBInstanceRecoveryConfig) error { if len(conf.OriginDBInstanceExternalId) == 0 { conf.OriginDBInstanceExternalId = rds.Id