Skip to content
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

fix cvm data-disk order #1968

Merged
merged 6 commits into from
Jul 19, 2023
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/1968.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/tencentcloud_instance: fix data-disks order issue
```
16 changes: 16 additions & 0 deletions tencentcloud/resource_tc_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ import (
"encoding/base64"
"fmt"
"log"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -992,6 +994,8 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{})
//set data_disks
dataDiskList := make([]map[string]interface{}, 0, len(instance.DataDisks))
diskSizeMap := map[string]*uint64{}
diskOrderMap := make(map[string]int)

if len(instance.DataDisks) > 0 {
var diskIds []*string
for i := range instance.DataDisks {
Expand All @@ -1017,6 +1021,13 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{})
return resource.RetryableError(fmt.Errorf("data_disk[%d] is expending", i))
}
diskSizeMap[*disk.DiskId] = disk.DiskSize
items := strings.Split(*disk.DiskName, "_")
diskOrder := items[len(items)-1]
diskOrderInt, err := strconv.Atoi(diskOrder)
if err != nil {
return resource.NonRetryableError(err)
}
diskOrderMap[*disk.DiskId] = diskOrderInt
}
return nil
})
Expand All @@ -1039,6 +1050,11 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{})
dataDisk["throughput_performance"] = disk.ThroughputPerformance
dataDiskList = append(dataDiskList, dataDisk)
}
sort.SliceStable(dataDiskList, func(idx1, idx2 int) bool {
dataDiskIdIdx1 := *dataDiskList[idx1]["data_disk_id"].(*string)
dataDiskIdIdx2 := *dataDiskList[idx2]["data_disk_id"].(*string)
return diskOrderMap[dataDiskIdIdx1] < diskOrderMap[dataDiskIdIdx2]
})
_ = d.Set("data_disks", dataDiskList)

if len(instance.PrivateIpAddresses) > 0 {
Expand Down
58 changes: 56 additions & 2 deletions tencentcloud/resource_tc_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func testSweepCvmInstance(region string) error {
func TestAccTencentCloudInstanceResource_Basic(t *testing.T) {
t.Parallel()

id := "tencentcloud_instance.foo"
id := "tencentcloud_instance.cvm_basic"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: id,
Expand Down Expand Up @@ -564,6 +564,31 @@ func TestAccTencentCloudInstanceResource_WithSpotpaid(t *testing.T) {
})
}

func TestAccTencentCloudInstanceResource_DataDiskOrder(t *testing.T) {
t.Parallel()

id := "tencentcloud_instance.foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: id,
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_COMMON) },
Config: testAccTencentCloudInstanceWithDataDiskOrder,
Check: resource.ComposeTestCheckFunc(
testAccCheckTencentCloudDataSourceID(id),
testAccCheckTencentCloudInstanceExists(id),
resource.TestCheckResourceAttr(id, "data_disks.0.data_disk_size", "100"),
resource.TestCheckResourceAttr(id, "data_disks.1.data_disk_size", "50"),
resource.TestCheckResourceAttr(id, "data_disks.2.data_disk_size", "70"),
),
},
},
})
}

func TestAccTencentCloudNeedFixInstancePostpaidToPrepaid(t *testing.T) {

id := "tencentcloud_instance.foo"
Expand Down Expand Up @@ -698,6 +723,19 @@ func testAccCheckInstanceDestroy(s *terraform.State) error {
}

const testAccTencentCloudInstanceBasic = defaultInstanceVariable + `
resource "tencentcloud_instance" "cvm_basic" {
instance_name = var.instance_name
availability_zone = var.availability_cvm_zone
image_id = data.tencentcloud_images.default.images.0.image_id
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
vpc_id = var.cvm_vpc_id
subnet_id = var.cvm_subnet_id
system_disk_type = "CLOUD_PREMIUM"
project_id = 0
}
`

const testAccTencentCloudInstanceWithDataDiskOrder = defaultInstanceVariable + `
resource "tencentcloud_instance" "foo" {
instance_name = var.instance_name
availability_zone = var.availability_cvm_zone
Expand All @@ -707,6 +745,22 @@ resource "tencentcloud_instance" "foo" {
subnet_id = var.cvm_subnet_id
system_disk_type = "CLOUD_PREMIUM"
project_id = 0

data_disks {
data_disk_size = 100
data_disk_type = "CLOUD_PREMIUM"
delete_with_instance = true
}
data_disks {
data_disk_size = 50
data_disk_type = "CLOUD_PREMIUM"
delete_with_instance = true
}
data_disks {
data_disk_size = 70
data_disk_type = "CLOUD_PREMIUM"
delete_with_instance = true
}
}
`

Expand Down Expand Up @@ -763,7 +817,7 @@ data "tencentcloud_instance_types" "new_type" {
memory_size = 2
}

resource "tencentcloud_instance" "foo" {
resource "tencentcloud_instance" "cvm_basic" {
instance_name = var.instance_name
availability_zone = var.availability_cvm_zone
image_id = data.tencentcloud_images.default.images.0.image_id
Expand Down