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

Import support for API Gateway Authorizer resource #11436

Merged
merged 3 commits into from
Jan 4, 2020
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
42 changes: 29 additions & 13 deletions aws/resource_aws_api_gateway_authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ func resourceAwsApiGatewayAuthorizer() *schema.Resource {
Update: resourceAwsApiGatewayAuthorizerUpdate,
Delete: resourceAwsApiGatewayAuthorizerDelete,
CustomizeDiff: resourceAwsApiGatewayAuthorizerCustomizeDiff,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("Unexpected format of ID (%q), expected REST-API-ID/AUTHORIZER-ID", d.Id())
}
restAPIId := idParts[0]
authorizerId := idParts[1]
d.Set("rest_api_id", restAPIId)
d.SetId(authorizerId)
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"authorizer_uri": {
Expand All @@ -44,7 +57,7 @@ func resourceAwsApiGatewayAuthorizer() *schema.Resource {
"type": {
Type: schema.TypeString,
Optional: true,
Default: "TOKEN",
Default: apigateway.AuthorizerTypeToken,
ValidateFunc: validation.StringInSlice([]string{
apigateway.AuthorizerTypeCognitoUserPools,
apigateway.AuthorizerTypeRequest,
Expand All @@ -68,7 +81,10 @@ func resourceAwsApiGatewayAuthorizer() *schema.Resource {
"provider_arns": {
Type: schema.TypeSet,
Optional: true, // provider_arns is required for authorizer COGNITO_USER_POOLS.
Elem: &schema.Schema{Type: schema.TypeString},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateArn,
},
},
},
}
Expand Down Expand Up @@ -125,7 +141,7 @@ func resourceAwsApiGatewayAuthorizerRead(d *schema.ResourceData, meta interface{

authorizer, err := conn.GetAuthorizer(&input)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == apigateway.ErrCodeNotFoundException {
log.Printf("[WARN] No API Gateway Authorizer found: %s", input)
d.SetId("")
return nil
Expand Down Expand Up @@ -164,49 +180,49 @@ func resourceAwsApiGatewayAuthorizerUpdate(d *schema.ResourceData, meta interfac

if d.HasChange("authorizer_uri") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Op: aws.String(apigateway.OpReplace),
Path: aws.String("/authorizerUri"),
Value: aws.String(d.Get("authorizer_uri").(string)),
})
}
if d.HasChange("identity_source") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Op: aws.String(apigateway.OpReplace),
Path: aws.String("/identitySource"),
Value: aws.String(d.Get("identity_source").(string)),
})
}
if d.HasChange("name") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Op: aws.String(apigateway.OpReplace),
Path: aws.String("/name"),
Value: aws.String(d.Get("name").(string)),
})
}
if d.HasChange("type") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Op: aws.String(apigateway.OpReplace),
Path: aws.String("/type"),
Value: aws.String(d.Get("type").(string)),
})
}
if d.HasChange("authorizer_credentials") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Op: aws.String(apigateway.OpReplace),
Path: aws.String("/authorizerCredentials"),
Value: aws.String(d.Get("authorizer_credentials").(string)),
})
}
if d.HasChange("authorizer_result_ttl_in_seconds") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Op: aws.String(apigateway.OpReplace),
Path: aws.String("/authorizerResultTtlInSeconds"),
Value: aws.String(fmt.Sprintf("%d", d.Get("authorizer_result_ttl_in_seconds").(int))),
})
}
if d.HasChange("identity_validation_expression") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Op: aws.String(apigateway.OpReplace),
Path: aws.String("/identityValidationExpression"),
Value: aws.String(d.Get("identity_validation_expression").(string)),
})
Expand All @@ -219,15 +235,15 @@ func resourceAwsApiGatewayAuthorizerUpdate(d *schema.ResourceData, meta interfac
additionList := ns.Difference(os)
for _, v := range additionList.List() {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("add"),
Op: aws.String(apigateway.OpAdd),
Path: aws.String("/providerARNs"),
Value: aws.String(v.(string)),
})
}
removalList := os.Difference(ns)
for _, v := range removalList.List() {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("remove"),
Op: aws.String(apigateway.OpRemove),
Path: aws.String("/providerARNs"),
Value: aws.String(v.(string)),
})
Expand Down Expand Up @@ -256,7 +272,7 @@ func resourceAwsApiGatewayAuthorizerDelete(d *schema.ResourceData, meta interfac
if err != nil {
// XXX: Figure out a way to delete the method that depends on the authorizer first
// otherwise the authorizer will be dangling until the API is deleted
if !strings.Contains(err.Error(), "ConflictException") {
if !strings.Contains(err.Error(), apigateway.ErrCodeConflictException) {
return fmt.Errorf("Deleting API Gateway Authorizer failed: %s", err)
}
}
Expand Down
Loading