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

r/api_gateway_authorizer - add arn attribute #23151

Merged
merged 2 commits into from
Feb 13, 2022
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
3 changes: 3 additions & 0 deletions .changelog/23151.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_api_gateway_authorizer: Add `arn` attribute.
```
66 changes: 39 additions & 27 deletions internal/service/apigateway/authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -25,6 +26,7 @@ func ResourceAuthorizer() *schema.Resource {
Update: resourceAuthorizerUpdate,
Delete: resourceAuthorizerDelete,
CustomizeDiff: resourceAuthorizerCustomizeDiff,

Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
Expand All @@ -40,33 +42,9 @@ func ResourceAuthorizer() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"authorizer_uri": {
Type: schema.TypeString,
Optional: true, // authorizer_uri is required for authorizer TOKEN/REQUEST
},
"identity_source": {
"arn": {
Type: schema.TypeString,
Optional: true,
Default: "method.request.header.Authorization",
},
"name": {
Type: schema.TypeString,
Required: true,
},
"rest_api_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
Default: apigateway.AuthorizerTypeToken,
ValidateFunc: validation.StringInSlice([]string{
apigateway.AuthorizerTypeCognitoUserPools,
apigateway.AuthorizerTypeRequest,
apigateway.AuthorizerTypeToken,
}, false),
Computed: true,
},
"authorizer_credentials": {
Type: schema.TypeString,
Expand All @@ -79,10 +57,23 @@ func ResourceAuthorizer() *schema.Resource {
ValidateFunc: validation.IntBetween(0, 3600),
Default: DefaultAuthorizerTTL,
},
"authorizer_uri": {
Type: schema.TypeString,
Optional: true, // authorizer_uri is required for authorizer TOKEN/REQUEST
},
"identity_source": {
Type: schema.TypeString,
Optional: true,
Default: "method.request.header.Authorization",
},
"identity_validation_expression": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"provider_arns": {
Type: schema.TypeSet,
Optional: true, // provider_arns is required for authorizer COGNITO_USER_POOLS.
Expand All @@ -91,6 +82,17 @@ func ResourceAuthorizer() *schema.Resource {
ValidateFunc: verify.ValidARN,
},
},
"rest_api_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
Default: apigateway.AuthorizerTypeToken,
ValidateFunc: validation.StringInSlice(apigateway.AuthorizerType_Values(), false),
},
},
}
}
Expand Down Expand Up @@ -165,9 +167,11 @@ func resourceAuthorizerRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).APIGatewayConn

log.Printf("[INFO] Reading API Gateway Authorizer %s", d.Id())

restApiId := d.Get("rest_api_id").(string)
input := apigateway.GetAuthorizerInput{
AuthorizerId: aws.String(d.Id()),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
RestApiId: aws.String(restApiId),
}

authorizer, err := conn.GetAuthorizer(&input)
Expand Down Expand Up @@ -196,6 +200,14 @@ func resourceAuthorizerRead(d *schema.ResourceData, meta interface{}) error {
d.Set("type", authorizer.Type)
d.Set("provider_arns", flex.FlattenStringSet(authorizer.ProviderARNs))

arn := arn.ARN{
Partition: meta.(*conns.AWSClient).Partition,
Service: "apigateway",
Region: meta.(*conns.AWSClient).Region,
Resource: fmt.Sprintf("/restapis/%s/authorizers/%s", restApiId, d.Id()),
}.String()
d.Set("arn", arn)

return nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/service/apigateway/authorizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAccAPIGatewayAuthorizer_basic(t *testing.T) {
Config: testAccAuthorizerConfig_lambda(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAuthorizerExists(resourceName, &conf),
acctest.MatchResourceAttrRegionalARNNoAccount(resourceName, "arn", "apigateway", regexp.MustCompile(`/restapis/.+/authorizers/.+`)),
resource.TestCheckResourceAttrPair(resourceName, "authorizer_uri", lambdaResourceName, "invoke_arn"),
resource.TestCheckResourceAttr(resourceName, "identity_source", "method.request.header.Authorization"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/api_gateway_authorizer.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ The following arguments are supported:

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

* `arn` - Amazon Resource Name (ARN) of the API Gateway Authorizer
* `id` - The Authorizer identifier.

## Import
Expand Down