From ac7ba50cc6c5e9e21b61dcd747573de265d2fdca Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Fri, 20 Jul 2018 15:12:52 -0400 Subject: [PATCH] New Data Source: aws_storagegateway_local_disk --- ...ta_source_aws_storagegateway_local_disk.go | 76 +++++++++++++++ ...urce_aws_storagegateway_local_disk_test.go | 97 +++++++++++++++++++ aws/provider.go | 1 + website/aws.erb | 3 + .../d/storagegateway_local_disk.html.markdown | 32 ++++++ 5 files changed, 209 insertions(+) create mode 100644 aws/data_source_aws_storagegateway_local_disk.go create mode 100644 aws/data_source_aws_storagegateway_local_disk_test.go create mode 100644 website/docs/d/storagegateway_local_disk.html.markdown diff --git a/aws/data_source_aws_storagegateway_local_disk.go b/aws/data_source_aws_storagegateway_local_disk.go new file mode 100644 index 00000000000..fda3fbe05f0 --- /dev/null +++ b/aws/data_source_aws_storagegateway_local_disk.go @@ -0,0 +1,76 @@ +package aws + +import ( + "errors" + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/storagegateway" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsStorageGatewayLocalDisk() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsStorageGatewayLocalDiskRead, + + Schema: map[string]*schema.Schema{ + "disk_id": { + Type: schema.TypeString, + Computed: true, + }, + "disk_path": { + Type: schema.TypeString, + Required: true, + }, + "gateway_arn": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validateArn, + }, + }, + } +} + +func dataSourceAwsStorageGatewayLocalDiskRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).storagegatewayconn + + input := &storagegateway.ListLocalDisksInput{ + GatewayARN: aws.String(d.Get("gateway_arn").(string)), + } + + log.Printf("[DEBUG] Reading Storage Gateway Local Disk: %s", input) + output, err := conn.ListLocalDisks(input) + if err != nil { + return fmt.Errorf("error reading Storage Gateway Local Disk: %s", err) + } + + if output == nil || len(output.Disks) == 0 { + 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 { + matchingDisks = append(matchingDisks, disk) + } + } + + if len(matchingDisks) == 0 { + return errors.New("no results found for query, try adjusting your search criteria") + } + + if len(matchingDisks) > 1 { + return errors.New("multiple results found for query, try adjusting your search criteria") + } + + disk := matchingDisks[0] + + d.SetId(aws.StringValue(disk.DiskId)) + d.Set("disk_id", disk.DiskId) + 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 new file mode 100644 index 00000000000..bf31af61d91 --- /dev/null +++ b/aws/data_source_aws_storagegateway_local_disk_test.go @@ -0,0 +1,97 @@ +package aws + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +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_NonExistent(rName), + ExpectError: regexp.MustCompile(`no results found`), + }, + { + Config: testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskPath(rName), + Check: resource.ComposeTestCheckFunc( + testAccAWSStorageGatewayLocalDiskDataSourceExists(dataSourceName), + resource.TestCheckResourceAttrSet(dataSourceName, "disk_id"), + ), + }, + }, + }) +} + +func testAccAWSStorageGatewayLocalDiskDataSourceExists(dataSourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + _, ok := s.RootModule().Resources[dataSourceName] + if !ok { + return fmt.Errorf("not found: %s", dataSourceName) + } + + return nil + } +} + +func testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskPath(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/xvdb" + force_detach = true + instance_id = "${aws_instance.test.id}" + volume_id = "${aws_ebs_volume.test.id}" +} + +data "aws_storagegateway_local_disk" "test" { + disk_path = "${aws_volume_attachment.test.device_name}" + gateway_arn = "${aws_storagegateway_gateway.test.arn}" +} +`, rName) +} + +func testAccAWSStorageGatewayLocalDiskDataSourceConfig_DiskPath_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/xvdb" + force_detach = true + instance_id = "${aws_instance.test.id}" + volume_id = "${aws_ebs_volume.test.id}" +} + +data "aws_storagegateway_local_disk" "test" { + disk_path = "/dev/xvdz" + gateway_arn = "${aws_storagegateway_gateway.test.arn}" +} +`, rName) +} diff --git a/aws/provider.go b/aws/provider.go index c52dea8bc56..2ff28991916 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -248,6 +248,7 @@ func Provider() terraform.ResourceProvider { "aws_sns_topic": dataSourceAwsSnsTopic(), "aws_sqs_queue": dataSourceAwsSqsQueue(), "aws_ssm_parameter": dataSourceAwsSsmParameter(), + "aws_storagegateway_local_disk": dataSourceAwsStorageGatewayLocalDisk(), "aws_subnet": dataSourceAwsSubnet(), "aws_subnet_ids": dataSourceAwsSubnetIDs(), "aws_vpcs": dataSourceAwsVpcs(), diff --git a/website/aws.erb b/website/aws.erb index 305d23a6551..3f8356bb6fd 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -328,6 +328,9 @@ > aws_ssm_parameter + > + aws_storagegateway_local_disk + > aws_subnet diff --git a/website/docs/d/storagegateway_local_disk.html.markdown b/website/docs/d/storagegateway_local_disk.html.markdown new file mode 100644 index 00000000000..a1b0b028179 --- /dev/null +++ b/website/docs/d/storagegateway_local_disk.html.markdown @@ -0,0 +1,32 @@ +--- +layout: "aws" +page_title: "AWS: aws_storagegateway_local_disk" +sidebar_current: "docs-aws-datasource-storagegateway-local-disk" +description: |- + Retrieve information about a Storage Gateway local disk +--- + +# Data Source: aws_storagegateway_local_disk + +Retrieve information about a Storage Gateway local disk. The disk identifier is useful for adding the disk as a cache or upload buffer to a gateway. + +## Example Usage + +```hcl +data "aws_storagegateway_local_disk" "test" { + disk_path = "${aws_volume_attachment.test.device_name}" + gateway_arn = "${aws_storagegateway_gateway.test.arn}" +} +``` + +## 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. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `disk_id` - The disk identifier. e.g. `pci-0000:03:00.0-scsi-0:0:0:0` +* `id` - The disk identifier. e.g. `pci-0000:03:00.0-scsi-0:0:0:0`