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

r/aws_apigateway_stage: add deployment_id to canary_settings #39929

Merged
merged 20 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions .changelog/39929.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_api_gateway_stage: Add `canary_settings.deployment_id` attribute
```

```release-note:bug
resource/aws_api_gateway_deployment: Fix destroy error when canary stage still exists on resource
```
24 changes: 21 additions & 3 deletions internal/service/apigateway/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func resourceDeployment() *schema.Resource {
"canary_settings": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -120,16 +121,17 @@ func resourceDeploymentCreate(ctx context.Context, d *schema.ResourceData, meta
Variables: flex.ExpandStringValueMap(d.Get("variables").(map[string]interface{})),
}

if v, ok := d.GetOk("canary_settings"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.CanarySettings = expandDeploymentCanarySettings(v.([]interface{})[0].(map[string]interface{}))
}

deployment, err := conn.CreateDeployment(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating API Gateway Deployment: %s", err)
}

d.SetId(aws.ToString(deployment.Id))
if v, ok := d.GetOk("canary_settings"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.CanarySettings = expandDeploymentCanarySettings(v.([]interface{})[0].(map[string]interface{}))
}

return append(diags, resourceDeploymentRead(ctx, d, meta)...)
}
Expand Down Expand Up @@ -236,6 +238,22 @@ func resourceDeploymentDelete(ctx context.Context, d *schema.ResourceData, meta
RestApiId: aws.String(restAPIID),
})

if errs.IsAErrorMessageContains[*types.BadRequestException](err, "Active stages with canary settings pointing to this deployment must be moved or deleted") {
_, err = conn.DeleteStage(ctx, &apigateway.DeleteStageInput{
StageName: aws.String(stageName),
RestApiId: aws.String(restAPIID),
})

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting API Gateway Stage (%s): %s", stageName, err)
}

_, err = conn.DeleteDeployment(ctx, &apigateway.DeleteDeploymentInput{
DeploymentId: aws.String(d.Id()),
RestApiId: aws.String(restAPIID),
})
}

if errs.IsA[*types.NotFoundException](err) {
return diags
}
Expand Down
18 changes: 18 additions & 0 deletions internal/service/apigateway/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,28 @@ resource "aws_lambda_function" "test" {

func testAccStageConfig_deploymentCanarySettings(rName, url string) string {
return acctest.ConfigCompose(testAccDeploymentConfig_base(rName, url), `
resource "aws_api_gateway_stage" "test" {
rest_api_id = aws_api_gateway_rest_api.test.id
stage_name = "prod"
deployment_id = aws_api_gateway_deployment.test_main.id

lifecycle {
ignore_changes = [variables, canary_settings]
}
}

resource "aws_api_gateway_deployment" "test_main" {
depends_on = [aws_api_gateway_integration.test]

description = "test-api-gateway"
rest_api_id = aws_api_gateway_rest_api.test.id
}

resource "aws_api_gateway_deployment" "test" {
depends_on = [aws_api_gateway_integration.test]

rest_api_id = aws_api_gateway_rest_api.test.id
stage_name = aws_api_gateway_stage.test.stage_name
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved
canary_settings {
percent_traffic = "33.33"
stage_variable_overrides = {
Expand Down
33 changes: 27 additions & 6 deletions internal/service/apigateway/stage.go
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func resourceStage() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"deployment_id": {
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"percent_traffic": {
Type: schema.TypeFloat,
Optional: true,
Expand Down Expand Up @@ -333,12 +338,14 @@ func resourceStageUpdate(ctx context.Context, d *schema.ResourceData, meta inter
Value: aws.String(d.Get("deployment_id").(string)),
})

if _, ok := d.GetOk("canary_settings"); ok {
operations = append(operations, types.PatchOperation{
Op: types.OpReplace,
Path: aws.String("/canarySettings/deploymentId"),
Value: aws.String(d.Get("deployment_id").(string)),
})
if v, ok := d.GetOk("canary_settings"); ok && v.([]any)[0] != nil {
if id, ok := v.([]any)[0].(map[string]any)["deployment_id"].(string); ok && id == "" { // only set if deployment_id is not present on canary_settings
operations = append(operations, types.PatchOperation{
Op: types.OpReplace,
Path: aws.String("/canarySettings/deploymentId"),
Value: aws.String(d.Get("deployment_id").(string)),
})
}
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved
}
}
if d.HasChange(names.AttrDescription) {
Expand Down Expand Up @@ -570,6 +577,10 @@ func expandCanarySettings(tfMap map[string]interface{}, deploymentId string) *ty
DeploymentId: aws.String(deploymentId),
}

if v, ok := (tfMap["deployment_id"]).(string); ok && v != "" {
apiObject.DeploymentId = aws.String(v)
}

if v, ok := tfMap["percent_traffic"].(float64); ok {
apiObject.PercentTraffic = v
}
Expand Down Expand Up @@ -600,6 +611,7 @@ func flattenCanarySettings(canarySettings *types.CanarySettings) []interface{} {

settings["percent_traffic"] = canarySettings.PercentTraffic
settings["use_stage_cache"] = canarySettings.UseStageCache
settings["deployment_id"] = canarySettings.DeploymentId

return []interface{}{settings}
}
Expand All @@ -621,6 +633,7 @@ func appendCanarySettingsPatchOperations(operations []types.PatchOperation, oldC
"percent_traffic": 0.0,
"stage_variable_overrides": make(map[string]interface{}),
"use_stage_cache": false,
"deployment_id": "",
}
}

Expand Down Expand Up @@ -648,6 +661,14 @@ func appendCanarySettingsPatchOperations(operations []types.PatchOperation, oldC
})
}

oldDeploymentID, newDeploymentID := oldSettings["deployment_id"].(string), newSettings["deployment_id"].(string)
if oldDeploymentID != newDeploymentID {
operations = append(operations, types.PatchOperation{
Op: types.OpReplace,
Path: aws.String("/canarySettings/deploymentId"),
Value: aws.String(newDeploymentID),
})
}
return operations
}

