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

New Datasource: aws_api_gateway_vpc_link #6763

Merged
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
62 changes: 62 additions & 0 deletions aws/data_source_aws_api_gateway_vpc_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsApiGatewayVpcLink() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsApiGatewayVpcLinkRead,

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},

"name": {
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourceAwsApiGatewayVpcLinkRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
params := &apigateway.GetVpcLinksInput{}

target := d.Get("name")
var matchedVpcLinks []*apigateway.UpdateVpcLinkOutput
log.Printf("[DEBUG] Reading API Gateway VPC links: %s", params)
err := conn.GetVpcLinksPages(params, func(page *apigateway.GetVpcLinksOutput, lastPage bool) bool {
for _, api := range page.Items {
if aws.StringValue(api.Name) == target {
matchedVpcLinks = append(matchedVpcLinks, api)
}
}
return !lastPage
})
if err != nil {
return fmt.Errorf("error describing API Gateway VPC links: %s", err)
}

if len(matchedVpcLinks) == 0 {
return fmt.Errorf("no API Gateway VPC link with name %q found in this region", target)
}
if len(matchedVpcLinks) > 1 {
return fmt.Errorf("multiple API Gateway VPC links with name %q found in this region", target)
}

match := matchedVpcLinks[0]

d.SetId(*match.Id)
d.Set("name", match.Name)

return nil
}
100 changes: 100 additions & 0 deletions aws/data_source_aws_api_gateway_vpc_link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAwsApiGatewayVpcLink(t *testing.T) {
rName := acctest.RandString(8)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsApiGatewayVpcLinkConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.aws_api_gateway_vpc_link.vpc_link", "name", "aws_api_gateway_vpc_link.vpc_link", "name"),
resource.TestCheckResourceAttrPair("data.aws_api_gateway_vpc_link.vpc_link", "id", "aws_api_gateway_vpc_link.vpc_link", "id"),
),
},
},
})
}

func testAccDataSourceAwsApiGatewayVpcLinkConfig(r string) string {
return fmt.Sprintf(`
resource "aws_vpc" "apigateway_vpclink_test" {
cidr_block = "10.0.0.0/16"

tags = {
Name = "terraform-testacc-lb-apigateway-vpc-link"
}
}

resource "aws_lb" "apigateway_vpclink_test" {
name = "%s"

subnets = [
"${aws_subnet.apigateway_vpclink_test_subnet1.id}"
]

load_balancer_type = "network"
internal = true
idle_timeout = 60
enable_deletion_protection = false
enable_cross_zone_load_balancing = false

tags = {
Name = "testAccDataSourceAwsApiGatewayVpcLinkConfig_networkLoadbalancer"
}
}

resource "aws_lb" "apigateway_vpclink_test2" {
name = "%s-wrong"

subnets = [
"${aws_subnet.apigateway_vpclink_test_subnet1.id}"
]

load_balancer_type = "network"
internal = true
idle_timeout = 60
enable_deletion_protection = false
enable_cross_zone_load_balancing = false

tags = {
Name = "testAccDataSourceAwsApiGatewayVpcLinkConfig_networkLoadbalancer"
}
}

resource "aws_subnet" "apigateway_vpclink_test_subnet1" {
vpc_id = "${aws_vpc.apigateway_vpclink_test.id}"
cidr_block = "10.0.1.0/24"

tags = {
Name = "tf-acc-lb-apigateway-vpclink"
}
}

resource "aws_api_gateway_vpc_link" "vpc_link" {
name = "%s"
target_arns = ["${aws_lb.apigateway_vpclink_test.arn}"]

}

resource "aws_api_gateway_vpc_link" "vpc_link2" {
name = "%s-wrong"
target_arns = ["${aws_lb.apigateway_vpclink_test2.arn}"]

}

data "aws_api_gateway_vpc_link" "vpc_link" {
name = "${aws_api_gateway_vpc_link.vpc_link.name}"
}

`, r, r, r, r)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func Provider() terraform.ResourceProvider {
"aws_api_gateway_api_key": dataSourceAwsApiGatewayApiKey(),
"aws_api_gateway_resource": dataSourceAwsApiGatewayResource(),
"aws_api_gateway_rest_api": dataSourceAwsApiGatewayRestApi(),
"aws_api_gateway_vpc_link": dataSourceAwsApiGatewayVpcLink(),
"aws_arn": dataSourceAwsArn(),
"aws_autoscaling_groups": dataSourceAwsAutoscalingGroups(),
"aws_availability_zone": dataSourceAwsAvailabilityZone(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
<li<%= sidebar_current("docs-aws_api_gateway_rest_api") %>>
<a href="/docs/providers/aws/d/api_gateway_rest_api.html">aws_api_gateway_rest_api</a>
</li>
<li<%= sidebar_current("docs-aws_api_gateway_vpc_link") %>>
<a href="/docs/providers/aws/d/api_gateway_vpc_link.html">aws_api_gateway_vpc_link</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-arn") %>>
<a href="/docs/providers/aws/d/arn.html">aws_arn</a>
</li>
Expand Down
31 changes: 31 additions & 0 deletions website/docs/d/api_gateway_vpc_link.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
layout: "aws"
page_title: "AWS: aws_api_gateway_vpc_link"
sidebar_current: "docs-aws_api_gateway_vpc_link"
description: |-
Get information on a API Gateway VPC Link
---

# Data Source: aws_api_gateway_vpc_link

Use this data source to get the id of a VPC Link in
API Gateway. To fetch the VPC Link you must provide a name to match against.
As there is no unique name constraint on API Gateway VPC Links this data source will
error if there is more than one match.

## Example Usage

```hcl
data "aws_api_gateway_vpc_link" "my_api_gateway_vpc_link" {
name = "my-vpc-link"
}
```

## Argument Reference

* `name` - (Required) The name of the API Gateway VPC Link to look up. If no API Gateway VPC Link is found with this name, an error will be returned.
If multiple API Gateway VPC Links are found with this name, an error will be returned.

## Attributes Reference

* `id` - Set to the ID of the found API Gateway VPC Link.