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

Add a body property to API Gateway RestAPI for Swagger import support #1197

Merged
merged 3 commits into from
Aug 29, 2017

Conversation

lukehoban
Copy link

@lukehoban lukehoban commented Jul 20, 2017

Description

The body property allows supplying a Swagger/OpenAPI spec to a RestAPI to create the API definition using the routes and integrations defined in the spec.

Related to hashicorp/terraform#11034 and the work done in hashicorp/terraform#6175.

Provides similar behaviour to what's available in CloudFormation with: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html#cfn-apigateway-restapi-body.

Tests

$ make testacc TEST=./aws TESTARGS='-run=TestAccAWSAPIGatewayRestApi_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSAPIGatewayRestApi_ -timeout 120m
=== RUN   TestAccAWSAPIGatewayRestApi_basic
--- PASS: TestAccAWSAPIGatewayRestApi_basic (50.66s)
=== RUN   TestAccAWSAPIGatewayRestApi_openapi
--- PASS: TestAccAWSAPIGatewayRestApi_openapi (28.66s)
PASS
ok  	github.com/pulumi/terraform-provider-aws/aws	79.354s

@radeksimko radeksimko added the enhancement Requests to existing resources that expand the functionality or scope. label Jul 24, 2017
@lukehoban
Copy link
Author

@radeksimko Please let me know if there's any questions on concerns on this that I can help address.

The body property allows supplying an Swagger/OpenAPI spec to a
RestAPI to create the API definition using the routes and integrations
defined in the spec.
@lukehoban
Copy link
Author

Updated tests to create valid REST API definitions and add test results to summary.

cc @radeksimko @Ninir @betabandido @apparentlymart for feedback.

Copy link
Contributor

@Ninir Ninir left a comment

Choose a reason for hiding this comment

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

Hey @lukehoban

This looks good at first sight :)
Just left a few comments to be addressed, more for styling & debugging :)

Thanks!

