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_cloudfront_distribution: Add wait_for_deployment argument #8116

Merged
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
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) {
testCases := map[string]struct {
StateVersion int
Attributes map[string]string
Expected map[string]string
Meta interface{}
}{
"v0_to_v1": {
StateVersion: 0,
Attributes: map[string]string{
"wait_for_deployment": "",
},
Expected: map[string]string{
"wait_for_deployment": "true",
},
},
}

for testName, testCase := range testCases {
instanceState := &terraform.InstanceState{
ID: "some_id",
Attributes: testCase.Attributes,
}

tfResource := resourceAwsCloudFrontDistribution()

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

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

for key, expectedValue := range testCase.Expected {
if instanceState.Attributes[key] != expectedValue {
t.Fatalf(
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
testName, key, expectedValue, key, instanceState.Attributes[key], instanceState.Attributes)
}
}
}
}
Loading