From 408711a43f5b7225bb77faa1180e35138c4818b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Tro=C3=9Fbach?= Date: Fri, 7 Dec 2018 16:58:56 +0100 Subject: [PATCH 1/4] Adding new DataSource for aws_api_gateway_vpc_link --- aws/data_source_aws_api_gateway_vpc_link.go | 62 +++++++++ ...ta_source_aws_api_gateway_vpc_link_test.go | 127 ++++++++++++++++++ aws/provider.go | 1 + website/aws.erb | 3 + .../docs/d/api_gateway_vpc_link.html.markdown | 31 +++++ 5 files changed, 224 insertions(+) create mode 100644 aws/data_source_aws_api_gateway_vpc_link.go create mode 100644 aws/data_source_aws_api_gateway_vpc_link_test.go create mode 100644 website/docs/d/api_gateway_vpc_link.html.markdown diff --git a/aws/data_source_aws_api_gateway_vpc_link.go b/aws/data_source_aws_api_gateway_vpc_link.go new file mode 100644 index 00000000000..7ce38d711f0 --- /dev/null +++ b/aws/data_source_aws_api_gateway_vpc_link.go @@ -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 +} diff --git a/aws/data_source_aws_api_gateway_vpc_link_test.go b/aws/data_source_aws_api_gateway_vpc_link_test.go new file mode 100644 index 00000000000..715491001a4 --- /dev/null +++ b/aws/data_source_aws_api_gateway_vpc_link_test.go @@ -0,0 +1,127 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +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( + testAccDataSourceAwsApiGatewayVpcLinkCheck("data.aws_api_gateway_vpc_link.vpc_link"), + resource.TestCheckResourceAttrSet("data.aws_api_gateway_vpc_link.vpc_link", "id"), + ), + }, + }, + }) +} + +func testAccDataSourceAwsApiGatewayVpcLinkCheck(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + resources, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("root module has no resource called %s", name) + } + + apiGatewayVpcLinkResources, ok := s.RootModule().Resources["aws_api_gateway_vpc_link.vpc_link"] + if !ok { + return fmt.Errorf("can't find aws_api_gateway_vpc_link.vpc_link in state") + } + + attr := resources.Primary.Attributes + if attr["name"] != apiGatewayVpcLinkResources.Primary.Attributes["name"] { + return fmt.Errorf( + "name is %s; want %s", + attr["name"], + apiGatewayVpcLinkResources.Primary.Attributes["name"], + ) + } + + return nil + } +} + +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" + availability_zone = "us-west-2a" + + 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) +} diff --git a/aws/provider.go b/aws/provider.go index 9e18bbd00e1..0e1d244119d 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), diff --git a/website/aws.erb b/website/aws.erb index 62c01613440..ca221a24a6d 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -67,6 +67,9 @@ > aws_api_gateway_rest_api + > + aws_api_gateway_vpc_link + > aws_arn diff --git a/website/docs/d/api_gateway_vpc_link.html.markdown b/website/docs/d/api_gateway_vpc_link.html.markdown new file mode 100644 index 00000000000..2413c9a728f --- /dev/null +++ b/website/docs/d/api_gateway_vpc_link.html.markdown @@ -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 and target arns 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. From 1061029b011384ff62512d0e7a4908ba6a5be82b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Tro=C3=9Fbach?= Date: Fri, 7 Dec 2018 17:16:44 +0100 Subject: [PATCH 2/4] Remove wrong info from documentation --- website/docs/d/api_gateway_vpc_link.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/d/api_gateway_vpc_link.html.markdown b/website/docs/d/api_gateway_vpc_link.html.markdown index 2413c9a728f..b1e15c653bb 100644 --- a/website/docs/d/api_gateway_vpc_link.html.markdown +++ b/website/docs/d/api_gateway_vpc_link.html.markdown @@ -8,7 +8,7 @@ description: |- # Data Source: aws_api_gateway_vpc_link -Use this data source to get the id and target arns of a VPC Link in +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. From b20d759b08575e4485def1daa9ec54b80c40d642 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Fri, 7 Dec 2018 20:28:38 +0100 Subject: [PATCH 3/4] Update aws/data_source_aws_api_gateway_vpc_link_test.go Co-Authored-By: ftrossbach --- aws/data_source_aws_api_gateway_vpc_link_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/data_source_aws_api_gateway_vpc_link_test.go b/aws/data_source_aws_api_gateway_vpc_link_test.go index 715491001a4..15b64cb60ca 100644 --- a/aws/data_source_aws_api_gateway_vpc_link_test.go +++ b/aws/data_source_aws_api_gateway_vpc_link_test.go @@ -19,7 +19,7 @@ func TestAccDataSourceAwsApiGatewayVpcLink(t *testing.T) { Config: testAccDataSourceAwsApiGatewayVpcLinkConfig(rName), Check: resource.ComposeTestCheckFunc( testAccDataSourceAwsApiGatewayVpcLinkCheck("data.aws_api_gateway_vpc_link.vpc_link"), - resource.TestCheckResourceAttrSet("data.aws_api_gateway_vpc_link.vpc_link", "id"), + resource.TestCheckResourceAttrSet("data.aws_api_gateway_vpc_link.vpc_link", "id", "aws_api_gateway_vpc_link.vpc_link", "id"), ), }, }, From 8b7d7093421fa59013dbb66c402e72d3eabe4ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Tro=C3=9Fbach?= Date: Fri, 7 Dec 2018 20:46:40 +0100 Subject: [PATCH 4/4] Incorporate review suggestions --- ...ta_source_aws_api_gateway_vpc_link_test.go | 33 ++----------------- 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/aws/data_source_aws_api_gateway_vpc_link_test.go b/aws/data_source_aws_api_gateway_vpc_link_test.go index 15b64cb60ca..bbded972f19 100644 --- a/aws/data_source_aws_api_gateway_vpc_link_test.go +++ b/aws/data_source_aws_api_gateway_vpc_link_test.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" - "github.com/hashicorp/terraform/terraform" ) func TestAccDataSourceAwsApiGatewayVpcLink(t *testing.T) { @@ -18,39 +17,14 @@ func TestAccDataSourceAwsApiGatewayVpcLink(t *testing.T) { { Config: testAccDataSourceAwsApiGatewayVpcLinkConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccDataSourceAwsApiGatewayVpcLinkCheck("data.aws_api_gateway_vpc_link.vpc_link"), - resource.TestCheckResourceAttrSet("data.aws_api_gateway_vpc_link.vpc_link", "id", "aws_api_gateway_vpc_link.vpc_link", "id"), + 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 testAccDataSourceAwsApiGatewayVpcLinkCheck(name string) resource.TestCheckFunc { - return func(s *terraform.State) error { - resources, ok := s.RootModule().Resources[name] - if !ok { - return fmt.Errorf("root module has no resource called %s", name) - } - - apiGatewayVpcLinkResources, ok := s.RootModule().Resources["aws_api_gateway_vpc_link.vpc_link"] - if !ok { - return fmt.Errorf("can't find aws_api_gateway_vpc_link.vpc_link in state") - } - - attr := resources.Primary.Attributes - if attr["name"] != apiGatewayVpcLinkResources.Primary.Attributes["name"] { - return fmt.Errorf( - "name is %s; want %s", - attr["name"], - apiGatewayVpcLinkResources.Primary.Attributes["name"], - ) - } - - return nil - } -} - func testAccDataSourceAwsApiGatewayVpcLinkConfig(r string) string { return fmt.Sprintf(` resource "aws_vpc" "apigateway_vpclink_test" { @@ -100,8 +74,7 @@ func testAccDataSourceAwsApiGatewayVpcLinkConfig(r string) string { resource "aws_subnet" "apigateway_vpclink_test_subnet1" { vpc_id = "${aws_vpc.apigateway_vpclink_test.id}" cidr_block = "10.0.1.0/24" - availability_zone = "us-west-2a" - + tags = { Name = "tf-acc-lb-apigateway-vpclink" }