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

resource/aws_api_gateway_method_settings: Prevent unexpected errors during creation and deletion #17234

Merged
merged 2 commits into from
Feb 3, 2021
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
7 changes: 7 additions & 0 deletions .changelog/17234.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_api_gateway_method_settings: Prevent confusing Terraform error on resource disappearance during creation
```

```release-note:bug
resource/aws_api_gateway_method_settings: Ignore non-existent resource errors during deletion
```
48 changes: 31 additions & 17 deletions aws/resource_aws_api_gateway_method_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand Down Expand Up @@ -112,6 +113,10 @@ func resourceAwsApiGatewayMethodSettings() *schema.Resource {
}

func flattenAwsApiGatewayMethodSettings(settings *apigateway.MethodSetting) []interface{} {
if settings == nil {
return nil
}

return []interface{}{
map[string]interface{}{
"metrics_enabled": settings.MetricsEnabled,
Expand All @@ -131,26 +136,28 @@ func flattenAwsApiGatewayMethodSettings(settings *apigateway.MethodSetting) []in
func resourceAwsApiGatewayMethodSettingsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayconn

log.Printf("[DEBUG] Reading API Gateway Method Settings %s", d.Id())
input := apigateway.GetStageInput{
input := &apigateway.GetStageInput{
RestApiId: aws.String(d.Get("rest_api_id").(string)),
StageName: aws.String(d.Get("stage_name").(string)),
}
stage, err := conn.GetStage(&input)

stage, err := conn.GetStage(input)

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, apigateway.ErrCodeNotFoundException) {
log.Printf("[WARN] API Gateway Stage Method Settings (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") {
log.Printf("[WARN] API Gateway Stage (%s) not found, removing method settings", d.Id())
d.SetId("")
return nil
}
return err
return fmt.Errorf("error getting API Gateway Stage Method Settings (%s): %w", d.Id(), err)
}
log.Printf("[DEBUG] Received API Gateway Stage: %s", stage)

methodPath := d.Get("method_path").(string)
settings, ok := stage.MethodSettings[methodPath]
if !ok {
log.Printf("[WARN] API Gateway Method Settings for %q not found, removing", methodPath)

if !d.IsNewResource() && !ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😎 😎

log.Printf("[WARN] API Gateway Stage Method Settings (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
Expand Down Expand Up @@ -265,9 +272,8 @@ func resourceAwsApiGatewayMethodSettingsUpdate(d *schema.ResourceData, meta inte

func resourceAwsApiGatewayMethodSettingsDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayconn
log.Printf("[DEBUG] Deleting API Gateway Method Settings: %s", d.Id())

input := apigateway.UpdateStageInput{
input := &apigateway.UpdateStageInput{
RestApiId: aws.String(d.Get("rest_api_id").(string)),
StageName: aws.String(d.Get("stage_name").(string)),
PatchOperations: []*apigateway.PatchOperation{
Expand All @@ -277,12 +283,20 @@ func resourceAwsApiGatewayMethodSettingsDelete(d *schema.ResourceData, meta inte
},
},
}
log.Printf("[DEBUG] Updating API Gateway Stage: %s", input)

_, err := conn.UpdateStage(&input)
_, err := conn.UpdateStage(input)

if tfawserr.ErrCodeEquals(err, apigateway.ErrCodeNotFoundException) {
return nil
}

// BadRequestException: Cannot remove method setting */* because there is no method setting for this method
if tfawserr.ErrMessageContains(err, apigateway.ErrCodeBadRequestException, "no method setting for this method") {
return nil
}

if err != nil {
return fmt.Errorf("updating API Gateway Stage failed: %w", err)
return fmt.Errorf("error deleting API Gateway Stage Method Settings (%s): %w", d.Id(), err)
}

return nil
Expand Down