Skip to content

Commit

Permalink
fix(region): 只读账号增量更新标签 (#18811)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito authored Nov 29, 2023
1 parent 74420ec commit 3bd3297
Show file tree
Hide file tree
Showing 55 changed files with 310 additions and 144 deletions.
4 changes: 2 additions & 2 deletions pkg/cloudcommon/db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ type IStandaloneModel interface {

SetUserMetadataValues(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential) error
SetUserMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential) error
SetCloudMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential) error
SetCloudMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential, readOnly bool) error
SetOrganizationMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential) error
SetSysCloudMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential) error
SetSysCloudMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential, readOnly bool) error

RemoveMetadata(ctx context.Context, key string, userCred mcclient.TokenCredential) error
RemoveAllMetadata(ctx context.Context, userCred mcclient.TokenCredential) error
Expand Down
15 changes: 15 additions & 0 deletions pkg/cloudcommon/db/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,21 @@ func (manager *SMetadataManager) rawSetValues(ctx context.Context, objType strin
return changes, nil
}

func (manager *SMetadataManager) SetAllWithoutDelelte(ctx context.Context, obj IModel, store map[string]interface{}, userCred mcclient.TokenCredential) error {
lockman.LockObject(ctx, obj)
defer lockman.ReleaseObject(ctx, obj)

changes, err := manager.rawSetValues(ctx, obj.Keyword(), obj.GetId(), infMap2StrMap(store), false, "")
if err != nil {
return errors.Wrap(err, "setValues")
}

if len(changes) > 0 {
OpsLog.LogEvent(obj.GetIModel(), ACT_SET_METADATA, jsonutils.Marshal(changes), userCred)
}
return nil
}

