-
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
resource/aws_api_gateway_deployment: Do not delete stages in use by other deployments #3896
Merged
bflad
merged 2 commits into
hashicorp:master
from
mdlavin:do-not-always-delete-gateway-stages
Oct 2, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,49 @@ func TestAccAWSAPIGatewayDeployment_basic(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccAWSAPIGatewayDeployment_createBeforeDestoryUpdate(t *testing.T) { | ||
var conf apigateway.Deployment | ||
var stage apigateway.Stage | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSAPIGatewayDeploymentDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSAPIGatewayDeploymentCreateBeforeDestroyConfig("description1", "https://www.google.com"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAPIGatewayDeploymentExists("aws_api_gateway_deployment.test", &conf), | ||
resource.TestCheckResourceAttr( | ||
"aws_api_gateway_deployment.test", "stage_name", "test"), | ||
resource.TestCheckResourceAttr( | ||
"aws_api_gateway_deployment.test", "description", "description1"), | ||
resource.TestCheckResourceAttr( | ||
"aws_api_gateway_deployment.test", "variables.a", "2"), | ||
resource.TestCheckResourceAttrSet( | ||
"aws_api_gateway_deployment.test", "created_date"), | ||
testAccCheckAWSAPIGatewayDeploymentStageExists("aws_api_gateway_deployment.test", "test", &stage), | ||
), | ||
}, | ||
{ | ||
Config: testAccAWSAPIGatewayDeploymentCreateBeforeDestroyConfig("description2", "https://www.google.de"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAPIGatewayDeploymentExists("aws_api_gateway_deployment.test", &conf), | ||
resource.TestCheckResourceAttr( | ||
"aws_api_gateway_deployment.test", "stage_name", "test"), | ||
resource.TestCheckResourceAttr( | ||
"aws_api_gateway_deployment.test", "description", "description2"), | ||
resource.TestCheckResourceAttr( | ||
"aws_api_gateway_deployment.test", "variables.a", "2"), | ||
resource.TestCheckResourceAttrSet( | ||
"aws_api_gateway_deployment.test", "created_date"), | ||
testAccCheckAWSAPIGatewayDeploymentStageExists("aws_api_gateway_deployment.test", "test", &stage), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSAPIGatewayDeploymentExists(n string, res *apigateway.Deployment) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
|
@@ -69,6 +112,32 @@ func testAccCheckAWSAPIGatewayDeploymentExists(n string, res *apigateway.Deploym | |
} | ||
} | ||
|
||
func testAccCheckAWSAPIGatewayDeploymentStageExists(deploymentName string, stageName string, res *apigateway.Stage) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).apigateway | ||
|
||
deploymentResource, ok := s.RootModule().Resources[deploymentName] | ||
if !ok { | ||
return fmt.Errorf("Deployment not found: %s", deploymentName) | ||
} | ||
|
||
restApiId := aws.String(deploymentResource.Primary.Attributes["rest_api_id"]) | ||
|
||
req := &apigateway.GetStageInput{ | ||
StageName: &stageName, | ||
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. Nitpick: the stage name can be fetched from attributes as well ( |
||
RestApiId: restApiId, | ||
} | ||
stage, err := conn.GetStage(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*res = *stage | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckAWSAPIGatewayDeploymentDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).apigateway | ||
|
||
|
@@ -103,7 +172,8 @@ func testAccCheckAWSAPIGatewayDeploymentDestroy(s *terraform.State) error { | |
return nil | ||
} | ||
|
||
const testAccAWSAPIGatewayDeploymentConfig = ` | ||
func buildAPIGatewayDeploymentConfig(description, url, extras string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_api_gateway_rest_api" "test" { | ||
name = "test" | ||
} | ||
|
@@ -134,7 +204,7 @@ resource "aws_api_gateway_integration" "test" { | |
http_method = "${aws_api_gateway_method.test.http_method}" | ||
|
||
type = "HTTP" | ||
uri = "https://www.google.de" | ||
uri = "%s" | ||
integration_http_method = "GET" | ||
} | ||
|
||
|
@@ -150,10 +220,24 @@ resource "aws_api_gateway_deployment" "test" { | |
|
||
rest_api_id = "${aws_api_gateway_rest_api.test.id}" | ||
stage_name = "test" | ||
description = "This is a test" | ||
description = "%s" | ||
stage_description = "%s" | ||
|
||
%s | ||
|
||
variables = { | ||
"a" = "2" | ||
} | ||
} | ||
` | ||
`, url, description, description, extras) | ||
} | ||
|
||
var testAccAWSAPIGatewayDeploymentConfig = buildAPIGatewayDeploymentConfig("This is a test", "https://www.google.de", "") | ||
|
||
func testAccAWSAPIGatewayDeploymentCreateBeforeDestroyConfig(description string, url string) string { | ||
return buildAPIGatewayDeploymentConfig(description, url, ` | ||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
`) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nitpick: we have a helper function for this type of logic and prefer the AWS Go SDK constants, e.g.
isAWSErr(err, apigateway.ErrCodeNotFoundException, "")