Skip to content

Commit

Permalink
Add 'TestAccCloudFrontDistribution_basic' and 'TestAccCloudFrontDistr…
Browse files Browse the repository at this point in the history
…ibution_tags'.
  • Loading branch information
ewbankkit committed Apr 5, 2023
1 parent d92c5e0 commit e7fdf66
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 96 deletions.
3 changes: 2 additions & 1 deletion internal/service/cloudfront/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,11 +835,12 @@ func resourceDistributionCreate(ctx context.Context, d *schema.ResourceData, met
input := &cloudfront.CreateDistributionWithTagsInput{
DistributionConfigWithTags: &cloudfront.DistributionConfigWithTags{
DistributionConfig: expandDistributionConfig(d),
Tags: &cloudfront.Tags{Items: []*cloudfront.Tag{}},
},
}

if tags := GetTagsIn(ctx); len(tags) > 0 {
input.DistributionConfigWithTags.Tags = &cloudfront.Tags{Items: tags}
input.DistributionConfigWithTags.Tags.Items = tags
}

var resp *cloudfront.CreateDistributionWithTagsOutput
Expand Down
12 changes: 4 additions & 8 deletions internal/service/cloudfront/distribution_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ import (
"testing"

"github.com/aws/aws-sdk-go/service/cloudfront"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccCloudFrontDistributionDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
dataSourceName := "data.aws_cloudfront_distribution.test"
resourceName := "aws_cloudfront_distribution.s3_distribution"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_cloudfront_distribution.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, cloudfront.EndpointsID) },
ErrorCheck: acctest.ErrorCheck(t, cloudfront.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDistributionDataSourceConfig_basic(rName),
Config: testAccDistributionDataSourceConfig_basic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "domain_name", resourceName, "domain_name"),
Expand All @@ -37,10 +35,8 @@ func TestAccCloudFrontDistributionDataSource_basic(t *testing.T) {
})
}

func testAccDistributionDataSourceConfig_basic(rName string) string {
return acctest.ConfigCompose(testAccDistributionConfig_s3Tags(rName), `
var testAccDistributionDataSourceConfig_basic = acctest.ConfigCompose(testAccDistributionConfig_enabled(false, false), `
data "aws_cloudfront_distribution" "test" {
id = aws_cloudfront_distribution.s3_distribution.id
id = aws_cloudfront_distribution.test.id
}
`)
}
199 changes: 112 additions & 87 deletions internal/service/cloudfront/distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,37 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func TestAccCloudFrontDistribution_basic(t *testing.T) {
ctx := acctest.Context(t)
var distribution cloudfront.Distribution
resourceName := "aws_cloudfront_distribution.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, cloudfront.EndpointsID) },
ErrorCheck: acctest.ErrorCheck(t, cloudfront.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckDistributionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccDistributionConfig_enabled(false, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionExists(ctx, resourceName, &distribution),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"retain_on_delete",
"wait_for_deployment",
},
},
},
})
}

func TestAccCloudFrontDistribution_disappears(t *testing.T) {
ctx := acctest.Context(t)
var distribution cloudfront.Distribution
Expand All @@ -44,19 +75,10 @@ func TestAccCloudFrontDistribution_disappears(t *testing.T) {
})
}

// TestAccCloudFrontDistribution_s3Origin runs an
// aws_cloudfront_distribution acceptance test with a single S3 origin.
//
// If you are testing manually and can't wait for deletion, set the
// TF_TEST_CLOUDFRONT_RETAIN environment variable.
func TestAccCloudFrontDistribution_s3Origin(t *testing.T) {
func TestAccCloudFrontDistribution_tags(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var distribution cloudfront.Distribution
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_cloudfront_distribution.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, cloudfront.EndpointsID) },
Expand All @@ -65,26 +87,49 @@ func TestAccCloudFrontDistribution_s3Origin(t *testing.T) {
CheckDestroy: testAccCheckDistributionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccDistributionConfig_s3(rName),
Config: testAccDistributionConfig_tags1("key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionExists(ctx, "aws_cloudfront_distribution.s3_distribution", &distribution),
resource.TestCheckResourceAttr("aws_cloudfront_distribution.s3_distribution", "hosted_zone_id", "Z2FDTNDATAQYW2"),
testAccCheckDistributionExists(ctx, resourceName, &distribution),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: "aws_cloudfront_distribution.s3_distribution",
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"retain_on_delete",
"wait_for_deployment",
},
},
{
Config: testAccDistributionConfig_tags2("key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionExists(ctx, resourceName, &distribution),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccDistributionConfig_tags1("key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionExists(ctx, resourceName, &distribution),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}

func TestAccCloudFrontDistribution_s3OriginWithTags(t *testing.T) {
// TestAccCloudFrontDistribution_s3Origin runs an
// aws_cloudfront_distribution acceptance test with a single S3 origin.
//
// If you are testing manually and can't wait for deletion, set the
// TF_TEST_CLOUDFRONT_RETAIN environment variable.
func TestAccCloudFrontDistribution_s3Origin(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
Expand All @@ -100,12 +145,10 @@ func TestAccCloudFrontDistribution_s3OriginWithTags(t *testing.T) {
CheckDestroy: testAccCheckDistributionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccDistributionConfig_s3Tags(rName),
Config: testAccDistributionConfig_s3(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionExists(ctx, "aws_cloudfront_distribution.s3_distribution", &distribution),
resource.TestCheckResourceAttr("aws_cloudfront_distribution.s3_distribution", "tags.%", "2"),
resource.TestCheckResourceAttr("aws_cloudfront_distribution.s3_distribution", "tags.environment", "production"),
resource.TestCheckResourceAttr("aws_cloudfront_distribution.s3_distribution", "tags.account", "main"),
resource.TestCheckResourceAttr("aws_cloudfront_distribution.s3_distribution", "hosted_zone_id", "Z2FDTNDATAQYW2"),
),
},
{
Expand All @@ -117,14 +160,6 @@ func TestAccCloudFrontDistribution_s3OriginWithTags(t *testing.T) {
"wait_for_deployment",
},
},
{
Config: testAccDistributionConfig_s3TagsUpdated(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionExists(ctx, "aws_cloudfront_distribution.s3_distribution", &distribution),
resource.TestCheckResourceAttr("aws_cloudfront_distribution.s3_distribution", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_cloudfront_distribution.s3_distribution", "tags.environment", "dev"),
),
},
},
})
}
Expand Down Expand Up @@ -1754,45 +1789,42 @@ resource "aws_cloudfront_distribution" "s3_distribution" {
`, testAccDistributionRetainConfig()))
}

