-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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 support for Api Gateway method request validators. #1064
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/apigateway" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"log" | ||
"time" | ||
) | ||
|
||
func resourceAwsApiGatewayRequestValidator() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsApiGatewayRequestValidatorCreate, | ||
Read: resourceAwsApiGatewayRequestValidatorRead, | ||
Update: resourceAwsApiGatewayRequestValidatorUpdate, | ||
Delete: resourceAwsApiGatewayRequestValidatorDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"rest_api_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"validate_request_body": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
|
||
"validate_request_parameters": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsApiGatewayRequestValidatorCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
|
||
input := apigateway.CreateRequestValidatorInput{ | ||
Name: aws.String(d.Get("name").(string)), | ||
RestApiId: aws.String(d.Get("rest_api_id").(string)), | ||
ValidateRequestBody: aws.Bool(d.Get("validate_request_body").(bool)), | ||
ValidateRequestParameters: aws.Bool(d.Get("validate_request_parameters").(bool)), | ||
} | ||
|
||
out, err := conn.CreateRequestValidator(&input) | ||
if err != nil { | ||
return fmt.Errorf("Error creating Request Validator: %s", err) | ||
} | ||
|
||
d.SetId(*out.Id) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsApiGatewayRequestValidatorRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
|
||
input := apigateway.GetRequestValidatorInput{ | ||
RequestValidatorId: aws.String(d.Id()), | ||
RestApiId: aws.String(d.Get("rest_api_id").(string)), | ||
} | ||
|
||
out, err := conn.GetRequestValidator(&input) | ||
if err != nil { | ||
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == apigateway.ErrCodeNotFoundException { | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
d.Set("name", out.Name) | ||
d.Set("validate_request_body", out.ValidateRequestBody) | ||
d.Set("validate_request_parameters", out.ValidateRequestParameters) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsApiGatewayRequestValidatorUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
log.Printf("[DEBUG] Updating Request Validator %s", d.Id()) | ||
|
||
operations := make([]*apigateway.PatchOperation, 0) | ||
|
||
if d.HasChange("name") { | ||
operations = append(operations, &apigateway.PatchOperation{ | ||
Op: aws.String("replace"), | ||
Path: aws.String("/name"), | ||
Value: aws.String(d.Get("name").(string)), | ||
}) | ||
} | ||
|
||
if d.HasChange("validate_request_body") { | ||
operations = append(operations, &apigateway.PatchOperation{ | ||
Op: aws.String("replace"), | ||
Path: aws.String("/validateRequestBody"), | ||
Value: aws.String(fmt.Sprintf("%t", d.Get("validate_request_body").(bool))), | ||
}) | ||
} | ||
|
||
if d.HasChange("validate_request_parameters") { | ||
operations = append(operations, &apigateway.PatchOperation{ | ||
Op: aws.String("replace"), | ||
Path: aws.String("/validateRequestParameters"), | ||
Value: aws.String(fmt.Sprintf("%t", d.Get("validate_request_parameters").(bool))), | ||
}) | ||
} | ||
|
||
input := apigateway.UpdateRequestValidatorInput{ | ||
RequestValidatorId: aws.String(d.Id()), | ||
RestApiId: aws.String(d.Get("rest_api_id").(string)), | ||
PatchOperations: operations, | ||
} | ||
|
||
_, err := conn.UpdateRequestValidator(&input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Updated Request Validator %s", d.Id()) | ||
|
||
return resourceAwsApiGatewayRequestValidatorRead(d, meta) | ||
} | ||
|
||
func resourceAwsApiGatewayRequestValidatorDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).apigateway | ||
log.Printf("[DEBUG] Deleting Request Validator %s", d.Id()) | ||
|
||
return resource.Retry(5*time.Minute, func() *resource.RetryError { | ||
_, err := conn.DeleteRequestValidator(&apigateway.DeleteRequestValidatorInput{ | ||
RequestValidatorId: aws.String(d.Id()), | ||
RestApiId: aws.String(d.Get("rest_api_id").(string)), | ||
}) | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
awsErr, ok := err.(awserr.Error) | ||
if awsErr.Code() == apigateway.ErrCodeNotFoundException { | ||
return nil | ||
} | ||
|
||
if !ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I adapted the delete function from aws_api_gateway_method. But, it seems to me that this In addition to that, I have realized that deleting a request validator might be more similar to deleting an authorizer (as seen here). That method contains the following comment:
The same thing happens for a request validator. If it is deleted before the method that uses it, AWS returns an error. Has any solution been found to that issue? |
||
return resource.NonRetryableError(err) | ||
} | ||
|
||
return resource.NonRetryableError(err) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to compare the old value here with the new one. If the old one is an ID, and the new one is
""
, then we're deleting a request valuator, and we should issue aremove
update here on themethod
, correct? It doesn't look like we can otherwise remove anything as-isThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked this out and tried manually, it seems we're sending
nil
in the operations below which is effectively doing what we want and removing it, so I guess this is fine 👍