-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
d/security_groups: Adds aws_security_groups data source.
- Loading branch information
Ian Dillon
committed
Jan 11, 2018
1 parent
b9b64ba
commit 6821dba
Showing
5 changed files
with
253 additions
and
0 deletions.
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,98 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"log" | ||
) | ||
|
||
func dataSourceAwsSecurityGroups() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsSecurityGroupsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"filter": dataSourceFiltersSchema(), | ||
"tags": tagsSchemaComputed(), | ||
|
||
"ids": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"vpc_ids": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsSecurityGroupsRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
req := &ec2.DescribeSecurityGroupsInput{} | ||
|
||
filters, filtersOk := d.GetOk("filter") | ||
tags, tagsOk := d.GetOk("tags") | ||
|
||
if !filtersOk && !tagsOk { | ||
return fmt.Errorf("One of filters or tags must be assigned") | ||
} | ||
|
||
if filtersOk { | ||
req.Filters = append(req.Filters, | ||
buildAwsDataSourceFilters(filters.(*schema.Set))...) | ||
} | ||
if tagsOk { | ||
req.Filters = append(req.Filters, buildEC2TagFilterList( | ||
tagsFromMap(tags.(map[string]interface{})), | ||
)...) | ||
} | ||
|
||
log.Printf("[DEBUG] Reading Security Groups with request: %s", req) | ||
|
||
var ids, vpc_ids []string | ||
nextToken := "" | ||
for { | ||
if nextToken != "" { | ||
req.NextToken = aws.String(nextToken) | ||
} | ||
|
||
resp, err := conn.DescribeSecurityGroups(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, sg := range resp.SecurityGroups { | ||
ids = append(ids, *sg.GroupId) | ||
vpc_ids = append(vpc_ids, *sg.VpcId) | ||
} | ||
|
||
if resp.NextToken == nil { | ||
break | ||
} | ||
nextToken = *resp.NextToken | ||
} | ||
|
||
if len(ids) < 1 { | ||
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.") | ||
} | ||
|
||
log.Printf("[DEBUG] Found %d securuity groups via given filter: %s", len(ids), req) | ||
|
||
d.SetId(resource.UniqueId()) | ||
err := d.Set("ids", ids) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = d.Set("vpc_ids", vpc_ids) | ||
if err != nil { | ||
return 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,99 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"testing" | ||
) | ||
|
||
func TestAccDataSourceAwsSecurityGroups_tag(t *testing.T) { | ||
rInt := acctest.RandInt() | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAwsSecurityGroupsConfig_tag(rInt), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.aws_security_groups.by_tag", "ids.#", "3"), | ||
resource.TestCheckResourceAttr("data.aws_security_groups.by_tag", "vpc_ids.#", "3"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceAwsSecurityGroups_filter(t *testing.T) { | ||
rInt := acctest.RandInt() | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAwsSecurityGroupsConfig_filter(rInt), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.aws_security_groups.by_filter", "ids.#", "3"), | ||
resource.TestCheckResourceAttr("data.aws_security_groups.by_filter", "vpc_ids.#", "3"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAwsSecurityGroupsConfig_tag(rInt int) string { | ||
return fmt.Sprintf(` | ||
resource "aws_vpc" "test_tag" { | ||
cidr_block = "172.16.0.0/16" | ||
tags { | ||
Name = "terraform-testacc-security-group-data-source" | ||
} | ||
} | ||
resource "aws_security_group" "test" { | ||
count = 3 | ||
vpc_id = "${aws_vpc.test_tag.id}" | ||
name = "tf-%[1]d-${count.index}" | ||
tags { | ||
Seed = "%[1]d" | ||
} | ||
} | ||
data "aws_security_groups" "by_tag" { | ||
tags { | ||
Seed = "${aws_security_group.test.0.tags["Seed"]}" | ||
} | ||
} | ||
`, rInt) | ||
} | ||
|
||
func testAccDataSourceAwsSecurityGroupsConfig_filter(rInt int) string { | ||
return fmt.Sprintf(` | ||
resource "aws_vpc" "test_filter" { | ||
cidr_block = "172.16.0.0/16" | ||
tags { | ||
Name = "terraform-testacc-security-group-data-source" | ||
} | ||
} | ||
resource "aws_security_group" "test" { | ||
count = 3 | ||
vpc_id = "${aws_vpc.test_filter.id}" | ||
name = "tf-%[1]d-${count.index}" | ||
tags { | ||
Seed = "%[1]d" | ||
} | ||
} | ||
data "aws_security_groups" "by_filter" { | ||
filter { | ||
name = "vpc-id" | ||
values = ["${aws_vpc.test_filter.id}"] | ||
} | ||
filter { | ||
name = "group-name" | ||
values = ["tf-${aws_security_group.test.0.tags["Seed"]}-*"] | ||
} | ||
} | ||
`, rInt) | ||
} |
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,52 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_security_groups" | ||
sidebar_current: "docs-aws-datasource-security-groups" | ||
description: |- | ||
Get information about a set of Security Groups. | ||
--- | ||
|
||
# Data Source: aws_security_groups | ||
|
||
Use this data source to get IDs and VPC membership of Security Groups that are created | ||
outside of Terraform. | ||
|
||
## Example Usage | ||
```hcl | ||
data "aws_security_groups" "test" { | ||
tags { | ||
Application = "k8s", | ||
Environment = "dev" | ||
} | ||
} | ||
``` | ||
|
||
```hcl | ||
data "aws_security_groups" "test" { | ||
filter { | ||
name = "group-name" | ||
values = ["*nodes*"] | ||
} | ||
filter { | ||
name = "vpc-id" | ||
values = ["${var.vpc_id}"] | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `tags` - (Optional) A mapping of tags, each pair of which must exactly match for | ||
desired security groups. | ||
|
||
* `filter` - (Optional) One or more name/value pairs to use as filters. There are | ||
several valid keys, for a full reference, check out | ||
[describe-security-groups in the AWS CLI reference][1]. | ||
|
||
## Attributes Reference | ||
|
||
* `ids` - IDs of the matches security groups. | ||
* `vpc_ids` - The VPC IDs of the matched security groups. The data source's tag or filter *will span VPCs* | ||
unless the `vpc-id` filter is also used. | ||
|
||
[1]: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-groups.html |