-
Notifications
You must be signed in to change notification settings - Fork 359
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
ozerovandrei
merged 11 commits into
terraform-provider-openstack:master
from
OpenSource-THG:master
Dec 20, 2019
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b5388b4
Add the openstack_blockstorage_volume_v2 data source
32be5e1
Fix styling issues
c73fe88
Add the openstack_blockstorage_volume_v2 data source
d960a1b
Fix styling issues
f6781a3
Add openstack_blockstorage_volume_v3
5cc441e
Update logging
86483e6
Fix accidental go import format
4fea0ec
Update Docs reference to not mention v2
d6c431c
Merge pull request #2 from OpenSource-THG/feature/SRE-593/openstack_b…
2690eed
Update data_source_openstack_blockstorage_volume_v2.go
ozerovandrei 2ec6800
Update data_source_openstack_blockstorage_volume_v3.go
ozerovandrei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
openstack/data_source_openstack_blockstorage_volume_v3.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
121
openstack/data_source_openstack_blockstorage_volume_v3_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.