Expand Down
110 changes: 110 additions & 0 deletions internal/service/apigateway/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ func testAccStage_canarySettings(t *testing.T) {
var conf apigateway.GetStageOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_api_gateway_stage.test"
deployment1 := "aws_api_gateway_deployment.test"
deployment2 := "aws_api_gateway_deployment.test2"
deployment3 := "aws_api_gateway_deployment.test3"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckAPIGatewayTypeEDGE(t) },
Expand All @@ -438,6 +441,7 @@ func testAccStage_canarySettings(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.percent_traffic", "33.33"),
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.stage_variable_overrides.one", "3"),
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.use_stage_cache", acctest.CtTrue),
resource.TestCheckResourceAttrPair(resourceName, "canary_settings.0.deployment_id", deployment1, names.AttrID),
),
},
{
Expand All @@ -461,6 +465,30 @@ func testAccStage_canarySettings(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.percent_traffic", "66.66"),
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.stage_variable_overrides.four", "5"),
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.use_stage_cache", acctest.CtFalse),
resource.TestCheckResourceAttrPair(resourceName, "canary_settings.0.deployment_id", deployment1, names.AttrID),
),
},
{
Config: testAccStageConfig_canarySettingsNewDeployment(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStageExists(ctx, resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "variables.one", "1"),
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.percent_traffic", "66.66"),
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.stage_variable_overrides.four", "5"),
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.use_stage_cache", acctest.CtFalse),
resource.TestCheckResourceAttrPair(resourceName, "canary_settings.0.deployment_id", deployment2, names.AttrID),
),
},
{
Config: testAccStageConfig_canarySettingsNewDeploymentUpdate(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStageExists(ctx, resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "variables.one", "1"),
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.percent_traffic", "66.66"),
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.stage_variable_overrides.four", "5"),
resource.TestCheckResourceAttr(resourceName, "canary_settings.0.use_stage_cache", acctest.CtFalse),
resource.TestCheckResourceAttrPair(resourceName, "canary_settings.0.deployment_id", deployment2, names.AttrID),
resource.TestCheckResourceAttrPair(resourceName, "deployment_id", deployment3, names.AttrID),
),
},
},
Expand Down Expand Up @@ -760,3 +788,85 @@ resource "aws_api_gateway_stage" "test" {
}
`)
}

func testAccStageConfig_canarySettingsNewDeployment(rName string) string {
return acctest.ConfigCompose(testAccStageConfig_base(rName), `
resource "aws_api_gateway_deployment" "test2" {
depends_on = [aws_api_gateway_integration.test]

rest_api_id = aws_api_gateway_rest_api.test.id
stage_name = "dev2"
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved
description = "This is another dev env"

variables = {
"a" = "2"
}
}

resource "aws_api_gateway_stage" "test" {
rest_api_id = aws_api_gateway_rest_api.test.id
stage_name = "prod"
deployment_id = aws_api_gateway_deployment.test.id

canary_settings {
percent_traffic = "66.66"
stage_variable_overrides = {
four = "5"
}
use_stage_cache = "false"
deployment_id = aws_api_gateway_deployment.test2.id
}
variables = {
one = "1"
two = "2"
}
}
`)
}

func testAccStageConfig_canarySettingsNewDeploymentUpdate(rName string) string {
return acctest.ConfigCompose(testAccStageConfig_base(rName), `
resource "aws_api_gateway_deployment" "test2" {
depends_on = [aws_api_gateway_integration.test]

rest_api_id = aws_api_gateway_rest_api.test.id
stage_name = "dev2"
description = "This is another dev env"

variables = {
"a" = "2"
}
}

resource "aws_api_gateway_deployment" "test3" {
depends_on = [aws_api_gateway_integration.test]

rest_api_id = aws_api_gateway_rest_api.test.id
stage_name = "dev2"
description = "This is another dev env"

variables = {
"a" = "2"
}
}

resource "aws_api_gateway_stage" "test" {
rest_api_id = aws_api_gateway_rest_api.test.id
stage_name = "prod"
deployment_id = aws_api_gateway_deployment.test3.id

canary_settings {
percent_traffic = "66.66"
stage_variable_overrides = {
four = "5"
}
use_stage_cache = "false"
deployment_id = aws_api_gateway_deployment.test2.id
}
variables = {
one = "1"
two = "2"
}
}
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved
`)
}
1 change: 1 addition & 0 deletions website/docs/r/api_gateway_stage.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ For more information on configuring the log format rules visit the AWS [document

### Canary Settings

* `deployment_id` - (Optional) ID of the deployment that the canary points to.
johnsonaj marked this conversation as resolved.
Show resolved Hide resolved
* `percent_traffic` - (Optional) Percent `0.0` - `100.0` of traffic to divert to the canary deployment.
* `stage_variable_overrides` - (Optional) Map of overridden stage `variables` (including new variables) for the canary deployment.
* `use_stage_cache` - (Optional) Whether the canary deployment uses the stage cache. Defaults to false.
Expand Down
Loading