mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-07-01 02:24:47 +08:00
feature(region): sync cloud tags to local user tags (#9752)
* refact(region): sync cloud tags to user tags * fix(region): sync exttags to user tags Co-authored-by: lvyangyang <lvyangyang@yunion.cn> Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
@@ -605,7 +605,7 @@ func (manager *SMetadataManager) GetAll(obj IModel, keys []string, keyPrefix str
|
||||
}
|
||||
ret := make(map[string]string)
|
||||
for _, rec := range records {
|
||||
if len(rec.Value) > 0 || strings.HasPrefix(rec.Key, USER_TAG_PREFIX) {
|
||||
if len(rec.Value) > 0 || strings.HasPrefix(rec.Key, USER_TAG_PREFIX) || strings.HasPrefix(rec.Key, CLOUD_TAG_PREFIX) {
|
||||
ret[rec.Key] = rec.Value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
@@ -290,6 +291,21 @@ func (model *SStandaloneAnonResourceBase) SetCloudMetadataAll(ctx context.Contex
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "SetAll")
|
||||
}
|
||||
userTags, err := model.GetAllUserMetadata()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "model.GetAllUserMetadata()")
|
||||
}
|
||||
cloudTags, err := model.GetAllCloudMetadata()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "model.GetAllCloudMetadata()")
|
||||
}
|
||||
if !reflect.DeepEqual(userTags, cloudTags) {
|
||||
cloudTags2 := make(map[string]interface{})
|
||||
for k, v := range cloudTags {
|
||||
cloudTags2[USER_TAG_PREFIX+k] = v
|
||||
}
|
||||
return model.SetUserMetadataAll(ctx, cloudTags2, userCred)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -333,6 +349,18 @@ func (model *SStandaloneAnonResourceBase) GetAllUserMetadata() (map[string]strin
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (model *SStandaloneAnonResourceBase) GetAllCloudMetadata() (map[string]string, error) {
|
||||
meta, err := Metadata.GetAll(model, nil, CLOUD_TAG_PREFIX, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Metadata.GetAll")
|
||||
}
|
||||
ret := make(map[string]string)
|
||||
for k, v := range meta {
|
||||
ret[k[len(CLOUD_TAG_PREFIX):]] = v
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (model *SStandaloneAnonResourceBase) AllowGetDetailsMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
||||
return IsAllowGetSpec(rbacutils.ScopeSystem, userCred, model, "metadata")
|
||||
}
|
||||
|
||||
@@ -1878,7 +1878,9 @@ func (bucket *SBucket) OnMetadataUpdated(ctx context.Context, userCred mcclient.
|
||||
err = cloudprovider.SetBucketMetadata(iBucket, tags, false)
|
||||
if err != nil {
|
||||
log.Errorf("iBucket.SetMetadata failed: %s", err)
|
||||
return
|
||||
}
|
||||
syncMetadata(ctx, userCred, bucket, iBucket)
|
||||
db.OpsLog.LogEvent(bucket, db.ACT_UPDATE_TAGS, tags, userCred)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user