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

Add tag filter to aws_efs_file_system data source #20399

Merged
merged 5 commits into from
Aug 26, 2021
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
30 changes: 27 additions & 3 deletions aws/data_source_aws_efs_file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func dataSourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) er
efsconn := meta.(*AWSClient).efsconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

tagsToMatch := keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().IgnoreConfig(ignoreTagsConfig)

describeEfsOpts := &efs.DescribeFileSystemsInput{}

if v, ok := d.GetOk("creation_token"); ok {
Expand All @@ -110,11 +112,33 @@ func dataSourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) er
return errors.New("error reading EFS FileSystem: empty output")
}

if len(describeResp.FileSystems) > 1 {
return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(describeResp.FileSystems))
var results []*efs.FileSystemDescription

if len(tagsToMatch) > 0 {

var fileSystems []*efs.FileSystemDescription

for _, fileSystem := range describeResp.FileSystems {

tags := keyvaluetags.EfsKeyValueTags(fileSystem.Tags)

if !tags.ContainsAll(tagsToMatch) {
continue
}

fileSystems = append(fileSystems, fileSystem)
}

results = fileSystems
} else {
results = describeResp.FileSystems
}

if len(results) > 1 {
return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(results))
}

fs := describeResp.FileSystems[0]
fs := results[0]

d.SetId(aws.StringValue(fs.FileSystemId))
d.Set("availability_zone_id", fs.AvailabilityZoneId)
Expand Down
51 changes: 51 additions & 0 deletions aws/data_source_aws_efs_file_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,36 @@ func TestAccDataSourceAwsEfsFileSystem_id(t *testing.T) {
})
}

func TestAccDataSourceAwsEfsFileSystem_tags(t *testing.T) {
dataSourceName := "data.aws_efs_file_system.test"
resourceName := "aws_efs_file_system.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, efs.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEfsFileSystemTagsConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsEfsFileSystemCheck(dataSourceName, resourceName),
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "performance_mode", resourceName, "performance_mode"),
resource.TestCheckResourceAttrPair(dataSourceName, "creation_token", resourceName, "creation_token"),
resource.TestCheckResourceAttrPair(dataSourceName, "encrypted", resourceName, "encrypted"),
resource.TestCheckResourceAttrPair(dataSourceName, "kms_key_id", resourceName, "kms_key_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags", resourceName, "tags"),
resource.TestCheckResourceAttrPair(dataSourceName, "dns_name", resourceName, "dns_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "provisioned_throughput_in_mibps", resourceName, "provisioned_throughput_in_mibps"),
resource.TestCheckResourceAttrPair(dataSourceName, "throughput_mode", resourceName, "throughput_mode"),
resource.TestCheckResourceAttrPair(dataSourceName, "lifecycle_policy", resourceName, "lifecycle_policy"),
resource.TestMatchResourceAttr(dataSourceName, "size_in_bytes", regexp.MustCompile(`^\d+$`)),
),
},
},
})
}

func TestAccDataSourceAwsEfsFileSystem_name(t *testing.T) {
dataSourceName := "data.aws_efs_file_system.test"
resourceName := "aws_efs_file_system.test"
Expand Down Expand Up @@ -162,6 +192,27 @@ data "aws_efs_file_system" "test" {
}
`

const testAccDataSourceAwsEfsFileSystemTagsConfig = `
resource "aws_efs_file_system" "test" {
tags = {
Name = "default-efs"
Environment = "dev"
}
}

resource "aws_efs_file_system" "wrong-env" {
tags = {
Environment = "test"
}
}

resource "aws_efs_file_system" "no-tags" {}

data "aws_efs_file_system" "test" {
tags = aws_efs_file_system.test.tags
}
`

const testAccDataSourceAwsEfsFileSystemAvailabilityZoneConfig = `
data "aws_availability_zones" "available" {
state = "available"
Expand Down
7 changes: 7 additions & 0 deletions website/docs/d/efs_file_system.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ variable "file_system_id" {
data "aws_efs_file_system" "by_id" {
file_system_id = var.file_system_id
}

data "aws_efs_file_system" "by_tag" {
tags = {
Environment = "dev"
}
}
```

## Argument Reference
Expand All @@ -29,6 +35,7 @@ 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.
* `tags` - (Optional) Restricts the list to the file system with these tags.

## Attributes Reference

Expand Down