Added data source for aws_efs_file_system, heavily based off of resource_aws_efs_file_systems read method.

This commit is contained in:
Craig Knott 2017-04-27 10:52:36 -05:00
parent 1601af7a07
commit aa2e02ab14
3 changed files with 186 additions and 0 deletions

View File

@ -0,0 +1,110 @@
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/efs"
"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,
ValidateFunc: validateMaxLength(64),
},
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"performance_mode": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func dataSourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) error {
efsconn := meta.(*AWSClient).efsconn
efsCreationToken := d.Get("creation_token").(string)
efsFileSystemId := d.Get("id").(string)
describeEfsOpts := &efs.DescribeFileSystemsInput{}
switch {
case efsCreationToken != "":
describeEfsOpts.CreationToken = aws.String(efsCreationToken)
case efsFileSystemId != "":
describeEfsOpts.FileSystemId = aws.String(efsFileSystemId)
}
describeResp, err := efsconn.DescribeFileSystems(describeEfsOpts)
if err != nil {
return fmt.Errorf("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)
return nil
}

View File

@ -0,0 +1,75 @@
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{
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 = `
provider "aws" {
region = "us-west-2"
}
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" {
id = "${aws_efs_file_system.test.id}"
}
`

View File

@ -169,6 +169,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(),