Body: []byte(body.(string)),
})
if err != nil {
return fmt.Errorf("Error putting API Gateway specification: %s", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you use errwrap.Wrapf("Error creating API Gateway specification: {{err}}", err) there please? (notice the creating word, to make it clear we are putting by creating)

Copy link
Member

Choose a reason for hiding this comment

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

It does no harm, but I think the wrapper here isn't necessary as the only place the error will escalate to is the UI, where all that matters is string-based error message user will read, not the original error type.

if body, ok := d.GetOk("body"); ok {
_, err := conn.PutRestApi(&apigateway.PutRestApiInput{
RestApiId: gateway.Id,
Mode: aws.String("overwrite"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use the apigateway.PutModeOverwrite constant here and line 178?

}
}
EOF
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you fix the space indentation here please? 😄 (mostly at the beginning of the JSON structure)

Body: []byte(body.(string)),
})
if err != nil {
return err
Copy link
Contributor

Choose a reason for hiding this comment

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

I would go for the same as line 91 here: errwrap.Wrapf("Error updating API Gateway specification: {{err}}", err). Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

It does no harm, but I think the wrapper here isn't necessary as the only place the error will escalate to is the UI, where all that matters is string-based error message user will read, not the original error type.

@@ -76,6 +81,17 @@ func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}

d.SetId(*gateway.Id)

if body, ok := d.GetOk("body"); ok {
_, err := conn.PutRestApi(&apigateway.PutRestApiInput{
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you log in the logs that we are setting the specification?
Just for verbosity & debugging :D

Same for the update would be highly appreciated!

@radeksimko
Copy link
Member

radeksimko commented Aug 22, 2017

Thanks @Ninir for the review and @lukehoban for sending the PR - other than what you mentioned we should also document clearly which resources collide with this field/approach, I believe these are:

  • aws_api_gateway_gateway_response
  • aws_api_gateway_integration
  • aws_api_gateway_integration_response
  • aws_api_gateway_method
  • aws_api_gateway_method_response
  • aws_api_gateway_method_settings
  • aws_api_gateway_model
  • aws_api_gateway_resource

i.e. it should be clear to the user that they cannot use both Swagger and these resources together for the same API.

@radeksimko radeksimko added the waiting-response Maintainers are waiting on response from community or contributor. label Aug 22, 2017
@lukehoban
Copy link
Author

Thanks @Ninir and @radeksimko!

Updated PR to address feedback. Please take a look at the doc updates in particular.

@radeksimko radeksimko removed the waiting-response Maintainers are waiting on response from community or contributor. label Aug 29, 2017
@@ -26,6 +26,18 @@ The following arguments are supported:
* `name` - (Required) The name of the REST API
* `description` - (Optional) The description of the REST API
* `binary_media_types` - (Optional) The list of binary media types supported by the RestApi. By default, the RestApi supports only UTF-8-encoded text payloads.
* `body` - (Optional) An OpenAPI specification that defines the set of routes and integrations to create as part of the REST API.

__Note__: If the `body` argument is provided, the OpenAPI specification will be used to configure the resources, methods and integrations for the Rest API. If this argument is provided, the following resources should not be configured manually for the same REST API, as updates may cause manual resource updates to be overwritten:
Copy link
Member

Choose a reason for hiding this comment

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

the following resources should not be configured manually

I'd rather say should not be managed as separate ones as that's more in-line with the language used in the rest of our docs.

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 now LGTM except one nitpick comment, but I'll defer the final decision & push of the merge button to @Ninir as he started the review originally.

@Ninir
Copy link
Contributor

Ninir commented Aug 29, 2017

hey @lukehoban !

Just pushed the documentation update following @radeksimko 's recommandation.
Thanks for all the work here, looks very good! :)

Now merging, cheers!

@Ninir Ninir merged commit 0335c99 into hashicorp:master Aug 29, 2017
@lukehoban lukehoban deleted the restapi_body branch August 29, 2017 16:00
@ghost
Copy link

ghost commented Sep 18, 2017

Hi All - When will this feature be available. I have v0.10.5 on my mac.

resource "aws_api_gateway_rest_api" "MyDemoAPI" {
name = "MyDemoAPI"
description = "This is my API for demonstration purposes"
body = "./api/swagger.yml"
}

when I run terraform plan , I get
1 error(s) occurred:

  • module.apigateway.aws_api_gateway_rest_api.MyDemoAPI: : invalid or unknown key: body

Am I missing some thing here. Please let me know

@bvingerh
Copy link

Same with v0.10.6: body argument not recognized.
That's the fourth release of Terraform without a release of the AWS provider (which is stuck at v0.1.4 of August 8)...

@ghost
Copy link

ghost commented Sep 25, 2017

I built the terraform-provider-aws using the instructions from README. Now when I run terraform plan, the body tag is recognized. But when I apply, I get the following error. I used the same swagger with AWS SDK/CLI and it worked fine. Can you please help

$ terraform apply
module.apigateway.aws_api_gateway_rest_api.MyDemoAPI: Creating...
body: "" => "../api/swagger.json"
created_date: "" => ""
description: "" => "This is my API for demonstration purposes"
name: "" => "MyDemoAPI"
root_resource_id: "" => ""
Error applying plan:

1 error(s) occurred:

  • module.apigateway.aws_api_gateway_rest_api.MyDemoAPI: 1 error(s) occurred:

  • aws_api_gateway_rest_api.MyDemoAPI: Error creating API Gateway specification: BadRequestException: Invalid Swagger 2.0 input.
    status code: 400, request id: 5ec75ec4-a235-11e7-b1f5-7b2e853b338e

@woody3549
Copy link

Hi all,

Thanks for this great work.
Can you please let me know when it's going to be released ?

Thanks

nbaztec pushed a commit to nbaztec/terraform-provider-aws that referenced this pull request Sep 26, 2017
nbaztec pushed a commit to nbaztec/terraform-provider-aws that referenced this pull request Sep 26, 2017
Add a body property to API Gateway RestAPI for Swagger import support
@ebarault
Copy link

ebarault commented Oct 13, 2017

@kirangangineni : after reviewing the pull request itself i realized that the provider does not expect the body param as a path to the file as you are doing, but as the body itself.

Hence i managed having this work with the following:

data "template_file" "swagger_api" {
  template = "${file("${path.module}/swagger/api.yml")}"
}

resource "aws_api_gateway_rest_api" "MyDemoAPI" {
  name = "${format("%s", var.name == "" ? "api" : "api-${var.name}")}"
  description = "This is my API for demonstration purposes"
  body = "${data.template_file.swagger_api.rendered}"
}

This should be added in the docs here : https://www.terraform.io/docs/providers/aws/r/api_gateway_rest_api.html#body

thanks @lukehoban for bringing this feature !

@Ninir
Copy link
Contributor

Ninir commented Oct 26, 2017

Hi folks,

the AWS provider (this repository) is release independently from the core one (https://github.com/hashicorp/terraform).
This means you can use Terraform 0.10.8 with the AWS Provider version 1.0.0, 1.1.0, etc..

the body property was added in the 1.0.0 release: to ensure you get it right, remove the .terraform/plugins directory and run terraform init again: this will get the latest version and you should be all set.

Thus being said, @ebarault is right: the body property expects the body of the file, not the path to the file. As in:

using data template

resource "aws_api_gateway_rest_api" "api" {
  ...
  body = "${data.template_file.swagger_api.rendered}"
}

using a file

resource "aws_api_gateway_rest_api" "api" {
  ...
  body = "${file("myfile.json"}"
}

using inline strings

resource "aws_api_gateway_rest_api" "api" {
  ...
  body = <<YAML
myfile
YAML
}

Tell us how it goes! :)

@Willis0826
Copy link

Willis0826 commented Sep 13, 2018

@Ninir
Thanks for the clearly demonstration : ) But here is a typo in your post :

body = "${file("myfile.json"}"

I believe it should be ${file("...")}

@ghost
Copy link

ghost commented Apr 3, 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 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants