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

Data source for openstack_blockstorage_volume_v3 #947

Merged
1 change: 0 additions & 1 deletion openstack/data_source_openstack_blockstorage_volume_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"

"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that we need this empty line given that we use it to separate terraform and gophercloud imports in other files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this file is unrelated to this PR.

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down
121 changes: 121 additions & 0 deletions openstack/data_source_openstack_blockstorage_volume_v3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package openstack

import (
"fmt"
"log"

"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceBlockStorageVolumeV3() *schema.Resource {
return &schema.Resource{
Read: dataSourceBlockStorageVolumeV3Read,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"status": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"metadata": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
},

// Computed values
"bootable": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"volume_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"size": {
Type: schema.TypeInt,
Computed: true,
},

"source_volume_id": {
Type: schema.TypeString,
Computed: true,
},

"multiattach": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func dataSourceBlockStorageVolumeV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.BlockStorageV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
}

listOpts := volumes.ListOpts{
Metadata: expandToMapStringString(d.Get("metadata").(map[string]interface{})),
Name: d.Get("name").(string),
Status: d.Get("status").(string),
}

allPages, err := volumes.List(client, listOpts).AllPages()
if err != nil {
return fmt.Errorf("Unable to query openstack_blockstorage_volume_v3: %s", err)
}

allVolumes, err := volumes.ExtractVolumes(allPages)
if err != nil {
return fmt.Errorf("Unable to retrieve openstack_blockstorage_volume_v3: %s", err)
}

if len(allVolumes) > 1 {
return fmt.Errorf("Your openstack_blockstorage_volume_v3 query returned multiple results.")
}

if len(allVolumes) < 1 {
return fmt.Errorf("Your openstack_blockstorage_volume_v3 query returned no results.")
}

return dataSourceBlockStorageVolumeV3Attributes(d, allVolumes[0])
}

func dataSourceBlockStorageVolumeV3Attributes(d *schema.ResourceData, volume volumes.Volume) error {
d.SetId(volume.ID)
d.Set("name", volume.Name)
d.Set("status", volume.Status)
d.Set("bootable", volume.Bootable)
d.Set("volume_type", volume.VolumeType)
d.Set("size", volume.Size)
d.Set("source_volume_id", volume.SourceVolID)
d.Set("multiattach", volume.Multiattach)

if err := d.Set("metadata", volume.Metadata); err != nil {
log.Printf("[DEBUG] Unable to set metadata for volume %s: %s", volume.ID, err)
}

return nil
}
121 changes: 121 additions & 0 deletions openstack/data_source_openstack_blockstorage_volume_v3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package openstack

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
)

func TestAccBlockStorageV3VolumeDataSource_basic(t *testing.T) {
resourceName := "data.openstack_blockstorage_volume_v3.volume_1"
volumeName := acctest.RandomWithPrefix("tf-acc-volume")

var volumeID string
if os.Getenv("TF_ACC") != "" {
var err error
volumeID, err = testAccBlockStorageV3CreateVolume(volumeName)
if err != nil {
t.Fatal(err)
}
defer testAccBlockStorageV3DeleteVolume(t, volumeID)
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccBlockStorageV3VolumeDataSource_basic(volumeName),
Check: resource.ComposeTestCheckFunc(
testAccCheckBlockStorageV3VolumeDataSourceID(resourceName, volumeID),
resource.TestCheckResourceAttr(resourceName, "name", volumeName),
resource.TestCheckResourceAttr(resourceName, "size", "1"),
resource.TestCheckResourceAttr(resourceName, "multiattach", "false"),
),
},
},
})
}

func testAccBlockStorageV3CreateVolume(volumeName string) (string, error) {
config, err := testAccAuthFromEnv()
if err != nil {
return "", err
}

bsClient, err := config.BlockStorageV3Client(OS_REGION_NAME)
if err != nil {
return "", err
}

volCreateOpts := volumes.CreateOpts{
Size: 1,
Name: volumeName,
}

volume, err := volumes.Create(bsClient, volCreateOpts).Extract()
if err != nil {
return "", err
}

err = volumes.WaitForStatus(bsClient, volume.ID, "available", 60)
if err != nil {
return "", err
}

return volume.ID, nil
}

func testAccBlockStorageV3DeleteVolume(t *testing.T, volumeID string) {
config, err := testAccAuthFromEnv()
if err != nil {
t.Fatal(err)
}

bsClient, err := config.BlockStorageV3Client(OS_REGION_NAME)
if err != nil {
t.Fatal(err)
}

err = volumes.Delete(bsClient, volumeID, nil).ExtractErr()
if err != nil {
t.Fatal(err)
}

err = volumes.WaitForStatus(bsClient, volumeID, "DELETED", 60)
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); !ok {
t.Fatal(err)
}
}
}

func testAccCheckBlockStorageV3VolumeDataSourceID(n, id string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find volume data source: %s", n)
}

if rs.Primary.ID != id {
return fmt.Errorf("Volume data source ID not set")
}

return nil
}
}

func testAccBlockStorageV3VolumeDataSource_basic(snapshotName string) string {
return fmt.Sprintf(`
data "openstack_blockstorage_volume_v3" "volume_1" {
name = "%s"
}
`, snapshotName)
}
1 change: 1 addition & 0 deletions openstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func Provider() terraform.ResourceProvider {
"openstack_blockstorage_snapshot_v2": dataSourceBlockStorageSnapshotV2(),
"openstack_blockstorage_snapshot_v3": dataSourceBlockStorageSnapshotV3(),
"openstack_blockstorage_volume_v2": dataSourceBlockStorageVolumeV2(),
"openstack_blockstorage_volume_v3": dataSourceBlockStorageVolumeV3(),
"openstack_compute_availability_zones_v2": dataSourceComputeAvailabilityZonesV2(),
"openstack_compute_flavor_v2": dataSourceComputeFlavorV2(),
"openstack_compute_keypair_v2": dataSourceComputeKeypairV2(),
Expand Down
45 changes: 45 additions & 0 deletions website/docs/d/blockstorage_volume_v3.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
layout: "openstack"
page_title: "OpenStack: openstack_blockstorage_volume_v3"
sidebar_current: "docs-openstack-datasource-blockstorage-volume-v3"
description: |-
Get information on an OpenStack Volume.
---

# openstack\_blockstorage\_volume\_v3

Use this data source to get information about an existing volume.

## Example Usage

```hcl
data "openstack_blockstorage_volume_v3" "volume_1" {
name = "volume_1"
}
```

## Argument Reference

* `region` - (Optional) The region in which to obtain the V3 Block Storage
client. If omitted, the `region` argument of the provider is used.

* `name` - (Optional) The name of the volume.

* `status` - (Optional) The status of the volume.

* `metadata` - (Optional) Metadata key/value pairs associated with the volume.

## Attributes Reference

`id` is set to the ID of the found volume. In addition, the following attributes
are exported:

* `region` - See Argument Reference above.
* `name` - See Argument Reference above.
* `status` - See Argument Reference above.
* `metadata` - See Argument Reference above.
* `volume_type` - The type of the volume.
* `bootable` - Indicates if the volume is bootable.
* `size` - The size of the volume in GBs.
* `source_volume_id` - The ID of the volume from which the current volume was created.
* `multiattach` - Indicates if the volume can be attached to more then one server.
3 changes: 3 additions & 0 deletions website/openstack.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<li<%= sidebar_current("docs-openstack-datasource-blockstorage-volume-v2") %>>
<a href="/docs/providers/openstack/d/blockstorage_volume_v2.html">openstack_blockstorage_volume_v2</a>
</li>
<li<%= sidebar_current("docs-openstack-datasource-blockstorage-volume-v3") %>>
<a href="/docs/providers/openstack/d/blockstorage_volume_v3.html">openstack_blockstorage_volume_v3</a>
</li>
<li<%= sidebar_current("docs-openstack-datasource-compute-availability-zones-v2") %>>
<a href="/docs/providers/openstack/d/compute_availability_zones_v2.html">openstack_compute_availability_zones_v2</a>
</li>
Expand Down