Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API Gateway API Key data source #6449

Merged
merged 2 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.