mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
fix: validate secgroup references
This commit is contained in:
@@ -36,4 +36,5 @@ func init() {
|
||||
cmd.Perform("purge", &options.SecgroupIdOptions{})
|
||||
cmd.Perform("change-owner", &options.SecgroupChangeOwnerOptions{})
|
||||
cmd.Perform("import-rules", &options.SecgroupImportRulesOptions{})
|
||||
cmd.Get("references", &options.SecgroupIdOptions{})
|
||||
}
|
||||
|
||||
@@ -27,7 +27,8 @@ import (
|
||||
)
|
||||
|
||||
type SecurityGroupReference struct {
|
||||
Id string
|
||||
Id string
|
||||
Name string
|
||||
}
|
||||
|
||||
type SecDriver interface {
|
||||
|
||||
@@ -1222,6 +1222,17 @@ func (manager *SSecurityGroupManager) InitializeData() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SSecurityGroup) GetSecurityGroupReferences() ([]SSecurityGroup, error) {
|
||||
sq := SecurityGroupRuleManager.Query("secgroup_id").Equals("peer_secgroup_id", self.Id).Distinct().SubQuery()
|
||||
q := SecurityGroupManager.Query().In("id", sq)
|
||||
groups := []SSecurityGroup{}
|
||||
err := db.FetchModelObjects(SecurityGroupManager, q, &groups)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "db.FetchModelObjects")
|
||||
}
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
func (self *SSecurityGroup) ValidateDeleteCondition(ctx context.Context) error {
|
||||
cnt, err := self.GetGuestsCount()
|
||||
if err != nil {
|
||||
@@ -1233,6 +1244,13 @@ func (self *SSecurityGroup) ValidateDeleteCondition(ctx context.Context) error {
|
||||
if self.Id == "default" {
|
||||
return httperrors.NewProtectedResourceError("not allow to delete default security group")
|
||||
}
|
||||
references, err := self.GetSecurityGroupReferences()
|
||||
if err != nil {
|
||||
return httperrors.NewGeneralError(err)
|
||||
}
|
||||
if len(references) > 0 {
|
||||
return httperrors.NewNotEmptyError("the other security group is in use")
|
||||
}
|
||||
return self.SSharableVirtualResourceBase.ValidateDeleteCondition(ctx)
|
||||
}
|
||||
|
||||
@@ -1302,6 +1320,26 @@ func (sg *SSecurityGroup) GetUsages() []db.IUsage {
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SSecurityGroup) AllowGetDetailsReferences(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
||||
return db.IsProjectAllowGetSpec(userCred, self, "references")
|
||||
}
|
||||
|
||||
// 获取引用信息
|
||||
func (self *SSecurityGroup) GetDetailsReferences(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) ([]cloudprovider.SecurityGroupReference, error) {
|
||||
groups, err := self.GetSecurityGroupReferences()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetSecurityGroupReferences")
|
||||
}
|
||||
ret := []cloudprovider.SecurityGroupReference{}
|
||||
for i := range groups {
|
||||
ret = append(ret, cloudprovider.SecurityGroupReference{
|
||||
Id: groups[i].Id,
|
||||
Name: groups[i].Name,
|
||||
})
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *SSecurityGroup) AllowPerformImportRules(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
return self.IsOwner(userCred) || db.IsAdminAllowPerform(userCred, self, "import-rules")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user