Skip to content

Commit 4c0a752

Browse files
authored
fix(vpc): [119637666] Fix the issue where field assistant_cidrs cannot be edited (#2817)
* add * add * add
1 parent 900829b commit 4c0a752

File tree

4 files changed

+87
-12
lines changed

4 files changed

+87
-12
lines changed

.changelog/2817.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_vpc: Fix the issue where field assistant_cidrs cannot be edited
3+
```

tencentcloud/common/common.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os/user"
1414
"path/filepath"
1515
"reflect"
16+
"sort"
1617
"strconv"
1718
"strings"
1819
"sync/atomic"
@@ -656,3 +657,62 @@ func GetAuthFromCAM(roleName string) (camResp *CAMResponse, err error) {
656657

657658
return
658659
}
660+
661+
func GetArrayIntersect(sliceA, sliceB []string) []string {
662+
intersection := make([]string, 0)
663+
temp := make(map[string]bool)
664+
665+
for _, a := range sliceA {
666+
temp[a] = true
667+
}
668+
669+
for _, b := range sliceB {
670+
if _, ok := temp[b]; ok {
671+
intersection = append(intersection, b)
672+
delete(temp, b)
673+
}
674+
}
675+
676+
return intersection
677+
}
678+
679+
func RemoveArrayIntersect(sliceA, elementsToRemove []string) []string {
680+
result := make([]string, 0)
681+
temp := make(map[string]bool)
682+
683+
for _, e := range elementsToRemove {
684+
temp[e] = true
685+
}
686+
687+
for _, s := range sliceA {
688+
if _, ok := temp[s]; !ok {
689+
result = append(result, s)
690+
}
691+
}
692+
693+
return result
694+
695+
}
696+
697+
func EqualArrayIgnoreOrder(sliceA, sliceB []string) bool {
698+
if len(sliceA) != len(sliceB) {
699+
return false
700+
}
701+
702+
sortedA := make([]string, len(sliceA))
703+
sortedB := make([]string, len(sliceB))
704+
705+
copy(sortedA, sliceA)
706+
copy(sortedB, sliceB)
707+
708+
sort.Strings(sortedA)
709+
sort.Strings(sortedB)
710+
711+
for i := range sortedA {
712+
if sortedA[i] != sortedB[i] {
713+
return false
714+
}
715+
}
716+
717+
return true
718+
}

tencentcloud/services/vpc/resource_tc_vpc.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func ResourceTencentCloudVpcInstance() *schema.Resource {
5959
Description: "Indicates whether VPC multicast is enabled. The default value is 'true'.",
6060
},
6161
"assistant_cidrs": {
62-
Type: schema.TypeList,
62+
Type: schema.TypeSet,
6363
Optional: true,
6464
Description: "List of Assistant CIDR, NOTE: Only `NORMAL` typed CIDRs included, check the Docker CIDR by readonly `assistant_docker_cidrs`.",
6565
Computed: true,
@@ -150,15 +150,22 @@ func resourceTencentCloudVpcInstanceCreate(d *schema.ResourceData, meta interfac
150150
d.SetId(vpcId)
151151

152152
if v, ok := d.GetOk("assistant_cidrs"); ok {
153-
assistantCidrs := v.([]interface{})
153+
assistantCidrs := v.(*schema.Set).List()
154+
tmpList := make([]*string, 0, len(assistantCidrs))
155+
for i := range assistantCidrs {
156+
userIdSet := assistantCidrs[i].(string)
157+
tmpList = append(tmpList, &userIdSet)
158+
}
159+
154160
request := vpc.NewCreateAssistantCidrRequest()
155161
request.VpcId = &vpcId
156-
request.CidrBlocks = helper.InterfacesStringsPoint(assistantCidrs)
157-
_, err := vpcService.CreateAssistantCidr(ctx, request)
162+
request.CidrBlocks = tmpList
163+
_, err = vpcService.CreateAssistantCidr(ctx, request)
158164
if err != nil {
159165
return err
160166
}
161167
}
168+
162169
// protected while tag is not ready, default is 1s
163170
time.Sleep(tccommon.WaitReadTimeout)
164171

@@ -296,17 +303,22 @@ func resourceTencentCloudVpcInstanceUpdate(d *schema.ResourceData, meta interfac
296303

297304
if d.HasChange("assistant_cidrs") {
298305
old, now := d.GetChange("assistant_cidrs")
306+
oldSet := old.(*schema.Set)
307+
nowSet := now.(*schema.Set)
308+
add := nowSet.Difference(oldSet).List()
309+
remove := oldSet.Difference(nowSet).List()
310+
addLen := len(add)
311+
removeLen := len(remove)
312+
299313
request := vpc.NewModifyAssistantCidrRequest()
300314
request.VpcId = &id
301-
302-
nowTmp, ok := now.([]interface{})
303-
if ok && len(nowTmp) > 0 {
304-
request.NewCidrBlocks = helper.InterfacesStringsPoint(nowTmp)
315+
if removeLen > 0 {
316+
request.OldCidrBlocks = helper.InterfacesStringsPoint(remove)
305317
}
306318

307-
oldTmp, ok := old.([]interface{})
308-
if ok && len(oldTmp) > 0 {
309-
request.OldCidrBlocks = helper.InterfacesStringsPoint(oldTmp)
319+
if addLen > 0 {
320+
request.OldCidrBlocks = nil
321+
request.NewCidrBlocks = helper.InterfacesStringsPoint(add)
310322
}
311323

312324
if err := vpcService.ModifyAssistantCidr(ctx, request); err != nil {

website/docs/r/vpc.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ The following arguments are supported:
4949

5050
* `cidr_block` - (Required, String, ForceNew) A network address block which should be a subnet of the three internal network segments (10.0.0.0/16, 172.16.0.0/12 and 192.168.0.0/16).
5151
* `name` - (Required, String) The name of the VPC.
52-
* `assistant_cidrs` - (Optional, List: [`String`]) List of Assistant CIDR, NOTE: Only `NORMAL` typed CIDRs included, check the Docker CIDR by readonly `assistant_docker_cidrs`.
52+
* `assistant_cidrs` - (Optional, Set: [`String`]) List of Assistant CIDR, NOTE: Only `NORMAL` typed CIDRs included, check the Docker CIDR by readonly `assistant_docker_cidrs`.
5353
* `dns_servers` - (Optional, Set: [`String`]) The DNS server list of the VPC. And you can specify 0 to 5 servers to this list.
5454
* `is_multicast` - (Optional, Bool) Indicates whether VPC multicast is enabled. The default value is 'true'.
5555
* `tags` - (Optional, Map) Tags of the VPC.

0 commit comments

Comments
 (0)