Automatic merge from release/2.3.0 -> release/2.4.0

* commit '8ce134fc7ec5c559baed1f1626675dbf6ba66bd6':
  同步安全组规则时携带项目信息
This commit is contained in:
邱剑
2018-11-08 20:04:07 +08:00
3 changed files with 16 additions and 10 deletions
+11 -5
View File
@@ -201,7 +201,7 @@ func (manager *SSecurityGroupManager) getSecurityGroups() ([]SSecurityGroup, err
}
}
func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCred mcclient.TokenCredential, secgroups []cloudprovider.ICloudSecurityGroup) ([]SSecurityGroup, []cloudprovider.ICloudSecurityGroup, compare.SyncResult) {
func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCred mcclient.TokenCredential, secgroups []cloudprovider.ICloudSecurityGroup, projectId string, projectSync bool) ([]SSecurityGroup, []cloudprovider.ICloudSecurityGroup, compare.SyncResult) {
localSecgroups := make([]SSecurityGroup, 0)
remoteSecgroups := make([]cloudprovider.ICloudSecurityGroup, 0)
syncResult := compare.SyncResult{}
@@ -223,7 +223,7 @@ func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCre
if rules, err := commonext[i].GetRules(); err != nil {
syncResult.Error(err)
} else if len(rules) > 0 {
if err = commondb[i].SyncWithCloudSecurityGroup(userCred, commonext[i]); err != nil {
if err = commondb[i].SyncWithCloudSecurityGroup(userCred, commonext[i], projectId, projectSync); err != nil {
syncResult.UpdateError(err)
} else {
localSecgroups = append(localSecgroups, commondb[i])
@@ -244,7 +244,7 @@ func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCre
if rules, err := added[i].GetRules(); err != nil {
syncResult.AddError(err)
} else if len(rules) > 0 {
if new, err := manager.newFromCloudVpc(userCred, added[i]); err != nil {
if new, err := manager.newFromCloudVpc(userCred, added[i], projectId); err != nil {
syncResult.AddError(err)
} else if len(rules) > 0 {
localSecgroups = append(localSecgroups, *new)
@@ -258,12 +258,15 @@ func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCre
return localSecgroups, remoteSecgroups, syncResult
}
func (self *SSecurityGroup) SyncWithCloudSecurityGroup(userCred mcclient.TokenCredential, extSec cloudprovider.ICloudSecurityGroup) error {
func (self *SSecurityGroup) SyncWithCloudSecurityGroup(userCred mcclient.TokenCredential, extSec cloudprovider.ICloudSecurityGroup, projectId string, projectSync bool) error {
if _, err := self.GetModelManager().TableSpec().Update(self, func() error {
extSec.Refresh()
self.Name = extSec.GetName()
self.Description = extSec.GetDescription()
self.ProjectId = userCred.GetProjectId()
if projectSync && len(projectId) > 0 {
self.ProjectId = projectId
}
return nil
}); err != nil {
log.Errorf("syncWithCloudSecurityGroup error %s", err)
@@ -272,13 +275,16 @@ func (self *SSecurityGroup) SyncWithCloudSecurityGroup(userCred mcclient.TokenCr
return nil
}
func (manager *SSecurityGroupManager) newFromCloudVpc(userCred mcclient.TokenCredential, extSec cloudprovider.ICloudSecurityGroup) (*SSecurityGroup, error) {
func (manager *SSecurityGroupManager) newFromCloudVpc(userCred mcclient.TokenCredential, extSec cloudprovider.ICloudSecurityGroup, projectId string) (*SSecurityGroup, error) {
secgroup := SSecurityGroup{}
secgroup.SetModelManager(manager)
secgroup.Name = extSec.GetName()
secgroup.ExternalId = extSec.GetGlobalId()
secgroup.Description = extSec.GetDescription()
secgroup.ProjectId = userCred.GetProjectId()
if len(projectId) > 0 {
secgroup.ProjectId = projectId
}
if err := manager.TableSpec().Insert(&secgroup); err != nil {
return nil, err
@@ -208,18 +208,18 @@ func syncRegionVPCs(ctx context.Context, provider *models.SCloudprovider, task *
logclient.AddActionLog(provider, getAction(task.Params), notes, task.UserCred, true)
for j := 0; j < len(localVpcs); j += 1 {
syncVpcWires(ctx, provider, task, &localVpcs[j], remoteVpcs[j], syncRange)
syncVpcSecGroup(ctx, provider, task, &localVpcs[j], remoteVpcs[j])
syncVpcSecGroup(ctx, provider, task, &localVpcs[j], remoteVpcs[j], syncRange)
}
}
func syncVpcSecGroup(ctx context.Context, provider *models.SCloudprovider, task *CloudProviderSyncInfoTask, localVpc *models.SVpc, remoteVpc cloudprovider.ICloudVpc) {
func syncVpcSecGroup(ctx context.Context, provider *models.SCloudprovider, task *CloudProviderSyncInfoTask, localVpc *models.SVpc, remoteVpc cloudprovider.ICloudVpc, syncRange *models.SSyncRange) {
if secgroups, err := remoteVpc.GetISecurityGroups(); err != nil {
msg := fmt.Sprintf("GetISecurityGroups for vpc %s failed %s", remoteVpc.GetId(), err)
log.Errorf(msg)
logSyncFailed(provider, task, msg)
return
} else {
_, _, result := models.SecurityGroupManager.SyncSecgroups(ctx, task.UserCred, secgroups)
_, _, result := models.SecurityGroupManager.SyncSecgroups(ctx, task.UserCred, secgroups, provider.ProjectId, syncRange.ProjectSync)
msg := result.Result()
notes := fmt.Sprintf("SyncSecurityGroup for VPC %s result: %s", localVpc.Name, msg)
log.Infof(notes)
+2 -2
View File
@@ -200,8 +200,8 @@ type TaskManager struct {
func NewTaskManager(stopCh <-chan struct{}) *TaskManager {
return &TaskManager{
taskExecutorQueueManager: NewTaskExecutorQueueManager(stopCh),
stopCh: stopCh,
lock: sync.Mutex{},
stopCh: stopCh,
lock: sync.Mutex{},
}
}