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

resource/aws_api_gateway_deployment: Do not delete stages in use by other deployments #3896

Merged
merged 2 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions aws/resource_aws_api_gateway_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,38 @@ func resourceAwsApiGatewayDeploymentDelete(d *schema.ResourceData, meta interfac

return resource.Retry(5*time.Minute, func() *resource.RetryError {
log.Printf("[DEBUG] schema is %#v", d)
if _, err := conn.DeleteStage(&apigateway.DeleteStageInput{

stage, err := conn.GetStage(&apigateway.GetStageInput{
StageName: aws.String(d.Get("stage_name").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
}); err == nil {
return nil
})
if err != nil {
// If the stage is already deleted then there is no need to attempt deletion
apigatewayErr, _ := err.(awserr.Error)
if apigatewayErr.Code() == "NotFoundException" {
Copy link
Contributor

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, "")

return nil
}

return resource.NonRetryableError(fmt.Errorf("error getting referenced stage: %s", err))
}

// If the stage has been updated to point at a different deployment, then
// the stage should not be removed then this deployment is deleted.
var shouldDeleteStage = true
if stage == nil || aws.StringValue(stage.DeploymentId) != d.Id() {
shouldDeleteStage = false
}

if shouldDeleteStage {
if _, err := conn.DeleteStage(&apigateway.DeleteStageInput{
StageName: aws.String(d.Get("stage_name").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
}); err == nil {
return nil
}
}

_, err := conn.DeleteDeployment(&apigateway.DeleteDeploymentInput{
_, err = conn.DeleteDeployment(&apigateway.DeleteDeploymentInput{
DeploymentId: aws.String(d.Id()),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
})
Expand Down
92 changes: 88 additions & 4 deletions aws/resource_aws_api_gateway_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: the stage name can be fetched from attributes as well (deploymentResource.Primary.Attributes["stage_name"]) instead of a parameter passed in from the acceptance test

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

Expand Down Expand Up @@ -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"
}
Expand Down Expand Up @@ -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"
}

Expand All @@ -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
}
`)
}