Skip to content

Commit

Permalink
Merge branch 'dooreelko-body_base_path'
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Jan 12, 2021
2 parents ec65ce8 + bc9018e commit 34bbfe0
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 6 deletions.
32 changes: 27 additions & 5 deletions aws/resource_aws_api_gateway_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func resourceAwsApiGatewayRestApi() *schema.Resource {
Optional: true,
},

"parameters": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"minimum_compression_size": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -177,11 +183,19 @@ func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}

if body, ok := d.GetOk("body"); ok {
log.Printf("[DEBUG] Initializing API Gateway from OpenAPI spec %s", d.Id())
_, err := conn.PutRestApi(&apigateway.PutRestApiInput{

input := &apigateway.PutRestApiInput{
RestApiId: gateway.Id,
Mode: aws.String(apigateway.PutModeOverwrite),
Body: []byte(body.(string)),
})
}

if v, ok := d.GetOk("parameters"); ok && len(v.(map[string]interface{})) > 0 {
input.Parameters = stringMapToPointers(v.(map[string]interface{}))
}

_, err := conn.PutRestApi(input)

if err != nil {
return fmt.Errorf("error creating API Gateway specification: %s", err)
}
Expand Down Expand Up @@ -410,14 +424,22 @@ func resourceAwsApiGatewayRestApiUpdate(d *schema.ResourceData, meta interface{}
}
}

if d.HasChange("body") {
if d.HasChanges("body", "parameters") {
if body, ok := d.GetOk("body"); ok {
log.Printf("[DEBUG] Updating API Gateway from OpenAPI spec: %s", d.Id())
_, err := conn.PutRestApi(&apigateway.PutRestApiInput{

input := &apigateway.PutRestApiInput{
RestApiId: aws.String(d.Id()),
Mode: aws.String(apigateway.PutModeOverwrite),
Body: []byte(body.(string)),
})
}

if v, ok := d.GetOk("parameters"); ok && len(v.(map[string]interface{})) > 0 {
input.Parameters = stringMapToPointers(v.(map[string]interface{}))
}

_, err := conn.PutRestApi(input)

if err != nil {
return fmt.Errorf("error updating API Gateway specification: %s", err)
}
Expand Down
82 changes: 81 additions & 1 deletion aws/resource_aws_api_gateway_rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,40 @@ func TestAccAWSAPIGatewayRestApi_openapi(t *testing.T) {
})
}

func TestAccAWSAPIGatewayRestApi_Parameters(t *testing.T) {
var conf apigateway.RestApi
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_api_gateway_rest_api.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccAPIGatewayTypeEDGEPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayRestAPIDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAPIGatewayRestAPIConfigParameters1(rName, "basepath", "prepend"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayRestAPIExists(resourceName, &conf),
testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/foo", "/foo/bar", "/foo/bar/baz", "/foo/bar/baz/test"}),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"body", "parameters"},
},
{
Config: testAccAWSAPIGatewayRestAPIConfigParameters1(rName, "basepath", "ignore"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayRestAPIExists(resourceName, &conf),
testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/test"}),
),
},
},
})
}

func testAccCheckAWSAPIGatewayRestAPINameAttribute(conf *apigateway.RestApi, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *conf.Name != name {
Expand Down Expand Up @@ -949,7 +983,7 @@ resource "aws_api_gateway_rest_api" "test" {
"info": {
"title": "%s",
"version": "2017-04-20T04:08:08Z"
},
},
"schemes": [
"https"
],
Expand Down Expand Up @@ -1020,3 +1054,49 @@ EOF
}
`, rName, rName)
}

func testAccAWSAPIGatewayRestAPIConfigParameters1(rName string, parameterKey1 string, parameterValue1 string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_rest_api" "test" {
name = %[1]q
body = <<EOF
{
"swagger": "2.0",
"info": {
"title": %[1]q,
"version": "2017-04-20T04:08:08Z"
},
"basePath": "/foo/bar/baz",
"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
parameters = {
%[2]s = %[3]q
}
}
`, rName, parameterKey1, parameterValue1)
}
1 change: 1 addition & 0 deletions website/docs/r/api_gateway_rest_api.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The following arguments are supported:
* `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.
* `minimum_compression_size` - (Optional) Minimum response size to compress for the REST API. Integer between -1 and 10485760 (10MB). Setting a value greater than -1 will enable compression, -1 disables compression (default).
* `body` - (Optional) An OpenAPI specification that defines the set of routes and integrations to create as part of the REST API.
* `parameters` - (Optional) Map of customizations for importing the specification in the `body` argument. For example, to exclude DocumentationParts from an imported API, set `ignore` equal to `documentation`. Additional documentation, including other parameters such as `basepath`, can be found in the [API Gateway Developer Guide](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-import-api.html).
* `policy` - (Optional) JSON formatted policy document that controls access to the API Gateway. For more information about building AWS IAM policy documents with Terraform, see the [AWS IAM Policy Document Guide](https://learn.hashicorp.com/terraform/aws/iam-policy). Terraform will only perform drift detection of its value when present in a configuration. It is recommended to use the [`aws_api_gateway_rest_api_policy` resource](/docs/providers/aws/r/api_gateway_rest_api_policy.html) instead.
* `api_key_source` - (Optional) The source of the API key for requests. Valid values are HEADER (default) and AUTHORIZER.
* `tags` - (Optional) Key-value map of resource tags
Expand Down

0 comments on commit 34bbfe0

Please sign in to comment.