func (manager *SMetadataManager) SetAll(ctx context.Context, obj IModel, store map[string]interface{}, userCred mcclient.TokenCredential, delRange string) error {
lockman.LockObject(ctx, obj)
defer lockman.ReleaseObject(ctx, obj)
Expand Down
26 changes: 20 additions & 6 deletions pkg/cloudcommon/db/standalone_anon.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,30 @@ func (model *SStandaloneAnonResourceBase) SetUserMetadataAll(ctx context.Context
return nil
}

func (model *SStandaloneAnonResourceBase) SetCloudMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential) error {
func (model *SStandaloneAnonResourceBase) SetCloudMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential, readOnly bool) error {
var err error
dictStore, err := ensurePrefixString(dictstore, CLOUD_TAG_PREFIX)
if err != nil {
return errors.Wrap(err, "ensurePrefix")
}
err = Metadata.SetAll(ctx, model, dictStore, userCred, CLOUD_TAG_PREFIX)
if err != nil {
return errors.Wrap(err, "SetAll")
if readOnly {
err = Metadata.SetAllWithoutDelelte(ctx, model, dictStore, userCred)
if err != nil {
return errors.Wrap(err, "SetAll")
}
} else {
err = Metadata.SetAll(ctx, model, dictStore, userCred, CLOUD_TAG_PREFIX)
if err != nil {
return errors.Wrap(err, "SetAll")
}
}
userTags := map[string]interface{}{}
for k, v := range dictstore {
userTags[strings.Replace(k, CLOUD_TAG_PREFIX, USER_TAG_PREFIX, 1)] = v
}
if readOnly {
return Metadata.SetAllWithoutDelelte(ctx, model, userTags, userCred)
}
return Metadata.SetAll(ctx, model, userTags, userCred, USER_TAG_PREFIX)
}

Expand Down Expand Up @@ -494,12 +504,16 @@ func (model *SStandaloneAnonResourceBase) IsInSameClass(ctx context.Context, pMo
return IsInSameClass(ctx, model, pModel)
}

func (model *SStandaloneAnonResourceBase) SetSysCloudMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential) error {
func (model *SStandaloneAnonResourceBase) SetSysCloudMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential, readOnly bool) error {
dictStore, err := ensurePrefixString(dictstore, SYS_CLOUD_TAG_PREFIX)
if err != nil {
return errors.Wrap(err, "ensurePrefixString")
}
err = Metadata.SetAll(ctx, model, dictStore, userCred, SYS_CLOUD_TAG_PREFIX)
if readOnly {
err = Metadata.SetAllWithoutDelelte(ctx, model, dictStore, userCred)
} else {
err = Metadata.SetAll(ctx, model, dictStore, userCred, SYS_CLOUD_TAG_PREFIX)
}
if err != nil {
return errors.Wrap(err, "SetAll")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/guestdrivers/managedvirtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ func (drv *SManagedVirtualizedGuestDriver) RequestRemoteUpdate(ctx context.Conte
// sync back cloud metadata
iVM.Refresh()
guest.SyncOsInfo(ctx, userCred, iVM)
err = models.SyncVirtualResourceMetadata(ctx, userCred, guest, iVM)
err = models.SyncVirtualResourceMetadata(ctx, userCred, guest, iVM, false)
if err != nil {
return errors.Wrap(err, "syncVirtualResourceMetadata")
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/compute/hostdrivers/managedvirtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ func (self *SManagedVirtualizationHostDriver) RequestAllocateDiskOnStorage(ctx c

cloudprovider.WaitStatus(iDisk, api.DISK_READY, time.Second*5, time.Minute*5)

models.SyncVirtualResourceMetadata(ctx, task.GetUserCred(), disk, iDisk)
if account := host.GetCloudaccount(); account != nil {
models.SyncVirtualResourceMetadata(ctx, task.GetUserCred(), disk, iDisk, account.ReadOnly)
}

data := jsonutils.NewDict()
data.Add(jsonutils.NewInt(int64(iDisk.GetDiskSizeMB())), "disk_size")
Expand Down
9 changes: 7 additions & 2 deletions pkg/compute/models/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (self *SCloudregion) newFromCloudApp(ctx context.Context, userCred mcclient
return &app, errors.Wrap(result.AllError(), "unable to SyncAppEnvironments")
}
SyncCloudProject(ctx, userCred, &app, provider.GetOwnerId(), ext, provider.Id)
syncVirtualResourceMetadata(ctx, userCred, &app, ext)
syncVirtualResourceMetadata(ctx, userCred, &app, ext, false)

db.OpsLog.LogEvent(&app, db.ACT_CREATE, app.GetShortDesc(ctx), userCred)
notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{
Expand Down Expand Up @@ -372,7 +372,9 @@ func (a *SApp) SyncWithCloudApp(ctx context.Context, userCred mcclient.TokenCred
Action: notifyclient.ActionSyncUpdate,
})
}
syncVirtualResourceMetadata(ctx, userCred, a, ext)
if account, _ := provider.GetCloudaccount(); account != nil {
syncVirtualResourceMetadata(ctx, userCred, a, ext, account.ReadOnly)
}
return nil
}

Expand Down Expand Up @@ -448,6 +450,9 @@ func (self *SApp) OnMetadataUpdated(ctx context.Context, userCred mcclient.Token
if len(self.ExternalId) == 0 {
return
}
if account := self.GetCloudaccount(); account != nil && account.ReadOnly {
return
}
err := self.StartRemoteUpdateTask(ctx, userCred, true, "")
if err != nil {
log.Errorf("StartRemoteUpdateTask fail: %s", err)
Expand Down
15 changes: 12 additions & 3 deletions pkg/compute/models/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (manager *SBucketManager) newFromCloudBucket(
})
bucket.SyncShareState(ctx, userCred, provider.getAccountShareInfo())

syncVirtualResourceMetadata(ctx, userCred, &bucket, extBucket)
syncVirtualResourceMetadata(ctx, userCred, &bucket, extBucket, false)
db.OpsLog.LogEvent(&bucket, db.ACT_CREATE, bucket.GetShortDesc(ctx), userCred)

return &bucket, nil
Expand Down Expand Up @@ -318,7 +318,9 @@ func (bucket *SBucket) syncWithCloudBucket(
return errors.Wrap(err, "db.UpdateWithLock")
}

syncVirtualResourceMetadata(ctx, userCred, bucket, extBucket)
if account := bucket.GetCloudaccount(); account != nil {
syncVirtualResourceMetadata(ctx, userCred, bucket, extBucket, account.ReadOnly)
}

db.OpsLog.LogSyncUpdate(bucket, diff, userCred)
if len(diff) > 0 {
Expand Down Expand Up @@ -1738,6 +1740,9 @@ func (bucket *SBucket) OnMetadataUpdated(ctx context.Context, userCred mcclient.
if len(bucket.ExternalId) == 0 {
return
}
if account := bucket.GetCloudaccount(); account != nil && account.ReadOnly {
return
}
iBucket, err := bucket.GetIBucket(ctx)
if err != nil {
return
Expand All @@ -1754,7 +1759,11 @@ func (bucket *SBucket) OnMetadataUpdated(ctx context.Context, userCred mcclient.
if diff.IsChanged() {
logclient.AddSimpleActionLog(bucket, logclient.ACT_UPDATE_TAGS, diff, userCred, true)
}
syncVirtualResourceMetadata(ctx, userCred, bucket, iBucket)
readOnly := false
if account := bucket.GetCloudaccount(); account != nil {
readOnly = account.ReadOnly
}
syncVirtualResourceMetadata(ctx, userCred, bucket, iBucket, readOnly)
}

func (manager *SBucketManager) ListItemExportKeys(ctx context.Context,
Expand Down
7 changes: 5 additions & 2 deletions pkg/compute/models/cdn_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ func (self *SCDNDomain) SyncWithCloudCDNDomain(ctx context.Context, userCred mcc
})
}

syncVirtualResourceMetadata(ctx, userCred, self, ext)
if account := self.GetCloudaccount(); account != nil {
syncVirtualResourceMetadata(ctx, userCred, self, ext, account.ReadOnly)
}

if provider := self.GetCloudprovider(); provider != nil {
SyncCloudProject(ctx, userCred, self, provider.GetOwnerId(), ext, self.ManagerId)
}
Expand Down Expand Up @@ -319,7 +322,7 @@ func (self *SCloudprovider) newFromCloudCDNDomain(ctx context.Context, userCred
return nil, err
}

syncVirtualResourceMetadata(ctx, userCred, &domain, ext)
syncVirtualResourceMetadata(ctx, userCred, &domain, ext, false)
SyncCloudProject(ctx, userCred, &domain, self.GetOwnerId(), ext, self.Id)

db.OpsLog.LogEvent(&domain, db.ACT_CREATE, domain.GetShortDesc(ctx), userCred)
Expand Down
4 changes: 2 additions & 2 deletions pkg/compute/models/cloudregions.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func (manager *SCloudregionManager) SyncRegions(
if err != nil {
syncResult.UpdateError(err)
} else {
syncMetadata(ctx, userCred, &commondb[i], commonext[i])
syncMetadata(ctx, userCred, &commondb[i], commonext[i], false)
cpr := CloudproviderRegionManager.FetchByIdsOrCreate(cloudProvider.Id, commondb[i].Id)
cpr.setCapabilities(ctx, userCred, commonext[i].GetCapabilities())
cloudProviderRegions = append(cloudProviderRegions, *cpr)
Expand All @@ -553,7 +553,7 @@ func (manager *SCloudregionManager) SyncRegions(
if err != nil {
syncResult.AddError(err)
} else {
syncMetadata(ctx, userCred, new, added[i])
syncMetadata(ctx, userCred, new, added[i], false)
cpr := CloudproviderRegionManager.FetchByIdsOrCreate(cloudProvider.Id, new.Id)
cpr.setCapabilities(ctx, userCred, added[i].GetCapabilities())
cloudProviderRegions = append(cloudProviderRegions, *cpr)
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/models/cloudsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ func syncHostStorages(ctx context.Context, userCred mcclient.TokenCredential, sy

newCacheIds := make([]sStoragecacheSyncPair, 0)
for i := 0; i < len(localStorages); i += 1 {
syncMetadata(ctx, userCred, &localStorages[i], remoteStorages[i])
syncMetadata(ctx, userCred, &localStorages[i], remoteStorages[i], false)
if !isInCache(storageCachePairs, localStorages[i].StoragecacheId) && !isInCache(newCacheIds, localStorages[i].StoragecacheId) {
cachePair, err := syncStorageCaches(ctx, userCred, provider, &localStorages[i], remoteStorages[i], xor)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions pkg/compute/models/dbinstances.go
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,9 @@ func (self *SDBInstance) SyncWithCloudDBInstance(ctx context.Context, userCred m
if err != nil {
return err
}
syncVirtualResourceMetadata(ctx, userCred, self, ext)
if account := self.GetCloudaccount(); account != nil {
syncVirtualResourceMetadata(ctx, userCred, self, ext, account.ReadOnly)
}
SyncCloudProject(ctx, userCred, self, provider.GetOwnerId(), ext, provider.Id)
db.OpsLog.LogSyncUpdate(self, diff, userCred)
if len(diff) > 0 {
Expand Down Expand Up @@ -1832,7 +1834,7 @@ func (manager *SDBInstanceManager) newFromCloudDBInstance(ctx context.Context, u
return nil, errors.Wrapf(err, "newFromCloudDBInstance.Insert")
}

syncVirtualResourceMetadata(ctx, userCred, &instance, extInstance)
syncVirtualResourceMetadata(ctx, userCred, &instance, extInstance, false)
SyncCloudProject(ctx, userCred, &instance, provider.GetOwnerId(), extInstance, provider.Id)

db.OpsLog.LogEvent(&instance, db.ACT_CREATE, instance.GetShortDesc(ctx), userCred)
Expand Down Expand Up @@ -2088,6 +2090,9 @@ func (self *SDBInstance) OnMetadataUpdated(ctx context.Context, userCred mcclien
if len(self.ExternalId) == 0 {
return
}
if account := self.GetCloudaccount(); account != nil && account.ReadOnly {
return
}
err := self.StartRemoteUpdateTask(ctx, userCred, true, "")
if err != nil {
log.Errorf("StartRemoteUpdateTask fail: %s", err)
Expand Down
10 changes: 6 additions & 4 deletions pkg/compute/models/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,9 @@ func (self *SDisk) syncWithCloudDisk(ctx context.Context, userCred mcclient.Toke
})
}

syncVirtualResourceMetadata(ctx, userCred, self, extDisk)
if account := storage.GetCloudaccount(); account != nil {
syncVirtualResourceMetadata(ctx, userCred, self, extDisk, account.ReadOnly)
}

if len(guests) == 0 {
SyncCloudProject(ctx, userCred, self, syncOwnerId, extDisk, storage.ManagerId)
Expand Down Expand Up @@ -1823,7 +1825,7 @@ func (manager *SDiskManager) newFromCloudDisk(ctx context.Context, userCred mccl
log.Warningln("SyncAttachDiskExt:", err)
}

syncVirtualResourceMetadata(ctx, userCred, &disk, extDisk)
syncVirtualResourceMetadata(ctx, userCred, &disk, extDisk, false)

SyncCloudProject(ctx, userCred, &disk, syncOwnerId, extDisk, storage.ManagerId)

Expand Down Expand Up @@ -3100,7 +3102,7 @@ func (self *SDisk) syncSnapshots(ctx context.Context, userCred mcclient.TokenCre
if err != nil {
syncResult.UpdateError(err)
} else {
syncMetadata(ctx, userCred, &commondb[i], commonext[i])
syncMetadata(ctx, userCred, &commondb[i], commonext[i], account.ReadOnly)
syncResult.Update()
}
if !hasCreating && commonext[i].GetStatus() == api.SNAPSHOT_CREATING {
Expand All @@ -3112,7 +3114,7 @@ func (self *SDisk) syncSnapshots(ctx context.Context, userCred mcclient.TokenCre
if err != nil {
syncResult.AddError(err)
} else {
syncMetadata(ctx, userCred, local, added[i])
syncMetadata(ctx, userCred, local, added[i], false)
syncResult.Add()
}
if !hasCreating && added[i].GetStatus() == api.SNAPSHOT_CREATING {
Expand Down
6 changes: 4 additions & 2 deletions pkg/compute/models/dnszones.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,10 @@ func (self *SDnsZone) syncWithDnsZone(ctx context.Context, userCred mcclient.Tok
}

privider := self.GetCloudprovider()
syncVirtualResourceMetadata(ctx, userCred, self, ext)
if privider != nil {
if account, _ := provider.GetCloudaccount(); account != nil {
syncVirtualResourceMetadata(ctx, userCred, self, ext, account.ReadOnly)
}
SyncCloudProject(ctx, userCred, self, provider.GetOwnerId(), ext, self.ManagerId)
}

Expand All @@ -531,7 +533,7 @@ func (self *SCloudprovider) newFromCloudDnsZone(ctx context.Context, userCred mc
return nil, errors.Wrapf(err, "Insert")
}

syncVirtualResourceMetadata(ctx, userCred, zone, ext)
syncVirtualResourceMetadata(ctx, userCred, zone, ext, false)
SyncCloudProject(ctx, userCred, zone, self.GetOwnerId(), ext, self.Id)

return zone, nil
Expand Down
10 changes: 8 additions & 2 deletions pkg/compute/models/elastic_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,10 @@ func (self *SElasticSearch) SyncWithCloudElasticSearch(ctx context.Context, user
})
}

syncVirtualResourceMetadata(ctx, userCred, self, ext)
if account := self.GetCloudaccount(); account != nil {
syncVirtualResourceMetadata(ctx, userCred, self, ext, account.ReadOnly)
}

if provider := self.GetCloudprovider(); provider != nil {
SyncCloudProject(ctx, userCred, self, provider.GetOwnerId(), ext, provider.Id)
}
Expand Down Expand Up @@ -610,7 +613,7 @@ func (self *SCloudregion) newFromCloudElasticSearch(ctx context.Context, userCre
Action: notifyclient.ActionSyncCreate,
})
// 同步标签
syncVirtualResourceMetadata(ctx, userCred, &es, ext)
syncVirtualResourceMetadata(ctx, userCred, &es, ext, false)
// 同步项目归属
SyncCloudProject(ctx, userCred, &es, provider.GetOwnerId(), ext, provider.Id)

Expand Down Expand Up @@ -696,6 +699,9 @@ func (self *SElasticSearch) OnMetadataUpdated(ctx context.Context, userCred mccl
if len(self.ExternalId) == 0 {
return
}
if account := self.GetCloudaccount(); account != nil && account.ReadOnly {
return
}
err := self.StartRemoteUpdateTask(ctx, userCred, true, "")
if err != nil {
log.Errorf("StartRemoteUpdateTask fail: %s", err)
Expand Down
9 changes: 7 additions & 2 deletions pkg/compute/models/elasticcache_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,9 @@ func (self *SElasticcache) SyncWithCloudElasticcache(ctx context.Context, userCr
return errors.Wrapf(err, "syncWithCloudElasticcache.Update")
}
SyncCloudProject(ctx, userCred, self, provider.GetOwnerId(), extInstance, provider.Id)
syncVirtualResourceMetadata(ctx, userCred, self, extInstance)
if account := self.GetCloudaccount(); account != nil {
syncVirtualResourceMetadata(ctx, userCred, self, extInstance, account.ReadOnly)
}
db.OpsLog.LogSyncUpdate(self, diff, userCred)
if len(diff) > 0 {
notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{
Expand Down Expand Up @@ -765,7 +767,7 @@ func (self *SCloudregion) newFromCloudElasticcache(ctx context.Context, userCred
}

SyncCloudProject(ctx, userCred, &instance, provider.GetOwnerId(), extInstance, provider.Id)
syncVirtualResourceMetadata(ctx, userCred, &instance, extInstance)
syncVirtualResourceMetadata(ctx, userCred, &instance, extInstance, false)
db.OpsLog.LogEvent(&instance, db.ACT_CREATE, instance.GetShortDesc(ctx), userCred)

notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{
Expand Down Expand Up @@ -1718,6 +1720,9 @@ func (self *SElasticcache) OnMetadataUpdated(ctx context.Context, userCred mccli
if len(self.ExternalId) == 0 {
return
}
if account := self.GetCloudaccount(); account != nil && account.ReadOnly {
return
}
err := self.StartRemoteUpdateTask(ctx, userCred, true, "")
if err != nil {
log.Errorf("StartRemoteUpdateTask fail: %s", err)
Expand Down
Loading

0 comments on commit 3bd3297

Please sign in to comment.