func testAccDistributionConfig_s3Tags(rName string) string {
return acctest.ConfigCompose(
originBucket(rName),
logBucket(rName),
fmt.Sprintf(`
resource "aws_cloudfront_distribution" "s3_distribution" {
origin {
domain_name = aws_s3_bucket.s3_bucket_origin.bucket_regional_domain_name
origin_id = "myS3Origin"
}
enabled = true
default_root_object = "index.html"
func testAccDistributionConfig_tags1(tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_cloudfront_distribution" "test" {
enabled = false
retain_on_delete = false
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "myS3Origin"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "test"
viewer_protocol_policy = "allow-all"
forwarded_values {
query_string = false
cookies {
forward = "none"
forward = "all"
}
}
viewer_protocol_policy = "allow-all"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
price_class = "PriceClass_200"
origin {
domain_name = "www.example.com"
origin_id = "test"
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "https-only"
origin_ssl_protocols = ["TLSv1.2"]
}
}
restrictions {
geo_restriction {
restriction_type = "whitelist"
locations = ["US", "CA", "GB", "DE"]
restriction_type = "none"
}
}
Expand All @@ -1801,54 +1833,48 @@ resource "aws_cloudfront_distribution" "s3_distribution" {
}
tags = {
environment = "production"
account = "main"
%[1]q = %[2]q
}
%[1]s
}
`, testAccDistributionRetainConfig()))
`, tagKey1, tagValue1)
}

func testAccDistributionConfig_s3TagsUpdated(rName string) string {
return acctest.ConfigCompose(
originBucket(rName),
logBucket(rName),
fmt.Sprintf(`
resource "aws_cloudfront_distribution" "s3_distribution" {
origin {
domain_name = aws_s3_bucket.s3_bucket_origin.bucket_regional_domain_name
origin_id = "myS3Origin"
}
enabled = true
default_root_object = "index.html"
func testAccDistributionConfig_tags2(tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_cloudfront_distribution" "test" {
enabled = false
retain_on_delete = false
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "myS3Origin"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "test"
viewer_protocol_policy = "allow-all"
forwarded_values {
query_string = false
cookies {
forward = "none"
forward = "all"
}
}
viewer_protocol_policy = "allow-all"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
price_class = "PriceClass_200"
origin {
domain_name = "www.example.com"
origin_id = "test"
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "https-only"
origin_ssl_protocols = ["TLSv1.2"]
}
}
restrictions {
geo_restriction {
restriction_type = "whitelist"
locations = ["US", "CA", "GB", "DE"]
restriction_type = "none"
}
}
Expand All @@ -1857,12 +1883,11 @@ resource "aws_cloudfront_distribution" "s3_distribution" {
}
tags = {
environment = "dev"
%[1]q = %[2]q
%[3]q = %[4]q
}
%[1]s
}
`, testAccDistributionRetainConfig()))
`, tagKey1, tagValue1, tagKey2, tagValue2)
}

func testAccDistributionConfig_custom(rName string) string {
Expand Down

0 comments on commit e7fdf66

Please sign in to comment.