mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 10:46:58 +08:00
fix: optimzed tag update and sync
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
|
||||
package cloudprovider
|
||||
|
||||
import "yunion.io/x/pkg/errors"
|
||||
|
||||
type SFakeOnPremiseRegion struct {
|
||||
}
|
||||
|
||||
@@ -54,7 +56,7 @@ func (region *SFakeOnPremiseRegion) GetSysTags() map[string]string {
|
||||
}
|
||||
|
||||
func (region *SFakeOnPremiseRegion) GetTags() (map[string]string, error) {
|
||||
return nil, nil
|
||||
return nil, errors.Wrap(ErrNotImplemented, "GetTags")
|
||||
}
|
||||
|
||||
func (region *SFakeOnPremiseRegion) SetTags(tags map[string]string, replace bool) error {
|
||||
|
||||
@@ -1229,26 +1229,39 @@ func (self *SManagedVirtualizedGuestDriver) RequestRemoteUpdate(ctx context.Cont
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "guest.GetIVM")
|
||||
}
|
||||
oldTags, err := iVM.GetTags()
|
||||
|
||||
err = func() error {
|
||||
oldTags, err := iVM.GetTags()
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "iVM.GetTags()")
|
||||
}
|
||||
tags, err := guest.GetAllUserMetadata()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "GetAllUserMetadata")
|
||||
}
|
||||
tagsUpdateInfo := cloudprovider.TagsUpdateInfo{OldTags: oldTags, NewTags: tags}
|
||||
err = iVM.SetTags(tags, replaceTags)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
|
||||
return nil
|
||||
}
|
||||
logclient.AddSimpleActionLog(guest, logclient.ACT_UPDATE_TAGS, err, userCred, false)
|
||||
return errors.Wrap(err, "iVM.SetTags")
|
||||
}
|
||||
logclient.AddSimpleActionLog(guest, logclient.ACT_UPDATE_TAGS, tagsUpdateInfo, userCred, true)
|
||||
// sync back cloud metadata
|
||||
iVM.Refresh()
|
||||
err = models.SyncVirtualResourceMetadata(ctx, userCred, guest, iVM)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "syncVirtualResourceMetadata")
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "iVM.GetTags()")
|
||||
}
|
||||
tags, err := guest.GetAllUserMetadata()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "GetAllUserMetadata")
|
||||
}
|
||||
tagsUpdateInfo := cloudprovider.TagsUpdateInfo{OldTags: oldTags, NewTags: tags}
|
||||
err = iVM.SetTags(tags, replaceTags)
|
||||
if err != nil {
|
||||
logclient.AddSimpleActionLog(guest, logclient.ACT_UPDATE_TAGS, err, userCred, false)
|
||||
return errors.Wrap(err, "iVM.SetTags")
|
||||
}
|
||||
logclient.AddSimpleActionLog(guest, logclient.ACT_UPDATE_TAGS, tagsUpdateInfo, userCred, true)
|
||||
// sync back cloud metadata
|
||||
iVM.Refresh()
|
||||
err = models.SyncVirtualResourceMetadata(ctx, userCred, guest, iVM)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "syncVirtualResourceMetadata")
|
||||
return err
|
||||
}
|
||||
|
||||
err = iVM.UpdateVM(ctx, guest.Name)
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"context"
|
||||
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
@@ -42,7 +41,7 @@ func syncMetadata(ctx context.Context, userCred mcclient.TokenCredential, model
|
||||
model.SetSysCloudMetadataAll(ctx, sysStore, userCred)
|
||||
|
||||
tags, err := remote.GetTags()
|
||||
if err == nil || errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
if err == nil {
|
||||
store := make(map[string]interface{}, 0)
|
||||
for key, value := range tags {
|
||||
store[db.CLOUD_TAG_PREFIX+key] = value
|
||||
@@ -71,7 +70,7 @@ func syncVirtualResourceMetadata(ctx context.Context, userCred mcclient.TokenCre
|
||||
model.SetSysCloudMetadataAll(ctx, sysStore, userCred)
|
||||
|
||||
tags, err := remote.GetTags()
|
||||
if err == nil || errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
if err == nil {
|
||||
store := make(map[string]interface{}, 0)
|
||||
for key, value := range tags {
|
||||
store[db.CLOUD_TAG_PREFIX+key] = value
|
||||
|
||||
@@ -332,6 +332,9 @@ func (self *SManagedVirtualizationRegionDriver) RequestRemoteUpdateLoadbalancer(
|
||||
}
|
||||
oldTags, err := iLoadbalancer.GetTags()
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, "iLoadbalancer.GetTags()")
|
||||
}
|
||||
tags, err := lb.GetAllUserMetadata()
|
||||
@@ -341,6 +344,9 @@ func (self *SManagedVirtualizationRegionDriver) RequestRemoteUpdateLoadbalancer(
|
||||
tagsUpdateInfo := cloudprovider.TagsUpdateInfo{OldTags: oldTags, NewTags: tags}
|
||||
err = iLoadbalancer.SetTags(tags, replaceTags)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
|
||||
return nil, nil
|
||||
}
|
||||
logclient.AddActionLogWithStartable(task, lb, logclient.ACT_UPDATE_TAGS, err, userCred, false)
|
||||
return nil, errors.Wrap(err, "iLoadbalancer.SetMetadata")
|
||||
}
|
||||
@@ -2628,7 +2634,10 @@ func (self *SManagedVirtualizationRegionDriver) RequestRemoteUpdateDBInstance(ct
|
||||
return nil, errors.Wrap(err, "instance.GetIDBInstance")
|
||||
}
|
||||
oldTags, err := iRds.GetTags()
|
||||
if err != nil && errors.Cause(err) != cloudprovider.ErrNotFound {
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, "iRds.GetTags()")
|
||||
}
|
||||
tags, err := instance.GetAllUserMetadata()
|
||||
@@ -2638,6 +2647,9 @@ func (self *SManagedVirtualizationRegionDriver) RequestRemoteUpdateDBInstance(ct
|
||||
tagsUpdateInfo := cloudprovider.TagsUpdateInfo{OldTags: oldTags, NewTags: tags}
|
||||
err = iRds.SetTags(tags, replaceTags)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
|
||||
return nil, nil
|
||||
}
|
||||
logclient.AddActionLogWithStartable(task, instance, logclient.ACT_UPDATE_TAGS, err, userCred, false)
|
||||
return nil, errors.Wrap(err, "iRds.SetTags")
|
||||
}
|
||||
@@ -3026,8 +3038,15 @@ func (self *SManagedVirtualizationRegionDriver) RequestRemoteUpdateElasticcache(
|
||||
}
|
||||
|
||||
iElasticcache, err := iRegion.GetIElasticcacheById(elasticcache.ExternalId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetIElasticcacheById(%s)", elasticcache.ExternalId)
|
||||
}
|
||||
|
||||
oldTags, err := iElasticcache.GetTags()
|
||||
if err != nil && errors.Cause(err) != cloudprovider.ErrNotFound {
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, "iElasticcache.GetTags()")
|
||||
}
|
||||
tags, err := elasticcache.GetAllUserMetadata()
|
||||
@@ -3037,6 +3056,10 @@ func (self *SManagedVirtualizationRegionDriver) RequestRemoteUpdateElasticcache(
|
||||
tagsUpdateInfo := cloudprovider.TagsUpdateInfo{OldTags: oldTags, NewTags: tags}
|
||||
err = iElasticcache.SetTags(tags, replaceTags)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotSupported || errors.Cause(err) == cloudprovider.ErrNotImplemented {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
logclient.AddActionLogWithStartable(task, elasticcache, logclient.ACT_UPDATE_TAGS, err, userCred, false)
|
||||
return nil, errors.Wrap(err, "iElasticcache.SetTags")
|
||||
}
|
||||
|
||||
@@ -102,9 +102,6 @@ func (self *SSecurityGroup) GetMetadata() *jsonutils.JSONDict {
|
||||
}
|
||||
|
||||
func (self *SSecurityGroup) GetTags() (map[string]string, error) {
|
||||
if len(self.Tags.Tag) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
tags := map[string]string{}
|
||||
for _, value := range self.Tags.Tag {
|
||||
tags[value.TagKey] = value.TagValue
|
||||
|
||||
@@ -798,7 +798,7 @@ func (rds *SDBInstance) GetTags() (map[string]string, error) {
|
||||
return nil, errors.Wrap(err, "rds.region.ListResourceTags")
|
||||
}
|
||||
if _, ok := tags[rds.GetId()]; !ok {
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
return *tags[rds.GetId()], nil
|
||||
}
|
||||
|
||||
@@ -943,7 +943,7 @@ func (instance *SElasticcache) GetTags() (map[string]string, error) {
|
||||
return nil, errors.Wrap(err, "instance.region.ListResourceTags")
|
||||
}
|
||||
if _, ok := tags[instance.GetId()]; !ok {
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
return *tags[instance.GetId()], nil
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ func (lb *SLoadbalancer) GetTags() (map[string]string, error) {
|
||||
return nil, errors.Wrap(err, "lb.region.ListResourceTags")
|
||||
}
|
||||
if _, ok := tags[lb.GetId()]; !ok {
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
return *tags[lb.GetId()], nil
|
||||
}
|
||||
|
||||
@@ -102,9 +102,6 @@ func (self *SSecurityGroup) GetMetadata() *jsonutils.JSONDict {
|
||||
}
|
||||
|
||||
func (self *SSecurityGroup) GetTags() (map[string]string, error) {
|
||||
if len(self.Tags.Tag) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
tags := map[string]string{}
|
||||
for _, value := range self.Tags.Tag {
|
||||
tags[value.TagKey] = value.TagValue
|
||||
|
||||
@@ -286,7 +286,7 @@ func (self *SInstance) GetTags() (map[string]string, error) {
|
||||
}
|
||||
tags, err := FetchTags(ec2Client, self.InstanceId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "FetchTags(self.host.zone.region.ec2Client, self.InstanceId)")
|
||||
return nil, errors.Wrap(err, "FetchTags()")
|
||||
}
|
||||
data := map[string]string{}
|
||||
err = tags.Unmarshal(&data)
|
||||
|
||||
@@ -1087,8 +1087,7 @@ func (b *SBucket) DeletePolicy(id []string) ([]cloudprovider.SBucketPolicyStatem
|
||||
func (b *SBucket) GetTags() (map[string]string, error) {
|
||||
coscli, err := b.region.GetCosClient(b)
|
||||
if err != nil {
|
||||
log.Errorf("GetCosClient fail %s", err)
|
||||
return nil, errors.Wrap(err, "b.region.GetCosClient(b)")
|
||||
return nil, errors.Wrap(err, "GetCosClient")
|
||||
}
|
||||
|
||||
tagresult, _, err := coscli.Bucket.GetTagging(context.Background())
|
||||
@@ -1096,7 +1095,7 @@ func (b *SBucket) GetTags() (map[string]string, error) {
|
||||
if strings.Contains(err.Error(), "404") {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, "coscli.Bucket.GetTagging(context.Background())")
|
||||
return nil, errors.Wrap(err, "GetTagging")
|
||||
}
|
||||
result := map[string]string{}
|
||||
for i := range tagresult.TagSet {
|
||||
|
||||
@@ -266,7 +266,7 @@ func (self *SElasticcache) GetTags() (map[string]string, error) {
|
||||
return nil, errors.Wrap(err, "self.region.FetchResourceTags")
|
||||
}
|
||||
if _, ok := tags[self.GetId()]; !ok {
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
return *tags[self.GetId()], nil
|
||||
}
|
||||
|
||||
@@ -220,7 +220,7 @@ func (self *SInstance) GetTags() (map[string]string, error) {
|
||||
if tags, ok := mtags[self.InstanceId]; ok {
|
||||
return *tags, nil
|
||||
}
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
|
||||
func (self *SInstance) getCloudMetadata() (map[string]string, error) {
|
||||
|
||||
@@ -903,7 +903,7 @@ func (self *SMySQLInstance) GetTags() (map[string]string, error) {
|
||||
return nil, errors.Wrap(err, "self.region.FetchResourceTags")
|
||||
}
|
||||
if _, ok := tags[self.GetId()]; !ok {
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
return *tags[self.GetId()], nil
|
||||
}
|
||||
|
||||
@@ -14,7 +14,11 @@
|
||||
|
||||
package multicloud
|
||||
|
||||
import "yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
import (
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
)
|
||||
|
||||
type SResourceBase struct{}
|
||||
|
||||
@@ -31,7 +35,7 @@ func (self *SResourceBase) GetSysTags() map[string]string {
|
||||
}
|
||||
|
||||
func (self *SResourceBase) GetTags() (map[string]string, error) {
|
||||
return nil, nil
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "GetTags")
|
||||
}
|
||||
|
||||
func (self *SResourceBase) SetTags(tags map[string]string, replace bool) error {
|
||||
|
||||
Reference in New Issue
Block a user