From 25d17774d9f46c800af52e67c0ee5837062405fd Mon Sep 17 00:00:00 2001 From: Tim Borrowdale Date: Wed, 11 Apr 2018 11:22:05 +0100 Subject: [PATCH 1/3] add new data source aws_api_gateway_rest_api --- aws/data_source_aws_api_gateway_rest_api.go | 88 +++++++++++++++++++ ...ta_source_aws_api_gateway_rest_api_test.go | 80 +++++++++++++++++ aws/provider.go | 1 + website/aws.erb | 3 + .../docs/d/api_gateway_rest_api.html.markdown | 32 +++++++ 5 files changed, 204 insertions(+) create mode 100644 aws/data_source_aws_api_gateway_rest_api.go create mode 100644 aws/data_source_aws_api_gateway_rest_api_test.go create mode 100644 website/docs/d/api_gateway_rest_api.html.markdown diff --git a/aws/data_source_aws_api_gateway_rest_api.go b/aws/data_source_aws_api_gateway_rest_api.go new file mode 100644 index 00000000000..9316358880a --- /dev/null +++ b/aws/data_source_aws_api_gateway_rest_api.go @@ -0,0 +1,88 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/apigateway" + "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsApiGatewayRestApi() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsApiGatewayRestApiRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "id": { + Type: schema.TypeString, + Computed: true, + }, + "root_resource_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigateway + params := &apigateway.GetRestApisInput{} + + target := d.Get("name") + var matchedApis []*apigateway.RestApi + log.Printf("[DEBUG] Reading API Gateway REST APIs: %s", params) + err := conn.GetRestApisPages(params, func(page *apigateway.GetRestApisOutput, lastPage bool) bool { + for _, api := range page.Items { + if *api.Name == target { + matchedApis = append(matchedApis, api) + } + } + return true + }) + if err != nil { + return errwrap.Wrapf("error describing API Gateway REST APIs: {{err}}", err) + } + + if len(matchedApis) == 0 { + return fmt.Errorf("no REST APIs with name %q found in this region", target) + } + if len(matchedApis) > 1 { + return fmt.Errorf("multiple REST APIs with name %q found in this region", target) + } + + match := matchedApis[0] + + d.SetId(*match.Id) + + if err = dataSourceAwsApiGatewayRestApiRefreshResources(d, meta); err != nil { + return err + } + + return nil +} + +func dataSourceAwsApiGatewayRestApiRefreshResources(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigateway + + resp, err := conn.GetResources(&apigateway.GetResourcesInput{ + RestApiId: aws.String(d.Id()), + }) + if err != nil { + return err + } + + for _, item := range resp.Items { + if *item.Path == "/" { + d.Set("root_resource_id", item.Id) + break + } + } + + return nil +} diff --git a/aws/data_source_aws_api_gateway_rest_api_test.go b/aws/data_source_aws_api_gateway_rest_api_test.go new file mode 100644 index 00000000000..7b07aa8ac2c --- /dev/null +++ b/aws/data_source_aws_api_gateway_rest_api_test.go @@ -0,0 +1,80 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceAwsApiGatewayRestApi(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDataSourceAwsApiGatewayRestApiConfig, + Check: resource.ComposeTestCheckFunc( + testAccDataSourceAwsApiGatewayRestApiCheck("data.aws_api_gateway_rest_api.by_name"), + ), + }, + }, + }) +} + +func testAccDataSourceAwsApiGatewayRestApiCheck(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) + } + + apiGatewayRestApiResources, ok := s.RootModule().Resources["aws_api_gateway_rest_api.tf_test"] + if !ok { + return fmt.Errorf("can't find aws_api_gateway_rest_api.tf_test in state") + } + + attr := resources.Primary.Attributes + + if attr["name"] != apiGatewayRestApiResources.Primary.Attributes["name"] { + return fmt.Errorf( + "name is %s; want %s", + attr["name"], + apiGatewayRestApiResources.Primary.Attributes["name"], + ) + } + + if attr["root_resource_id"] != apiGatewayRestApiResources.Primary.Attributes["root_resource_id"] { + return fmt.Errorf( + "root_resource_id is %s; want %s", + attr["root_resource_id"], + apiGatewayRestApiResources.Primary.Attributes["root_resource_id"], + ) + } + + return nil + } +} + +const testAccDataSourceAwsApiGatewayRestApiConfig = ` +provider "aws" { + region = "us-west-2" +} + +resource "aws_api_gateway_rest_api" "tf_wrong1" { + name = "wrong1" +} + +resource "aws_api_gateway_rest_api" "tf_test" { + name = "tf_test" +} + +resource "aws_api_gateway_rest_api" "tf_wrong2" { + name = "wrong2" +} + +data "aws_api_gateway_rest_api" "by_name" { + name = "${aws_api_gateway_rest_api.tf_test.name}" +} +` diff --git a/aws/provider.go b/aws/provider.go index 682ed272fdb..2d48e5ee6a2 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -163,6 +163,7 @@ func Provider() terraform.ResourceProvider { "aws_acm_certificate": dataSourceAwsAcmCertificate(), "aws_ami": dataSourceAwsAmi(), "aws_ami_ids": dataSourceAwsAmiIds(), + "aws_api_gateway_rest_api": dataSourceAwsApiGatewayRestApi(), "aws_autoscaling_groups": dataSourceAwsAutoscalingGroups(), "aws_availability_zone": dataSourceAwsAvailabilityZone(), "aws_availability_zones": dataSourceAwsAvailabilityZones(), diff --git a/website/aws.erb b/website/aws.erb index 10ba486f806..d122c44fa08 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -43,6 +43,9 @@ > aws_ami_ids + > + aws_api_gateway_rest_api + > aws_autoscaling_groups diff --git a/website/docs/d/api_gateway_rest_api.html.markdown b/website/docs/d/api_gateway_rest_api.html.markdown new file mode 100644 index 00000000000..630153aba8c --- /dev/null +++ b/website/docs/d/api_gateway_rest_api.html.markdown @@ -0,0 +1,32 @@ +--- +layout: "aws" +page_title: "AWS: aws_api_gateway_rest_api" +sidebar_current: "docs-aws_api_gateway_rest_api" +description: |- + Get information on a API Gateway REST API +--- + +# Data Source: aws_api_gateway_rest_api + +Use this data source to get the id and root_resource_id of a REST API in +API Gateway. To fetch the REST API you must provide a name to match against. +As there is no unique name constraint on REST APIs this data source will +error if there is more than one match. + +## Example Usage + +```hcl +data "aws_api_gateway_rest_api" "my_rest_api" { + name = "my-rest-api" +} +``` + +## Argument Reference + + * `name` - (Required) The name of the REST API to look up. If no REST API is found with this name, an error will be returned. + If multiple REST APIs are found with this name, an error will be returned. + +## Attributes Reference + + * `id` - Set to the ID of the found REST API. + * `root_resource_id` - Set to the ID of the API Gateway Resource on the found REST API where the route matches '/'. From 9d5bd75d204b09f7edb092f152da19e66ebd1078 Mon Sep 17 00:00:00 2001 From: Tim Borrowdale Date: Thu, 12 Apr 2018 12:11:04 +0100 Subject: [PATCH 2/3] refactor d/aws_api_gateway_rest_api and it's test after code review --- aws/data_source_aws_api_gateway_rest_api.go | 21 +++--------------- ...ta_source_aws_api_gateway_rest_api_test.go | 22 +++++++++---------- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/aws/data_source_aws_api_gateway_rest_api.go b/aws/data_source_aws_api_gateway_rest_api.go index 9316358880a..cc71d87d2c6 100644 --- a/aws/data_source_aws_api_gateway_rest_api.go +++ b/aws/data_source_aws_api_gateway_rest_api.go @@ -6,7 +6,6 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/apigateway" - "github.com/hashicorp/errwrap" "github.com/hashicorp/terraform/helper/schema" ) @@ -18,10 +17,6 @@ func dataSourceAwsApiGatewayRestApi() *schema.Resource { Type: schema.TypeString, Required: true, }, - "id": { - Type: schema.TypeString, - Computed: true, - }, "root_resource_id": { Type: schema.TypeString, Computed: true, @@ -39,14 +34,14 @@ func dataSourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{} log.Printf("[DEBUG] Reading API Gateway REST APIs: %s", params) err := conn.GetRestApisPages(params, func(page *apigateway.GetRestApisOutput, lastPage bool) bool { for _, api := range page.Items { - if *api.Name == target { + if aws.StringValue(api.Name) == target { matchedApis = append(matchedApis, api) } } - return true + return !lastPage }) if err != nil { - return errwrap.Wrapf("error describing API Gateway REST APIs: {{err}}", err) + return fmt.Errorf("error describing API Gateway REST APIs: %s", err) } if len(matchedApis) == 0 { @@ -60,16 +55,6 @@ func dataSourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{} d.SetId(*match.Id) - if err = dataSourceAwsApiGatewayRestApiRefreshResources(d, meta); err != nil { - return err - } - - return nil -} - -func dataSourceAwsApiGatewayRestApiRefreshResources(d *schema.ResourceData, meta interface{}) error { - conn := meta.(*AWSClient).apigateway - resp, err := conn.GetResources(&apigateway.GetResourcesInput{ RestApiId: aws.String(d.Id()), }) diff --git a/aws/data_source_aws_api_gateway_rest_api_test.go b/aws/data_source_aws_api_gateway_rest_api_test.go index 7b07aa8ac2c..9a09610373d 100644 --- a/aws/data_source_aws_api_gateway_rest_api_test.go +++ b/aws/data_source_aws_api_gateway_rest_api_test.go @@ -4,17 +4,19 @@ import ( "fmt" "testing" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccDataSourceAwsApiGatewayRestApi(t *testing.T) { + rName := acctest.RandString(8) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccDataSourceAwsApiGatewayRestApiConfig, + Config: testAccDataSourceAwsApiGatewayRestApiConfig(rName), Check: resource.ComposeTestCheckFunc( testAccDataSourceAwsApiGatewayRestApiCheck("data.aws_api_gateway_rest_api.by_name"), ), @@ -57,24 +59,22 @@ func testAccDataSourceAwsApiGatewayRestApiCheck(name string) resource.TestCheckF } } -const testAccDataSourceAwsApiGatewayRestApiConfig = ` -provider "aws" { - region = "us-west-2" -} - +func testAccDataSourceAwsApiGatewayRestApiConfig(r string) string { + return fmt.Sprintf(` resource "aws_api_gateway_rest_api" "tf_wrong1" { - name = "wrong1" +name = "%s_wrong1" } resource "aws_api_gateway_rest_api" "tf_test" { - name = "tf_test" +name = "%s_correct" } resource "aws_api_gateway_rest_api" "tf_wrong2" { - name = "wrong2" +name = "%s_wrong1" } data "aws_api_gateway_rest_api" "by_name" { - name = "${aws_api_gateway_rest_api.tf_test.name}" +name = "${aws_api_gateway_rest_api.tf_test.name}" +} +`, r) } -` From 9dcff38f20b4b144c6854dfa10a3046ad002e90c Mon Sep 17 00:00:00 2001 From: Tim Borrowdale Date: Thu, 12 Apr 2018 12:21:53 +0100 Subject: [PATCH 3/3] add missing Sprintf arguments to d/aws_api_gateway_rest_api test --- aws/data_source_aws_api_gateway_rest_api_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/data_source_aws_api_gateway_rest_api_test.go b/aws/data_source_aws_api_gateway_rest_api_test.go index 9a09610373d..0ca45af601c 100644 --- a/aws/data_source_aws_api_gateway_rest_api_test.go +++ b/aws/data_source_aws_api_gateway_rest_api_test.go @@ -76,5 +76,5 @@ name = "%s_wrong1" data "aws_api_gateway_rest_api" "by_name" { name = "${aws_api_gateway_rest_api.tf_test.name}" } -`, r) +`, r, r, r) }