diff --git a/pkg/apis/compute/secgroup.go b/pkg/apis/compute/secgroup.go index d0a24615a4..4d01d22740 100644 --- a/pkg/apis/compute/secgroup.go +++ b/pkg/apis/compute/secgroup.go @@ -82,7 +82,7 @@ type SSecgroupRuleResource struct { // example: test to create rule Description string `json:"description"` - // 对端安全组Id, 此参数和cidr参数互斥,并且优先级高于cidr, 同事peer_secgroup_id不能和它所在的安全组ID相同 + // 对端安全组Id, 此参数和cidr参数互斥,并且优先级高于cidr, 同时peer_secgroup_id不能和它所在的安全组ID相同 // required: false PeerSecgroupId string `json:"peer_secgroup_id"` } @@ -247,11 +247,11 @@ type SecgroupDetails struct { // 安全组缓存数量 CacheCnt int `json:"cache_cnt,allowempty"` // 规则信息 - Rules []SSecurityGroupRule `json:"rules"` + Rules []SecgroupRuleDetails `json:"rules"` // 入方向规则信息 - InRules []SSecurityGroupRule `json:"in_rules"` + InRules []SecgroupRuleDetails `json:"in_rules"` // 出方向规则信息 - OutRules []SSecurityGroupRule `json:"out_rules"` + OutRules []SecgroupRuleDetails `json:"out_rules"` } type SecurityGroupResourceInfo struct { diff --git a/pkg/compute/models/secgrouprules.go b/pkg/compute/models/secgrouprules.go index e622b11145..87706865b6 100644 --- a/pkg/compute/models/secgrouprules.go +++ b/pkg/compute/models/secgrouprules.go @@ -327,45 +327,47 @@ func (manager *SSecurityGroupRuleManager) ValidateCreateData(ctx context.Context func (self *SSecurityGroupRule) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.SSecgroupRuleUpdateInput) (api.SSecgroupRuleUpdateInput, error) { priority := int(self.Priority) - output := api.SSecgroupRuleUpdateInput{ - SSecgroupRuleResource: api.SSecgroupRuleResource{ - Priority: &priority, - Protocol: self.Protocol, - Ports: self.Ports, - Direction: self.Direction, - CIDR: self.CIDR, - Action: self.Action, - Description: self.Description, - PeerSecgroupId: self.PeerSecgroupId, - }, + if input.Priority == nil { + input.Priority = &priority + } + if len(input.Direction) == 0 { + input.Direction = self.Direction + } + if len(input.Action) == 0 { + input.Action = self.Action + } + if len(input.Protocol) == 0 { + input.Protocol = self.Protocol + } + if len(input.Ports) == 0 { + input.Ports = self.Ports } - jsonutils.Update(&output, input) - if *output.Priority < 1 || *output.Priority > 100 { - return output, httperrors.NewOutOfRangeError("Invalid priority %d, must be in range or 1 ~ 100", input.Priority) + if *input.Priority < 1 || *input.Priority > 100 { + return input, httperrors.NewOutOfRangeError("Invalid priority %d, must be in range or 1 ~ 100", input.Priority) } if len(input.PeerSecgroupId) > 0 { _, err := validators.ValidateModel(userCred, SecurityGroupManager, &input.PeerSecgroupId) if err != nil { - return output, err + return input, err } if input.PeerSecgroupId == self.Id { - return output, httperrors.NewInputParameterError("peer_secgroup_id can not point to secgroup self") + return input, httperrors.NewInputParameterError("peer_secgroup_id can not point to secgroup self") } } - err := output.Check() + err := input.Check() if err != nil { - return output, err + return input, err } - output.ResourceBaseUpdateInput, err = self.SResourceBase.ValidateUpdateData(ctx, userCred, query, input.ResourceBaseUpdateInput) + input.ResourceBaseUpdateInput, err = self.SResourceBase.ValidateUpdateData(ctx, userCred, query, input.ResourceBaseUpdateInput) if err != nil { - return output, errors.Wrap(err, "SResourceBase.ValidateUpdateData") + return input, errors.Wrap(err, "SResourceBase.ValidateUpdateData") } - return output, nil + return input, nil } func (self *SSecurityGroupRule) String() string { @@ -435,12 +437,14 @@ func (self *SSecurityGroupRule) PreDelete(ctx context.Context, userCred mcclient func (self *SSecurityGroupRule) PostUpdate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) { self.SResourceBase.PostUpdate(ctx, userCred, query, data) - if len(self.PeerSecgroupId) > 0 { - db.Update(self, func() error { + db.Update(self, func() error { + if data.Contains("peer_secgroup_id") { self.CIDR = "" - return nil - }) - } + } else if data.Contains("cidr") { + self.PeerSecgroupId = "" + } + return nil + }) log.Debugf("POST Update %s", data) if secgroup := self.GetSecGroup(); secgroup != nil { diff --git a/pkg/compute/models/secgroups.go b/pkg/compute/models/secgroups.go index cd87f687bd..09f477d5f8 100644 --- a/pkg/compute/models/secgroups.go +++ b/pkg/compute/models/secgroups.go @@ -465,6 +465,19 @@ func (manager *SSecurityGroupManager) FetchCustomizeColumns( log.Errorf("db.FetchModelObjects error: %v", err) return rows } + + peerSecgroupIds := []string{} + for _, rule := range rules { + if len(rule.PeerSecgroupId) > 0 { + peerSecgroupIds = append(peerSecgroupIds, rule.PeerSecgroupId) + } + } + + peerMaps, err := db.FetchIdNameMap2(SecurityGroupManager, peerSecgroupIds) + if err != nil { + return rows + } + ruleMaps := map[string][]SSecurityGroupRule{} for i := range rules { if _, ok := ruleMaps[rules[i].SecgroupId]; !ok { @@ -477,19 +490,14 @@ func (manager *SSecurityGroupManager) FetchCustomizeColumns( if !ok { continue } - _rules := []api.SSecurityGroupRule{} - _inRules := []api.SSecurityGroupRule{} - _outRules := []api.SSecurityGroupRule{} + _rules := []api.SecgroupRuleDetails{} + _inRules := []api.SecgroupRuleDetails{} + _outRules := []api.SecgroupRuleDetails{} for j := range rules { - rule := api.SSecurityGroupRule{ - Id: rules[j].Id, - Priority: rules[j].Priority, - Protocol: rules[j].Protocol, - Ports: rules[j].Ports, - Direction: rules[j].Direction, - CIDR: rules[j].CIDR, - Action: rules[j].Action, - Description: rules[j].Description, + rule := api.SecgroupRuleDetails{} + jsonutils.Update(&rule, rules[j]) + if len(rules[j].PeerSecgroupId) > 0 { + rule.PeerSecgroup, _ = peerMaps[rules[j].PeerSecgroupId] } _rules = append(_rules, rule) switch rule.Direction {