Skip to content

Commit

Permalink
resource/aws_api_gateway_rest_api: Fix disable_execute_api_endpoint a…
Browse files Browse the repository at this point in the history
…nd endpoint_configuration vpc_endpoint_ids handling with OpenAPI specification import (body argument) (#17209)

Reference: #13841

Before code updates, these new acceptance tests show how the Terraform configuration value would not be applied if an OpenAPI specification was imported:

```
=== CONT  TestAccAWSAPIGatewayRestApi_DisableExecuteApiEndpoint_OverrideBody
    resource_aws_api_gateway_rest_api_test.go:847: Step 1/4 error: Check failed: 1 error occurred:
        	* Check 2/2 error: aws_api_gateway_rest_api.test: Attribute 'disable_execute_api_endpoint' expected "false", got "true"

--- FAIL: TestAccAWSAPIGatewayRestApi_DisableExecuteApiEndpoint_OverrideBody (10.30s)

=== CONT  TestAccAWSAPIGatewayRestApi_EndpointConfiguration_VpcEndpointIds_OverrideBody
    resource_aws_api_gateway_rest_api_test.go:369: Step 1/2 error: After applying this test step and performing a `terraform refresh`, the plan was not empty.
        stdout

        An execution plan has been generated and is shown below.
        Resource actions are indicated with the following symbols:
          ~ update in-place

        Terraform will perform the following actions:

          # aws_api_gateway_rest_api.test will be updated in-place
          ~ resource "aws_api_gateway_rest_api" "test" {
                id                           = "m9ajz6izjl"
                name                         = "tf-acc-test-6139822644948363723"
                tags                         = {}
                # (9 unchanged attributes hidden)

              ~ endpoint_configuration {
                  ~ vpc_endpoint_ids = [
                      + "vpce-0ba0b61be45886a6f",
                      - "vpce-0ee1a2ccd6af8f011",
                    ]
                    # (1 unchanged attribute hidden)
                }
            }

        Plan: 0 to add, 1 to change, 0 to destroy.
--- FAIL: TestAccAWSAPIGatewayRestApi_EndpointConfiguration_VpcEndpointIds_OverrideBody (190.95s)
```

Before code updates, these new acceptance tests show how the Terraform resource would report an unexpected difference for missing configurations that were imported by the OpenAPI specification:

```
=== CONT  TestAccAWSAPIGatewayRestApi_DisableExecuteApiEndpoint_SetByBody
    resource_aws_api_gateway_rest_api_test.go:890: Step 1/2 error: After applying this test step, the plan was not empty.
        stdout:

        An execution plan has been generated and is shown below.
        Resource actions are indicated with the following symbols:
          ~ update in-place

        Terraform will perform the following actions:

          # aws_api_gateway_rest_api.test will be updated in-place
          ~ resource "aws_api_gateway_rest_api" "test" {
              ~ disable_execute_api_endpoint = true -> false
                id                           = "c2t6iky152"
                name                         = "tf-acc-test-8877194198775672292"
                # (8 unchanged attributes hidden)

                # (1 unchanged block hidden)
            }

        Plan: 0 to add, 1 to change, 0 to destroy.
--- FAIL: TestAccAWSAPIGatewayRestApi_DisableExecuteApiEndpoint_SetByBody (11.03s)

=== CONT  TestAccAWSAPIGatewayRestApi_EndpointConfiguration_VpcEndpointIds_SetByBody
    resource_aws_api_gateway_rest_api_test.go:415: Step 1/2 error: After applying this test step, the plan was not empty.
        stdout:

        An execution plan has been generated and is shown below.
        Resource actions are indicated with the following symbols:
          ~ update in-place

        Terraform will perform the following actions:

          # aws_api_gateway_rest_api.test will be updated in-place
          ~ resource "aws_api_gateway_rest_api" "test" {
                id                           = "adeg2zcky2"
                name                         = "tf-acc-test-7848512816247428885"
                # (9 unchanged attributes hidden)

              ~ endpoint_configuration {
                  ~ vpc_endpoint_ids = [
                      - "vpce-0653ca468e4c6ba4a",
                    ]
                    # (1 unchanged attribute hidden)
                }
            }

        Plan: 0 to add, 1 to change, 0 to destroy.
--- FAIL: TestAccAWSAPIGatewayRestApi_EndpointConfiguration_VpcEndpointIds_SetByBody (150.67s)
```

Output from acceptance testing:

```
--- PASS: TestAccAWSAPIGatewayRestApi_ApiKeySource (30.53s)
--- PASS: TestAccAWSAPIGatewayRestApi_ApiKeySource_OverrideBody (37.78s)
--- PASS: TestAccAWSAPIGatewayRestApi_ApiKeySource_SetByBody (15.25s)
--- PASS: TestAccAWSAPIGatewayRestApi_basic (669.92s)
--- PASS: TestAccAWSAPIGatewayRestApi_BinaryMediaTypes (34.56s)
--- PASS: TestAccAWSAPIGatewayRestApi_BinaryMediaTypes_OverrideBody (34.23s)
--- PASS: TestAccAWSAPIGatewayRestApi_BinaryMediaTypes_SetByBody (514.96s)
--- PASS: TestAccAWSAPIGatewayRestApi_Body (38.01s)
--- PASS: TestAccAWSAPIGatewayRestApi_Description (23.87s)
--- PASS: TestAccAWSAPIGatewayRestApi_Description_OverrideBody (60.58s)
--- PASS: TestAccAWSAPIGatewayRestApi_Description_SetByBody (92.79s)
--- PASS: TestAccAWSAPIGatewayRestApi_DisableExecuteApiEndpoint (3442.94s)
--- PASS: TestAccAWSAPIGatewayRestApi_DisableExecuteApiEndpoint_OverrideBody (2292.47s)
--- PASS: TestAccAWSAPIGatewayRestApi_DisableExecuteApiEndpoint_SetByBody (1083.96s)
--- PASS: TestAccAWSAPIGatewayRestApi_disappears (529.45s)
--- PASS: TestAccAWSAPIGatewayRestApi_EndpointConfiguration (1034.47s)
--- PASS: TestAccAWSAPIGatewayRestApi_EndpointConfiguration_Private (15.42s)
--- PASS: TestAccAWSAPIGatewayRestApi_EndpointConfiguration_VpcEndpointIds (330.11s)
--- PASS: TestAccAWSAPIGatewayRestApi_EndpointConfiguration_VpcEndpointIds_OverrideBody (205.73s)
--- PASS: TestAccAWSAPIGatewayRestApi_EndpointConfiguration_VpcEndpointIds_SetByBody (159.85s)
--- PASS: TestAccAWSAPIGatewayRestApi_MinimumCompressionSize (39.32s)
--- PASS: TestAccAWSAPIGatewayRestApi_MinimumCompressionSize_OverrideBody (1278.64s)
--- PASS: TestAccAWSAPIGatewayRestApi_MinimumCompressionSize_SetByBody (753.00s)
--- PASS: TestAccAWSAPIGatewayRestApi_Name_OverrideBody (2212.45s)
--- PASS: TestAccAWSAPIGatewayRestApi_Parameters (948.67s)
--- PASS: TestAccAWSAPIGatewayRestApi_Policy (28.00s)
--- PASS: TestAccAWSAPIGatewayRestApi_Policy_OverrideBody (80.78s)
--- PASS: TestAccAWSAPIGatewayRestApi_Policy_SetByBody (1967.74s)
--- PASS: TestAccAWSAPIGatewayRestApi_tags (1413.12s)
```
  • Loading branch information
bflad committed Jan 21, 2021
1 parent 2ad761d commit 8dfc019
Show file tree
Hide file tree
Showing 4 changed files with 568 additions and 152 deletions.
5 changes: 3 additions & 2 deletions aws/data_source_aws_api_gateway_rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestAccDataSourceAwsApiGatewayRestApi_EndpointConfiguration_VpcEndpointIds(
Steps: []resource.TestStep{
{
Config: composeConfig(
testAccAWSAPIGatewayRestAPIConfig_VPCEndpointConfiguration(rName),
testAccAWSAPIGatewayRestAPIConfigEndpointConfigurationVpcEndpointIds1(rName),
testAccDataSourceAwsApiGatewayRestApiConfigName(),
),
Check: resource.ComposeTestCheckFunc(
Expand All @@ -61,7 +61,8 @@ func TestAccDataSourceAwsApiGatewayRestApi_EndpointConfiguration_VpcEndpointIds(
resource.TestCheckResourceAttrPair(dataSourceName, "api_key_source", resourceName, "api_key_source"),
resource.TestCheckResourceAttrPair(dataSourceName, "minimum_compression_size", resourceName, "minimum_compression_size"),
resource.TestCheckResourceAttrPair(dataSourceName, "binary_media_types", resourceName, "binary_media_types"),
resource.TestCheckResourceAttrPair(dataSourceName, "endpoint_configuration", resourceName, "endpoint_configuration"),
resource.TestCheckResourceAttrPair(dataSourceName, "endpoint_configuration.#", resourceName, "endpoint_configuration.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "endpoint_configuration.0.vpc_endpoint_ids.#", resourceName, "endpoint_configuration.0.vpc_endpoint_ids.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "execution_arn", resourceName, "execution_arn"),
),
},
Expand Down
67 changes: 66 additions & 1 deletion aws/resource_aws_api_gateway_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func resourceAwsApiGatewayRestApi() *schema.Resource {
"disable_execute_api_endpoint": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Computed: true,
},

"parameters": {
Expand Down Expand Up @@ -122,6 +122,7 @@ func resourceAwsApiGatewayRestApi() *schema.Resource {
"vpc_endpoint_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -250,6 +251,38 @@ func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}
})
}

if v, ok := d.GetOk("disable_execute_api_endpoint"); ok && v.(bool) != aws.BoolValue(output.DisableExecuteApiEndpoint) {
updateInput.PatchOperations = append(updateInput.PatchOperations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpReplace),
Path: aws.String("/disableExecuteApiEndpoint"),
Value: aws.String(strconv.FormatBool(v.(bool))),
})
}

if v, ok := d.GetOk("endpoint_configuration"); ok {
endpointConfiguration := expandApiGatewayEndpointConfiguration(v.([]interface{}))

if endpointConfiguration != nil && len(endpointConfiguration.VpcEndpointIds) > 0 {
if output.EndpointConfiguration != nil {
for _, elem := range output.EndpointConfiguration.VpcEndpointIds {
updateInput.PatchOperations = append(updateInput.PatchOperations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpRemove),
Path: aws.String("/endpointConfiguration/vpcEndpointIds"),
Value: elem,
})
}
}

for _, elem := range endpointConfiguration.VpcEndpointIds {
updateInput.PatchOperations = append(updateInput.PatchOperations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpAdd),
Path: aws.String("/endpointConfiguration/vpcEndpointIds"),
Value: elem,
})
}
}
}

