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..4bd140c62a7 --- /dev/null +++ b/aws/data_source_aws_api_gateway_api_key_test.go @@ -0,0 +1,42 @@ +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_api_key" "example_key" { + name = "%s" +} + +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 @@