Skip to content

Commit

Permalink
add volume_attached/type to compute instance data source (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiChangkuo authored Jan 16, 2021
1 parent e2704da commit 4e56673
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/compute_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ The `volume_attached` block supports:
* `volume_id` - The volume id on that attachment.
* `boot_index` - The volume boot index on that attachment.
* `size` - The volume size on that attachment.
* `type` - The volume type on that attachment.
* `pci_address` - The volume pci address on that attachment.

The `scheduler_hints` block supports:
Expand Down
34 changes: 27 additions & 7 deletions huaweicloud/data_source_huaweicloud_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/blockstorage/v2/volumes"
"github.com/huaweicloud/golangsdk/openstack/common/tags"
"github.com/huaweicloud/golangsdk/openstack/ecs/v1/block_devices"
"github.com/huaweicloud/golangsdk/openstack/ecs/v1/cloudservers"
Expand Down Expand Up @@ -121,6 +122,10 @@ func DataSourceComputeInstance() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"pci_address": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -244,26 +249,41 @@ func dataSourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) err
}

// Set volume attached
instanceVolumes := []map[string]interface{}{}
if len(server.VolumeAttached) > 0 {
for _, b := range server.VolumeAttached {
blockStorageClient, err := config.BlockStorageV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud EVS client: %s", err)
}

bds := make([]map[string]interface{}, len(server.VolumeAttached))
for i, b := range server.VolumeAttached {
// retrieve volume `size` and `type`
volumeInfo, err := volumes.Get(blockStorageClient, b.ID).Extract()
if err != nil {
return err
}
log.Printf("[DEBUG] Retrieved volume %s: %#v", b.ID, volumeInfo)

// retrieve volume `pci_address`
va, err := block_devices.Get(ecsClient, d.Id(), b.ID).Extract()
if err != nil {
return err
}
log.Printf("[DEBUG] Retrieved volume attachment %s: %#v", d.Id(), va)
v := map[string]interface{}{
log.Printf("[DEBUG] Retrieved block device %s: %#v", b.ID, va)

bds[i] = map[string]interface{}{
"volume_id": b.ID,
"size": volumeInfo.Size,
"type": volumeInfo.VolumeType,
"boot_index": va.BootIndex,
"size": va.Size,
"pci_address": va.PciAddress,
}
instanceVolumes = append(instanceVolumes, v)

if va.BootIndex == 0 {
d.Set("system_disk_id", b.ID)
}
}
d.Set("volume_attached", instanceVolumes)
d.Set("volume_attached", bds)
}

// Set instance tags
Expand Down
2 changes: 2 additions & 0 deletions huaweicloud/resource_huaweicloud_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ func ResourceComputeInstanceV2() *schema.Resource {
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ConflictsWith: []string{"block_device", "metadata"},
},
"delete_disks_on_termination": {
Expand Down

0 comments on commit 4e56673

Please sign in to comment.