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

Added API Gateway Gateway response resource #1168

Merged
merged 1 commit into from
Jul 18, 2017

Conversation

Ninir
Copy link
Contributor

@Ninir Ninir commented Jul 17, 2017

Description

This adds the API Gateway Gateway response, which allows to configure.
This is a required step to complete the basic-auth protected HTTPs endpoint test to end #861.

Using this resource, we are now able to specify headers & response templates on a https status code basis, so per 401, 403, 500 errors.

Documentation: https://docs.aws.amazon.com/apigateway/api-reference/link-relation/gatewayresponse-update/

Tests

$ make testacc TEST=./aws TESTARGS='-run=TestAccAWSAPIGatewayGatewayResponse_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSAPIGatewayGatewayResponse_ -timeout 120m
=== RUN   TestAccAWSAPIGatewayGatewayResponse_basic
--- PASS: TestAccAWSAPIGatewayGatewayResponse_basic (39.62s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	39.649s

TODOs

  • Add the resource + acceptance tests
  • Handle timeouts in the resource CRUD
  • Add the documentation

@Ninir Ninir force-pushed the f-apigw-gateway-responses branch 3 times, most recently from d2a0d15 to a6e1113 Compare July 18, 2017 07:28
@Ninir Ninir changed the title [WIP] Added API Gateway Gateway response resource Added API Gateway Gateway response resource Jul 18, 2017
@Ninir Ninir requested a review from radeksimko July 18, 2017 07:29
@Ninir Ninir added the new-resource Introduces a new resource. label Jul 18, 2017
@Ninir Ninir force-pushed the f-apigw-gateway-responses branch from a6e1113 to 7daeb71 Compare July 18, 2017 08:27
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

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

This looks overall ok, acceptance tests passing.

I pointed out a few small things. Just ping me when it's ready for another round.


# aws\_api\_gateway\_gateway\_response

Provides an API Gateway Gateway Response for a REST API Gateway.
Copy link
Member

Choose a reason for hiding this comment

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

... Gateway Gateway ... Gateway 😂 🤷‍♂️

RestApiId: aws.String(d.Get("rest_api_id").(string)),
ResponseType: aws.String(d.Get("response_type").(string)),
ResponseTemplates: aws.StringMap(templates),
ResponseParameters: aws.StringMap(parameters),
Copy link
Member

Choose a reason for hiding this comment

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

I think if the two fields (response_templates & response_parameters) are optional and user didn't define those we shouldn't be sending them to the API here.

Copy link
Member

Choose a reason for hiding this comment

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

As discussed via Slack, this doesn't matter and we need it for updates, so 👍

}

d.SetId(fmt.Sprintf("aggr-%s-%s", d.Get("rest_api_id").(string), d.Get("response_type").(string)))
log.Printf("[DEBUG] API Gateway Gateway Response ID: %s", d.Id())
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps a nitpick, but more informative log message here would be for example

[DEBUG] API Gateway Gateway Response created (%q)

I think it's more obvious that the unique string is ID, but less obvious that this ID comes from creation (not update or deletion).

if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
log.Printf("[WARN] API Gateway Gateway Response (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
Copy link
Member

Choose a reason for hiding this comment

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

👌 👍

log.Printf("[DEBUG] Reading API Gateway Gateway Response %s", d.Id())
gatewayResponse, err := conn.GetGatewayResponse(&apigateway.GetGatewayResponseInput{
RestApiId: aws.String(d.Get("rest_api_id").(string)),
ResponseType: aws.String(d.Get("response_type").(string)),
Copy link
Member

Choose a reason for hiding this comment

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

Since the combination of both rest_api_id and response_type is treated as "identifier" for the purpose of the lookup here shouldn't response_type also be ForceNew?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, don't see any counter argument :)

return resource.NonRetryableError(err)
}

if apigatewayErr.Code() == "TooManyRequestsException" {
Copy link
Member

Choose a reason for hiding this comment

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

This code is handled (and retried on) already in the AWS SDK: https://github.com/aws/aws-sdk-go/blob/master/aws/request/retryer.go#L41

If you think we don't retry enough times we should pass that feedback back to AWS SDK and deal with it there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't even know that :O Big win on that, thanks!


if !ok {
return resource.NonRetryableError(err)
}
Copy link
Member

Choose a reason for hiding this comment

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

You can omit these three lines if you add ok && to the condition handling NotFoundException - it will just fall through to the last return. 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch!

),
},

resource.TestStep{
Copy link
Member

Choose a reason for hiding this comment

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

As in any slice/array of structs in Go this "annotation" is optional and we tend to omit it (less characters to read 😃 ) - like you did in the schema map.

https://play.golang.org/p/JcV3cTcxvn

}

if rs.Primary.ID == "" {
return fmt.Errorf("No API Gateway Method ID is set")
Copy link
Member

Choose a reason for hiding this comment

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

👀

_, err := conn.GetGatewayResponse(req)

if err == nil {
return fmt.Errorf("API Gateway Method still exists")
Copy link
Member

Choose a reason for hiding this comment

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

👀

@Ninir Ninir force-pushed the f-apigw-gateway-responses branch from 7daeb71 to c613df6 Compare July 18, 2017 15:36
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

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

I just left you two (last) comments, otherwise this is looking good. Thanks for fixing all the previous things.

conn := meta.(*AWSClient).apigateway

templates := make(map[string]string)
for k, v := range d.Get("response_templates").(map[string]interface{}) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be also wrapped in GetOk() like the other optional field, as it's optional? Otherwise I think we might crash here when casting nil to a map.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Guhuhu, indeed it should have been set...!

RestApiId: aws.String(d.Get("rest_api_id").(string)),
ResponseType: aws.String(d.Get("response_type").(string)),
ResponseTemplates: aws.StringMap(templates),
ResponseParameters: aws.StringMap(parameters),
Copy link
Member

Choose a reason for hiding this comment

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

As discussed via Slack, this doesn't matter and we need it for updates, so 👍


d.Set("response_type", gatewayResponse.ResponseType)
d.Set("status_code", gatewayResponse.StatusCode)
d.Set("response_templates", gatewayResponse.ResponseTemplates)
Copy link
Member

Choose a reason for hiding this comment

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

This comes back as map[string]*string, so shouldn't we also convert it to flat map via aws.StringValueMap like the other field below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch >_<"

@radeksimko radeksimko added the waiting-response Maintainers are waiting on response from community or contributor. label Jul 18, 2017
@Ninir Ninir force-pushed the f-apigw-gateway-responses branch from c613df6 to 86cd267 Compare July 18, 2017 19:32
@radeksimko radeksimko removed the waiting-response Maintainers are waiting on response from community or contributor. label Jul 18, 2017
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

@Ninir Ninir merged commit ee59d89 into hashicorp:master Jul 18, 2017
@Ninir Ninir deleted the f-apigw-gateway-responses branch July 18, 2017 21:29
@ghost
Copy link

ghost commented Apr 11, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
new-resource Introduces a new resource.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants