@@ -173,6 +173,11 @@ func resourceTencentCloudSecurityGroupRuleSet() *schema.Resource {
173173 Computed : true ,
174174 Description : "Range of the port. The available value can be one, multiple or one segment. E.g. `80`, `80,90` and `80-90`. Default to all ports, and conflicts with `service_template_*`." ,
175175 },
176+ "policy_index" : {
177+ Type : schema .TypeInt ,
178+ Computed : true ,
179+ Description : "The security group rule index number, whose value dynamically changes with changes in security group rules." ,
180+ },
176181 }
177182 return & schema.Resource {
178183 Create : resourceTencentCloudSecurityGroupRuleSetCreate ,
@@ -320,31 +325,83 @@ func resourceTencentCloudSecurityGroupRuleSetUpdate(d *schema.ResourceData, m in
320325
321326 if needChange {
322327 version := d .Get ("version" ).(string )
323- ver , vErr := strconv .ParseInt (version , 10 , 64 )
328+ ver , _ := strconv .ParseInt (version , 10 , 64 )
329+ ver += 1
324330 request .SecurityGroupId = helper .String (securityGroupId )
325331 request .SecurityGroupPolicySet = & vpc.SecurityGroupPolicySet {}
326332 request .SortPolicys = helper .Bool (true )
327- if vErr == nil {
328- nextVer = fmt .Sprintf ("%d" , ver + 1 )
329- request .SecurityGroupPolicySet .Version = helper .String (nextVer )
330- }
331333
332- if d .HasChange ("ingress" ) {
333- ingressRules := d .Get ("ingress" ).([]interface {})
334+ ingressRules := d .Get ("ingress" ).([]interface {})
335+ egressRules := d .Get ("egress" ).([]interface {})
336+ if len (ingressRules ) == 0 && len (egressRules ) == 0 {
337+ request .SecurityGroupPolicySet .Version = helper .String ("0" )
338+ } else if len (ingressRules ) != 0 && len (egressRules ) == 0 {
339+ request .SecurityGroupPolicySet .Ingress , err = unmarshalSecurityPolicy (ingressRules )
340+ if err != nil {
341+ return err
342+ }
343+
344+ result , e := service .DescribeSecurityGroupPolicies (ctx , securityGroupId )
345+ if e != nil {
346+ return e
347+ }
348+
349+ if result .Egress != nil {
350+ tmpList := []* int64 {}
351+ egressRulesList := marshalSecurityPolicy (result .Egress )
352+ for _ , v := range egressRulesList {
353+ item := v .(map [string ]interface {})
354+ tmpList = append (tmpList , item ["policy_index" ].(* int64 ))
355+ }
356+
357+ e = service .DeleteSecurityGroupPolicyByPolicyIndexList (ctx , securityGroupId , tmpList , "egress" )
358+ if e != nil {
359+ return e
360+ }
361+
362+ ver += 1
363+ }
364+
365+ } else if len (ingressRules ) == 0 && len (egressRules ) != 0 {
366+ request .SecurityGroupPolicySet .Egress , err = unmarshalSecurityPolicy (egressRules )
367+ if err != nil {
368+ return err
369+ }
370+
371+ result , e := service .DescribeSecurityGroupPolicies (ctx , securityGroupId )
372+ if e != nil {
373+ return e
374+ }
375+
376+ if result .Ingress != nil {
377+ tmpList := []* int64 {}
378+ ingressRulesList := marshalSecurityPolicy (result .Ingress )
379+ for _ , v := range ingressRulesList {
380+ item := v .(map [string ]interface {})
381+ tmpList = append (tmpList , item ["policy_index" ].(* int64 ))
382+ }
383+
384+ e = service .DeleteSecurityGroupPolicyByPolicyIndexList (ctx , securityGroupId , tmpList , "ingress" )
385+ if e != nil {
386+ return e
387+ }
388+
389+ ver += 1
390+ }
391+ } else {
334392 request .SecurityGroupPolicySet .Ingress , err = unmarshalSecurityPolicy (ingressRules )
335393 if err != nil {
336394 return err
337395 }
338- }
339396
340- if d .HasChange ("egress" ) {
341- egressRules := d .Get ("egress" ).([]interface {})
342397 request .SecurityGroupPolicySet .Egress , err = unmarshalSecurityPolicy (egressRules )
343398 if err != nil {
344399 return err
345400 }
346401 }
347402
403+ nextVer = fmt .Sprintf ("%d" , ver )
404+ request .SecurityGroupPolicySet .Version = helper .String (nextVer )
348405 err = service .ModifySecurityGroupPolicies (ctx , request )
349406 if err != nil {
350407 return err
@@ -467,7 +524,6 @@ func unmarshalSecurityPolicy(policies []interface{}) (output []*vpc.SecurityGrou
467524 if desc != "" {
468525 result .PolicyDescription = & desc
469526 }
470- //result.PolicyIndex = helper.IntInt64(i)
471527
472528 output = append (output , result )
473529 }
@@ -481,6 +537,9 @@ func marshalSecurityPolicy(policies []*vpc.SecurityGroupPolicy) []interface{} {
481537 dMap := map [string ]interface {}{
482538 "action" : policy .Action ,
483539 }
540+ if policy .PolicyIndex != nil {
541+ dMap ["policy_index" ] = policy .PolicyIndex
542+ }
484543 if policy .CidrBlock != nil {
485544 dMap ["cidr_block" ] = policy .CidrBlock
486545 }
0 commit comments