From 0d93e34cadceecdb2d1d892108cbf0eaf0644351 Mon Sep 17 00:00:00 2001 From: Greg Thole Date: Mon, 12 Nov 2018 11:08:35 -0500 Subject: [PATCH 1/2] Add API Gateway API Key data source --- aws/data_source_aws_api_gateway_api_key.go | 45 ++++++++++ ...ata_source_aws_api_gateway_api_key_test.go | 87 +++++++++++++++++++ aws/provider.go | 1 + website/aws.erb | 3 + .../docs/d/api_gateway_api_key.html.markdown | 30 +++++++ 5 files changed, 166 insertions(+) create mode 100644 aws/data_source_aws_api_gateway_api_key.go create mode 100644 aws/data_source_aws_api_gateway_api_key_test.go create mode 100644 website/docs/d/api_gateway_api_key.html.markdown diff --git a/aws/data_source_aws_api_gateway_api_key.go b/aws/data_source_aws_api_gateway_api_key.go new file mode 100644 index 00000000000..e9a7d7a9733 --- /dev/null +++ b/aws/data_source_aws_api_gateway_api_key.go @@ -0,0 +1,45 @@ +package aws + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/apigateway" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsApiGatewayApiKey() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsApiGatewayApiKeyRead, + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Required: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "value": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + }, + } +} + +func dataSourceAwsApiGatewayApiKeyRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigateway + apiKey, err := conn.GetApiKey(&apigateway.GetApiKeyInput{ + ApiKey: aws.String(d.Get("id").(string)), + IncludeValue: aws.Bool(true), + }) + + if err != nil { + return err + } + + d.SetId(aws.StringValue(apiKey.Id)) + d.Set("name", apiKey.Name) + d.Set("value", apiKey.Value) + return nil +} diff --git a/aws/data_source_aws_api_gateway_api_key_test.go b/aws/data_source_aws_api_gateway_api_key_test.go new file mode 100644 index 00000000000..658190535a5 --- /dev/null +++ b/aws/data_source_aws_api_gateway_api_key_test.go @@ -0,0 +1,87 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAwsApiGatewayApiKey(t *testing.T) { + rName := acctest.RandString(8) + resourceName1 := "aws_api_gateway_api_key.example_key" + dataSourceName1 := "data.aws_api_gateway_api_key.test_key" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsApiGatewayApiKeyConfig(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName1, "id"), + resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName1, "name"), + resource.TestCheckResourceAttrPair(resourceName1, "value", dataSourceName1, "value"), + ), + }, + }, + }) +} + +func testAccDataSourceAwsApiGatewayApiKeyConfig(r string) string { + return fmt.Sprintf(` +resource "aws_api_gateway_rest_api" "example" { + name = "example" +} + +resource "aws_api_gateway_resource" "example_v1" { + rest_api_id = "${aws_api_gateway_rest_api.example.id}" + parent_id = "${aws_api_gateway_rest_api.example.root_resource_id}" + path_part = "v1" +} + +resource "aws_api_gateway_method" "example_v1_method" { + rest_api_id = "${aws_api_gateway_rest_api.example.id}" + resource_id = "${aws_api_gateway_resource.example_v1.id}" + http_method = "GET" + authorization = "NONE" +} + +resource "aws_api_gateway_integration" "example_v1_integration" { + rest_api_id = "${aws_api_gateway_rest_api.example.id}" + resource_id = "${aws_api_gateway_resource.example_v1.id}" + http_method = "${aws_api_gateway_method.example_v1_method.http_method}" + type = "MOCK" +} + +resource "aws_api_gateway_deployment" "example_deployment" { + rest_api_id = "${aws_api_gateway_rest_api.example.id}" + stage_name = "example" + depends_on = ["aws_api_gateway_resource.example_v1", "aws_api_gateway_method.example_v1_method", "aws_api_gateway_integration.example_v1_integration"] +} + +resource "aws_api_gateway_api_key" "example_key" { + name = "%s" +} + +resource "aws_api_gateway_usage_plan" "example_plan" { + name = "example" + + api_stages { + api_id = "${aws_api_gateway_rest_api.example.id}" + stage = "${aws_api_gateway_deployment.example_deployment.stage_name}" + } +} + +resource "aws_api_gateway_usage_plan_key" "plan_key" { + key_id = "${aws_api_gateway_api_key.example_key.id}" + key_type = "API_KEY" + usage_plan_id = "${aws_api_gateway_usage_plan.example_plan.id}" +} + +data "aws_api_gateway_api_key" "test_key" { + id = "${aws_api_gateway_api_key.example_key.id}" +} +`, r) +} diff --git a/aws/provider.go b/aws/provider.go index 5745b1f20f2..f89c428713c 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -164,6 +164,7 @@ func Provider() terraform.ResourceProvider { "aws_acmpca_certificate_authority": dataSourceAwsAcmpcaCertificateAuthority(), "aws_ami": dataSourceAwsAmi(), "aws_ami_ids": dataSourceAwsAmiIds(), + "aws_api_gateway_api_key": dataSourceAwsApiGatewayApiKey(), "aws_api_gateway_resource": dataSourceAwsApiGatewayResource(), "aws_api_gateway_rest_api": dataSourceAwsApiGatewayRestApi(), "aws_arn": dataSourceAwsArn(), diff --git a/website/aws.erb b/website/aws.erb index e6826e9f71c..88c1b9bb5af 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -58,6 +58,9 @@ > aws_ami_ids + > + aws_api_gateway_api_key + > aws_api_gateway_resource diff --git a/website/docs/d/api_gateway_api_key.html.markdown b/website/docs/d/api_gateway_api_key.html.markdown new file mode 100644 index 00000000000..254f04b1c96 --- /dev/null +++ b/website/docs/d/api_gateway_api_key.html.markdown @@ -0,0 +1,30 @@ +--- +layout: "aws" +page_title: "AWS: aws_api_gateway_api_key" +sidebar_current: "docs-aws_api_gateway_api_key" +description: |- + Get information on an API Gateway API Key +--- + +# Data Source: aws_api_gateway_api_key + +Use this data source to get the name and value of a pre-existing API Key, for +example to supply credentials for a dependency microservice. + +## Example Usage + +```hcl +data "aws_api_gateway_api_key" "my_api_key" { + id = "my-rest-api-key" +} +``` + +## Argument Reference + + * `id` - (Required) The ID of the API Key to look up. If no API Key is found with this name, an error will be returned. + +## Attributes Reference + + * `id` - Set to the ID of the API Key. + * `name` - Set to the name of the API Key. + * `value` - Set to the value of the API Key. From 520ab35bb68817ae0032bd70b06f6b021bfd57c8 Mon Sep 17 00:00:00 2001 From: Greg Thole Date: Wed, 14 Nov 2018 13:41:05 -0500 Subject: [PATCH 2/2] Review comments addressed --- ...ata_source_aws_api_gateway_api_key_test.go | 45 ------------------- .../docs/d/api_gateway_api_key.html.markdown | 4 +- 2 files changed, 2 insertions(+), 47 deletions(-) diff --git a/aws/data_source_aws_api_gateway_api_key_test.go b/aws/data_source_aws_api_gateway_api_key_test.go index 658190535a5..4bd140c62a7 100644 --- a/aws/data_source_aws_api_gateway_api_key_test.go +++ b/aws/data_source_aws_api_gateway_api_key_test.go @@ -31,55 +31,10 @@ func TestAccDataSourceAwsApiGatewayApiKey(t *testing.T) { func testAccDataSourceAwsApiGatewayApiKeyConfig(r string) string { return fmt.Sprintf(` -resource "aws_api_gateway_rest_api" "example" { - name = "example" -} - -resource "aws_api_gateway_resource" "example_v1" { - rest_api_id = "${aws_api_gateway_rest_api.example.id}" - parent_id = "${aws_api_gateway_rest_api.example.root_resource_id}" - path_part = "v1" -} - -resource "aws_api_gateway_method" "example_v1_method" { - rest_api_id = "${aws_api_gateway_rest_api.example.id}" - resource_id = "${aws_api_gateway_resource.example_v1.id}" - http_method = "GET" - authorization = "NONE" -} - -resource "aws_api_gateway_integration" "example_v1_integration" { - rest_api_id = "${aws_api_gateway_rest_api.example.id}" - resource_id = "${aws_api_gateway_resource.example_v1.id}" - http_method = "${aws_api_gateway_method.example_v1_method.http_method}" - type = "MOCK" -} - -resource "aws_api_gateway_deployment" "example_deployment" { - rest_api_id = "${aws_api_gateway_rest_api.example.id}" - stage_name = "example" - depends_on = ["aws_api_gateway_resource.example_v1", "aws_api_gateway_method.example_v1_method", "aws_api_gateway_integration.example_v1_integration"] -} - resource "aws_api_gateway_api_key" "example_key" { name = "%s" } -resource "aws_api_gateway_usage_plan" "example_plan" { - name = "example" - - api_stages { - api_id = "${aws_api_gateway_rest_api.example.id}" - stage = "${aws_api_gateway_deployment.example_deployment.stage_name}" - } -} - -resource "aws_api_gateway_usage_plan_key" "plan_key" { - key_id = "${aws_api_gateway_api_key.example_key.id}" - key_type = "API_KEY" - usage_plan_id = "${aws_api_gateway_usage_plan.example_plan.id}" -} - data "aws_api_gateway_api_key" "test_key" { id = "${aws_api_gateway_api_key.example_key.id}" } diff --git a/website/docs/d/api_gateway_api_key.html.markdown b/website/docs/d/api_gateway_api_key.html.markdown index 254f04b1c96..ba8e17701ae 100644 --- a/website/docs/d/api_gateway_api_key.html.markdown +++ b/website/docs/d/api_gateway_api_key.html.markdown @@ -15,13 +15,13 @@ example to supply credentials for a dependency microservice. ```hcl data "aws_api_gateway_api_key" "my_api_key" { - id = "my-rest-api-key" + id = "ru3mpjgse6" } ``` ## Argument Reference - * `id` - (Required) The ID of the API Key to look up. If no API Key is found with this name, an error will be returned. + * `id` - (Required) The ID of the API Key to look up. ## Attributes Reference