-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1233 from trung/f-aws-internet-gateway
#1196: added data source aws_internet_gateway
- Loading branch information
Showing
5 changed files
with
267 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" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsInternetGateway() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsInternetGatewayRead, | ||
Schema: map[string]*schema.Schema{ | ||
"internet_gateway_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"filter": ec2CustomFiltersSchema(), | ||
"tags": tagsSchemaComputed(), | ||
"attachments": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"state": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"vpc_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
req := &ec2.DescribeInternetGatewaysInput{} | ||
internetGatewayId, internetGatewayIdOk := d.GetOk("internet_gateway_id") | ||
tags, tagsOk := d.GetOk("tags") | ||
filter, filterOk := d.GetOk("filter") | ||
|
||
if !internetGatewayIdOk && !filterOk && !tagsOk { | ||
return fmt.Errorf("One of internet_gateway_id or filter or tags must be assigned") | ||
} | ||
|
||
req.Filters = buildEC2AttributeFilterList(map[string]string{ | ||
"internet-gateway-id": internetGatewayId.(string), | ||
}) | ||
req.Filters = append(req.Filters, buildEC2TagFilterList( | ||
tagsFromMap(tags.(map[string]interface{})), | ||
)...) | ||
req.Filters = append(req.Filters, buildEC2CustomFilterList( | ||
filter.(*schema.Set), | ||
)...) | ||
log.Printf("[DEBUG] Describe Internet Gateways %v\n", req) | ||
|
||
resp, err := conn.DescribeInternetGateways(req) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
if resp == nil || len(resp.InternetGateways) == 0 { | ||
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.") | ||
} | ||
if len(resp.InternetGateways) > 1 { | ||
return fmt.Errorf("Multiple Internet Gateways matched; use additional constraints to reduce matches to a single Internet Gateway") | ||
} | ||
|
||
igw := resp.InternetGateways[0] | ||
d.SetId(aws.StringValue(igw.InternetGatewayId)) | ||
d.Set("tags", tagsToMap(igw.Tags)) | ||
d.Set("internet_gateway_id", igw.InternetGatewayId) | ||
if err := d.Set("attachments", dataSourceAttachmentsRead(igw.Attachments)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func dataSourceAttachmentsRead(igwAttachments []*ec2.InternetGatewayAttachment) []map[string]interface{} { | ||
attachments := make([]map[string]interface{}, 0, len(igwAttachments)) | ||
for _, a := range igwAttachments { | ||
m := make(map[string]interface{}) | ||
m["state"] = *a.State | ||
m["vpc_id"] = *a.VpcId | ||
attachments = append(attachments, m) | ||
} | ||
|
||
return attachments | ||
} |
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,106 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccDataSourceAwsInternetGateway_typical(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAwsInternetGatewayConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccDataSourceAwsInternetGatewayCheck("data.aws_internet_gateway.by_id"), | ||
testAccDataSourceAwsInternetGatewayCheck("data.aws_internet_gateway.by_filter"), | ||
testAccDataSourceAwsInternetGatewayCheck("data.aws_internet_gateway.by_tags"), | ||
), | ||
ExpectNonEmptyPlan: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAwsInternetGatewayCheck(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) | ||
} | ||
|
||
igwRs, ok := s.RootModule().Resources["aws_internet_gateway.test"] | ||
if !ok { | ||
return fmt.Errorf("can't find aws_internet_gateway.test in state") | ||
} | ||
vpcRs, ok := s.RootModule().Resources["aws_vpc.test"] | ||
if !ok { | ||
return fmt.Errorf("can't find aws_vpc.test in state") | ||
} | ||
|
||
attr := rs.Primary.Attributes | ||
|
||
if attr["internet_gateway_id"] != igwRs.Primary.Attributes["id"] { | ||
return fmt.Errorf( | ||
"internet_gateway_id is %s; want %s", | ||
attr["internet_gateway_id"], | ||
igwRs.Primary.Attributes["id"], | ||
) | ||
} | ||
|
||
if attr["attachments.0.vpc_id"] != vpcRs.Primary.Attributes["id"] { | ||
return fmt.Errorf( | ||
"vpc_id is %s; want %s", | ||
attr["attachments.0.vpc_id"], | ||
vpcRs.Primary.Attributes["id"], | ||
) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
const testAccDataSourceAwsInternetGatewayConfig = ` | ||
provider "aws" { | ||
region = "eu-central-1" | ||
} | ||
resource "aws_vpc" "test" { | ||
cidr_block = "172.16.0.0/16" | ||
tags { | ||
Name = "terraform-testacc-data-source-igw-vpc" | ||
} | ||
} | ||
resource "aws_internet_gateway" "test" { | ||
vpc_id = "${aws_vpc.test.id}" | ||
tags { | ||
Name = "terraform-testacc-data-source-igw" | ||
} | ||
} | ||
data "aws_internet_gateway" "by_id" { | ||
internet_gateway_id = "${aws_internet_gateway.test.id}" | ||
} | ||
data "aws_internet_gateway" "by_tags" { | ||
tags { | ||
Name = "${aws_internet_gateway.test.tags["Name"]}" | ||
} | ||
} | ||
data "aws_internet_gateway" "by_filter" { | ||
filter { | ||
name = "attachment.vpc-id" | ||
values = ["${aws_vpc.test.id}"] | ||
} | ||
depends_on = ["aws_internet_gateway.test"] | ||
} | ||
` |
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,59 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_internet_gateway" | ||
sidebar_current: "docs-aws-datasource-internet_gateway" | ||
description: |- | ||
Provides details about a specific Internet Gateway | ||
--- | ||
|
||
# aws\_internet\_gateway | ||
|
||
`aws_internet_gateway` provides details about a specific Internet Gateway. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "vpc_id" {} | ||
data "aws_internet_gateway" "default" { | ||
filter { | ||
name = "attachment.vpc-id" | ||
values = ["${var.vpc_id}"] | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The arguments of this data source act as filters for querying the available | ||
Internet Gateway in the current region. The given filters must match exactly one | ||
Internet Gateway whose data will be exported as attributes. | ||
|
||
* `internet_gateway_id` - (Optional) The id of the specific Internet Gateway to retrieve. | ||
|
||
* `tags` - (Optional) A mapping of tags, each pair of which must exactly match | ||
a pair on the desired Internet Gateway. | ||
|
||
* `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_DescribeInternetGateways.html). | ||
|
||
* `values` - (Required) Set of values that are accepted for the given field. | ||
An Internet Gateway will be selected if any one of the given values matches. | ||
|
||
## Attributes Reference | ||
|
||
All of the argument attributes except `filter` block are also exported as | ||
result attributes. This data source will complete the data by populating | ||
any fields that are not included in the configuration with the data for | ||
the selected Internet Gateway. | ||
|
||
`attachments` are also exported with the following attributes, when there are relevants: | ||
Each attachement supports the following: | ||
|
||
* `state` - The current state of the attachment between the gateway and the VPC. Present only if a VPC is attached | ||
* `vpc_id` - The ID of an attached VPC. |