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/aws_storagegateway_local_disk: Add disk_node argument #5595

Merged
merged 1 commit into from
Aug 20, 2018
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
15 changes: 12 additions & 3 deletions aws/data_source_aws_storagegateway_local_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ func dataSourceAwsStorageGatewayLocalDisk() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"disk_node": {
Type: schema.TypeString,
Optional: true,
},
"disk_path": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
"gateway_arn": {
Type: schema.TypeString,
Expand Down Expand Up @@ -49,12 +53,16 @@ func dataSourceAwsStorageGatewayLocalDiskRead(d *schema.ResourceData, meta inter
return errors.New("no results found for query, try adjusting your search criteria")
}

diskPath := d.Get("disk_path").(string)
var matchingDisks []*storagegateway.Disk

for _, disk := range output.Disks {
if aws.StringValue(disk.DiskPath) == diskPath {
if v, ok := d.GetOk("disk_node"); ok && v.(string) == aws.StringValue(disk.DiskNode) {
matchingDisks = append(matchingDisks, disk)
continue
}
if v, ok := d.GetOk("disk_path"); ok && v.(string) == aws.StringValue(disk.DiskPath) {
matchingDisks = append(matchingDisks, disk)
continue
}
}

Expand All @@ -70,6 +78,7 @@ func dataSourceAwsStorageGatewayLocalDiskRead(d *schema.ResourceData, meta inter

d.SetId(aws.StringValue(disk.DiskId))
d.Set("disk_id", disk.DiskId)
d.Set("disk_node", disk.DiskNode)
d.Set("disk_path", disk.DiskPath)

return nil
Expand Down
79 changes: 77 additions & 2 deletions aws/data_source_aws_storagegateway_local_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSStorageGatewayLocalDiskDataSource_DiskPath(t *testing.T) {
func TestAccAWSStorageGatewayLocalDiskDataSource_DiskNode(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_storagegateway_local_disk.test"

Expand All @@ -19,16 +19,39 @@ func TestAccAWSStorageGatewayLocalDiskDataSource_DiskPath(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskPath_NonExistent(rName),
Config: testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskNode(rName),
Check: resource.ComposeTestCheckFunc(
testAccAWSStorageGatewayLocalDiskDataSourceExists(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "disk_id"),
),
},
{
Config: testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskNode_NonExistent(rName),
ExpectError: regexp.MustCompile(`no results found`),
},
},
})
}

func TestAccAWSStorageGatewayLocalDiskDataSource_DiskPath(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_storagegateway_local_disk.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskPath(rName),
Check: resource.ComposeTestCheckFunc(
testAccAWSStorageGatewayLocalDiskDataSourceExists(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "disk_id"),
),
},
{
Config: testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskPath_NonExistent(rName),
ExpectError: regexp.MustCompile(`no results found`),
},
},
})
}
Expand All @@ -44,6 +67,58 @@ func testAccAWSStorageGatewayLocalDiskDataSourceExists(dataSourceName string) re
}
}

func testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskNode(rName string) string {
return testAccAWSStorageGatewayGatewayConfig_GatewayType_FileS3(rName) + fmt.Sprintf(`
resource "aws_ebs_volume" "test" {
availability_zone = "${aws_instance.test.availability_zone}"
size = "10"
type = "gp2"

tags {
Name = %q
}
}

resource "aws_volume_attachment" "test" {
device_name = "/dev/sdb"
force_detach = true
instance_id = "${aws_instance.test.id}"
volume_id = "${aws_ebs_volume.test.id}"
}

data "aws_storagegateway_local_disk" "test" {
disk_node = "${aws_volume_attachment.test.device_name}"
gateway_arn = "${aws_storagegateway_gateway.test.arn}"
}
`, rName)
}

func testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskNode_NonExistent(rName string) string {
return testAccAWSStorageGatewayGatewayConfig_GatewayType_FileS3(rName) + fmt.Sprintf(`
resource "aws_ebs_volume" "test" {
availability_zone = "${aws_instance.test.availability_zone}"
size = "10"
type = "gp2"

tags {
Name = %q
}
}

resource "aws_volume_attachment" "test" {
device_name = "/dev/sdb"
force_detach = true
instance_id = "${aws_instance.test.id}"
volume_id = "${aws_ebs_volume.test.id}"
}

data "aws_storagegateway_local_disk" "test" {
disk_node = "/dev/sdz"
gateway_arn = "${aws_storagegateway_gateway.test.arn}"
}
`, rName)
}

func testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskPath(rName string) string {
return testAccAWSStorageGatewayGatewayConfig_GatewayType_FileS3(rName) + fmt.Sprintf(`
resource "aws_ebs_volume" "test" {
Expand Down
3 changes: 2 additions & 1 deletion website/docs/d/storagegateway_local_disk.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ data "aws_storagegateway_local_disk" "test" {

## Argument Reference

* `disk_path` - (Required) The device path of the local disk to retrieve. For example, `/dev/sdb` or `/dev/xvdb`.
* `gateway_arn` - (Required) The Amazon Resource Name (ARN) of the gateway.
* `disk_node` - (Optional) The device node of the local disk to retrieve. For example, `/dev/sdb`.
* `disk_path` - (Optional) The device path of the local disk to retrieve. For example, `/dev/xvdb` or `/dev/nvme1n1`.

## Attributes Reference

Expand Down