Skip to content

Commit

Permalink
fix: 支持同步project标签到本地资源
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu Xuan committed Jun 30, 2020
1 parent b25295b commit e31334b
Show file tree
Hide file tree
Showing 20 changed files with 134 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pkg/compute/hostdrivers/managedvirtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (self *SManagedVirtualizationHostDriver) RequestAllocateDiskOnStorage(ctx c

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

models.SyncMetadata(ctx, task.GetUserCred(), disk, iDisk)
models.SyncVirtualResourceMetadata(ctx, task.GetUserCred(), disk, iDisk)

data := jsonutils.NewDict()
data.Add(jsonutils.NewInt(int64(iDisk.GetDiskSizeMB())), "disk_size")
Expand Down
8 changes: 6 additions & 2 deletions pkg/compute/models/cloudaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2899,19 +2899,23 @@ func (self *SCloudaccount) SyncProject(ctx context.Context, userCred mcclient.To
lockman.LockRawObject(ctx, self.Id, id)
defer lockman.ReleaseRawObject(ctx, self.Id, id)

project, _, err := self.GetExternalProject(ctx, userCred, id)
project, projectName, err := self.GetExternalProject(ctx, userCred, id)
if err == nil {
return project.ExternalId, nil
}
if err != cloudprovider.ErrNotFound {
return "", err
}

if len(projectName) == 0 {
return "", fmt.Errorf("empty project name")
}

provider, err := self.GetProvider()
if err != nil {
return "", errors.Wrap(err, "GetProvider")
}
iProject, err := provider.CreateIProject(project.GetName())
iProject, err := provider.CreateIProject(projectName)
if err != nil {
return "", errors.Wrap(err, "CreateIProject")
}
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 @@ -636,7 +636,7 @@ func syncHostVMs(ctx context.Context, userCred mcclient.TokenCredential, syncRes
}

func syncVMPeripherals(ctx context.Context, userCred mcclient.TokenCredential, local *SGuest, remote cloudprovider.ICloudVM, host *SHost, provider *SCloudprovider, driver cloudprovider.ICloudProvider) {
syncMetadata(ctx, userCred, local, remote)
syncVirtualResourceMetadata(ctx, userCred, local, remote)
err := syncVMNics(ctx, userCred, provider, host, local, remote)
if err != nil {
log.Errorf("syncVMNics error %s", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/compute/models/dbinstances.go
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ func (manager *SDBInstanceManager) SyncDBInstances(ctx context.Context, userCred
syncResult.UpdateError(err)
continue
}
syncMetadata(ctx, userCred, &commondb[i], commonext[i])
syncVirtualResourceMetadata(ctx, userCred, &commondb[i], commonext[i])
localDBInstances = append(localDBInstances, commondb[i])
remoteDBInstances = append(remoteDBInstances, commonext[i])
syncResult.Update()
Expand All @@ -1320,7 +1320,7 @@ func (manager *SDBInstanceManager) SyncDBInstances(ctx context.Context, userCred
syncResult.AddError(err)
continue
}
syncMetadata(ctx, userCred, instance, added[i])
syncVirtualResourceMetadata(ctx, userCred, instance, added[i])
localDBInstances = append(localDBInstances, *instance)
remoteDBInstances = append(remoteDBInstances, added[i])
syncResult.Add()
Expand Down
12 changes: 10 additions & 2 deletions pkg/compute/models/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,14 @@ func (self *SDisk) IsLocal() bool {
return false
}

func (self *SDisk) GetCloudproviderId() string {
storage := self.GetStorage()
if storage != nil {
return storage.GetCloudproviderId()
}
return ""
}

func (self *SDisk) GetStorage() *SStorage {
store, _ := StorageManager.FetchById(self.StorageId)
if store != nil {
Expand Down Expand Up @@ -1284,7 +1292,7 @@ func (manager *SDiskManager) SyncDisks(ctx context.Context, userCred mcclient.To
if err != nil {
syncResult.UpdateError(err)
} else {
syncMetadata(ctx, userCred, &commondb[i], commonext[i])
syncVirtualResourceMetadata(ctx, userCred, &commondb[i], commonext[i])
localDisks = append(localDisks, commondb[i])
remoteDisks = append(remoteDisks, commonext[i])
syncResult.Update()
Expand Down Expand Up @@ -1314,7 +1322,7 @@ func (manager *SDiskManager) SyncDisks(ctx context.Context, userCred mcclient.To
if err != nil {
syncResult.AddError(err)
} else {
syncMetadata(ctx, userCred, new, added[i])
syncVirtualResourceMetadata(ctx, userCred, new, added[i])
localDisks = append(localDisks, *new)
remoteDisks = append(remoteDisks, added[i])
syncResult.Add()
Expand Down
4 changes: 2 additions & 2 deletions pkg/compute/models/elasticcache_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func (manager *SElasticcacheManager) SyncElasticcaches(ctx context.Context, user
syncResult.UpdateError(err)
continue
}
syncMetadata(ctx, userCred, &commondb[i], commonext[i])
syncVirtualResourceMetadata(ctx, userCred, &commondb[i], commonext[i])
localElasticcaches = append(localElasticcaches, commondb[i])
remoteElasticcaches = append(remoteElasticcaches, commonext[i])
syncResult.Update()
Expand All @@ -488,7 +488,7 @@ func (manager *SElasticcacheManager) SyncElasticcaches(ctx context.Context, user
syncResult.AddError(err)
continue
}
syncMetadata(ctx, userCred, instance, added[i])
syncVirtualResourceMetadata(ctx, userCred, instance, added[i])
localElasticcaches = append(localElasticcaches, *instance)
remoteElasticcaches = append(remoteElasticcaches, added[i])
syncResult.Add()
Expand Down
4 changes: 2 additions & 2 deletions pkg/compute/models/elasticips.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (manager *SElasticipManager) SyncEips(ctx context.Context, userCred mcclien
if err != nil {
syncResult.UpdateError(err)
} else {
syncMetadata(ctx, userCred, &commondb[i], commonext[i])
syncVirtualResourceMetadata(ctx, userCred, &commondb[i], commonext[i])
syncResult.Update()
}
}
Expand All @@ -371,7 +371,7 @@ func (manager *SElasticipManager) SyncEips(ctx context.Context, userCred mcclien
if err != nil {
syncResult.AddError(err)
} else {
syncMetadata(ctx, userCred, new, added[i])
syncVirtualResourceMetadata(ctx, userCred, new, added[i])
syncResult.Add()
}
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/compute/models/guests.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,14 @@ func (guest *SGuest) CustomizeCreate(ctx context.Context, userCred mcclient.Toke
return guest.SVirtualResourceBase.CustomizeCreate(ctx, userCred, ownerId, query, data)
}

func (guest *SGuest) GetCloudproviderId() string {
host := guest.GetHost()
if host != nil {
return host.GetCloudproviderId()
}
return ""
}

func (guest *SGuest) GetHost() *SHost {
if len(guest.HostId) > 0 && regutils.MatchUUID(guest.HostId) {
host, _ := HostManager.FetchById(guest.HostId)
Expand Down
8 changes: 8 additions & 0 deletions pkg/compute/models/loadbalancerbackends.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ func (lbb *SLoadbalancerBackend) AllowPerformStatus(ctx context.Context, userCre
return false
}

func (lbb *SLoadbalancerBackend) GetCloudproviderId() string {
lbbg := lbb.GetLoadbalancerBackendGroup()
if lbbg != nil {
return lbbg.GetCloudproviderId()
}
return ""
}

func (lbb *SLoadbalancerBackend) GetLoadbalancerBackendGroup() *SLoadbalancerBackendGroup {
backendgroup, err := LoadbalancerBackendGroupManager.FetchById(lbb.BackendGroupId)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/compute/models/loadbalancerlistenerresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func (self *SLoadbalancerListenerResourceBase) GetLoadbalancerListener() *SLoadb
return listener.(*SLoadbalancerListener)
}

func (self *SLoadbalancerListenerResourceBase) GetCloudproviderId() string {
cloudprovider := self.GetCloudprovider()
if cloudprovider != nil {
return cloudprovider.Id
}
return ""
}

func (self *SLoadbalancerListenerResourceBase) GetCloudprovider() *SCloudprovider {
listener := self.GetLoadbalancerListener()
if listener != nil {
Expand Down
8 changes: 6 additions & 2 deletions pkg/compute/models/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ func (lb *SLoadbalancer) GetRegion() *SCloudregion {
return lb.SCloudregionResourceBase.GetRegion()
}

func (lb *SLoadbalancer) GetCloudproviderId() string {
return lb.SManagedResourceBase.GetCloudproviderId()
}

func (lb *SLoadbalancer) GetZone() *SZone {
return lb.SZoneResourceBase.GetZone()
}
Expand Down Expand Up @@ -752,7 +756,7 @@ func (man *SLoadbalancerManager) SyncLoadbalancers(ctx context.Context, userCred
if err != nil {
syncResult.UpdateError(err)
} else {
syncMetadata(ctx, userCred, &commondb[i], commonext[i])
syncVirtualResourceMetadata(ctx, userCred, &commondb[i], commonext[i])
localLbs = append(localLbs, commondb[i])
remoteLbs = append(remoteLbs, commonext[i])
syncResult.Update()
Expand All @@ -763,7 +767,7 @@ func (man *SLoadbalancerManager) SyncLoadbalancers(ctx context.Context, userCred
if err != nil {
syncResult.AddError(err)
} else {
syncMetadata(ctx, userCred, new, added[i])
syncVirtualResourceMetadata(ctx, userCred, new, added[i])
localLbs = append(localLbs, *new)
remoteLbs = append(remoteLbs, added[i])
syncResult.Add()
Expand Down
4 changes: 4 additions & 0 deletions pkg/compute/models/managedresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type SManagedResourceBaseManager struct {
managerIdFieldName string
}

func (self *SManagedResourceBase) GetCloudproviderId() string {
return self.ManagerId
}

func ValidateCloudproviderResourceInput(userCred mcclient.TokenCredential, query api.CloudproviderResourceInput) (*SCloudprovider, api.CloudproviderResourceInput, error) {
managerObj, err := CloudproviderManager.FetchByIdOrName(userCred, query.Cloudprovider)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/compute/models/natdtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ func (manager *SNatDEntryManager) SyncNatDTable(ctx context.Context, userCred mc
return result
}

func (self *SNatDEntry) GetCloudproviderId() string {
nat, _ := self.GetNatgateway()
if nat != nil {
return nat.GetCloudproviderId()
}
return ""
}

func (self *SNatDEntry) syncRemoveCloudNatDTable(ctx context.Context, userCred mcclient.TokenCredential) error {
lockman.LockObject(ctx, self)
defer lockman.ReleaseObject(ctx, self)
Expand Down
8 changes: 8 additions & 0 deletions pkg/compute/models/natstable.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ type SNatSEntry struct {
SourceCIDR string `width:"22" charset:"ascii" list:"user" create:"required"`
}

func (self *SNatSEntry) GetCloudproviderId() string {
network, err := self.GetNetwork()
if err == nil {
return network.GetCloudproviderId()
}
return ""
}

func (self *SNatSEntry) GetNetwork() (*SNetwork, error) {
if len(self.NetworkId) == 0 {
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/compute/models/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ func (manager *SNetworkManager) SyncNetworks(ctx context.Context, userCred mccli
if err != nil {
syncResult.UpdateError(err)
} else {
syncMetadata(ctx, userCred, &commondb[i], commonext[i])
syncVirtualResourceMetadata(ctx, userCred, &commondb[i], commonext[i])
localNets = append(localNets, commondb[i])
remoteNets = append(remoteNets, commonext[i])
syncResult.Update()
Expand All @@ -642,7 +642,7 @@ func (manager *SNetworkManager) SyncNetworks(ctx context.Context, userCred mccli
if err != nil {
syncResult.AddError(err)
} else {
syncMetadata(ctx, userCred, new, added[i])
syncVirtualResourceMetadata(ctx, userCred, new, added[i])
localNets = append(localNets, *new)
remoteNets = append(remoteNets, added[i])
syncResult.Add()
Expand Down
6 changes: 5 additions & 1 deletion pkg/compute/models/snapshotpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ func (sp *SSnapshotPolicy) getMoreDetails(out api.SnapshotPolicyDetails) api.Sna
return out
}

func (sp *SSnapshotPolicy) GetCloudproviderId() string {
return ""
}

// ==================================================== sync ===========================================================
func (manager *SSnapshotPolicyManager) SyncSnapshotPolicies(ctx context.Context, userCred mcclient.TokenCredential,
provider *SCloudprovider, region *SCloudregion, cloudSPs []cloudprovider.ICloudSnapshotPolicy,
Expand Down Expand Up @@ -507,7 +511,7 @@ func (manager *SSnapshotPolicyManager) allNewFromCloudSnapshotPolicy(
if err != nil {
syncResult.AddError(err)
} else {
syncMetadata(ctx, userCred, local, added[i])
syncVirtualResourceMetadata(ctx, userCred, local, added[i])
syncResult.Add()
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/compute/models/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ func (manager *SSnapshotManager) SyncSnapshots(ctx context.Context, userCred mcc
if err != nil {
syncResult.UpdateError(err)
} else {
syncMetadata(ctx, userCred, &commondb[i], commonext[i])
syncVirtualResourceMetadata(ctx, userCred, &commondb[i], commonext[i])
syncResult.Update()
}
}
Expand All @@ -964,7 +964,7 @@ func (manager *SSnapshotManager) SyncSnapshots(ctx context.Context, userCred mcc
if err != nil {
syncResult.AddError(err)
} else {
syncMetadata(ctx, userCred, local, added[i])
syncVirtualResourceMetadata(ctx, userCred, local, added[i])
syncResult.Add()
}
}
Expand Down
41 changes: 39 additions & 2 deletions pkg/compute/models/syncutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (
)

type IMetadataSetter interface {
// SetAllMetadata(ctx context.Context, meta map[string]interface{}, userCred mcclient.TokenCredential) error
// SetMetadata(ctx context.Context, key string, value interface{}, userCred mcclient.TokenCredential) error
SetCloudMetadataAll(ctx context.Context, meta map[string]interface{}, userCred mcclient.TokenCredential) error
Keyword() string
GetName() string
GetCloudproviderId() string
}

func syncMetadata(ctx context.Context, userCred mcclient.TokenCredential, model IMetadataSetter, remote cloudprovider.ICloudResource) error {
Expand All @@ -50,6 +51,42 @@ func syncMetadata(ctx context.Context, userCred mcclient.TokenCredential, model
return nil
}

func syncVirtualResourceMetadata(ctx context.Context, userCred mcclient.TokenCredential, model IMetadataSetter, remote cloudprovider.IVirtualResource) error {
metaData := remote.GetMetadata()
store := make(map[string]interface{}, 0)
if metaData != nil {
meta := make(map[string]interface{}, 0)
err := metaData.Unmarshal(meta)
if err != nil {
log.Errorf("Get VM Metadata error: %v", err)
return err
}
for key, value := range meta {
store[db.CLOUD_TAG_PREFIX+key] = value
}

}

extProjectId := remote.GetProjectId()
if len(extProjectId) > 0 {
extProject, err := ExternalProjectManager.GetProject(extProjectId, model.GetCloudproviderId())
if err != nil {
log.Errorf("sync project metadata for %s %s error: %v", model.Keyword(), model.GetName(), err)
} else {
store[db.CLOUD_TAG_PREFIX+"project"] = extProject.Name
}
}

if len(store) > 0 {
model.SetCloudMetadataAll(ctx, store, userCred)
}
return nil
}

func SyncMetadata(ctx context.Context, userCred mcclient.TokenCredential, model IMetadataSetter, remote cloudprovider.ICloudResource) error {
return syncMetadata(ctx, userCred, model, remote)
}

func SyncVirtualResourceMetadata(ctx context.Context, userCred mcclient.TokenCredential, model IMetadataSetter, remote cloudprovider.IVirtualResource) error {
return syncVirtualResourceMetadata(ctx, userCred, model, remote)
}
8 changes: 8 additions & 0 deletions pkg/compute/models/wireresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func (self *SWireResourceBase) GetWire() *SWire {
return nil
}

func (self *SWireResourceBase) GetCloudproviderId() string {
vpc := self.GetVpc()
if vpc != nil {
return vpc.ManagerId
}
return ""
}

func (self *SWireResourceBase) GetVpc() *SVpc {
wire := self.GetWire()
if wire != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/compute/models/zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ func (zone *SZone) GetExtraDetails(ctx context.Context, userCred mcclient.TokenC
return api.ZoneDetails{}, nil
}

func (zone *SZone) GetCloudproviderId() string {
return ""
}

func (zone *SZone) GetCloudRegionId() string {
if len(zone.CloudregionId) == 0 {
return "default"
Expand Down

0 comments on commit e31334b

Please sign in to comment.