-
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 a body property to API Gateway RestAPI for Swagger import support #1197
Changes from 1 commit
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 |
---|---|---|
|
@@ -37,6 +37,11 @@ func resourceAwsApiGatewayRestApi() *schema.Resource { | |
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
|
||
"body": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
|
||
"root_resource_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
|
@@ -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{ | ||
RestApiId: gateway.Id, | ||
Mode: aws.String("overwrite"), | ||
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. Can you use the |
||
Body: []byte(body.(string)), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error putting API Gateway specification: %s", err) | ||
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. Could you use 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. 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 err = resourceAwsApiGatewayRestApiRefreshResources(d, meta); err != nil { | ||
return err | ||
} | ||
|
@@ -155,6 +171,19 @@ func resourceAwsApiGatewayRestApiUpdate(d *schema.ResourceData, meta interface{} | |
conn := meta.(*AWSClient).apigateway | ||
log.Printf("[DEBUG] Updating API Gateway %s", d.Id()) | ||
|
||
if d.HasChange("body") { | ||
if body, ok := d.GetOk("body"); ok { | ||
_, err := conn.PutRestApi(&apigateway.PutRestApiInput{ | ||
RestApiId: aws.String(d.Id()), | ||
Mode: aws.String("overwrite"), | ||
Body: []byte(body.(string)), | ||
}) | ||
if err != nil { | ||
return err | ||
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 would go for the same as line 91 here: 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. 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. |
||
} | ||
} | ||
} | ||
|
||
_, err := conn.UpdateRestApi(&apigateway.UpdateRestApiInput{ | ||
RestApiId: aws.String(d.Id()), | ||
PatchOperations: resourceAwsApiGatewayRestApiUpdateOperations(d), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,47 @@ func TestAccAWSAPIGatewayRestApi_basic(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccAWSAPIGatewayRestApi_openapi(t *testing.T) { | ||
var conf apigateway.RestApi | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSAPIGatewayRestAPIDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSAPIGatewayRestAPIConfigOpenAPI, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf), | ||
testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "test"), | ||
testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/test"}), | ||
resource.TestCheckResourceAttr( | ||
"aws_api_gateway_rest_api.test", "name", "test"), | ||
resource.TestCheckResourceAttr( | ||
"aws_api_gateway_rest_api.test", "description", ""), | ||
resource.TestCheckResourceAttrSet( | ||
"aws_api_gateway_rest_api.test", "created_date"), | ||
resource.TestCheckNoResourceAttr( | ||
"aws_api_gateway_rest_api.test", "binary_media_types"), | ||
), | ||
}, | ||
|
||
{ | ||
Config: testAccAWSAPIGatewayRestAPIUpdateConfigOpenAPI, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf), | ||
testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "test"), | ||
testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/update"}), | ||
resource.TestCheckResourceAttr( | ||
"aws_api_gateway_rest_api.test", "name", "test"), | ||
resource.TestCheckResourceAttrSet( | ||
"aws_api_gateway_rest_api.test", "created_date"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSAPIGatewayRestAPINameAttribute(conf *apigateway.RestApi, name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if *conf.Name != name { | ||
|
@@ -76,6 +117,37 @@ func testAccCheckAWSAPIGatewayRestAPIDescriptionAttribute(conf *apigateway.RestA | |
} | ||
} | ||
|
||
func testAccCheckAWSAPIGatewayRestAPIRoutes(conf *apigateway.RestApi, routes []string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).apigateway | ||
|
||
resp, err := conn.GetResources(&apigateway.GetResourcesInput{ | ||
RestApiId: conf.Id, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
actualRoutePaths := map[string]bool{} | ||
for _, resource := range resp.Items { | ||
actualRoutePaths[*resource.Path] = true | ||
} | ||
|
||
for _, route := range routes { | ||
if _, ok := actualRoutePaths[route]; !ok { | ||
return fmt.Errorf("Expected path %v but did not find it in %v", route, actualRoutePaths) | ||
} | ||
delete(actualRoutePaths, route) | ||
} | ||
|
||
if len(actualRoutePaths) > 0 { | ||
return fmt.Errorf("Found unexpected paths %v", actualRoutePaths) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckAWSAPIGatewayRestAPIExists(n string, res *apigateway.RestApi) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
|
@@ -144,3 +216,81 @@ resource "aws_api_gateway_rest_api" "test" { | |
binary_media_types = ["application/octet-stream"] | ||
} | ||
` | ||
|
||
const testAccAWSAPIGatewayRestAPIConfigOpenAPI = ` | ||
resource "aws_api_gateway_rest_api" "test" { | ||
name = "test" | ||
body = <<EOF | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"title": "test", | ||
"version": "2017-04-20T04:08:08Z" | ||
}, | ||
"schemes": [ | ||
"https" | ||
], | ||
"paths": { | ||
"/test": { | ||
"get": { | ||
"responses": { | ||
"200": { | ||
"description": "200 response" | ||
} | ||
}, | ||
"x-amazon-apigateway-integration": { | ||
"type": "HTTP", | ||
"uri": "https://www.google.de", | ||
"httpMethod": "GET", | ||
"responses": { | ||
"default": { | ||
"statusCode": 200 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
EOF | ||
} | ||
` | ||
|
||
const testAccAWSAPIGatewayRestAPIUpdateConfigOpenAPI = ` | ||
resource "aws_api_gateway_rest_api" "test" { | ||
name = "test" | ||
body = <<EOF | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"title": "test", | ||
"version": "2017-04-20T04:08:08Z" | ||
}, | ||
"schemes": [ | ||
"https" | ||
], | ||
"paths": { | ||
"/update": { | ||
"get": { | ||
"responses": { | ||
"200": { | ||
"description": "200 response" | ||
} | ||
}, | ||
"x-amazon-apigateway-integration": { | ||
"type": "HTTP", | ||
"uri": "https://www.google.de", | ||
"httpMethod": "GET", | ||
"responses": { | ||
"default": { | ||
"statusCode": 200 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
EOF | ||
} | ||
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. Can you fix the space indentation here please? 😄 (mostly at the beginning of the JSON structure) |
||
` |
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.
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!