fix(region): aliyun rds tags sync

This commit is contained in:
Qu Xuan
2021-10-12 18:04:08 +08:00
parent 4e309a24ed
commit baa6557b82
3 changed files with 84 additions and 2 deletions
@@ -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"
+24 -1
View File
@@ -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)
}
+59
View File
@@ -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