diff --git a/aws/data_source_aws_storagegateway_local_disk.go b/aws/data_source_aws_storagegateway_local_disk.go index fda3fbe05f0..ed86c4a4b5c 100644 --- a/aws/data_source_aws_storagegateway_local_disk.go +++ b/aws/data_source_aws_storagegateway_local_disk.go @@ -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, @@ -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 } } @@ -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 diff --git a/aws/data_source_aws_storagegateway_local_disk_test.go b/aws/data_source_aws_storagegateway_local_disk_test.go index bf31af61d91..d2cb7f806f2 100644 --- a/aws/data_source_aws_storagegateway_local_disk_test.go +++ b/aws/data_source_aws_storagegateway_local_disk_test.go @@ -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" @@ -19,9 +19,28 @@ 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( @@ -29,6 +48,10 @@ func TestAccAWSStorageGatewayLocalDiskDataSource_DiskPath(t *testing.T) { resource.TestCheckResourceAttrSet(dataSourceName, "disk_id"), ), }, + { + Config: testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskPath_NonExistent(rName), + ExpectError: regexp.MustCompile(`no results found`), + }, }, }) } @@ -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" { diff --git a/website/docs/d/storagegateway_local_disk.html.markdown b/website/docs/d/storagegateway_local_disk.html.markdown index a1b0b028179..3c607542688 100644 --- a/website/docs/d/storagegateway_local_disk.html.markdown +++ b/website/docs/d/storagegateway_local_disk.html.markdown @@ -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