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

fix(resource_ssm_document): handle dynamic field refresh when content is updated #28489

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 19 additions & 1 deletion internal/service/ssm/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ssm
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"log"
"regexp"
"strings"
Expand Down Expand Up @@ -192,7 +193,24 @@ func ResourceDocument() *schema.Resource {
},
},

CustomizeDiff: verify.SetTagsDiff,
CustomizeDiff: customdiff.Sequence(
customdiff.ComputedIf("default_version", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("content")
}),
customdiff.ComputedIf("document_version", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("content")
}),
customdiff.ComputedIf("hash", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("content")
}),
customdiff.ComputedIf("latest_version", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("content")
}),
customdiff.ComputedIf("parameter", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("content")
}),
verify.SetTagsDiff,
),
}
}

Expand Down
56 changes: 53 additions & 3 deletions internal/service/ssm/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,14 @@ func TestAccSSMDocument_update(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckDocumentExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "schema_version", "2.0"),
resource.TestCheckResourceAttr(resourceName, "document_version", "1"),
resource.TestCheckResourceAttr(resourceName, "latest_version", "1"),
resource.TestCheckResourceAttr(resourceName, "default_version", "1"),
resource.TestCheckOutput("default_version", "1"),
resource.TestCheckOutput("document_version", "1"),
resource.TestCheckOutput("hash", "1a200df3fefa0e7f8814829781d6295e616474945a239a956561876b4c820cde"),
resource.TestCheckOutput("latest_version", "1"),
resource.TestCheckOutput("parameter_len", "0"),
),
},
{
Expand All @@ -175,9 +181,15 @@ func TestAccSSMDocument_update(t *testing.T) {
{
Config: testAccDocumentConfig_20Updated(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckDocumentExists(ctx, resourceName),
testAccCheckDocumentExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "document_version", "2"),
resource.TestCheckResourceAttr(resourceName, "latest_version", "2"),
resource.TestCheckResourceAttr(resourceName, "default_version", "2"),
resource.TestCheckOutput("default_version", "2"),
resource.TestCheckOutput("document_version", "2"),
resource.TestCheckOutput("hash", "214c51d87f98ae07b868a63cd866955578c1ef41c3ab8c36f80039dfd9565f53"),
resource.TestCheckOutput("latest_version", "2"),
resource.TestCheckOutput("parameter_len", "1"),
),
},
},
Expand Down Expand Up @@ -805,6 +817,22 @@ resource "aws_ssm_document" "test" {
DOC

}

output "default_version" {
value = aws_ssm_document.test.default_version
}
output "document_version" {
value = aws_ssm_document.test.document_version
}
output "hash" {
value = aws_ssm_document.test.hash
}
output "latest_version" {
value = aws_ssm_document.test.latest_version
}
output "parameter_len" {
value = length(aws_ssm_document.test.parameter)
}
`, rName)
}

Expand All @@ -818,14 +846,20 @@ resource "aws_ssm_document" "test" {
{
"schemaVersion": "2.0",
"description": "Sample version 2.0 document v2",
"parameters": {},
"parameters": {
"processOptions": {
"type": "String",
"default": "-Verbose",
"description": "(Optional) Get-Process command options."
}
},
"mainSteps": [
{
"action": "aws:runPowerShellScript",
"name": "runPowerShellScript",
"inputs": {
"runCommand": [
"Get-Process -Verbose"
"Get-Process {{processOptions}}"
]
}
}
Expand All @@ -834,6 +868,22 @@ resource "aws_ssm_document" "test" {
DOC

}

output "default_version" {
value = aws_ssm_document.test.default_version
}
output "document_version" {
value = aws_ssm_document.test.document_version
}
output "hash" {
value = aws_ssm_document.test.hash
}
output "latest_version" {
value = aws_ssm_document.test.latest_version
}
output "parameter_len" {
value = length(aws_ssm_document.test.parameter)
}
`, rName)
}

Expand Down