Skip to content

fix(vpc): [119637666] Fix the issue where field assistant_cidrs cannot be edited #2817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2817.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_vpc: Fix the issue where field assistant_cidrs cannot be edited
```
60 changes: 60 additions & 0 deletions tencentcloud/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os/user"
"path/filepath"
"reflect"
"sort"
"strconv"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -656,3 +657,62 @@ func GetAuthFromCAM(roleName string) (camResp *CAMResponse, err error) {

return
}

func GetArrayIntersect(sliceA, sliceB []string) []string {
intersection := make([]string, 0)
temp := make(map[string]bool)

for _, a := range sliceA {
temp[a] = true
}

for _, b := range sliceB {
if _, ok := temp[b]; ok {
intersection = append(intersection, b)
delete(temp, b)
}
}

return intersection
}

func RemoveArrayIntersect(sliceA, elementsToRemove []string) []string {
result := make([]string, 0)
temp := make(map[string]bool)

for _, e := range elementsToRemove {
temp[e] = true
}

for _, s := range sliceA {
if _, ok := temp[s]; !ok {
result = append(result, s)
}
}

return result

}

func EqualArrayIgnoreOrder(sliceA, sliceB []string) bool {
if len(sliceA) != len(sliceB) {
return false
}

sortedA := make([]string, len(sliceA))
sortedB := make([]string, len(sliceB))

copy(sortedA, sliceA)
copy(sortedB, sliceB)

sort.Strings(sortedA)
sort.Strings(sortedB)

for i := range sortedA {
if sortedA[i] != sortedB[i] {
return false
}
}

return true
}
34 changes: 23 additions & 11 deletions tencentcloud/services/vpc/resource_tc_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func ResourceTencentCloudVpcInstance() *schema.Resource {
Description: "Indicates whether VPC multicast is enabled. The default value is 'true'.",
},
"assistant_cidrs": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Description: "List of Assistant CIDR, NOTE: Only `NORMAL` typed CIDRs included, check the Docker CIDR by readonly `assistant_docker_cidrs`.",
Computed: true,
Expand Down Expand Up @@ -150,15 +150,22 @@ func resourceTencentCloudVpcInstanceCreate(d *schema.ResourceData, meta interfac
d.SetId(vpcId)

if v, ok := d.GetOk("assistant_cidrs"); ok {
assistantCidrs := v.([]interface{})
assistantCidrs := v.(*schema.Set).List()
tmpList := make([]*string, 0, len(assistantCidrs))
for i := range assistantCidrs {
userIdSet := assistantCidrs[i].(string)
tmpList = append(tmpList, &userIdSet)
}

request := vpc.NewCreateAssistantCidrRequest()
request.VpcId = &vpcId
request.CidrBlocks = helper.InterfacesStringsPoint(assistantCidrs)
_, err := vpcService.CreateAssistantCidr(ctx, request)
request.CidrBlocks = tmpList
_, err = vpcService.CreateAssistantCidr(ctx, request)
if err != nil {
return err
}
}

// protected while tag is not ready, default is 1s
time.Sleep(tccommon.WaitReadTimeout)

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

if d.HasChange("assistant_cidrs") {
old, now := d.GetChange("assistant_cidrs")
oldSet := old.(*schema.Set)
nowSet := now.(*schema.Set)
add := nowSet.Difference(oldSet).List()
remove := oldSet.Difference(nowSet).List()
addLen := len(add)
removeLen := len(remove)

request := vpc.NewModifyAssistantCidrRequest()
request.VpcId = &id

nowTmp, ok := now.([]interface{})
if ok && len(nowTmp) > 0 {
request.NewCidrBlocks = helper.InterfacesStringsPoint(nowTmp)
if removeLen > 0 {
request.OldCidrBlocks = helper.InterfacesStringsPoint(remove)
}

oldTmp, ok := old.([]interface{})
if ok && len(oldTmp) > 0 {
request.OldCidrBlocks = helper.InterfacesStringsPoint(oldTmp)
if addLen > 0 {
request.OldCidrBlocks = nil
request.NewCidrBlocks = helper.InterfacesStringsPoint(add)
}

if err := vpcService.ModifyAssistantCidr(ctx, request); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/vpc.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ The following arguments are supported:

* `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).
* `name` - (Required, String) The name of the VPC.
* `assistant_cidrs` - (Optional, List: [`String`]) List of Assistant CIDR, NOTE: Only `NORMAL` typed CIDRs included, check the Docker CIDR by readonly `assistant_docker_cidrs`.
* `assistant_cidrs` - (Optional, Set: [`String`]) List of Assistant CIDR, NOTE: Only `NORMAL` typed CIDRs included, check the Docker CIDR by readonly `assistant_docker_cidrs`.
* `dns_servers` - (Optional, Set: [`String`]) The DNS server list of the VPC. And you can specify 0 to 5 servers to this list.
* `is_multicast` - (Optional, Bool) Indicates whether VPC multicast is enabled. The default value is 'true'.
* `tags` - (Optional, Map) Tags of the VPC.
Expand Down
Loading