-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Merged
catsby
merged 3 commits into
hashicorp:master
from
betabandido:feature/add-method-request-validator
Jul 7, 2017
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
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/schema" | ||
"log" | ||
"strings" | ||
) | ||
|
||
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()) | ||
|
||
_, err := conn.DeleteRequestValidator(&apigateway.DeleteRequestValidatorInput{ | ||
RequestValidatorId: aws.String(d.Id()), | ||
RestApiId: aws.String(d.Get("rest_api_id").(string)), | ||
}) | ||
if err != nil { | ||
// XXX: Figure out a way to delete the method that depends on the request validator first | ||
// otherwise the validator will be dangling until the API is deleted | ||
if !strings.Contains(err.Error(), apigateway.ErrCodeConflictException) { | ||
return fmt.Errorf("Deleting Request Validator failed: %s", err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 👍