if v := d.Get("minimum_compression_size").(int); v > -1 && int64(v) != aws.Int64Value(output.MinimumCompressionSize) {
updateInput.PatchOperations = append(updateInput.PatchOperations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpReplace),
Expand Down Expand Up @@ -577,6 +610,38 @@ func resourceAwsApiGatewayRestApiUpdate(d *schema.ResourceData, meta interface{}
})
}

if v, ok := d.GetOk("disable_execute_api_endpoint"); ok && v.(bool) != aws.BoolValue(output.DisableExecuteApiEndpoint) {
updateInput.PatchOperations = append(updateInput.PatchOperations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpReplace),
Path: aws.String("/disableExecuteApiEndpoint"),
Value: aws.String(strconv.FormatBool(v.(bool))),
})
}

if v, ok := d.GetOk("endpoint_configuration"); ok {
endpointConfiguration := expandApiGatewayEndpointConfiguration(v.([]interface{}))

if endpointConfiguration != nil && len(endpointConfiguration.VpcEndpointIds) > 0 {
if output.EndpointConfiguration != nil {
for _, elem := range output.EndpointConfiguration.VpcEndpointIds {
updateInput.PatchOperations = append(updateInput.PatchOperations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpRemove),
Path: aws.String("/endpointConfiguration/vpcEndpointIds"),
Value: elem,
})
}
}

for _, elem := range endpointConfiguration.VpcEndpointIds {
updateInput.PatchOperations = append(updateInput.PatchOperations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpAdd),
Path: aws.String("/endpointConfiguration/vpcEndpointIds"),
Value: elem,
})
}
}
}

if v := d.Get("minimum_compression_size").(int); v > -1 && int64(v) != aws.Int64Value(output.MinimumCompressionSize) {
updateInput.PatchOperations = append(updateInput.PatchOperations, &apigateway.PatchOperation{
Op: aws.String(apigateway.OpReplace),
Expand Down
Loading

0 comments on commit 8dfc019

Please sign in to comment.