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 aws_api_gateway_authorizer datasource #28148

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .changelog/28148.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_api_gateway_authorizer
```
84 changes: 84 additions & 0 deletions internal/service/apigateway/authorizers_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package apigateway

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
)

// @SDKDataSource("aws_api_gateway_authorizers")
func DataSourceAuthorizers() *schema.Resource {
return &schema.Resource{
Read: dataSourceAuthorizersRead,
Schema: map[string]*schema.Schema{
"rest_api_id": {
Type: schema.TypeString,
Required: true,
},
"items": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"auth_type": {
Type: schema.TypeString,
Computed: true,
},
"authorizer_uri": {
Type: schema.TypeString,
Computed: true,
},
"identity_source": {
Type: schema.TypeString,
Computed: true,
},
"authorizer_result_ttl_in_seconds": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}

func dataSourceAuthorizersRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).APIGatewayConn

restApiId := d.Get("rest_api_id").(string)

authorizers, err := conn.GetAuthorizers(&apigateway.GetAuthorizersInput{
RestApiId: aws.String(restApiId),
})
if err != nil {
return err
}
items := make([]map[string]interface{}, 0, len(authorizers.Items))
for _, item := range authorizers.Items {
s := make(map[string]interface{})
s["name"] = item.Name
s["name"] = item.Name
s["type"] = item.Type
s["auth_type"] = item.AuthType
s["authorizer_uri"] = item.AuthorizerUri
s["identity_source"] = item.IdentitySource
s["authorizer_result_ttl_in_seconds"] = item.AuthorizerResultTtlInSeconds
items = append(items, s)
}
if err := d.Set("items", items); err != nil {
return fmt.Errorf("unable to set authorizer items: %s", err)
}
d.SetId(fmt.Sprintf("%s:authorizer", restApiId))
return nil
}
43 changes: 43 additions & 0 deletions internal/service/apigateway/authorizers_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package apigateway_test

import (
tfapigateway "github.com/hashicorp/terraform-provider-aws/internal/service/apigateway"
"strconv"
"testing"

"github.com/aws/aws-sdk-go/service/apigateway"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccAPIGatewayAuthorizersDataSource(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "data.aws_api_gateway_authorizers.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, apigateway.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccAuthorizerDataSource(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "items.0.identity_source", "method.request.header.Authorization"),
resource.TestCheckResourceAttr(resourceName, "items.0.name", rName),
resource.TestCheckResourceAttr(resourceName, "items.0.type", "TOKEN"),
resource.TestCheckResourceAttr(resourceName, "items.0.authorizer_result_ttl_in_seconds", strconv.Itoa(tfapigateway.DefaultAuthorizerTTL)),
),
},
},
})
}

func testAccAuthorizerDataSource(rName string) string {
return testAccAuthorizerConfig_lambda(rName) + `
data "aws_api_gateway_authorizers" "test" {
rest_api_id = aws_api_gateway_rest_api.test.id
depends_on = [aws_api_gateway_authorizer.test]
}
`
}
4 changes: 4 additions & 0 deletions internal/service/apigateway/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions website/docs/d/api_gateway_authorizers.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
subcategory: "API Gateway"
layout: "aws"
page_title: "AWS: aws_api_gateway_authorizers"
description: |-
Terraform data source for managing an AWS API Gateway authorizer.
---

# Data Source: aws_api_gateway_authorizers

Terraform data source for managing an AWS API Gateway authorizer.

## Example Usage

### Basic Usage

```terraform
data "aws_api_gateway_authorizers" "example" {
rest_api_id = aws_api_gateway_rest_api.test.id
}
```

## Argument Reference

The following arguments are required:

* `rest_api_id` - (Required) REST API id that owns the resource. If no REST API is found, an error will be returned.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

- `items` (List of Object).

### Nested Attributes for `items`

* `name` - Name of the REST API Authorizer to look up. If no REST API Authorizer is found with this name, an error will be returned.
* `authorizer_uri` - Authorizer's Uniform Resource Identifier (URI). This must be a well-formed Lambda function URI in the form of `arn:aws:apigateway:{region}:lambda:path/{service_api}`,
e.g., `arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:012345678912:function:my-function/invocations`
* `identity_source` - Source of the identity in an incoming request. Defaults to `method.request.header.Authorization`. For `REQUEST` type, this may be a comma-separated list of values, including headers, query string parameters and stage variables - e.g., `"method.request.header.SomeHeaderName,method.request.querystring.SomeQueryStringName,stageVariables.SomeStageVariableName"`
* `type` - Type of the authorizer. Possible values are `TOKEN` for a Lambda function using a single authorization token submitted in a custom header, `REQUEST` for a Lambda function using incoming request parameters, or `COGNITO_USER_POOLS` for using an Amazon Cognito user pool. Defaults to `TOKEN`.
* `authorizer_result_ttl_in_seconds` - TTL of cached authorizer results in seconds. Defaults to `300`.