Skip to content

Commit

Permalink
Merge pull request #3861 from mdlavin/fix-1696
Browse files Browse the repository at this point in the history
resource/aws_s3_bucket_object: mark version_id as recomputed if a new object will be uploaded
  • Loading branch information
bflad committed Oct 2, 2018
2 parents 09027c5 + 0e4a09f commit d2c2d06
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
6 changes: 0 additions & 6 deletions aws/resource_aws_lambda_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,17 +960,11 @@ func TestAccAWSLambdaFunction_s3Update_basic(t *testing.T) {
),
},
{
ExpectNonEmptyPlan: true,
PreConfig: func() {
// Upload 2nd version
testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile)
},
Config: genAWSLambdaFunctionConfig_s3(bucketName, key, path, roleName, funcName),
},
// Extra step because of missing ComputedWhen
// See https://github.com/hashicorp/terraform/pull/4846 & https://github.com/hashicorp/terraform/pull/5330
{
Config: genAWSLambdaFunctionConfig_s3(bucketName, key, path, roleName, funcName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", funcName, &conf),
testAccCheckAwsLambdaFunctionName(&conf, funcName),
Expand Down
9 changes: 9 additions & 0 deletions aws/resource_aws_s3_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func resourceAwsS3BucketObject() *schema.Resource {
Update: resourceAwsS3BucketObjectPut,
Delete: resourceAwsS3BucketObjectDelete,

CustomizeDiff: updateComputedAttributes,

Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Expand Down Expand Up @@ -152,6 +154,13 @@ func resourceAwsS3BucketObject() *schema.Resource {
}
}

func updateComputedAttributes(d *schema.ResourceDiff, meta interface{}) error {
if d.HasChange("etag") {
d.SetNewComputed("version_id")
}
return nil
}

func resourceAwsS3BucketObjectPut(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn

Expand Down
25 changes: 25 additions & 0 deletions aws/resource_aws_s3_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,29 @@ func TestAccAWSS3BucketObject_updatesWithVersioning(t *testing.T) {
testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object", &modifiedObj),
resource.TestCheckResourceAttr("aws_s3_bucket_object.object", "etag", "00b8c73b1b50e7cc932362c7225b8e29"),
testAccCheckAWSS3BucketObjectVersionIdDiffers(&originalObj, &modifiedObj),
testAccCheckResourceAttrMatchesVersionId("data.template_file.object_version", "rendered", &modifiedObj),
),
},
},
})
}

func testAccCheckResourceAttrMatchesVersionId(resourceName string, attribute string, object *s3.GetObjectOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not Found: %s", resourceName)
}

attrValue := aws.String(rs.Primary.Attributes[attribute])
if *attrValue != *object.VersionId {
return fmt.Errorf("Expected Version IDs to be the same, but they were different (%s vs %s)", *attrValue, *object.VersionId)
}

return nil
}
}

func testAccCheckAWSS3BucketObjectVersionIdDiffers(first, second *s3.GetObjectOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
if first.VersionId == nil {
Expand Down Expand Up @@ -634,6 +651,14 @@ resource "aws_s3_bucket_object" "object" {
source = "%s"
etag = "${md5(file("%s"))}"
}
data "template_file" "object_version" {
template = "$${object_version}"
vars {
object_version = "${aws_s3_bucket_object.object.version_id}"
}
}
`, randInt, source, source)
}

Expand Down

0 comments on commit d2c2d06

Please sign in to comment.