-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Issue #4260 New datasource: aws_network_acls #4966
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsNetworkAcls() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsNetworkAclsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"filter": ec2CustomFiltersSchema(), | ||
|
||
"tags": tagsSchemaComputed(), | ||
|
||
"vpc_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"ids": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsNetworkAclsRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
req := &ec2.DescribeNetworkAclsInput{} | ||
|
||
filters, filtersOk := d.GetOk("filter") | ||
tags, tagsOk := d.GetOk("tags") | ||
|
||
req.Filters = buildEC2AttributeFilterList( | ||
map[string]string{ | ||
"vpc-id": d.Get("vpc_id").(string), | ||
}, | ||
) | ||
|
||
if tagsOk { | ||
req.Filters = append(req.Filters, buildEC2TagFilterList( | ||
tagsFromMap(tags.(map[string]interface{})), | ||
)...) | ||
} | ||
|
||
if filtersOk { | ||
req.Filters = append(req.Filters, buildEC2CustomFilterList( | ||
filters.(*schema.Set), | ||
)...) | ||
} | ||
|
||
if len(req.Filters) == 0 { | ||
// Don't send an empty filters list; the EC2 API won't accept it. | ||
req.Filters = nil | ||
} | ||
|
||
log.Printf("[DEBUG] DescribeNetworkAcls %s\n", req) | ||
resp, err := conn.DescribeNetworkAcls(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp == nil || len(resp.NetworkAcls) == 0 { | ||
return fmt.Errorf("no matching network ACL found for vpc with id %s", d.Get("vpc_id").(string)) | ||
} | ||
|
||
networkAcls := make([]string, 0) | ||
|
||
for _, networkAcl := range resp.NetworkAcls { | ||
networkAcls = append(networkAcls, aws.StringValue(networkAcl.NetworkAclId)) | ||
} | ||
|
||
d.SetId(d.Get("vpc_id").(string)) | ||
if err := d.Set("ids", networkAcls); err != nil { | ||
return fmt.Errorf("Error setting network ACL ids: %s", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAwsNetworkAcls_basic(t *testing.T) { | ||
rName := acctest.RandString(5) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckVpcDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAwsNetworkAclsConfig(rName), | ||
}, | ||
{ | ||
Config: testAccDataSourceAwsNetworkAclsConfigWithDataSource(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.aws_network_acls.all", "ids.#", "3"), | ||
resource.TestCheckResourceAttr("data.aws_network_acls.with_tags", "ids.#", "2"), | ||
resource.TestCheckResourceAttr("data.aws_network_acls.with_filter", "ids.#", "1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAwsNetworkAclsConfigWithDataSource(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_vpc" "test-vpc" { | ||
cidr_block = "10.0.0.0/16" | ||
} | ||
|
||
resource "aws_network_acl" "acl1" { | ||
vpc_id = "${aws_vpc.test-vpc.id}" | ||
|
||
tags { | ||
Name = "testacc-acl-%s" | ||
} | ||
} | ||
|
||
resource "aws_subnet" "test" { | ||
vpc_id = "${aws_vpc.test-vpc.id}" | ||
cidr_block = "10.0.0.0/24" | ||
availability_zone = "us-west-2a" | ||
|
||
tags { | ||
Name = "tf-acc-subnet" | ||
} | ||
} | ||
|
||
resource "aws_network_acl" "acl2" { | ||
vpc_id = "${aws_vpc.test-vpc.id}" | ||
subnet_ids = ["${aws_subnet.test.id}"] | ||
|
||
tags { | ||
Name = "testacc-acl-%s" | ||
} | ||
} | ||
|
||
data "aws_network_acls" "all" { | ||
vpc_id = "${aws_vpc.test-vpc.id}" | ||
} | ||
|
||
data "aws_network_acls" "with_tags" { | ||
vpc_id = "${aws_vpc.test-vpc.id}" | ||
|
||
tags { | ||
Name = "testacc-acl-%s" | ||
} | ||
} | ||
|
||
data "aws_network_acls" "with_filter" { | ||
vpc_id = "${aws_vpc.test-vpc.id}" | ||
|
||
filter { | ||
name = "association.subnet-id" | ||
values = ["${aws_subnet.test.id}"] | ||
} | ||
} | ||
`, rName, rName, rName) | ||
} | ||
|
||
func testAccDataSourceAwsNetworkAclsConfig(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_vpc" "test-vpc" { | ||
cidr_block = "10.0.0.0/16" | ||
} | ||
|
||
resource "aws_network_acl" "acl1" { | ||
vpc_id = "${aws_vpc.test-vpc.id}" | ||
|
||
tags { | ||
Name = "testacc-acl-%s" | ||
} | ||
} | ||
|
||
resource "aws_subnet" "test" { | ||
vpc_id = "${aws_vpc.test-vpc.id}" | ||
cidr_block = "10.0.0.0/24" | ||
availability_zone = "us-west-2a" | ||
|
||
tags { | ||
Name = "tf-acc-subnet" | ||
} | ||
} | ||
|
||
resource "aws_network_acl" "acl2" { | ||
vpc_id = "${aws_vpc.test-vpc.id}" | ||
subnet_ids = ["${aws_subnet.test.id}"] | ||
|
||
tags { | ||
Name = "testacc-acl-%s" | ||
} | ||
} | ||
`, rName, rName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_network_acls" | ||
sidebar_current: "docs-aws-datasource-network-acls" | ||
description: |- | ||
Provides a list of network ACL ids for a VPC | ||
--- | ||
|
||
# Data Source: aws_network_acls | ||
|
||
## Example Usage | ||
|
||
The following shows outputing all network ACL ids in a vpc. | ||
|
||
```hcl | ||
data "aws_network_acls" "example" { | ||
vpc_id = "${var.vpc_id}" | ||
} | ||
|
||
output "example" { | ||
value = "${data.aws_network_acls.example.ids}" | ||
} | ||
``` | ||
|
||
The following example retrieves a list of all network ACL ids in a VPC with a custom | ||
tag of `Tier` set to a value of "Private". | ||
|
||
```hcl | ||
data "aws_network_acls" "example" { | ||
vpc_id = "${var.vpc_id}" | ||
tags { | ||
Tier = "Private" | ||
} | ||
} | ||
``` | ||
|
||
The following example retrieves a network ACL id in a VPC which associated | ||
with specific subnet. | ||
|
||
```hcl | ||
data "aws_network_acls" "example" { | ||
vpc_id = "${var.vpc_id}" | ||
filter { | ||
name = "association.subnet-id" | ||
values = ["${aws_subnet.test.id}"] | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `vpc_id` - (Required) The VPC ID that you want to filter from. | ||
|
||
* `tags` - (Optional) A mapping of tags, each pair of which must exactly match | ||
a pair on the desired network ACLs. | ||
|
||
* `filter` - (Optional) Custom filter block as described below. | ||
|
||
More complex filters can be expressed using one or more `filter` sub-blocks, | ||
which take the following arguments: | ||
|
||
* `name` - (Required) The name of the field to filter by, as defined by | ||
[the underlying AWS API](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeNetworkAcls.html). | ||
|
||
* `values` - (Required) Set of values that are accepted for the given field. | ||
A VPC will be selected if any one of the given values matches. | ||
|
||
## Attributes Reference | ||
|
||
* `ids` - A list of all the network ACL ids found. This data source will fail if none are found. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This parameter does not look required for the API call and its valid to want to search for Network ACLs across multiple VPCs, so it should be made optional or removed (in preference of
filter
): https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#DescribeNetworkAclsInput