冲突解决

This commit is contained in:
屈轩
2018-12-16 13:07:47 +08:00
3 changed files with 33 additions and 43 deletions
+22 -35
View File
@@ -14,6 +14,7 @@ import (
"yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
"yunion.io/x/onecloud/pkg/cloudcommon/validators"
"yunion.io/x/onecloud/pkg/compute/options"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
@@ -638,48 +639,34 @@ func (self *SGuest) AllowPerformRevokeSecgroup(ctx context.Context, userCred mcc
func (self *SGuest) PerformRevokeSecgroup(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
if !utils.IsInStringArray(self.Status, []string{VM_READY, VM_RUNNING, VM_SUSPEND}) {
return nil, httperrors.NewInputParameterError("Cannot revoke security rules in status %s", self.Status)
} else {
if _, err := self.GetModelManager().TableSpec().Update(self, func() error {
self.SecgrpId = "default"
return nil
}); err != nil {
return nil, err
}
if err := self.StartSyncTask(ctx, userCred, true, ""); err != nil {
return nil, err
}
}
return nil, nil
if _, err := self.GetModelManager().TableSpec().Update(self, func() error {
self.SecgrpId = "default"
return nil
}); err != nil {
return nil, err
}
logclient.AddActionLog(self, logclient.ACT_VM_REVOKESECGROUP, nil, userCred, true)
return nil, self.StartSyncTask(ctx, userCred, true, "")
}
func (self *SGuest) PerformAssignSecgroup(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
if !utils.IsInStringArray(self.Status, []string{VM_READY, VM_RUNNING, VM_SUSPEND}) {
logclient.AddActionLog(self, logclient.ACT_VM_ASSIGNSECGROUP, "Cannot assign security rules in status "+self.Status, userCred, false)
return nil, httperrors.NewInputParameterError("Cannot assign security rules in status %s", self.Status)
} else {
if secgrp, err := data.GetString("secgrp"); err != nil {
logclient.AddActionLog(self, logclient.ACT_VM_ASSIGNSECGROUP, err, userCred, false)
return nil, err
} else if sg, err := SecurityGroupManager.FetchByIdOrName(userCred, secgrp); err != nil {
msg := fmt.Sprintf("SecurityGroup %s not found", secgrp)
logclient.AddActionLog(self, logclient.ACT_VM_ASSIGNSECGROUP, msg, userCred, false)
return nil, httperrors.NewNotFoundError("SecurityGroup %s not found", secgrp)
} else {
if _, err := self.GetModelManager().TableSpec().Update(self, func() error {
self.SecgrpId = sg.GetId()
return nil
}); err != nil {
logclient.AddActionLog(self, logclient.ACT_VM_ASSIGNSECGROUP, err, userCred, false)
return nil, err
}
if err := self.StartSyncTask(ctx, userCred, true, ""); err != nil {
logclient.AddActionLog(self, logclient.ACT_VM_ASSIGNSECGROUP, err, userCred, false)
return nil, err
}
}
}
logclient.AddActionLog(self, logclient.ACT_VM_ASSIGNSECGROUP, nil, userCred, true)
return nil, nil
secgrpV := validators.NewModelIdOrNameValidator("secgrp", "secgroup", userCred.GetProjectId())
if err := secgrpV.Validate(data.(*jsonutils.JSONDict)); err != nil {
return nil, err
}
if _, err := self.GetModelManager().TableSpec().Update(self, func() error {
self.SecgrpId = secgrpV.Model.GetId()
return nil
}); err != nil {
return nil, err
}
logclient.AddActionLog(self, logclient.ACT_VM_ASSIGNSECGROUP, fmt.Sprintf("secgroup: %s", secgrpV.Model.GetName()), userCred, true)
return nil, self.StartSyncTask(ctx, userCred, true, "")
}
func (self *SGuest) AllowPerformPurge(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
+10 -8
View File
@@ -230,8 +230,11 @@ func (self *SSecurityGroup) SyncWithCloudSecurityGroup(userCred mcclient.TokenCr
func (manager *SSecurityGroupManager) newFromCloudVpc(userCred mcclient.TokenCredential, extSec cloudprovider.ICloudSecurityGroup, vpc *SVpc, projectId string) (*SSecurityGroup, bool, error) {
if secgroup, exist := SecurityGroupCacheManager.CheckExist(context.Background(), userCred, extSec.GetGlobalId(), extSec.GetVpcId(), vpc.CloudregionId, vpc.ManagerId); exist {
if secgroup.GetGuestsCount() == 0 {
return secgroup, true, nil
}
//避免重复同步
return secgroup, true, nil
return secgroup, false, nil
}
secgroup := SSecurityGroup{}
@@ -245,7 +248,7 @@ func (manager *SSecurityGroupManager) newFromCloudVpc(userCred mcclient.TokenCre
}
if err := manager.TableSpec().Insert(&secgroup); err != nil {
return nil, false, err
return nil, true, err
}
if secgroupcache := SecurityGroupCacheManager.Register(context.Background(), userCred, secgroup.Id, extSec.GetVpcId(), vpc.CloudregionId, vpc.ManagerId); secgroupcache != nil {
@@ -254,7 +257,7 @@ func (manager *SSecurityGroupManager) newFromCloudVpc(userCred mcclient.TokenCre
}
}
return &secgroup, false, nil
return &secgroup, true, nil
}
func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCred mcclient.TokenCredential, secgroups []cloudprovider.ICloudSecurityGroup, vpc *SVpc, projectId string, projectSync bool) ([]SSecurityGroup, []cloudprovider.ICloudSecurityGroup, compare.SyncResult) {
@@ -298,18 +301,17 @@ func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCre
syncResult.AddError(err)
continue
}
new, exist, err := manager.newFromCloudVpc(userCred, added[i], vpc, projectId)
new, ruleSync, err := manager.newFromCloudVpc(userCred, added[i], vpc, projectId)
if err != nil {
syncResult.AddError(err)
continue
}
if exist {
continue
}
localSecgroups = append(localSecgroups, *new)
remoteSecgroups = append(remoteSecgroups, added[i])
SecurityGroupRuleManager.SyncRules(ctx, userCred, new, rules)
syncResult.Add()
if ruleSync {
SecurityGroupRuleManager.SyncRules(ctx, userCred, new, rules)
}
}
return localSecgroups, remoteSecgroups, syncResult
}
+1
View File
@@ -56,6 +56,7 @@ const (
ACT_VM_SYNC_STATUS = "同步状态"
ACT_VM_UNBIND_KEYPAIR = "解绑密钥"
ACT_VM_ASSIGNSECGROUP = "关联安全组"
ACT_VM_REVOKESECGROUP = "取消关联安全组"
ACT_RESET_DISK = "回滚磁盘"
)