Skip to content

Commit

Permalink
resource/aws_cloudfront_distribution: Add wait_for_deployment argument
Browse files Browse the repository at this point in the history
Reference: #8073

Since we are adding a virtual attribute with a `Default`, we must use include a schema state migration to prevent the "" => "true" default difference on upgrade.

Output from acceptance testing:

```

```
  • Loading branch information
bflad committed Mar 28, 2019
1 parent d7d05c7 commit 229456b
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 54 deletions.
1 change: 1 addition & 0 deletions aws/import_aws_cloudfront_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func resourceAwsCloudFrontDistributionImport(d *schema.ResourceData, meta interf
// This is a non API attribute
// We are merely setting this to the same value as the Default setting in the schema
d.Set("retain_on_delete", false)
d.Set("wait_for_deployment", true)

conn := meta.(*AWSClient).cloudfrontconn
id := d.Id()
Expand Down
23 changes: 17 additions & 6 deletions aws/resource_aws_cloudfront_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
Importer: &schema.ResourceImporter{
State: resourceAwsCloudFrontDistributionImport,
},
MigrateState: resourceAwsCloudFrontDistributionMigrateState,
SchemaVersion: 1,

Schema: map[string]*schema.Schema{
"arn": {
Expand Down Expand Up @@ -714,6 +716,11 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
Optional: true,
Default: false,
},
"wait_for_deployment": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"is_ipv6_enabled": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -765,9 +772,11 @@ func resourceAwsCloudFrontDistributionCreate(d *schema.ResourceData, meta interf

d.SetId(*resp.Distribution.Id)

log.Printf("[DEBUG] Waiting until CloudFront Distribution (%s) is deployed", d.Id())
if err := resourceAwsCloudFrontDistributionWaitUntilDeployed(d.Id(), meta); err != nil {
return fmt.Errorf("error waiting until CloudFront Distribution (%s) is deployed: %s", d.Id(), err)
if d.Get("wait_for_deployment").(bool) {
log.Printf("[DEBUG] Waiting until CloudFront Distribution (%s) is deployed", d.Id())
if err := resourceAwsCloudFrontDistributionWaitUntilDeployed(d.Id(), meta); err != nil {
return fmt.Errorf("error waiting until CloudFront Distribution (%s) is deployed: %s", d.Id(), err)
}
}

return resourceAwsCloudFrontDistributionRead(d, meta)
Expand Down Expand Up @@ -858,9 +867,11 @@ func resourceAwsCloudFrontDistributionUpdate(d *schema.ResourceData, meta interf
return fmt.Errorf("error updating CloudFront Distribution (%s): %s", d.Id(), err)
}

log.Printf("[DEBUG] Waiting until CloudFront Distribution (%s) is deployed", d.Id())
if err := resourceAwsCloudFrontDistributionWaitUntilDeployed(d.Id(), meta); err != nil {
return fmt.Errorf("error waiting until CloudFront Distribution (%s) is deployed: %s", d.Id(), err)
if d.Get("wait_for_deployment").(bool) {
log.Printf("[DEBUG] Waiting until CloudFront Distribution (%s) is deployed", d.Id())
if err := resourceAwsCloudFrontDistributionWaitUntilDeployed(d.Id(), meta); err != nil {
return fmt.Errorf("error waiting until CloudFront Distribution (%s) is deployed: %s", d.Id(), err)
}
}

if err := setTagsCloudFront(conn, d, d.Get("arn").(string)); err != nil {
Expand Down
34 changes: 34 additions & 0 deletions aws/resource_aws_cloudfront_distribution_migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package aws

import (
"fmt"
"log"

"github.com/hashicorp/terraform/terraform"
)

func resourceAwsCloudFrontDistributionMigrateState(v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
switch v {
case 0:
log.Println("[INFO] Found CloudFront Distribution state v0; migrating to v1")
return migrateCloudFrontDistributionStateV0toV1(is)
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}

func migrateCloudFrontDistributionStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
if is.Empty() || is.Attributes == nil {
log.Println("[DEBUG] Empty CloudFront Distribution state; nothing to migrate.")
return is, nil
}

log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)

// Add wait_for_deployment virtual attribute with Default
is.Attributes["wait_for_deployment"] = "true"

log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)

return is, nil
}
52 changes: 52 additions & 0 deletions aws/resource_aws_cloudfront_distribution_migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform/terraform"
)

func TestAwsCloudFrontDistributionMigrateState(t *testing.T) {
cases := map[string]struct {
StateVersion int
Attributes map[string]string
Expected map[string]string
Meta interface{}
}{
"v0_1": {
StateVersion: 0,
Attributes: map[string]string{
"wait_for_deployment": "",
},
Expected: map[string]string{
"wait_for_deployment": "true",
},
},
}

for tn, tc := range cases {
is := &terraform.InstanceState{
ID: "some_id",
Attributes: tc.Attributes,
}

tfResource := resourceAwsCloudFrontDistribution()

if tfResource.MigrateState == nil {
t.Fatalf("bad: %s, err: missing MigrateState function in resource", tn)
}

is, err := tfResource.MigrateState(tc.StateVersion, is, tc.Meta)
if err != nil {
t.Fatalf("bad: %s, err: %#v", tn, err)
}

for k, v := range tc.Expected {
if is.Attributes[k] != v {
t.Fatalf(
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
tn, k, v, k, is.Attributes[k], is.Attributes)
}
}
}
}
Loading

0 comments on commit 229456b

Please sign in to comment.