diff --git a/builtin/providers/aws/data_source_aws_efs_file_system.go b/builtin/providers/aws/data_source_aws_efs_file_system.go new file mode 100644 index 000000000..014ae1353 --- /dev/null +++ b/builtin/providers/aws/data_source_aws_efs_file_system.go @@ -0,0 +1,113 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/efs" + "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsEfsFileSystem() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsEfsFileSystemRead, + + Schema: map[string]*schema.Schema{ + "creation_token": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ValidateFunc: validateMaxLength(64), + }, + "file_system_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "performance_mode": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tagsSchemaComputed(), + }, + } +} + +func dataSourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) error { + efsconn := meta.(*AWSClient).efsconn + + describeEfsOpts := &efs.DescribeFileSystemsInput{} + + if v, ok := d.GetOk("creation_token"); ok { + describeEfsOpts.CreationToken = aws.String(v.(string)) + } + + if v, ok := d.GetOk("file_system_id"); ok { + describeEfsOpts.FileSystemId = aws.String(v.(string)) + } + + describeResp, err := efsconn.DescribeFileSystems(describeEfsOpts) + if err != nil { + return errwrap.Wrapf("Error retrieving EFS: {{err}}", err) + } + if len(describeResp.FileSystems) != 1 { + return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(describeResp.FileSystems)) + } + + d.SetId(*describeResp.FileSystems[0].FileSystemId) + + tags := make([]*efs.Tag, 0) + var marker string + for { + params := &efs.DescribeTagsInput{ + FileSystemId: aws.String(d.Id()), + } + if marker != "" { + params.Marker = aws.String(marker) + } + + tagsResp, err := efsconn.DescribeTags(params) + if err != nil { + return fmt.Errorf("Error retrieving EC2 tags for EFS file system (%q): %s", + d.Id(), err.Error()) + } + + for _, tag := range tagsResp.Tags { + tags = append(tags, tag) + } + + if tagsResp.NextMarker != nil { + marker = *tagsResp.NextMarker + } else { + break + } + } + + err = d.Set("tags", tagsToMapEFS(tags)) + if err != nil { + return err + } + + var fs *efs.FileSystemDescription + for _, f := range describeResp.FileSystems { + if d.Id() == *f.FileSystemId { + fs = f + break + } + } + if fs == nil { + log.Printf("[WARN] EFS (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + + d.Set("creation_token", fs.CreationToken) + d.Set("performance_mode", fs.PerformanceMode) + d.Set("file_system_id", fs.FileSystemId) + + return nil +} diff --git a/builtin/providers/aws/data_source_aws_efs_file_system_test.go b/builtin/providers/aws/data_source_aws_efs_file_system_test.go new file mode 100644 index 000000000..925e6afd8 --- /dev/null +++ b/builtin/providers/aws/data_source_aws_efs_file_system_test.go @@ -0,0 +1,71 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceAwsEfsFileSystem(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsEfsFileSystemConfig, + Check: resource.ComposeTestCheckFunc( + testAccDataSourceAwsEfsFileSystemCheck("data.aws_efs_file_system.by_creation_token"), + testAccDataSourceAwsEfsFileSystemCheck("data.aws_efs_file_system.by_id"), + ), + }, + }, + }) +} + +func testAccDataSourceAwsEfsFileSystemCheck(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("root module has no resource called %s", name) + } + + efsRs, ok := s.RootModule().Resources["aws_efs_file_system.test"] + if !ok { + return fmt.Errorf("can't find aws_efs_file_system.test in state") + } + + attr := rs.Primary.Attributes + + if attr["creation_token"] != efsRs.Primary.Attributes["creation_token"] { + return fmt.Errorf( + "creation_token is %s; want %s", + attr["creation_token"], + efsRs.Primary.Attributes["creation_token"], + ) + } + + if attr["id"] != efsRs.Primary.Attributes["id"] { + return fmt.Errorf( + "file_system_id is %s; want %s", + attr["id"], + efsRs.Primary.Attributes["id"], + ) + } + + return nil + } +} + +const testAccDataSourceAwsEfsFileSystemConfig = ` +resource "aws_efs_file_system" "test" {} + +data "aws_efs_file_system" "by_creation_token" { + creation_token = "${aws_efs_file_system.test.creation_token}" +} + +data "aws_efs_file_system" "by_id" { + file_system_id = "${aws_efs_file_system.test.id}" +} +` diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 99240ea31..e6165f33e 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -178,6 +178,7 @@ func Provider() terraform.ResourceProvider { "aws_ecs_cluster": dataSourceAwsEcsCluster(), "aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(), "aws_ecs_task_definition": dataSourceAwsEcsTaskDefinition(), + "aws_efs_file_system": dataSourceAwsEfsFileSystem(), "aws_eip": dataSourceAwsEip(), "aws_elb_hosted_zone_id": dataSourceAwsElbHostedZoneId(), "aws_elb_service_account": dataSourceAwsElbServiceAccount(), diff --git a/website/source/docs/providers/aws/d/efs_file_system.html.markdown b/website/source/docs/providers/aws/d/efs_file_system.html.markdown new file mode 100644 index 000000000..4441e01eb --- /dev/null +++ b/website/source/docs/providers/aws/d/efs_file_system.html.markdown @@ -0,0 +1,39 @@ +--- +layout: "aws" +page_title: "AWS: efs_file_system" +sidebar_current: "docs-aws-datasource-efs-file-system" +description: |- + Provides an Elastic File System (EFS) data source. +--- + +# aws_efs_file_system + +Provides information about an Elastic File System (EFS). + +## Example Usage + +```hcl +variable "file_system_id" { + type = "string" + default = "" +} + +data "aws_efs_file_system" "by_id" { + file_system_id = "${var.file_system_id}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `file_system_id` - (Optional) The ID that identifies the file system (e.g. fs-ccfc0d65). +* `creation_token` - (Optional) Restricts the list to the file system with this creation token + +## Attributes Reference + +The following attributes are exported: + +* `performance_mode` - The PerformanceMode of the file system. +* `tags` - The list of tags assigned to the file system. + diff --git a/website/source/layouts/aws.erb b/website/source/layouts/aws.erb index 646ac01f7..11994afee 100644 --- a/website/source/layouts/aws.erb +++ b/website/source/layouts/aws.erb @@ -62,6 +62,9 @@