Skip to content

Commit

Permalink
Merge pull request #6449 from gthole/data-apig-api-key
Browse files Browse the repository at this point in the history
Add API Gateway API Key data source
  • Loading branch information
bflad authored Nov 20, 2018
2 parents 255af2d + 520ab35 commit cca4fbb
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
45 changes: 45 additions & 0 deletions aws/data_source_aws_api_gateway_api_key.go
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 42 additions & 0 deletions aws/data_source_aws_api_gateway_api_key_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
<li<%= sidebar_current("docs-aws-datasource-ami-ids") %>>
<a href="/docs/providers/aws/d/ami_ids.html">aws_ami_ids</a>
</li>
<li<%= sidebar_current("docs-aws_api_gateway_api_key") %>>
<a href="/docs/providers/aws/d/api_gateway_api_key.html">aws_api_gateway_api_key</a>
</li>
<li<%= sidebar_current("docs-aws_api_gateway_resource") %>>
<a href="/docs/providers/aws/d/api_gateway_resource.html">aws_api_gateway_resource</a>
</li>
Expand Down
30 changes: 30 additions & 0 deletions website/docs/d/api_gateway_api_key.html.markdown
Original file line number Diff line number Diff line change
@@ -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 = "ru3mpjgse6"
}
```

## Argument Reference

* `id` - (Required) The ID of the API Key to look up.

## 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.

0 comments on commit cca4fbb

Please sign in to comment.