Skip to content

Commit

Permalink
add (#3033)
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenEarth authored Dec 20, 2024
1 parent 7a3f7d1 commit 48cf2e9
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions tencentcloud/services/cvm/resource_tc_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,27 +1010,68 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{})
}

// set data_disks
var hasDataDisks, isCombineDataDisks, hasDataDisksName bool
var hasDataDisks, isCombineDataDisks, hasDataDisksId, hasDataDisksName bool
dataDiskList := make([]map[string]interface{}, 0, len(instance.DataDisks))
diskSizeMap := map[string]*uint64{}
diskOrderMap := make(map[string]int)
dataDiskIds := make([]*string, 0, len(instance.DataDisks))
refreshDataDisks := make([]interface{}, 0, len(instance.DataDisks))

if v, ok := d.GetOk("data_disks"); ok {
hasDataDisks = true
// check has data disk name
// check has data disk id and name
dataDisks := v.([]interface{})
for _, item := range dataDisks {
value := item.(map[string]interface{})
if v, ok := value["data_disk_id"]; ok && v != nil {
diskId := v.(string)
if diskId != "" {
dataDiskIds = append(dataDiskIds, &diskId)
hasDataDisksId = true
}
}

if v, ok := value["data_disk_name"]; ok && v != nil {
diskName := v.(string)
if diskName != "" {
hasDataDisksName = true
break
}
}
}
}

// refresh data disk name and size
if hasDataDisksId && len(dataDiskIds) > 0 {
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
disks, err := cbsService.DescribeDiskList(ctx, dataDiskIds)
if err != nil {
return resource.NonRetryableError(err)
}

if v, ok := d.GetOk("data_disks"); ok {
dataDisks := v.([]interface{})
for _, item := range dataDisks {
value := item.(map[string]interface{})
for _, item := range disks {
if value["data_disk_id"].(string) == *item.DiskId {
value["data_disk_name"] = *item.DiskName
value["data_disk_size"] = int(*item.DiskSize)
break
}
}
}

refreshDataDisks = dataDisks
}

return nil
})

if err != nil {
return err
}
}

// scene with has disks name
if len(instance.DataDisks) > 0 && !hasDataDisksName {
var diskIds []*string
Expand Down Expand Up @@ -1162,6 +1203,10 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{})
// get source disk hash
if v, ok := d.GetOk("data_disks"); ok {
dataDisks := v.([]interface{})
if hasDataDisksId {
dataDisks = refreshDataDisks
}

for index, item := range dataDisks {
value := item.(map[string]interface{})
tmpMap := make(map[string]interface{})
Expand Down

0 comments on commit 48cf2e9

Please sign in to comment.